-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
34 lines (27 loc) · 986 Bytes
/
utils.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
############################## IO UTILS ##############################
def read_lines_as_text(file):
input_text = open(file, "r").read()
input_text = input_text.split("\n")
if input_text[-1] == "":
input_text = input_text[:-1]
return input_text
def read_lines_as_int(file):
text = read_lines_as_text(file)
return [int(i) for i in text]
def read_lines_as_float(file):
text = read_lines_as_text(file)
return [float(i) for i in text]
def read_lines_as_text_raw(file):
input_text = open(file, "r").read()
return input_text
def read_line_as_integer_list(file):
input_text = open(file, "r").read()
return [int(i) for i in input_text.split(",")]
def print_list_pretty(mat):
print("\n")
for row in mat:
print(row)
############################## MATH UTILS ##############################
def calc_median(x):
x = sorted(x)
return (x[len(x)//2 - 1] + x[len(x)//2]) / 2 if len(x) % 2 == 0 else x[(len(x))//2]