-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeCa_for_str_lst_dict.py
134 lines (92 loc) · 2.74 KB
/
CodeCa_for_str_lst_dict.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
phrase = "A bird in the hand..."
# Add your for loop
for char in phrase:
if char == 'A' or char == 'a':
char = 'X'
print char,
else:
print char,
# Don't delete this print statement!
print
numbers = [7, 9, 12, 54, 99]
print "This list contains: "
for num in numbers:
print num
print "---"
# Add your loop below!
for num in numbers:
print num ** 2
d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}
for key in d:
# Your code her
print key + " " + d[key]
#----------------
choices = ['pizza', 'pasta', 'salad', 'nachos']
print 'Your choices are:'
for index, item in enumerate(choices):
print index + 1, item
#----------------
list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
for a, b in zip(list_a, list_b):
# Add your code here!
print max(a, b)
#---------------
# for else
fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']
print 'You have...'
for f in fruits:
print 'A', f
if f == 'tomato':
print 'A tomato is not a fruit!' # (It actually is.)
else:
print 'A fine selection of fruits!'
#-------
for i in 'Tennis':
print i,
else:
print 'What a game!'
print 'Now printing a bit lower'
# cfreates an empty list and fills it with hobbies based on user inputs
hobbies = []
# Add your code below!
"""
for i in range(3):
hobby = raw_input("What is your hobby?")
hobbies.append(hobby)
print hobbies
"""
# counter with for loop in a list and an object needed to be counted
def count(sequence, item):
count = 0
for i in sequence:
if i == item:
count += 1
return count
# remove certain elements from list. Important point - if iterate from left to right, not all the elements will be
# found and removed. When removing or deleting elements from the list we need to go backwards, from right to left.
def purify(list):
for n in range(len(list), 0, -1): # here we loop from the end of the list, but we put it as start
# - len(list), then we indicate the end - 0 and afterwards indicate the step "-1"
# so it goes backward in decrementing steps.
if list[n - 1] % 2 != 0:
list.remove(list[n - 1])
return list
print purify([4, 5, 5, 4])
# removing duplicates from a list
list = [4, 5, 5, 4]
def remove_duplicates(list):
new_list = []
for i in list:
if i not in new_list:
new_list.append(i)
return new_list
print remove_duplicates([4, 5, 5, 4])
# median
list = [1,1,2]
def median(list):
list = sorted(list)
if len(list) % 2 == 0:
return (list[(len(list) / 2) - 1] + list[(len(list) / 2)]) / 2.0
return list[(len(list) / 2)]
print median(list)