-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.py
83 lines (67 loc) · 1.99 KB
/
funcs.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys
from random import randint
# read and return all data from file
def read_file(filename):
with open(filename, "r") as f:
data = f.read().replace("\n", " ").replace("\t", " ")
data = data.split(" ")
data = [x if ("," not in x) else x.replace(",", ".") for x in data]
while 1:
try:
data.remove("")
except ValueError:
break
try:
data = [float(x) for x in data]
except ValueError:
print("[!Err] Can not read file correctly! Reset to 0")
return [0]
return data
def find_min(data):
res = data[0]
for i in data[1:]:
if i < res:
res = i
return res
def find_max(data):
res = data[0]
for i in data[1:]:
if i > res:
res = i
return res
def find_sum(data):
summ = 0
for i in data:
summ += int(i)
return summ
def find_mult(data, flag=0):
multip = 1
for i in data:
multip *= i
if multip >= sys.maxsize ** 2 and flag == 0:
print(f"Value {multip} creates overflow")
raise OverflowError("Result is greater than sys.maxsize ^2!")
return round(multip, 3)
def main_func():
filename = ""
while 1:
filename = str(input("Enter name of file from which to read data: "))
try:
data = read_file(filename)
break
except FileNotFoundError:
print("File can't be found. Try again!")
print(f"Минимальное = {find_min(data)}")
print(f"Максимальное = {find_max(data)}")
print(f"Сумма = {find_sum(data)}")
try:
print(f"Произведение = {find_mult(data)}")
except OverflowError:
print("During multiplication overflow was caught!")
def random_arr(amount, low_lim, up_lim):
arr = []
low_lim = int(low_lim)
up_lim = int(up_lim)
for i in range(amount):
arr.append(randint(low_lim, up_lim))
return arr