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
38 changes: 16 additions & 22 deletions BinarySearch.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
#A searching technique which has a time complexity of O(log n)
def binarySearch(arr, l, r, x):
mid = (l + r) // 2
if l <= r:
if x == arr[mid]:
return mid
elif x < arr[mid]:
return binarySearch(arr, l, mid-1, x)
else:
return binarySearch(arr, mid+1, r, x)
return -1

l = [int(x) for x in input().split()] #takes list as input
ele = int(input()) #reading the required number
lb, ub = 0, len(l) - 1 #defining the lower bound and the upper bound
mid = (lb + ub) // 2
flag = 0
# add l.sort() if input can be unsorted as this searching method is applicable only for sorted list

while(lb <= ub):
mid = (lb + ub) // 2

if ele == l[mid]:
flag = 1
break
elif ele > l[mid]:
lb = mid + 1
else:
ub = mid - 1

if flag == 1:
print("Element Found")
else:
print("Element NOT Found")
if __name__ == '__main__':
number = 26
listOfNumber = [0, 1, 6, 14, 15, 17, 18, 20, 21, 26, 28, 32, 44,
47, 59, 61, 63, 64, 65, 67, 70, 71, 75, 78, 80, 82, 96, 99]
index = binarySearch(listOfNumber, 0, len(listOfNumber) - 1, number)
print(index)
11 changes: 3 additions & 8 deletions bartender.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
# A little python program in light of Hacktoberfest 2019 :)

legal_age = 21

print("-- Welcome to the Hacktoberfest bar --")

legalage = 21
age = int(input("How old are you? "))

if age >= legal_age:
if age >= legalage:
print("Have a beer!")
elif age == 20:
print("So close..one more year kid.")
else:
print(f"Sorry, {age} is too young for a beer, how about a soda?")
print("Sorry",age," is too young for a beer, how about a soda?")

33 changes: 13 additions & 20 deletions fibonacci.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import sys

def generate_fibonacci(num):
if num <= 1 :
return num
else :
return (generate_fibonacci (num - 2 ) + generate_fibonacci (num - 1 ))

def main():
terms = int ( input ( " How many terms? " ))
if terms <= 0 :
print("Enter more than 0 terms!")
sys.exit()
else :
print("Fibonacci sequence for {} terms is: ".format(terms))
for term in range (terms):
print (generate_fibonacci (term))

if __name__ == '__main__':
main()
n=int(input())
def Fibonacci(n):
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==1:
return 0
# Second Fibonacci number is 1
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
print(Fibonacci(n))