diff --git a/BinarySearch.py b/BinarySearch.py index 51607e5..cd97dd9 100644 --- a/BinarySearch.py +++ b/BinarySearch.py @@ -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) diff --git a/bartender.py b/bartender.py index f176f28..ae3f4a5 100644 --- a/bartender.py +++ b/bartender.py @@ -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?") \ No newline at end of file diff --git a/fibonacci.py b/fibonacci.py index b19b836..853313c 100644 --- a/fibonacci.py +++ b/fibonacci.py @@ -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() \ No newline at end of file +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))