Advertisement
UniQuet0p1

Untitled

Oct 20th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. """Formula One."""
  2. import re
  3. import csv
  4.  
  5.  
  6. class Driver:
  7. """Driver class."""
  8.  
  9. def __init__(self, name: str, team: str):
  10. """
  11. Driver constructor.
  12.  
  13. Here you should save driver's results as dictionary,
  14. where key is race number and value is points from that race.
  15. You must also save driver's points into a variable "points".
  16.  
  17. :param name: Driver name
  18. :param team: Driver team
  19. """
  20. pass
  21.  
  22. def get_results(self) -> dict:
  23. """
  24. Get all driver's results.
  25.  
  26. :return: Results as dictionary
  27. """
  28. return {}
  29.  
  30. def get_points(self) -> int:
  31. """
  32. Return calculated driver points.
  33.  
  34. :return: Calculated points
  35. """
  36. return 0
  37.  
  38. def set_points(self):
  39. """Set points for driver."""
  40. pass
  41.  
  42. def add_result(self, race_number: int, points: int):
  43. """
  44. Add new result to dictionary of results.
  45.  
  46. Dictionary is located in the constructor.
  47.  
  48. :param race_number: Race number
  49. :param points: Number of points from the race
  50. """
  51. pass
  52.  
  53.  
  54. class Race:
  55. """Race class."""
  56.  
  57. def __init__(self, file):
  58. """
  59. Race constructor.
  60.  
  61. Here you should keep data collected from file.
  62. You must read file rows to list.
  63.  
  64. :param file: File with race data
  65. """
  66. pass
  67.  
  68. def read_file_to_list(self):
  69. """
  70. Read file data to list in constructor.
  71.  
  72. First line shows number of races in data file.
  73. Rest of the data follows same rules. Each line consists of 'Driver Team Time Race'.
  74. There are 2 or more spaces between each 'category'.
  75. E.g. "Mika HΓ€kkinen McLaren-Mercedes 42069 3"
  76.  
  77. If file does NOT exist, throw FileNotFoundError with message "No file found!".
  78. """
  79. pass
  80.  
  81. @staticmethod
  82. def extract_info(line: str) -> dict:
  83. """
  84. Helper method for read_file_to_list.
  85.  
  86. Here you should convert one data line to dictionary.
  87. Dictionary must contain following key-value pairs:
  88. 'Name': driver's name as string
  89. 'Team': driver's team as string
  90. 'Time': driver's time as integer (time is always in milliseconds)
  91. 'Diff': empty string
  92. 'Race': race number as integer
  93.  
  94. :param line: Data string
  95. :return: Converted dictionary
  96. """
  97. return {}
  98.  
  99. def filter_data_by_race(self, race_number: int) -> list:
  100. """
  101. Filter data by race number.
  102.  
  103. :param race_number: Race number
  104. :return: Filtered race data
  105. """
  106. return []
  107.  
  108. @staticmethod
  109. def format_time(time: str) -> str:
  110. """
  111. Format time from milliseconds to M:SS.SSS
  112.  
  113. format_time('12') -> 0:00.012
  114. format_time('1234') -> 0:01.234
  115. format_time('123456') -> 2:03.456
  116.  
  117. :param time: Time in milliseconds
  118. :return: Time as M:SS.SSS string
  119. """
  120. return ""
  121.  
  122. @staticmethod
  123. def calculate_time_difference(first_time: int, second_time: int) -> str:
  124. """
  125. Calculate difference between two times.
  126.  
  127. First time is always smaller than second time. Both times are in milliseconds.
  128. You have to return difference in format +M:SS.SSS
  129.  
  130. calculate_time_difference(4201, 57411) -> +0:53.210
  131.  
  132. :param first_time: First time in milliseconds
  133. :param second_time: Second time in milliseconds
  134. :return: Time difference as +M:SS.SSS string
  135. """
  136. return ""
  137.  
  138. @staticmethod
  139. def sort_data_by_time(results: list) -> list:
  140. """
  141. Sort results data list of dictionaries by 'Time'.
  142.  
  143. :param results: List of dictionaries
  144. :return: Sorted list of dictionaries
  145. """
  146. return []
  147.  
  148. def get_results_by_race(self, race_number: int) -> list:
  149. """
  150. Final results by race number.
  151.  
  152. This method combines the rest of the methods.
  153. You have to filter data by race number and sort them by time.
  154. You must also fill 'Diff' as time difference with first position.
  155. You must add 'Place' and 'Points' key-value pairs for each dictionary.
  156.  
  157. :param race_number: Race number for filtering
  158. :return: Final dictionary with complete data
  159. """
  160. return []
  161.  
  162.  
  163. class FormulaOne:
  164. """FormulaOne class."""
  165.  
  166. def __init__(self, file):
  167. """
  168. FormulaOne constructor.
  169.  
  170. It is reasonable to create Race instance here to collect all data from file.
  171.  
  172. :param file: File with race data
  173. """
  174. pass
  175.  
  176. def write_race_results_to_file(self, race_number: int):
  177. """
  178. Write one race results to a file.
  179.  
  180. File name is 'results_for_race_{race_number}.txt'.
  181. Exact specifications are described in the text.
  182.  
  183. :param race_number: Race to write to file
  184. """
  185. pass
  186.  
  187. def write_race_results_to_csv(self, race_number: int):
  188. """
  189. Write one race results to a csv file.
  190.  
  191. File name is 'race_{race_number}_results.csv'.
  192. Exact specifications are described in the text.
  193.  
  194. :param race_number: Race to write to file
  195. """
  196. pass
  197.  
  198. def write_championship_to_file(self):
  199. """
  200. Write championship results to file.
  201.  
  202. It is reasonable to create Driver class instance for each unique driver name and collect their points
  203. using methods from Driver class.
  204. Exact specifications are described in the text.
  205. """
  206. pass
  207.  
  208.  
  209. if __name__ == '__main__':
  210. f1 = FormulaOne("example.txt")
  211. f1.write_race_results_to_file(1)
  212. f1.write_race_results_to_csv(2)
  213. f1.write_championship_to_file()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement