diff --git a/cs171/lect09/hello.py b/cs171/lect09/hello.py new file mode 100644 index 0000000000000000000000000000000000000000..47bcf165f80d46b9275ba2083e5f2131474bad3a --- /dev/null +++ b/cs171/lect09/hello.py @@ -0,0 +1 @@ +print("Hello CS 171") \ No newline at end of file diff --git a/cs171/lect09/list_viewer.py b/cs171/lect09/list_viewer.py new file mode 100644 index 0000000000000000000000000000000000000000..4c0b97e56cc06a2708635a39af1afc07676ef0bf --- /dev/null +++ b/cs171/lect09/list_viewer.py @@ -0,0 +1,33 @@ +import random + +def generate_random_list(min_val, max_val, length): + random_list = [] + for _ in range(length): + random_list.append(random.randint(min_val, max_val)) + return random_list + +if __name__ == "__main__": + min_val = int(input("Enter the minimum value: ")) + max_val = int(input("Enter the maximum value: ")) + length = int(input("Enter the length of the list: ")) + sort_list = input("Do you want the list to be sorted? (y/n): ").strip().lower() + + random_list = generate_random_list(min_val, max_val, length) + + if sort_list == 'y': + random_list.sort() + + views = 0 + + while True: + try: + index = int(input("Enter the index you want to view: ")) + if index < 0 or index >= length: + raise IndexError + print(f"Value at index {index}: {random_list[index]}") + views += 1 + except IndexError: + print("Invalid index entered.") + print(f"Full list: {random_list}") + print(f"Number of views: {views}") + break \ No newline at end of file