-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
246 lines (177 loc) · 6.46 KB
/
algorithms.py
File metadata and controls
246 lines (177 loc) · 6.46 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import time
import math
# seed the pseudorandom number generator
from random import seed
from random import random
from random import randint
class data:
def create_list(length):
list=[]
for i in range (length):
list.append((i))
return list
def string_list(list):
text=""
for i in range (len(list)):
text+=(str(list[i])+", ")
return text
def checkList(lista):
for i in range (len(lista)-1):
if(lista[i]>lista[i+1]):
return False
return True
def sequence_search(list,search_value):
start = time.perf_counter()
for i in range (len(list)):
if(list[i]==search_value):
end = time.perf_counter()
print("The time of execution of above program is :",
(end-start) * 10**3, "ms")
return i
end = time.perf_counter()
print("The time of execution of above program is :",
(end-start) ,"sek")
return -1
def binary_search(list,search_value):
start = time.perf_counter()
if(data.checkList(list)==False):
list=data.quicksort(list)
#list=data.bubbleSort(list)
#list=data.insertion_sort(list)
lowest_index, highest_index=0,len(list)-1
while(lowest_index<=highest_index):
current_index=((lowest_index+highest_index)//2)
if(list[current_index]==search_value):
end = time.perf_counter()
print("The time of execution of above program is :",
(end-start) ,"sek")
return current_index
elif(list[current_index]<search_value):
lowest_index=current_index+1
else:
highest_index=current_index-1
end = time.perf_counter()
print("The time of execution of above program is :",
(end-start),"sek")
return -1
def bubbleSort(lista):
while True:
for i in range (len(lista)-1):
if(lista[i]>lista[i+1]):
x=lista[i]
lista[i]=lista[i+1]
lista[i+1]=x
if(data.checkList(lista[:])):
return lista[:]
def insertion_sort(lista):
#Går igenom processen ett vist antal gånger
for i in range (len(lista)):
if(data.checkList==True):
return lista
for x in range (len(lista)-1):
if(lista[x]>lista[x+1]):
for y in range(x+1):
if(lista[x+1]<lista[x-y]):
o=lista[x-y]
lista[x-y]=lista[x+1]
lista[x+1]=o
else:
break
return lista
def quicksort(lista):
if(len(lista)<=1):
return lista
left=[]
right=[]
for i in range(len(lista)):
if(lista[i]>lista[0]):
right.append(lista[i])
elif(lista[i]<lista[0]):
left.append(lista[i])
return data.quicksort(left) + [lista[0]] + data.quicksort(right)
def quicksort_v_2(lista,start,end):
if(end-start<=1):
return lista
max=end
min=start
while(max>min):
while(lista[0]>lista[max]):
max-=1
while(lista[0]<lista[min]):
min+=1
temp=lista[max]
lista[max]=lista[min]
lista[min]=temp
return data.quicksort_v_2(lista) + [lista[0]] + data.quicksort_v_2(lista)
def findBinary(decimal_number):
print("Nytt call, Decimal=:",decimal_number)
if (decimal_number == 0):
print("Decimal är 0 så returna noll")
return 0;
else:
print("Decimal%2=:",decimal_number%2,"Ska få ut",10*data.test(decimal_number // 2))
return (decimal_number % 2 + 10 *
data.findBinary(decimal_number // 2))
def test(decimal_number):
if (decimal_number == 0):
return 0;
else:
return (decimal_number % 2 + 10 * data.test(decimal_number // 2))
def to_binary(tal):
if(tal==0):
binary=0
else:
binary=tal%2+10*(data.to_binary(tal/2))
print(binary)
def fib(i,start=1,prev=0):
if(i<=0):return start
return data.fib((i-1),(prev+start),start)
def countdown(i):
if(i<=0):return "i är noll"
print(i)
return data.countdown(i-1)
def count_digits(tal):
if(tal>0):
return (1+data.count_digits(tal//10))
else:
return 0
def show_digits(tal):
if(tal>0):
return (data.count_digits())
else:
return 0
def reverse(string):
if(len(string)==0):
return ""
return data.reverse(string[1:])+string[0]
def check_palindrome(string):
if(len(string)<=1):
return True
elif(string[0]==string[len(string)-1]):
return (data.check_palindrome," ",(string[1:len(string)-1]))
return False
def digital_root(number):
if(number<10):
return number
return data.digital_root(data.digital_sum(number))
def digital_sum(number):
if(number<10):
return number
return number%10+data.digital_sum(number//10)
def in_order(node):
sträng=""
sträng+=data.in_order(node.left)
node.value+" "
sträng+=data.in_order(node.right)
def list_to_linked(list):
if(len(list)==0):
return
x=Node(list[len(list)//2])
x.left=data.list_to_linked(list[:len(list)//2])
x.right=data.list_to_linked(list[(len(list)//2):])
return x
class Node:
def __init__(self,value):
self.value=value
self.left=None
self.right=None