Skip to content

Commit 0132c65

Browse files
authored
Create Exercise-23-File-Overlap.py
1 parent 1d7b0bf commit 0132c65

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Exercise-23-File-Overlap.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
Exercise 23: File Overlap
3+
4+
Given two .txt files that have lists of numbers in them, find the
5+
numbers that are overlapping. One .txt file has a list of all prime
6+
numbers under 1000, and the other .txt file has a list of happy
7+
numbers up to 1000.
8+
9+
'''
10+
# Solution
11+
def read_file(filename):
12+
"""
13+
Read a text file and store all lines into a list.
14+
15+
Arguments:
16+
filename -- file name.
17+
18+
Returns:
19+
lines -- a list contain all lines in the file.
20+
"""
21+
with open(filename, 'r') as open_file:
22+
lines = open_file.read().splitlines()
23+
return lines
24+
25+
def main():
26+
file1 = map(int, read_file('primenumbers.txt'))
27+
file2 = map(int, read_file('happynumbers.txt'))
28+
overlap = list(set(file1) & set(file2))
29+
print(sorted(overlap))
30+
31+
if __name__ == "__main__":
32+
main()
33+
34+
# Test Part
35+
# >>> %Run test.py
36+
# [7, 13, 19, 23, 31, 79, 97, 103, 109, 139, 167, 193, 239, 263, 293, 313, 331, 367, 379, 383, 397, 409, 487, 563, 617, 653, 673, 683, 709, 739, 761, 863, 881, 907, 937]

0 commit comments

Comments
 (0)