Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Week_2/week2.py
Original file line number Diff line number Diff line change
@@ -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()