Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div align="center"><h1>Algorithm</h1></div>

Algorithm implemented in Python.
<div align="center"><h1>Algorithm</h1></div>
Algorithm implemented in Python.
68 changes: 34 additions & 34 deletions dp/dice_combinations.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
'''
Dice Combinations

Task is to count the number of ways to construct sum n
by throwing a dice one or more times. Each throw produces
an outcome between 1 and 6.

For example, if n=3, there are 4 ways:
* 1+1+1
* 1+2
* 2+1
* 3

'''

n = int(input("Enter the value of n: "))

dp = [0] * (n+1)
dp[0] = 1

for i in range(1,n+1):
dp[i] += dp[i-1]
if i>1:
dp[i]+=dp[i-2]
if i>2:
dp[i]+=dp[i-3]
if i>3:
dp[i]+=dp[i-4]
if i>4:
dp[i]+=dp[i-5]
if i>4:
dp[i]+=dp[i-6]

print(dp[n])
'''
Dice Combinations
Task is to count the number of ways to construct sum n
by throwing a dice one or more times. Each throw produces
an outcome between 1 and 6.
For example, if n=3, there are 4 ways:
* 1+1+1
* 1+2
* 2+1
* 3
'''
n = int(input("Enter the value of n: "))
dp = [0] * (n+1)
dp[0] = 1
for i in range(1,n+1):
dp[i] += dp[i-1]
if i>1:
dp[i]+=dp[i-2]
if i>2:
dp[i]+=dp[i-3]
if i>3:
dp[i]+=dp[i-4]
if i>4:
dp[i]+=dp[i-5]
if i>4:
dp[i]+=dp[i-6]
print(dp[n])
38 changes: 19 additions & 19 deletions dp/fibonacci.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
'''
Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, ....
Fib(0) = 0
Fib(1) = 1
Fib(N) = Fib(N-1) + Fib(N-2) for N>1

This program uses dynamic programming to solve this
Time complexity : O(N)
'''

n = int(input("Howmany terms of series do you want to print? "))

dp = [0,1]

for i in range(n-1):
dp.append( dp[i] + dp[i+1] )

print("The first n terms of Fibonacci series are: ")
print(*dp)
'''
Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, ....
Fib(0) = 0
Fib(1) = 1
Fib(N) = Fib(N-1) + Fib(N-2) for N>1
This program uses dynamic programming to solve this
Time complexity : O(N)
'''
n = int(input("Howmany terms of series do you want to print? "))
dp = [0,1]
for i in range(n-1):
dp.append( dp[i] + dp[i+1] )
print("The first n terms of Fibonacci series are: ")
print(*dp)
70 changes: 35 additions & 35 deletions searching/binarySearch.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@


def binarySearch (arr, l, r, x):


if r >= l:

mid = l + (r - l) // 2


if arr[mid] == x:
return mid


elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)


else:
return binarySearch(arr, mid + 1, r, x)

else:

return -1

n = int(input("Enter the size of array : "))

arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n]
x=int(input("Enter the element to search : "))

result = binarySearch(arr, 0, len(arr)-1, x)

if result != -1:
print ("Element is present at index % d" % result)
else:
def binarySearch (arr, l, r, x):
if r >= l:
mid = l + (r - l) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)
else:
return binarySearch(arr, mid + 1, r, x)
else:
return -1
n = int(input("Enter the size of array : "))
arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n]
x=int(input("Enter the element to search : "))
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print ("Element is present at index % d" % result)
else:
print ("Element is not present in array")
78 changes: 39 additions & 39 deletions searching/fibonacciSearch.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
from bisect import bisect_left

def fibMonaccianSearch(arr, x, n):
fibMMm2 = 0
fibMMm1 = 1
fibM = fibMMm2 + fibMMm1

while (fibM < n):
fibMMm2 = fibMMm1
fibMMm1 = fibM
fibM = fibMMm2 + fibMMm1
offset = -1

while (fibM > 1):
i = min(offset+fibMMm2, n-1)
if (arr[i] < x):
fibM = fibMMm1
fibMMm1 = fibMMm2
fibMMm2 = fibM - fibMMm1
offset = i
elif (arr[i] > x):
fibM = fibMMm2
fibMMm1 = fibMMm1 - fibMMm2
fibMMm2 = fibM - fibMMm1
else:
return i
if(fibMMm1 and arr[n-1] == x):
return n-1
return -1


n = int(input("Enter the size of array : "))
arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n]
x = int(input("Enter the number to search : "))
ind = fibMonaccianSearch(arr, x, n)
if ind>=0:
print("Found at index:",ind)
else:
print(x,"isn't present in the array");
from bisect import bisect_left
def fibMonaccianSearch(arr, x, n):
fibMMm2 = 0
fibMMm1 = 1
fibM = fibMMm2 + fibMMm1
while (fibM < n):
fibMMm2 = fibMMm1
fibMMm1 = fibM
fibM = fibMMm2 + fibMMm1
offset = -1
while (fibM > 1):
i = min(offset+fibMMm2, n-1)
if (arr[i] < x):
fibM = fibMMm1
fibMMm1 = fibMMm2
fibMMm2 = fibM - fibMMm1
offset = i
elif (arr[i] > x):
fibM = fibMMm2
fibMMm1 = fibMMm1 - fibMMm2
fibMMm2 = fibM - fibMMm1
else:
return i
if(fibMMm1 and arr[n-1] == x):
return n-1
return -1
n = int(input("Enter the size of array : "))
arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n]
x = int(input("Enter the number to search : "))
ind = fibMonaccianSearch(arr, x, n)
if ind>=0:
print("Found at index:",ind)
else:
print(x,"isn't present in the array");
36 changes: 36 additions & 0 deletions searching/interpolationSearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@


def interpolationSearch(arr, lo, hi, x):


if (lo <= hi and x >= arr[lo] and x <= arr[hi]):


pos = lo + ((hi - lo) // (arr[hi] - arr[lo]) *
(x - arr[lo]))


if arr[pos] == x:
return pos

]
if arr[pos] < x:
return interpolationSearch(arr, pos + 1,
hi, x)

if arr[pos] > x:
return interpolationSearch(arr, lo,
pos - 1, x)
return -1

n = int(input("Enter the size of array : "))

arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n]
x=int(input("Enter element to search : "))

index = interpolationSearch(arr, 0, n - 1, x)

if index != -1:
print("Element found at index", index)
else:
print("Element not found")
78 changes: 39 additions & 39 deletions searching/jumpSearch.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import math

def jumpSearch( arr , x , n ):


step = math.sqrt(n)


prev = 0
while arr[int(min(step, n)-1)] < x:
prev = step
step += math.sqrt(n)
if prev >= n:
return -1


while arr[int(prev)] < x:
prev += 1


if prev == min(step, n):
return -1


if arr[int(prev)] == x:
return prev

return -1


n = int(input("Enter the size of array : "))

arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n]
x=int(input("Enter element to search : "))


index = jumpSearch(arr, x, n)


import math
def jumpSearch( arr , x , n ):
step = math.sqrt(n)
prev = 0
while arr[int(min(step, n)-1)] < x:
prev = step
step += math.sqrt(n)
if prev >= n:
return -1
while arr[int(prev)] < x:
prev += 1
if prev == min(step, n):
return -1
if arr[int(prev)] == x:
return prev
return -1
n = int(input("Enter the size of array : "))
arr = list(map(int, input("Enter the array elements :\n").strip().split()))[:n]
x=int(input("Enter element to search : "))
index = jumpSearch(arr, x, n)
print("Number" , x, "is at index" ,"%.0f"%index)
Loading