Skip to content

Commit ceea743

Browse files
practice programs
1 parent 260a4e8 commit ceea743

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

Native-Datatypes/reverse_string.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def len_str(string):
2+
count = 0
3+
for _ in string:
4+
count += 1
5+
return count
6+
7+
def string_reversed(string):
8+
len_string = len_str(string)-1
9+
# print(len_string)
10+
string_reversed = ''
11+
dict_count = {}
12+
13+
while len_string >= 0:
14+
string_reversed += string[len_string]
15+
16+
if string[len_string] not in dict_count:
17+
dict_count[string[len_string]] = 1
18+
else:
19+
dict_count[string[len_string]] += 1
20+
21+
len_string -= 1
22+
return string_reversed # , dict_count
23+
24+
# print(string_reversed('Hello World!'))
25+
26+
def string_splitter(string):
27+
tmp = ''
28+
splitted_list = []
29+
for c in string:
30+
if c != ' ':
31+
tmp += c
32+
else:
33+
splitted_list += tmp[::-1]
34+
tmp = ''
35+
if tmp:
36+
splitted_list += tmp[::-1]
37+
38+
return ''.join(splitted_list)
39+
40+
print(string_splitter("Hello World"))

binary_search_tree/binary_search_question_2.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
77
"Sorted list" refers to a list where the elements are arranged in the increasing order e.g. [1, 3, 5, 7].'''
88

9-
10-
119
def linear_rotated_search(list_of_num):
1210

1311
count = 0
@@ -20,4 +18,44 @@ def linear_rotated_search(list_of_num):
2018
count += 1
2119
return 0
2220

23-
print(linear_rotated_search([3,1,0]))
21+
def binary_search(lo, hi, condition):
22+
while lo <= hi:
23+
mid = (lo + hi)//2
24+
result = condition(mid)
25+
if result == 'found':
26+
return mid
27+
elif result == 'left':
28+
hi = mid - 1
29+
else:
30+
lo = mid + 1
31+
return 0
32+
33+
34+
def binary_rotated_array(list_of_num):
35+
lo = 0
36+
hi = len(list_of_num)-1
37+
def condition(mid):
38+
# position = 0
39+
if mid > 0 and list_of_num[mid - 1] > list_of_num[mid]:
40+
return 'found'
41+
elif list_of_num[mid] < list_of_num[hi]:
42+
return 'left'
43+
else:
44+
return 'right'
45+
return binary_search(0, len(list_of_num)-1, condition)
46+
47+
def target_array_value(list_of_num, target):
48+
pivot = binary_rotated_array(list_of_num)
49+
def condition(mid):
50+
if list_of_num[mid] == target:
51+
return 'found'
52+
if list_of_num[pivot] < list_of_num[len(list_of_num)-1]:
53+
return 'right'
54+
else:
55+
return 'left'
56+
return binary_search(0, len(list_of_num)-1, condition)
57+
58+
59+
# print(linear_rotated_search([3,1,0]))
60+
# print(binary_rotated_array([5,1,2,3,4]))
61+
print(target_array_value([1,2,3,4,5,6,7,8,9], 4))

sorting/sort_program.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# problem 1- write aprogram to sort a list of numbers
2+
3+
# input-output format: a list of unsorted numbers will be given to our function and function will give ouput sorted list.
4+
5+
# testcases
6+
# 1. randomly unsorted array
7+
# 2. empty list
8+
# 3. containing one element
9+
# 4. list containing repeating element
10+
# 5. list sorted in descending order.
11+
# 6. Long list
12+
# 7. Already Sorted

0 commit comments

Comments
 (0)