-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCode_2_27.py
More file actions
156 lines (110 loc) · 3.46 KB
/
Copy pathCode_2_27.py
File metadata and controls
156 lines (110 loc) · 3.46 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
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
# How are the following functions implemented
# count, insert
my_list = [1,2,1,2,1,2,3,4,4,4,3,3]
print(my_list)
print(my_list.count(3))
def counter(L, x):
"""
Function to count the number of times x is in L
:param L: list
:param x: element
:return: count of x in L
"""
countX = 0
for e in L:
if e == x:
countX += 1
return countX
print(counter(my_list, 3))
# Creating .insert
my_list.insert(1, "object")
print(my_list)
# Insert:
def insert_v1(L, i, e):
return L[:i] + [e] + L[i:]
print(insert_v1(my_list, 1, 'object'))
def insert_v2(L, i, e):
# make a copy of tail of the list starting at i
tail = L[i:]
# replace the value at i in L for e
L[i] = e
# replace all elements in L with tail elements starting at i + 1
for j in range(len(tail) - 1):
L[i + 1] = tail[j]
i += 1
# add the last element on the tail to L
L.append(tail[-1])
insert_v2(my_list, 1, 'object')
print(my_list)
# Shuffle a list
# 1. Select a random index i from the set of indices of a list L
# 2. Remove the element at index i from L and append it to a new list M
# 3. repeat step 2 until L is empty
def shuffle_v1(L):
from random import randint
new_list = []
while not len(L) == 0:
# slect a random index out of L
idx = randint(0, len(L) - 1)
# remove element at index i
e = L.pop(idx)
# append element to a new list
new_list.append(e)
return new_list
my_list = [i for i in range(10)]
print(my_list)
new_list = shuffle_v1(my_list)
print(new_list)
# Shuffle in place
def shuffle_v2(L, n):
import random
for i in range(n):
# slect a random index out of L
idx = random.randint(0, len(L)-1)
# remove element at index i
e = L.pop(idx)
# append element to a new list
new_list.append(e)
#my_list = [i for i in range(10)]
#print(my_list)
#shuffle_v2(my_list, 20)
#print(my_list)
# Element Uniqueness : determine if all elements in a collection are unique
# return True if all elements are distinct, False otherwise
def unique(L):
new_L = sorted(L) # sort the list in place
for i in range(len(new_L)-1):
if new_L[i + 1] == new_L[i]:
return False
return True
my_list = [1,3,5,7,2,8,9,4]
print(unique(my_list))
# Send + More = Money
solutions = []
x = [i for i in range(10)]
for s in x:
S = s
for e in x:
E = e
for n in x:
N = n
for d in x:
D = d
for m in x:
M = m
for o in x:
O = o
for r in x:
R = r
for y in x:
Y = y
# if the assignment is unique continue
L = [S, E, N, D, M, O, R, Y]
if unique(L):
# check if the selection is a solution
send = int(''.join([str(S), str(E), str(N), str(D)]))
more = int(''.join([str(M), str(O), str(R), str(E)]))
money = int(''.join([str(M), str(O), str(N), str(E), str(Y)]))
if send + more == money:
solutions.append(L)
print(L)