You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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.
4
4
# Work out which of the files contains fake data.
5
5
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
+
forxinf:
23
+
ifx:
24
+
count[x[0]] +=1
25
+
26
+
foryinrange(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
+
forxinrange(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
+
fornuminf:
47
+
ifnum:
48
+
count[num[0]] +=1
49
+
print(x)
50
+
foryinrange(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
6
54
forxinrange(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
-
fornuminf:
22
-
ifnum:
23
-
count[num[0]] +=1
24
-
25
-
print(x)
26
-
foryinrange(1,10):
27
-
print(str(y) +" = "+str(count[str(y)]/100) +"%")
55
+
f=open("accounts_"+str(x) +".txt", "r")
56
+
57
+
count= {}
58
+
foriinrange(1,10):
59
+
count[str(i)] =0
60
+
61
+
fornuminf:
62
+
ifnum:
63
+
count[num[0]] +=1
64
+
print(x)
65
+
foryinrange(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
+
defbenford_calc(file_name):
71
+
f=open(file_name, "r")
72
+
73
+
count= {}
74
+
foriinrange(1,10):
75
+
count[str(i)] =0
76
+
77
+
fornuminf:
78
+
ifnum:
79
+
count[num[0]] +=1
80
+
print("Results for:"+file_name)
81
+
foryinrange(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
+
defbenford_calc(file_name):
90
+
f=open(file_name, "r")
91
+
92
+
count= {}
93
+
foriinrange(1,10):
94
+
count[str(i)] =0
95
+
96
+
fornuminf:
97
+
ifnum:
98
+
count[num[0]] +=1
99
+
print("Results for:"+file_name)
100
+
foryinrange(1,10):
101
+
print(str(y) +" = "+str(count[str(y)]/100) +"%")
102
+
103
+
forxinrange (1,4):
104
+
file_name="accounts_"+str(x) +".txt"
105
+
benford_calc(file_name)
106
+
107
+
# STAGE 6 - Separating file function from data function
0 commit comments