-
Notifications
You must be signed in to change notification settings - Fork 0
/
Udacity_lists.py
169 lines (112 loc) · 2.45 KB
/
Udacity_lists.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Given the variable,
"""
days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]
# define a procedure, how_many_days,
# that takes as input a number
# representing a month, and returns
# the number of days in that month.
def how_many_days(month_number):
return days_in_month[month_number - 1]
print how_many_days(1)
#>>> 31
print how_many_days(9)
#>>> 30
# append lists
p = [1, 2]
q = [3, 4]
p.append(q)
print p
s = 0.4 * 1000000000
sp = s * 299792458
print sp
#-------------
# while loop on lists
def print_all_elements(p):
j = 0
while j < len(p):
print p[j]
j += 1
p = ['q', 'w', 'e', 'r']
print_all_elements(p)
# for loop in lists
def print_all_elements2(q):
for e in q:
print e
q = [1, 2, 3, [4, 5], [6, 7, 8]]
print_all_elements2(q)
# summing with for loop
def sum_list(p):
summ = 0
for i in p:
summ += i
return summ
p = [1, 7, 4]
print sum_list(p)
# Measure Udacity
def measure_udacity(list1):
result = 0
for i in list1:
if i[0] == 'U':
result += 1
return result
list1 = ['Dave', 'Sebastian', 'Katy']
list1 = ['Umika','Umberto']
print measure_udacity(list1)
"""
# Find element 'b' in a list 'a'. Return the index of the list 'a' where the 'b' is found. If nothing is found then \
# return -1
# with while loop
"""
def find_element(a, b):
index = 0
while index < len(a):
if a[index] == b:
return index
index += 1
return -1
#a = ['tr', 'yu', 'op']
#b = 'yu'
a = ['rt', 'yu', 'jk']
b = 'yu'
print find_element(a, b)
# with for loop
def find_element1(a, b):
index = 0
for element in a:
if element == b:
return index
index += 1
return -1
a = [1, 2, 3]
b = 3
print find_element1(a, b)
"""
# find element in with 'in' and index()
def find_element(a, b):
if b in a:
return a.index(b)
return -1
a = ['rt', 'yu', 'jk']
b = 'yu'
print find_element(a, b)
# with 'not in'
def find_element1(a, b):
if b not in a:
return -1
return a.index(b)
a = ['rt', 'yu', 'jk']
b = 'y'
print find_element1(a, b)
# calculates the product of elements in a list
def product_list(list_of_numbers):
prod = 1
for i in list_of_numbers:
prod *= i
return prod
print product_list([1,2,3,4])
# the greatest number in a list
def greatest(list_of_numbers):
while list_of_numbers:
return max(list_of_numbers)
return 0
print greatest([4, 23, 1])