Advertisement
Guest User

Untitled

a guest
Nov 6th, 2021
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. """
  2. PROGRAM ID: Nested Lists.py / Nested Lists
  3. Author: ---------, Date initiated: 11/3/2021
  4. INSTALLATION: Python v3.9
  5. REMARKS: This is a HackerRank problem. Given the names and grades for
  6. each student in a class of students, store them in a nested list and print
  7. the name(s) of any student(s) having the second lowest grade.
  8. """
  9.  
  10. import random
  11. import string
  12.  
  13. EXAMPLE_ONE = [["chi", 20.0],
  14. ["beta", 50.0],
  15. ["alpha", 50.0]]
  16.  
  17. EXAMPLE_TWO = [['Harry', 37.21],
  18. ['Berry', 37.21],
  19. ['Tina', 37.2],
  20. ['Akriti', 41],
  21. ['Harsh', 39]]
  22.  
  23. TWEAKED_EXAMPLE_TWO = [['Harry', 37.21],
  24. ['Berry', 37.21],
  25. ['Tina', 37.21],
  26. ['Akriti', 41],
  27. ['Harsh', 39]]
  28.  
  29. EDGE_CASE_EXAMPLE = [['SIaEO', 57.16],
  30. ['ybBls', 86.24],
  31. ['HZBRg', 7.95],
  32. ['xEfKq', 7.95]]
  33.  
  34. def main():
  35. # The HackerRank website uses standard input, commented out for testing.
  36. ## records = []
  37. ## for _ in range(int(input())):
  38. ## name = input()
  39. ## score = float(input())
  40. ## records.append([name, score])
  41. records = EXAMPLE_TWO
  42.  
  43. lowest_score, second_lowest_score = get_two_lowest_scores(records)
  44. print_score(second_lowest_score, records)
  45.  
  46. def get_two_lowest_scores(records):
  47. """1. Return the tuple (lowest_score, second_lowest_score). The
  48. lowest score is returned only for testing purposes. The records take the
  49. form [["name", score], ...]."""
  50. lowest_score = 100.0
  51. second_lowest_score = 100.0
  52. for record in records:
  53. if record[1] < lowest_score:
  54. second_lowest_score = lowest_score
  55. lowest_score = record[1]
  56. else:
  57. if record[1] < second_lowest_score and record[1] != lowest_score:
  58. second_lowest_score = record[1]
  59.  
  60. return lowest_score, second_lowest_score
  61.  
  62. def print_score(score, records):
  63. """2. Print the name of each person with a certain score --
  64. there may be ties. The records take the form [["name", score], ...].
  65. The names are printed in alphabetical order."""
  66. # Order the records with names arranged alphabetically, so they
  67. # will be printed alphabetically.
  68. records = sorted(records, key=lambda row: row[0].lower())
  69. for record in records:
  70. if record[1] == score:
  71. print(record[0])
  72.  
  73. def test_get_lowest_two_scores_with_random_values(n=1000):
  74. """0. Run n random tests within argument constraints to help find hidden
  75. edge cases, though there may still be rare cases which are better found through
  76. step-by-step examination. If the asserts within this function's hierarchy are
  77. carefully written, this will help find logic errors as well as run-time
  78. errors. Start with a small number of tests, then try large numbers such as
  79. 1,000,000 if there appear to be rare edge cases."""
  80. i = 0
  81. while i < n:
  82. records = get_random_records()
  83. test_get_lowest_two_scores(records)
  84. i += 1
  85.  
  86. def get_random_records():
  87. """1. This function generates a random function argument for
  88. get_lowest_two_scores(records), within specified constraints, for
  89. testing purposes."""
  90. MIN_VALUE = 0
  91. MAX_VALUE = 100
  92. MIN_ELEMENTS = 2
  93. MAX_ELEMENTS = 5
  94.  
  95. # Generate pairs of strings and floats with up to two digits
  96. # after the decimal point.
  97. records = [[get_random_string(5),
  98. random.randint(MIN_VALUE * 100, MAX_VALUE * 100) / 100]
  99. for x in range(random.randint(MIN_ELEMENTS, MAX_ELEMENTS))]
  100. print("List:",records)
  101. return records
  102.  
  103. def get_random_string(str_length, allowed_chars = string.ascii_letters):
  104. """1.1. Generate a random string."""
  105. return ''.join(random.choice(allowed_chars) for x in range(str_length))
  106.  
  107.  
  108. def test_get_lowest_two_scores(records=EXAMPLE_TWO):
  109. """2. Test get_lowest_two_scores(records) using a specific example."""
  110. lowest_score, second_lowest_score = get_two_lowest_scores(records)
  111. test_get_lowest_two_scores_asserts(lowest_score,
  112. second_lowest_score,
  113. records)
  114.  
  115. def test_get_lowest_two_scores_asserts(lowest_score,
  116. second_lowest_score,
  117. records):
  118. """2.2. Given get_two_lowest_scores() return values, test that it makes
  119. sense."""
  120. for record in records:
  121. assert(lowest_score <= record[1])
  122. assert(second_lowest_score <= record[1] or record[1] == lowest_score)
  123.  
  124. if __name__ == '__main__':
  125. main()
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement