-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_targeted_using_sum.py
More file actions
71 lines (52 loc) · 1.42 KB
/
Copy pathfind_targeted_using_sum.py
File metadata and controls
71 lines (52 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# input: [2,11,7,15]
# target=9
# output:[0,1]
# explaination: nums[0]+nums[2]==9
nums=[5,16,13,10,1,24,4]
target=9
j=0
i=0
for n1 in nums:
for n2 in nums:
if n1+n2==target:
print([j,i])
else:
i=i+1
break
'''
# Input list of numbers
nums = [5, 16, 13, 10, 1, 24, 4]
# Target sum to find
target = 9
# Initializing indices (note: this approach needs adjustment to work correctly)
i = 0
j = 0
# Outer loop to select the first number (n1)
for n1 in nums:
# Inner loop to select the second number (n2)
for n2 in nums:
# Check if the sum of the two numbers equals the target
# AND ensured we are not using the same element at the same index
if n1 + n2 == target and i != j:
# If pair is found, print their indices
print([i,j])
else:
# This logic increments j based on the inner loop,
# but is unconventional for finding two-sum indices
j = j + 1
break # Break the inner loop
# ---------------------------------------------------------------------------
'''
'''
items=[4,1,2,1,2]
for i in items:
if items.count(i)==1:
print(i)
items=[4,1,2,1,2]
# Iterate over each element in the list
for i in items:
# Check if the current element appears exactly once in the entire list
if items.count(i)==1:
# If it is unique, print the element
print(i)
'''