From 899e07718dfd06656e422b1ff7c759146be15ba6 Mon Sep 17 00:00:00 2001 From: Benedict Joseph <152013064+Benedict1010@users.noreply.github.com> Date: Tue, 27 Aug 2024 22:19:32 +0530 Subject: [PATCH] Create week2.py --- Week_2/week2.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Week_2/week2.py diff --git a/Week_2/week2.py b/Week_2/week2.py new file mode 100644 index 0000000..04aaf4d --- /dev/null +++ b/Week_2/week2.py @@ -0,0 +1,40 @@ +# dictionary to store number and its index +def findsums(nums, target): + num_to_index = {} + + # iterate through the list + for i, num in enumerate(nums): + complement = target - num + # check if the complement exists in the dictionary + if complement in num_to_index: + return (num_to_index[complement], i) + # add the number and its index to the dictionary + num_to_index[num] = i + + # return None if no solution is found + return None + +def main(): + # input the number of elements + nums_size = int(input("Enter the number of elements: ")) + + # input the numbers + nums = [] + print("Enter the numbers:") + for _ in range(nums_size): + nums.append(int(input())) + + # input the target + target = int(input("Enter the target value: ")) + + # find the two indices + indices = findsums(nums, target) + + #print the indices + if indices: + print(f"Indices: {indices[0]}, {indices[1]}") + else: + print("No two numbers add up to the target.") + +if __name__ == "__main__": + main()