forked from DarkMemem/PythonIntro09
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c10d71d
commit 22a83e3
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from random import randint | ||
|
||
|
||
def binary_search(array, key_value, left=0, right=None): | ||
if right is None: | ||
right = len(array) | ||
|
||
middle = (left + right) // 2 | ||
while array[middle] != key_value and left <= right: | ||
if array[middle] < key_value: | ||
left = middle + 1 | ||
else: | ||
right = middle - 1 | ||
|
||
middle = (left + right) // 2 | ||
|
||
return (True, middle) if not (left > right) else (False, middle+1) | ||
|
||
|
||
# Creating a sorted list of random values | ||
lst = [] | ||
for i in range(15): | ||
lst.append(randint(1, 50)) | ||
lst.sort() | ||
print(lst) | ||
|
||
key = 23 | ||
flag, idx = binary_search(lst, key) | ||
|
||
print('Flag =', flag) | ||
print('Index =', idx) | ||
|
||
# if key value not found, we can insert this value by index | ||
if not flag: | ||
lst.insert(idx, key) | ||
|
||
print(lst) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import random | ||
|
||
|
||
def bubble_sort(my_list): | ||
cnt = 0 | ||
for i in range(len(my_list)-1): | ||
cnt += 1 | ||
flag = True | ||
for j in range(len(my_list)-1-i): | ||
if my_list[j] > my_list[j+1]: | ||
my_list[j], my_list[j+1] = my_list[j+1], my_list[j] | ||
flag = False | ||
|
||
if flag: | ||
break | ||
|
||
return cnt | ||
|
||
|
||
lst = [random.randint(10, 99) for _ in range(30)] | ||
print(lst) | ||
res = bubble_sort(lst) | ||
print(res) | ||
print(lst) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import random | ||
|
||
lst = [random.randint(10, 99) for _ in range(30)] | ||
print(lst) | ||
|
||
key = int(input('Please enter a value: ')) | ||
|
||
for idx in range(len(lst)): | ||
if lst[idx] == key: | ||
print('idx =', idx) | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import random | ||
|
||
|
||
def quick_sort(array, first_idx, last_idx): | ||
if first_idx >= last_idx: | ||
return | ||
|
||
i, j = first_idx, last_idx | ||
middle_value = array[(first_idx + last_idx) // 2] | ||
while i <= j: | ||
while array[i] < middle_value: | ||
i += 1 | ||
|
||
while array[j] > middle_value: | ||
j -= 1 | ||
|
||
if i <= j: | ||
array[i], array[j] = array[j], array[i] | ||
i, j = i+1, j-1 | ||
|
||
quick_sort(array, first_idx, j) | ||
quick_sort(array, i, last_idx) | ||
|
||
|
||
lst = [random.randint(10, 99) for _ in range(30)] | ||
print(lst) | ||
quick_sort(lst, 0, len(lst)-1) | ||
print(lst) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
def pow_iter(num, ex): | ||
res = 1 | ||
while ex > 0: | ||
res *= num | ||
ex -= 1 | ||
|
||
return res | ||
|
||
|
||
def pow_rec(num, ex): | ||
if ex == 0: | ||
return 1 | ||
|
||
return num * pow_rec(num, ex-1) | ||
|
||
|
||
print(pow_iter(2, 4)) | ||
print(pow_rec(2, 4)) | ||
|
||
|
||
def fact_iter(num): | ||
res = 1 | ||
for i in range(1, num+1): | ||
res *= i | ||
|
||
return res | ||
|
||
|
||
def fact_rec(num): | ||
if num == 1: | ||
return 1 | ||
|
||
return num * fact_rec(num-1) | ||
|
||
|
||
print(fact_iter(5)) | ||
print(fact_rec(5)) | ||
|
||
|
||
def fib_iter(num): | ||
n1 = 0 | ||
n2 = 1 | ||
while num > 1: | ||
res = n1 + n2 | ||
n1 = n2 | ||
n2 = res | ||
num -= 1 | ||
|
||
return n2 | ||
|
||
|
||
def fib_rec(num): | ||
# if num == 0 or num == 1: | ||
# return num | ||
|
||
return num if num == 0 or num == 1 else fib_rec(num-1) + fib_rec(num-2) | ||
|
||
|
||
print(fib_iter(14)) | ||
print(fib_rec(14)) |