-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomDouble.py
More file actions
68 lines (51 loc) · 1.84 KB
/
RandomDouble.py
File metadata and controls
68 lines (51 loc) · 1.84 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
import random
import time
import datetime
# Reads integers from the given file and returns them in an array.
def read_integers_from_file(file_path):
integer_array = []
with open(file_path, 'r') as file:
for line in file:
try:
integer_array.append(int(line.strip()))
except ValueError:
print(f"Skipping invalid line: {line.strip()}")
return integer_array
# Reads integers from the given file line by line and writes them in another file after doubling.
def read_integers_from_file_and_write(file_path, output_file_path):
f = open("newfile2.txt", "a")
with open(file_path, 'r') as file:
for line in file:
try:
f.write(f'{2 * int(line.strip())}\n')
except ValueError:
print(f"Skipping invalid line: {line.strip()}")
f.close()
print(datetime.datetime.now())
start_time = time.time()
# Code to read all at once and write later. Uncomment to execute.
#integer_array = read_integers_from_file("file2.txt")
#f = open("newfile2.txt", "a")
#for i in integer_array:
# f.write(f'{i * 2}\n')
#f.close()
# Code to read line by line and write it. Uncomment to execute.
# read_integers_from_file_and_write("file2.txt", "newfile2.txt")
#Code to read two files separately.
integer_array = read_integers_from_file("file2.txt")
f = open("newfile2.txt", "a")
for i in integer_array:
f.write(f'{i * 2}\n')
f.close()
integer_array = read_integers_from_file("file3.txt")
f = open("newfile2.txt", "a")
for i in integer_array:
f.write(f'{i * 2}\n')
f.close()
print(datetime.datetime.now())
end_time = time.time()
elapsed_time = end_time - start_time
hours = int(elapsed_time // 3600)
minutes = int((elapsed_time % 3600) // 60)
seconds = int(elapsed_time % 60)
print(f"Time taken: {hours} hours, {minutes} minutes, {seconds} seconds")