DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #193 - What's the Real Floor?

Setup

Americans can be weird sometimes. The first floor of a building is typically referred to as the ground floor. In some American buildings, there is no 13th floor because of old superstitions.

Implement a function that takes an American floor passed as an integer and returns the actual floor number. Your function should also work for basement floors.

Examples

getRealFloor(1) == 0
getRealFloor(0) == 0
getRealFloor(5) == 4
getRealFloor(15) == 13
getRealFloor(-3) == -3

Tests

getRealFloor(3)
getRealFloor(7)
getRealFloor(20)
getRealFloor(1)
getRealFloor(-6)

Good luck!


This challenge comes from acadet on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (10)

Collapse
 
savagepixie profile image
SavagePixie • Edited

Elixir

I wasn't sure if the 13th floor thingie was also supposed to apply to basement levels. I wrote a solution that does it.

defmodule Floor do
  def get_real_floor(n) when n == 0, do: 0
  def get_real_floor(n) when n > 13, do: n - 2
  def get_real_floor(n) when n > 0, do: n - 1
  def get_real_floor(n) when n < -13, do: n + 1
  def get_real_floor(n), do: n
end
Collapse
 
aminnairi profile image
Amin

Elm

getRealFloor : Int -> Int
getRealFloor floor =
    if floor > 12 then
        floor - 2
        -- 15 -> 13

    else if floor > 0 then
        floor - 1
        -- 5 -> 4

    else
        floor
        -- -5 -> -5
Collapse
 
madstk1 profile image
Natamo

C#

Same as @savagepixie , not sure about negative superstitions, though I opted not to include it.

static int getRealFloor(int floor) {
    if(floor >  0 ) { floor--; }
    if(floor >= 13) { floor--; }
    return floor;
}
Collapse
 
vidit1999 profile image
Vidit Sarkar

C++ one liner

int getRealFloor(int americanFloor){
    // there is no 13th floor because of old superstitions, so no case of 13 and -13 is considered
    return (americanFloor<1) ? (americanFloor>=-12) ? americanFloor : americanFloor+1 : (americanFloor<=12) ? americanFloor-1 : americanFloor-2;
}

Here is what that thing actually is

if(americanFloor < 1){
    if(americanFloor >= -12)
        return americanFloor;
    else
        return americanFloor+1;
}
else{
    if(americanFloor <= 12)
        return americanFloor-1;
    else
        return americanFloor-2;
}
Collapse
 
maskedman99 profile image
Rohit Prasad

Python

var = int(input("Enter the floor number: "))

if var <= 0:
        print(var)
elif var > 13:
        print(var-2)
else:
        print(var-1)
Collapse
 
nijeesh4all profile image
Nijeesh Joshy

Ruby Oneliner


def getRealFloor floor 
 floor <= 0 ? floor : floor > 13 ? floor - 2 : floor - 1
end

Some tests


require "test/unit"

class GetRealFloorTest < Test::Unit::TestCase
  def test_get_real_floor
    assert_equal 0,  get_real_floor(0)
    assert_equal 2,  get_real_floor(3)
    assert_equal 6,  get_real_floor(7)
    assert_equal 18, get_real_floor(20)
    assert_equal 0,  get_real_floor(1)
    assert_equal -6, get_real_floor(-6)
  end
end


#1 tests, 6 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications

Collapse
 
bkaguilar profile image
Bk Aguilar • Edited

In JavaScript


const getRealFloor = n => {
  return n <= 0 ? n : n > 13 ? n-2 : n-1;
}

Collapse
 
jay profile image
Jay

Rust pattern matching

fn get_real_floor(n: i32) -> i32 {
    match n {
        0 => 0,
        1..=12 => n-1,
        13..=std::i32::MAX => n - 2,
        _ => n
    }
}
Collapse
 
stevenbey profile image
Steven Bey

return floor - (floor < 1 ? 0 : floor < 13 ? 1 : 2);