Skip to content

Commit 8b40d71

Browse files
authored
Merge branch 'main' into patch-1
2 parents 0f39db6 + 852428e commit 8b40d71

File tree

6 files changed

+154
-0
lines changed

6 files changed

+154
-0
lines changed

BinarySearch

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def binary_search(arr, x):
2+
low = 0
3+
high = len(arr) - 1
4+
mid = 0
5+
6+
while low <= high:
7+
8+
mid = (high + low) // 2
9+
10+
11+
if arr[mid] < x:
12+
low = mid + 1
13+
14+
15+
elif arr[mid] > x:
16+
high = mid - 1
17+
18+
19+
else:
20+
return mid
21+
22+
23+
return -1
24+
25+
26+
# Test array
27+
arr = [ 1, 10, 15, 20, 30 ]
28+
x = 20
29+
30+
# Function call
31+
result = binary_search(arr, x)
32+
33+
if result != -1:
34+
print("Element is present at index", str(result))
35+
else:
36+
print("Element is not present in array")

MatrixMultiplier.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
# taking a 3x3 matrix
4+
A = [[12, 7, 3],
5+
[4, 5, 6],
6+
[7, 8, 9]]
7+
8+
# take a 3x4 matrix
9+
B = [[5, 8, 1, 2],
10+
[6, 7, 3, 0],
11+
[4, 5, 9, 1]]
12+
13+
# result will be 3x4
14+
result = [[sum(a * b for a, b in zip(A_row, B_col))
15+
for B_col in zip(*B)]
16+
for A_row in A]
17+
#Now displaying the result
18+
for r in result:
19+
print(r)

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,13 @@ Feel free to add basic python scripts/programs that helped you learn python. I h
3636
19. [Insertion Sort](https://github.com/musaibbatghar/Beginners-Python-Projects/blob/main/Insertion_Sort.py) - [Musaib Batghar](https://github.com/musaibbatghar)
3737
20. [Snake game](https://github.com/Aishanipach/Beginners-Python-Projects/compare/main...meanshkumar:Beginners-Python-Projects:main?expand=1)- [Ansh Kumar](https://github.com/meanshkumar)
3838
21. [Merge_Sort](https://github.com/proPrateekSahu/Beginners-Python-Projects/blob/main/MergeSort.py) - [Prateek Kumar Sahu](https://github.com/proPrateekSahu)
39+
3940
22. [Binary_Search](https://github.com/proPrateekSahu/Beginners-Python-Projects/blob/main/BinarySearch) - [Prateek Kumar Sahu](https://github.com/proPrateekSahu)
41+
42+
43+
23. [Fibonacci_Series](https://github.com/Skillz619/Beginners-Python-Projects/blob/main/fibonacci_series.py) - [Shreekar Kolanu](https://github.com/Skillz619)
44+
45+
24. [Binary_Search](https://github.com/proPrateekSahu/Beginners-Python-Projects/blob/main/BinarySearch) - [Prateek Kumar Sahu](https://github.com/proPrateekSahu)
46+
25. [TowerOfHoni](https://github.com/proPrateekSahu/Beginners-Python-Projects/blob/main/towerOfHoni.py) - [Prateek Kumar Sahu](https://github.com/proPrateekSahu)
47+
26. [Matrix_Multiplier](https://github.com/proPrateekSahu/Beginners-Python-Projects/blob/main/MatrixMultiplier.py) - [Prateek Kumar Sahu](https://github.com/proPrateekSahu)
48+

fibonacci_series.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Program to display the Fibonacci sequence up to n-th term
2+
3+
nterms = int(input("How many terms? "))
4+
5+
# first two terms
6+
n1, n2 = 0, 1
7+
count = 0
8+
9+
# check if the number of terms is valid
10+
if nterms <= 0:
11+
print("Please enter a positive integer")
12+
13+
# if there is only one term, return n1
14+
elif nterms == 1:
15+
print("Fibonacci sequence upto",nterms,":")
16+
print(n1)
17+
18+
# generate fibonacci sequence
19+
else:
20+
print("Fibonacci sequence:")
21+
while count < nterms:
22+
print(n1)
23+
nth = n1 + n2
24+
# update values
25+
n1 = n2
26+
n2 = nth
27+
count += 1

snake_game.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from random import randrange
2+
from freegames import square,vector
3+
4+
food=vector(0,0)
5+
snake=[vector(10,0)]
6+
aim=vector(0,-10)
7+
8+
def change(x,y):
9+
aim.x=x
10+
aim.y=y
11+
12+
def inside(head):
13+
return -200 < head.x < 190 and -200 < head.y < 190
14+
15+
def move():
16+
head=snake[-1].copy()
17+
head.move(aim)
18+
if not inside(head) or head in snake:
19+
square(head.x,head.y,9,"red")
20+
update()
21+
return
22+
snake.append()
23+
24+
if head==food:
25+
print("snake",len(snake))
26+
food.x=randrange(-15,15)*10
27+
food.y=randrange(-15,15)*10
28+
else:
29+
snake.pop(0)
30+
clear()
31+
32+
for body in snake:
33+
square(body.x,body.y,9,"green")
34+
35+
square(food.x,food,y,9,"red")
36+
update()
37+
ontimer(move,100)
38+
39+
hideturtle()
40+
tracer(false)
41+
listen()
42+
onkey(lambda:changes(10,0),"Right")
43+
onkey(lambda:changes(-10,0),"Left")
44+
onkey(lambda:changes(0,10),"Up")
45+
onkey(lambda:changes(0,-10),"Down")
46+
47+
move()
48+
done()

towerOfHoni.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Recursive Code
2+
3+
def TowerOfHanoi(n , source, destination, auxiliary):
4+
if n==1:
5+
print ("Move disk 1 from source",source,"to destination",destination)
6+
return
7+
TowerOfHanoi(n-1, source, auxiliary, destination)
8+
print ("Move disk",n,"from source",source,"to destination",destination)
9+
TowerOfHanoi(n-1, auxiliary, destination, source)
10+
11+
#Test Input
12+
n = 4
13+
TowerOfHanoi(n,'A','B','C')
14+
# Consider A B and C as the names of rods
15+

0 commit comments

Comments
 (0)