-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_element.py
152 lines (56 loc) · 1.51 KB
/
find_element.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# this procedure, find_element,
# takes as its inputs a list
# and a value of any type, and
# returns the index of the first
# element in the input list that
# matches the value.
# If there is no matching element,
# return -1.
def find_element(lst, any_data):
ans = 0
for elements in lst:
if elements == any_data:
return ans
ans +=1
return -1
#print find_element([1,2,3],3)
#>>> 2
#print find_element(['alpha','beta'],'gamma')
#>>> -1
# this procedure, find_element2,
# uses index that takes as its
# inputs a list and a value of any
# type, and returns the index of
# the first element in the input
# list that matches the value.
# If there is no matching element,
# return -1.
def find_element2(lst, any_value):
if any_value in lst:
return lst.index(any_value)
else:
return -1
#print find_element2([1,2,3],3)
#>>> 2
#print find_element2(['alpha','beta'],'gamma')
#>>> -1
# this procedure, union,
# takes as inputs two lists.
# It should modify the first input
# list to be the set union of the two
# lists. You may assume the first list
# is a set, that is, it contains no
# repeated elements.
def union(lst_a, lst_b):
for num in lst_b:
if num not in lst_a:
a = lst_a.append(num)
# To test, uncomment all lines
# below except those beginning with >>>.
a = [1,2,3]
b = [2,4,6]
union(a,b)
print a
#>>> [1,2,3,4,6]
#print b
#>>> [2,4,6]