Advertisement
makispaiktis

Find the Speedcuber's times (Codewars)

Oct 29th, 2019 (edited)
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. '''
  2.  
  3. Speedcubing is the hobby involving solving a variety of twisty puzzles, the most famous being the 3x3x3 puzzle or Rubik's Cube, as quickly as possible.
  4.  
  5. In the majority of Speedcubing competitions, a Cuber solves a scrambled cube 5 times, and their result is found by taking the average of the middle 3 solves (ie. the slowest and fastest times are disregarded, and an average is taken of the remaining times).
  6.  
  7. In some competitions, the unlikely event of a tie situation is resolved by comparing Cuber's fastest times.
  8.  
  9. Write a function cube_times(times) that, given an array of floats times returns an array / tuple with the Cuber's result (to 2 decimal places) AND their fastest solve.
  10.  
  11. For example:
  12.  
  13. cube_times([9.5, 7.6, 11.4, 10.5, 8.1]) = (9.37, 7.6)
  14. # Because (9.5 + 10.5 + 8.1) / 3 = 9.37 and 7.6 was the fastest solve.
  15. Note: times will always be a valid array of 5 positive floats (representing seconds)
  16.  
  17. '''
  18.  
  19.  
  20. def cube_times(times):
  21.     max = times[0]
  22.     min = times[1]
  23.     for i in range(1, len(times)):
  24.         if times[i] > max:
  25.             max = times[i]
  26.         if times[i] < min:
  27.             min = times[i]
  28.  
  29.     # I've found the min and max elements of the list
  30.     mediumElements = []
  31.     for i in range(0, len(times)):
  32.         if times[i] != min and times[i] != max:
  33.             mediumElements.append(times[i])
  34.  
  35.     # My list with medium elements is ready
  36.     sum = 0
  37.     for i in range(0, len(mediumElements)):
  38.         sum += mediumElements[i]
  39.     average = sum / 3
  40.     # Rounding average in 2 digits
  41.     average = round(100 * average) / 100
  42.  
  43.     # New list = returned list
  44.     returnedList = []
  45.     returnedList.append(average)
  46.     returnedList.append(min)
  47.  
  48.     # Convert the list to a tuple
  49.     returnedTuple = tuple(returnedList)
  50.  
  51.     return returnedTuple
  52.  
  53.  
  54. # MAIN FUNCTION
  55. print()
  56. print("cube_times([10.5, 9.2, 8.4, 11.3, 12.6]) = " + str(cube_times([10.5, 9.2, 8.4, 11.3, 12.6])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement