-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchap01.py
More file actions
147 lines (133 loc) · 3.82 KB
/
chap01.py
File metadata and controls
147 lines (133 loc) · 3.82 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
#P29
import copy
def recFind(inputL):
def find(inputL):
possL = list()
for i in range(len(inputL)):
currentI = [inputL[i]]
remainder = inputL[:i] + inputL[i+1:]
if len(remainder) == 0:
return currentI
else:
returnL = copy.deepcopy(recFind(remainder))
for j in returnL:
tempCurrentI = copy.deepcopy(currentI)
if isinstance(j, list):
for k in j:
tempCurrentI.append(k)
possL.append(tempCurrentI)
else:
tempCurrentI.append(j)
possL.append(tempCurrentI)
return possL
return [''.join(b) for b in find(inputL)]
#P30
def counter(number, count=0):
if number < 2:
raise ValueError('too small')
else:
while number >= 2:
number /= 2
count += 1
return count
# P31
def makechange(charge, given):
difference = given - charge
hund = difference // 100
difference -= hund * 100
twenties = difference // 20
difference -= twenties * 20
return ('hundred bills {0} twenty bills {1}'.format(hund, twenties))
# P32
def calculator():
allL = list()
temL = list()
finished = False
while finished == False:
inputted = input('enter number, operator, or finish ')
if inputted == 'finish':
finished = True
else:
allL.append(inputted)
print(allL)
for i in range(len(allL)):
if allL[i] == '*':
times = int(allL[i-1])*int(allL[i+1])
temL.pop()
temL.append(times)
elif allL[i-1] == '*':
continue
elif allL[i] == '/':
divide = int(allL[i-1])/int(allL[i+1])
temL.pop()
temL.append(divide)
elif allL[i-1] == '/':
continue
else:
temL.append(allL[i])
allL = temL
temL = list()
for i in range(len(allL)):
if allL[i] == '+':
times = int(allL[i-1])+int(allL[i+1])
temL.pop()
temL.append(times)
elif allL[i-1] == '+':
continue
elif allL[i] == '-':
divide = int(allL[i-1]) - int(allL[i+1])
temL.pop()
temL.append(divide)
elif allL[i-1] == '-':
continue
else:
temL.append(allL[i])
return allL
# P33
def calc(previous=None):
finished = False
list1 = []
if previous != None:
list1.append(previous)
while len(list1) < 3 and finished == False:
inputted = input('#, op, finish or clear ')
if inputted == 'clear':
calc()
elif inputted == 'finish':
finished = True
else:
list1.append(inputted)
if finished == False and isinstance(int(list1[0]), int) and isinstance(int(list1[2]), int) and list1[1] == '*':
temp = int(list1[0])*int(list1[2])
print(temp)
calc(temp)
# P34
from random import *
list2 = ['$','#','FLor','??']
for i in range(100):
list1 = list('I will never spam my friends again')
randError = randrange(len(list1))
list1[randError] = choice(list2)
print(i+1,''.join(list1))
# P35
from random import *
def birthparadox(list1):
list2 = []
list3 = []
for i in list1:
for j in range(i):
list2.append(randrange(365))
if len(set(list2)) != len(list2):
list3.append('paradox')
return len(list3)/len(list1)
print(birthparadox([5,10,15,20,15,30, 35, 40]))
#P36
def counter():
dic = {}
for i in range(10):
inputted = input('input word ')
if inputted not in dic:
dic[inputted] = 1
else:
dic[inputted] += 1
return dic