8000 Updated questions to show multiple ways of doing - also added the ref… · zfoxpython/intro-to-python@e307f9a · GitHub
[go: up one dir, main page]

Skip to content

Commit e307f9a

Browse files
committed
Updated questions to show multiple ways of doing - also added the refactor option to B4
1 parent 61044d4 commit e307f9a

File tree

5 files changed

+188
-22
lines changed

5 files changed

+188
-22
lines changed

session_08/answers/A2.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# A2 - Read the file 'austen.txt' and print the amount of lines in the file
2+
3+
#Option 1
24
total = 0
35
for x in open("austen.txt"):
46
total += 1
7+
58
print(total)
9+
10+
#Option 2
11+
f = open("austen.txt", "r")
12+
count = 0
13+
for x in f:
14+
count += 1
15+
16+
print(count)

session_08/answers/A3.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# A3 - Each line of the file 'numbers.txt' contains a number, write a script to add up all the values in the file
2+
3+
#Option 1
24
total = 0
35
for x in open("numbers.txt"):
46
total += int(x)
7+
58
print(total)
9+
10+
#Option 2
11+
f = open("numbers.txt", "r")
12+
sum = 0
13+
for x in f:
14+
x = int(x)
15+
sum += x
16+
17+
print(sum)

session_08/answers/B1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# B1 - Ask the user to enter their name and append this to a file called 'register.txt'
22

33
file1 = open("register.txt", "a")
4+
45
name = True
56
while name != "":
6-
name = input("whats uyour anme? ")
7+
name = input("What's your name?\n")
78
if name:
89
file1.write(name + "\n")
910

session_08/answers/B2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# B2 - Create a new file called 'even.txt' that contains only the even numbers from the file 'numbers.txt"
22

33
f = open("even.txt", "w")
4+
45
for x in open("numbers.txt"):
56
x = int(x)
67
if x % 2 == 0:
78
f.write(str(x) + "\n")
9+
10+
f.close()

session_08/answers/B4.py

Lines changed: 160 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,164 @@
33
# Fake data is usually evenly distributed, where as real data The files 'accounts_1.txt', 'accounts_2.txt' and 'accounts_3.txt' contain financial transaction data.
44
# Work out which of the files contains fake data.
55

6+
# STAGE 1 - this code allows us to check the values of one of the account files.
7+
f = open("accounts_1.txt", "r")
8+
9+
count = {
10+
"0":0,
11+
"1":0,
12+
"2":0,
13+
"3":0,
14+
"4":0,
15+
"5":0,
16+
"6":0,
17+
"7":0,
18+
"8":0,
19+
"9":0
20+
}
21+
22+
for x in f:
23+
if x:
24+
count[x[0]] += 1
25+
26+
for y in range(1,10):
27+
print(str(y) + " = " + str(count[str(y)]/100) + "%")
28+
29+
# STAGE 2 - Adding a for loop so we are able to loop through all accounts.
30+
for x in range(1,4):
31+
f = open("accounts_" + str(x) + ".txt", "r")
32+
33+
count = {
34+
"0":0,
35+
"1":0,
36+
"2":0,
37+
"3":0,
38+
"4":0,
39+
"5":0,
40+
"6":0,
41+
"7":0,
42+
"8":0,
43+
"9":0
44+
}
45+
46+
for num in f:
47+
if num:
48+
count[num[0]] += 1
49+
print(x)
50+
for y in range(1,10):
51+
print(str(y) + " = " + str(count[str(y)]/100) + "%")
52+
53+
#STAGE 3 - Changing count so instead of us manually having to input count, it will create itself
654
for x in range(1,4):
7-
f = open("accounts_" + str(x) + ".txt", "r")
8-
count = {
9-
"0": 0,
10-
"1": 0,
11-
"2": 0,
12-
"3": 0,
13-
"4": 0,
14-
"5": 0,
15-
"6": 0,
16-
"7": 0,
17-
"8": 0,
18-
"9": 0,
19-
}
20-
21-
for num in f:
22-
if num:
23-
count[num[0]] += 1
24-
25-
print(x)
26-
for y in range(1,10):
27-
print(str(y) + " = " + str(count[str(y)]/100) + "%")
55+
f = open("accounts_" + str(x) + ".txt", "r")
56+
57+
count = {}
58+
for i in range(1,10):
59+
count[str(i)] = 0
60+
61+
for num in f:
62+
if num:
63+
count[num[0]] += 1
64+
print(x)
65+
for y in range(1,10):
66+
print(str(y) + " = " + str(count[str(y)]/100) + "%")
67+
68+
# STAGE 4 - Putting our code into a function - call 3 times
69+
70+
def benford_calc(file_name):
71+
f = open(file_name, "r")
72+
73+
count = {}
74+
for i in range(1,10):
75+
count[str(i)] = 0
76+
77+
for num in f:
78+
if num:
79+
count[num[0]] += 1
80+
print("Results for:" + file_name)
81+
for y in range(1,10):
82+
print(str(y) + " = " + str(count[str(y)]/100) + "%")
83+
84+
benford_calc("accounts_1.txt")
85+
benford_calc("accounts_2.txt")
86+
benford_calc("accounts_3.txt")
87+
88+
# STAGE 5 - Looping through file names, so we don't need to call the function 3 times, as per stage 4
89+
def benford_calc(file_name):
90+
f = open(file_name, "r")
91+
92+
count = {}
93+
for i in range(1,10):
94+
count[str(i)] = 0
95+
96+
for num in f:
97+
if num:
98+
count[num[0]] += 1
99+
print("Results for:" + file_name)
100+
for y in range(1,10):
101+
print(str(y) + " = " + str(count[str(y)]/100) + "%")
102+
103+
for x in range (1,4):
104+
file_name = "accounts_" + str(x) + ".txt"
105+
benford_calc(file_name)
106+
107+
# STAGE 6 - Separating file function from data function
108+
109+
def benford_calc_file(file_name):
110+
f = open(file_name, "r")
111+
return benford_calc(f)
112+
113+
def benford_calc(data):
114+
115+
count = {}
116+
for x in range(1,10):
117+
count[str(x)] = 0
118+
119+
for num in data:
120+
if num:
121+
count[str(num[0])] += 1
122+
123+
return count
124+
125+
for x in range (1,4):
126+
file_name = "accounts_" + str(x) + ".txt"
127+
data = benford_calc_file(file_name)
128+
129+
print("Results for:" + file_name)
130+
131+
for y in range(1,10):
132+
print(str(y) + " = " + str(data[str(y)]/100) + "%")
133+
134+
# STAGE 7 - Sending in a list of random numbers
135+
136+
def benford_calc_file(file_name):
137+
f = open(file_name, "r")
138+
return benford_calc(f)
139+
140+
def benford_calc(data):
141+
142+
count = {}
143+
for x in range(1,10):
144+
count[str(x)] = 0
145+
146+
for num in data:
147+
if num:
148+
count[str(num[0])] += 1
149+
150+
return count
151+
152+
for x in range (1,4):
153+
file_name = "accounts_" + str(x) + ".txt"
154+
data = benford_calc_file(file_name)
155+
156+
print("Results for:" + file_name)
157+
158+
for y in range(1,10):
159+
print(str(y) + " = " + str(data[str(y)]/100) + "%")
160+
161+
random_nums = [str(random.randint(1,100)) for x in range(1,10001)]
162+
data = benford_calc(random_nums)
163+
print("Results for Random Nums")
164+
165+
for y in range(1,10):
166+
print(str(y) + " = " + str(data[str(y)]/100) + "%")

0 commit comments

Comments
 (0)
0