Advertisement
Rementai

Dekorator

Mar 23rd, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. from time import perf_counter
  2.  
  3. def my_range(*args):
  4.     number_of_args = len(args)
  5.     start_timer = perf_counter()
  6.     if number_of_args == 1:
  7.         start, stop, step = 0.0, args[0], 1.0
  8.     elif number_of_args == 2:
  9.         start, stop, step = args[0], args[1], 1.0
  10.     elif number_of_args == 3:
  11.         start, stop, step = args[0], args[1], args[2]
  12.     else:
  13.         raise ValueError("Function needs 1 to 3 arguments.")
  14.  
  15.     if step == 0:
  16.         raise ValueError("Step argument cannot be 0")
  17.  
  18.     result = []
  19.     while step > 0 and start < stop:
  20.         result.append(start)
  21.         start += step
  22.     while step < 0 and start < stop:
  23.         result.append(stop)
  24.         stop += step
  25.     end_timer = perf_counter()
  26.     print("Czas działania programu: ", end_timer - start_timer)
  27.     return result
  28.  
  29.  
  30. def main():
  31.     try:
  32.         my_range(1.0, 1000, 0.5)
  33.         my_range(1.0, 10000, 0.5)
  34.         my_range(1.0, 100000, 0.5)
  35.         my_range(1.0, 1000000, 0.5)
  36.     except ValueError as e:
  37.         print("Error: ", e)
  38.  
  39. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement