Skip to content

Commit 1831c72

Browse files
committed
6thCommit
1 parent 6c47e61 commit 1831c72

10 files changed

+141
-21
lines changed

BinarySearch.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def binarySearch(list, key):
2+
first = 0
3+
last = len(list) - 1
4+
while first <= last: # Changed from 'while(first < last)' to 'while first <= last'
5+
mid = (first + last) // 2 # Use floor division '//' to get integer value
6+
if list[mid] == key:
7+
return mid
8+
elif list[mid] < key:
9+
first = mid + 1
10+
else:
11+
last = mid - 1
12+
return -1 # Return -1 if the key is not found in the list
13+
14+
list = [1, 2, 3, 4, 5]
15+
key = 3
16+
print(binarySearch(list, key))

Fib.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def fibonacci(n):
2+
if n <= 0:
3+
return "Invalid input. Please provide a positive integer."
4+
elif n == 1:
5+
return 0
6+
elif n == 2:
7+
return 1
8+
else:
9+
return fibonacci(n - 1) + fibonacci(n - 2)
10+
11+
# Input the number of terms for the Fibonacci series
12+
num_terms = int(input("Enter the number of terms for the Fibonacci series: "))
13+
14+
if num_terms <= 0:
15+
print("Please enter a positive integer.")
16+
else:
17+
print("Fibonacci Series:")
18+
for i in range(1, num_terms + 1):
19+
print(fibonacci(i), end=" ")

Filetr.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def greater(n):
2+
if(n > 50):
3+
return n
4+
numbers = [20, 10, 60, 70, 5, 90, 110]
5+
result = filter(greater, numbers)
6+
print(list(result))

HOF.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Higher Order Function - A function can be passed as argument, return as output
2+
def small(txt):
3+
return txt.lower()
4+
def capital(txt):
5+
return txt.upper()
6+
def all(func):
7+
greeting = func("Hi This is Ram, programming for function as an argument")
8+
print(greeting)
9+
all(small)
10+
all(capital)

LinearSearch.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#LinearSearch
2+
def linearSearch(list, key):
3+
for i in range(len(list)):
4+
if (list[i] == key):
5+
return True
6+
7+
return False
8+
list = [1, 2, 3, 4, 5]
9+
key = 4
10+
if(linearSearch(list, key)):
11+
print("Found")
12+
else:
13+
print("Not found")

MapFunction.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Function to square a number
2+
def square(x):
3+
return x ** 2
4+
# List of numbers
5+
numbers = [1, 2, 3, 4, 5]
6+
# Applying the square function to each element in the numbers list using map()
7+
squared_numbers = map(square, numbers)
8+
# Convert the map object to a list to see the results
9+
squared_numbers_list = list(squared_numbers)
10+
print(squared_numbers_list) # Output: [1, 4, 9, 16, 25]

SelectionSort.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def selectionSort(list):
2+
for i in range (len(list)-1):
3+
tmp = i
4+
for j in range(i+1, len(list)):
5+
if(list[tmp] > list[j]):
6+
tmp = j
7+
temp = list[tmp]
8+
list[tmp] = list[i]
9+
list[i] = temp
10+
def printList(list):
11+
for i in range(len(list)):
12+
print(list[i], end=" ")
13+
list = [9, 6, 2, 5, 8]
14+
selectionSort(list)
15+
printList(list)

TOH.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
# Tower of Hanoi
2-
3-
def Toh(n, source, destination, medium):
4-
if(n == 1):
5-
print("Move disk 1 from", source, "to", destination)
1+
def TowerOfHanoi(n, source, medium, destination):
2+
if n == 1:
3+
print("Move disc " + str(n) + " from Source: " + source + " to Destination: " + destination)
64
return
7-
Toh(n-1, source, medium, destination)
8-
print("Move disk n from", source, "to", destination)
9-
Toh(n-1, destination, source, medium)
10-
Toh(3, 'A', 'B', 'C')
11-
12-
# def tower_of_hanoi(n, source, destination, auxiliary):
13-
# if n == 1:
14-
# print("Move disk 1 from", source, "to", destination)
15-
# return
16-
# tower_of_hanoi(n - 1, source, auxiliary, destination)
17-
# print("Move disk", n, "from", source, "to", destination)
18-
# tower_of_hanoi(n - 1, auxiliary, destination, source)
19-
5+
TowerOfHanoi(n-1, source, destination, medium)
6+
print("Move disc " + str(n) + " from Source: " + source + " to Destination: " + destination)
7+
TowerOfHanoi(n-1, medium, source, destination)
208

21-
# # Example usage
22-
# n = 3 # Number of disks
23-
# tower_of_hanoi(n, 'A', 'C', 'B')
9+
print("Steps to solve Tower of Hanoi with 3 disks:")
10+
TowerOfHanoi(3, "A", "B", "C")

mergeSort.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def mergeSort(list, si, ei):
2+
if si >= ei:
3+
return
4+
mid = (si + ei) // 2
5+
mergeSort(list, si, mid)
6+
mergeSort(list, mid + 1, ei)
7+
merge(list, mid, si, ei)
8+
9+
def merge(list, mid, si, ei):
10+
temp = []
11+
i = si
12+
j = mid + 1
13+
while i <= mid and j <= ei:
14+
if list[i] < list[j]:
15+
temp.append(list[i])
16+
i += 1
17+
else:
18+
temp.append(list[j])
19+
j += 1
20+
21+
while i <= mid:
22+
temp.append(list[i])
23+
i += 1
24+
25+
while j <= ei:
26+
temp.append(list[j])
27+
j += 1
28+
29+
i = si
30+
for item in temp:
31+
list[i] = item
32+
i += 1
33+
34+
def printList(list):
35+
for i in range(len(list)):
36+
print(list[i], end=" ")
37+
38+
list = [6, 3, 9, 5, 2, 8]
39+
si = 0
40+
ei = len(list) - 1
41+
mergeSort(list, si, ei)
42+
printList(list)

tempCodeRunnerFile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
elif n == 2:
2+
# return 1

0 commit comments

Comments
 (0)