Skip to content

Commit

Permalink
function1
Browse files Browse the repository at this point in the history
  • Loading branch information
Elshikh-Amro committed Dec 31, 2024
1 parent 2a68281 commit e50d627
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 deletions solutions/steps_to_floor.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
def steps_to_floor(target_floor: int = None) -> int:
def steps_to_floor_core(target_floor: int) -> int:
"""
Calculates the number of steps required to reach a given floor in a building,
Core function to calculate the number of steps required to reach a given floor in a building,
assuming it takes 10 steps to move between consecutive floors.
If no argument is provided, the function will prompt the user for input.
Parameters:
target_floor: int (optional), the floor number to reach (positive, negative, or zero).
target_floor: int, the floor number to reach (positive, negative, or zero).
Returns -> int, the total number of steps required.
"""
if target_floor is None:
try:
target_floor = int(input("Enter the floor number you want to reach: "))
except ValueError:
raise ValueError("Invalid input. Please enter an integer.")
Returns:
int: the total number of steps required.
Raises:
AssertionError: If the target_floor is not an integer.
# Ensure the target floor is an integer
Examples:
>>> steps_to_floor_core(0)
0
>>> steps_to_floor_core(1)
10
>>> steps_to_floor_core(5)
50
>>> steps_to_floor_core(-3)
30
"""
# Defensive assertion to ensure valid input
assert isinstance(target_floor, int), "Target floor must be an integer"

# Calculate steps based on the absolute floor value
return abs(target_floor) * 10


if __name__ == "__main__":
def steps_to_floor():
"""
Wrapper function to handle user input for steps_to_floor_core.
"""
try:
print(f"Steps required: {steps_to_floor()}")
except Exception as e:
target_floor = int(input("Enter the floor number you want to reach: "))
steps = steps_to_floor_core(target_floor)
if target_floor < 0:
print(f"You are going down. Steps required: {steps}")
elif target_floor > 0:
print(f"You are going up. Steps required: {steps}")
else:
print(f"You are staying on the same floor. Steps required: {steps}")
except ValueError:
print("Invalid input. Please enter an integer.")
except AssertionError as e:
print(f"Error: {e}")


if __name__ == "__main__":
import doctest
doctest.testmod() # Run tests on the core function
# Uncomment the following line for interactive user input
steps_to_floor()

0 comments on commit e50d627

Please sign in to comment.