-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPC002.py
More file actions
34 lines (28 loc) · 919 Bytes
/
PC002.py
File metadata and controls
34 lines (28 loc) · 919 Bytes
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
# http://www.pythonchallenge.com/pc/def/ocr.html
file = open("PC002_text.txt", "r")
text = "".join(line for line in file)
text = text.replace("\n", "")
# make a dict with the count of occurrence of every character
counter = {}
for i in text:
if i in counter.keys():
counter[i] += 1
else:
counter[i] = 1
# filter out only characters occurring only once
for char in counter:
if counter[char] > 1:
text = text.replace(char, "")
else:
continue
print(text)
# more compact solution
text = ''.join([line.rstrip() for line in open('PC002_text.txt')])
counter = {}
for c in text:
counter[c] = counter.get(c, 0) + 1
avg = len(text) // len(counter)
# regular dict in counter can be replaced by collections.OrderedDict() by
# which the next line would iterate only over the dict, thus making next line
# more efficient
print(''.join([c for c in text if counter[c] < avg]))