-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
130 lines (115 loc) · 3.5 KB
/
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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'''
Author: Tomer kigel
Contact info: e-mail: [email protected]
phone number: +972507650153
github: https://github.com/TomerKigel
'''
import copy
import math
import string
from collections import Iterable
import random
import matplotlib.pyplot as plt
from importlib import reload
import pandas as pd
def Calculate_Distance(iterable_one : Iterable ,iterable_two : Iterable) -> float:
return round(math.sqrt(sum((px - qx) ** 2.0 for px, qx in zip(iterable_one, iterable_two))), 2)
def plot_xy_graph( YSeries : list,XSeries : list) -> None:
reload(plt)
plt.plot(XSeries,YSeries)
plt.ylabel('Utilitiy')
plt.xlabel('nclo')
plt.show()
def export_to_file(data : list) -> None:
resulted_zip = zip()
for d in data:
resulted_zip = zip(resulted_zip,d)
df = pd.DataFrame(resulted_zip)
df.to_excel("results.xlsx")
def to_binary_list(number : int,amount_of_bits : int) -> list[bool]:
output_list = []
tstr = format(number,"b")
tstr = padding(tstr,amount_of_bits)
for char in tstr:
if char == '1':
output_list.append(True)
else:
output_list.append(False)
return output_list
def padding(str :string ,amount_of_bits : int) -> string:
if len(str) < amount_of_bits:
str = '0' + str
return str
def travel_time(start_location, end_location,travel_speed) -> float:
distance = Calculate_Distance(start_location, end_location)
distance_in_time = distance / travel_speed
return distance_in_time
def truth_table(source : list,value : int) -> dict:
res = {}
row = [0 for i in range(0,len(source))]
for i in range(value):
res[i] = copy.deepcopy(row)
if i == value -1:
break
j = 1
row[-j] += 1
while row[-j] == 2:
row[-j] = 0
j+=1
row[-j] += 1
row = [i for i in source]
res[value] = row
return res
def prep_permutate(source : dict,value : int) -> dict:
res = {}
max_indexes = []
indexes = []
for key in source.keys():
max_indexes.append(len(source[key]))
indexes.append(0)
for row in range(0,value):
res[row] = copy.deepcopy(indexes)
if row == value - 1:
return res
place = 0
placed = False
while not placed:
if indexes[place] < max_indexes[place]-1:
indexes[place]+=1
placed = True
else:
indexes[place] = 0
place+=1
return res
def rotate_dict(dic : dict) -> dict:
init_key = (-1,0)
prev_key = 0
for key in dic.keys():
if init_key == (-1,0):
init_key = (key,dic[key])
else:
dic[prev_key] = dic[key]
prev_key = key
if dic:
dic[key] = init_key[1]
return dic
def heapPermutation(id_list, size , lst ):
# if size becomes 1 then prints the obtained
# permutation
if len(lst) > 100:
return
if size == 1:
lst.append(copy.deepcopy(id_list))
return
for i in range(size):
heapPermutation(id_list, size - 1,lst)
if len(lst) > 100:
return
# if size is odd, swap 0th i.e (first)
# and (size-1)th i.e (last) element
# else If size is even, swap ith
# and (size-1)th i.e (last) element
if size & 1:
id_list[0], id_list[size - 1] = id_list[size - 1], id_list[0]
else:
id_list[i], id_list[size - 1] = id_list[size - 1], id_list[i]