Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- PROGRAM ID: Nested Lists.py / Nested Lists
- Author: ---------, Date initiated: 11/3/2021
- INSTALLATION: Python v3.9
- REMARKS: This is a HackerRank problem. Given the names and grades for
- each student in a class of students, store them in a nested list and print
- the name(s) of any student(s) having the second lowest grade.
- """
- import random
- import string
- EXAMPLE_ONE = [["chi", 20.0],
- ["beta", 50.0],
- ["alpha", 50.0]]
- EXAMPLE_TWO = [['Harry', 37.21],
- ['Berry', 37.21],
- ['Tina', 37.2],
- ['Akriti', 41],
- ['Harsh', 39]]
- TWEAKED_EXAMPLE_TWO = [['Harry', 37.21],
- ['Berry', 37.21],
- ['Tina', 37.21],
- ['Akriti', 41],
- ['Harsh', 39]]
- EDGE_CASE_EXAMPLE = [['SIaEO', 57.16],
- ['ybBls', 86.24],
- ['HZBRg', 7.95],
- ['xEfKq', 7.95]]
- def main():
- # The HackerRank website uses standard input, commented out for testing.
- ## records = []
- ## for _ in range(int(input())):
- ## name = input()
- ## score = float(input())
- ## records.append([name, score])
- records = EXAMPLE_TWO
- lowest_score, second_lowest_score = get_two_lowest_scores(records)
- print_score(second_lowest_score, records)
- def get_two_lowest_scores(records):
- """1. Return the tuple (lowest_score, second_lowest_score). The
- lowest score is returned only for testing purposes. The records take the
- form [["name", score], ...]."""
- lowest_score = 100.0
- second_lowest_score = 100.0
- for record in records:
- if record[1] < lowest_score:
- second_lowest_score = lowest_score
- lowest_score = record[1]
- else:
- if record[1] < second_lowest_score and record[1] != lowest_score:
- second_lowest_score = record[1]
- return lowest_score, second_lowest_score
- def print_score(score, records):
- """2. Print the name of each person with a certain score --
- there may be ties. The records take the form [["name", score], ...].
- The names are printed in alphabetical order."""
- # Order the records with names arranged alphabetically, so they
- # will be printed alphabetically.
- records = sorted(records, key=lambda row: row[0].lower())
- for record in records:
- if record[1] == score:
- print(record[0])
- def test_get_lowest_two_scores_with_random_values(n=1000):
- """0. Run n random tests within argument constraints to help find hidden
- edge cases, though there may still be rare cases which are better found through
- step-by-step examination. If the asserts within this function's hierarchy are
- carefully written, this will help find logic errors as well as run-time
- errors. Start with a small number of tests, then try large numbers such as
- 1,000,000 if there appear to be rare edge cases."""
- i = 0
- while i < n:
- records = get_random_records()
- test_get_lowest_two_scores(records)
- i += 1
- def get_random_records():
- """1. This function generates a random function argument for
- get_lowest_two_scores(records), within specified constraints, for
- testing purposes."""
- MIN_VALUE = 0
- MAX_VALUE = 100
- MIN_ELEMENTS = 2
- MAX_ELEMENTS = 5
- # Generate pairs of strings and floats with up to two digits
- # after the decimal point.
- records = [[get_random_string(5),
- random.randint(MIN_VALUE * 100, MAX_VALUE * 100) / 100]
- for x in range(random.randint(MIN_ELEMENTS, MAX_ELEMENTS))]
- print("List:",records)
- return records
- def get_random_string(str_length, allowed_chars = string.ascii_letters):
- """1.1. Generate a random string."""
- return ''.join(random.choice(allowed_chars) for x in range(str_length))
- def test_get_lowest_two_scores(records=EXAMPLE_TWO):
- """2. Test get_lowest_two_scores(records) using a specific example."""
- lowest_score, second_lowest_score = get_two_lowest_scores(records)
- test_get_lowest_two_scores_asserts(lowest_score,
- second_lowest_score,
- records)
- def test_get_lowest_two_scores_asserts(lowest_score,
- second_lowest_score,
- records):
- """2.2. Given get_two_lowest_scores() return values, test that it makes
- sense."""
- for record in records:
- assert(lowest_score <= record[1])
- assert(second_lowest_score <= record[1] or record[1] == lowest_score)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement