diff --git a/day1/solution d1-1.py b/day1/solution d1-1.py new file mode 100644 index 0000000..b69e3b9 --- /dev/null +++ b/day1/solution d1-1.py @@ -0,0 +1,11 @@ +Name = input("Enter your Name : ") +Branch = input("Tell me your Branch : ") +Gender = input("what is your Gender : ") +College = input("Enter your College Name : ") +Age = int(input("Enter your Age : ")) + +print("Name: ",Name) +print("Branch:",Branch) +print("Gender:",Gender) +print("College:",College) +print("Age:",Age) diff --git a/day1/solution d1-2.py b/day1/solution d1-2.py new file mode 100644 index 0000000..ef3869f --- /dev/null +++ b/day1/solution d1-2.py @@ -0,0 +1,3 @@ +number = float(input("Enter the number :")) +square_root = number ** 2 +print("square root of ",number , "is",square_root ) diff --git a/day1/solution d1-3.py b/day1/solution d1-3.py new file mode 100644 index 0000000..13d50ae --- /dev/null +++ b/day1/solution d1-3.py @@ -0,0 +1,9 @@ +a = int(input("Enter the value of a : ")) #3 +b = int(input("Enter the value of b : ")) #4 +a = a + b #a=7 ,b=4 +b = a - b # b=3 ,a=7 +a = a - b # a=4 ,b=3 + +print("After Swaping") +print("Value of a =", a) +print("Value of b =", b) diff --git a/day1/solution d1-4.py b/day1/solution d1-4.py new file mode 100644 index 0000000..64faf55 --- /dev/null +++ b/day1/solution d1-4.py @@ -0,0 +1,6 @@ +cost_price = float(input("Enter the cost price of the product :")) +sell_price = float(input("Enter the sell price of the product :")) +profit = sell_price - cost_price +NewSellingPrice = 1.05 * profit + cost_price +print("The profit from this sell is " ,profit) +print("To increase the profit by 5% the selling price should be ",NewSellingPrice) diff --git a/day1/solution d1-5.py b/day1/solution d1-5.py new file mode 100644 index 0000000..03a94cf --- /dev/null +++ b/day1/solution d1-5.py @@ -0,0 +1,19 @@ +Batsman1 = int(input("Enter the runs scored by first batsman in 60 balls: ")) +Batsman2 = int(input("Enter the runs scored by second batsman in 60 balls: ")) +Batsman3 = int(input("Enter the runs scored by third batsman in 60 balls: ")) + +Strikerate1 = Batsman1 * 100 / 60 +Strikerate2 = Batsman2 * 100 / 60 +Strikerate3 = Batsman3 * 100 / 60 + +print("strike rate of first batsman is ", Strikerate1) +print("strike rate of second batsman is ", Strikerate2) +print("strike rate of third batsman is ", Strikerate3) + +print("Runs scored by first batsman if they played 60 more balls is ", Batsman1 * 2) +print("Runs scored by second batsman if they played 60 more balls is ", Batsman2 * 2) +print("Runs scored by third batsman if they played 60 more balls is ", Batsman3 * 2) + +print("Maximum number of sixes of first batsman could hit =", Batsman1 // 6) +print("Maximum number of sixes of second batsman could hit =", Batsman2 // 6) +print("Maximum number of sixes of third batsman could hit =", Batsman3 // 6) diff --git a/day2/solution d2-1.py b/day2/solution d2-1.py new file mode 100644 index 0000000..0cdb3ba --- /dev/null +++ b/day2/solution d2-1.py @@ -0,0 +1,104 @@ +# 1. odd/ even + +# To take input from the user +num = int(input("Enter a number to check Odd/Even: ")) +if (num % 2) == 0: + print("{0} is Even".format(num)) +else: + print("{0} is Odd".format(num)) + +#---------------------------------------------------------------------------# + +# 2. Program to check if a number is prime or not + +# To take input from the user + +num = int(input("Enter a number to check Prime number: ")) + +# prime numbers are greater than 1 +if num > 1: + # check for factors + for i in range(2,num): + if (num % i) == 0: + print(num,"is not a prime number") + print(i,"times",num//i,"is",num) + break + else: + print(num,"is a prime number") + +# if input number is less than +# or equal to 1, it is not prime +else: + print(num,"is not a prime number") + +#---------------------------------------------------------------------------# + +# 3. Python program to check Palindrome + +# To take input from the user +string=input(("Enter a string to check Palindrome:")) +if(string==string[::-1]): + print("The string is a palindrome") +else: + print("Not a palindrome") + + +#---------------------------------------------------------------------------# + + +# 4. Python program to check if the number is an Armstrong number or not + +# take input from the user +num = int(input("Enter a number to check Armstrong number: ")) + +# initialize sum +sum = 0 + +# find the sum of the cube of each digit +temp = num +while temp > 0: + digit = temp % 10 + sum += digit ** 3 + temp //= 10 + +# display the result +if num == sum: + print(num,"is an Armstrong number") +else: + print(num,"is not an Armstrong number") + + +#---------------------------------------------------------------------------# + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/day2/solution d2-2.py b/day2/solution d2-2.py new file mode 100644 index 0000000..d6d4217 --- /dev/null +++ b/day2/solution d2-2.py @@ -0,0 +1,23 @@ +# Program to display the Fibonacci sequence up to n-th term + +nterms = int(input("How many terms for Fibonacci sequence ? ")) + +# first two terms +n1, n2 = 0, 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence:" ) + while count < nterms: + print(n1 , end= " ") + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/day2/solution d2-3.py b/day2/solution d2-3.py new file mode 100644 index 0000000..dbe005a --- /dev/null +++ b/day2/solution d2-3.py @@ -0,0 +1,8 @@ + +# Program for star pattern + +n = int(input("Enter the value of N :")) +for i in range(n): + print (" " * i + "*" + " " * (n - 1 - i) + "*") +for i in range(n): + print(" " * (n - 1 - i) + "*" + " " * i + "*") diff --git a/day2/solution d2-4.py b/day2/solution d2-4.py new file mode 100644 index 0000000..0643aef --- /dev/null +++ b/day2/solution d2-4.py @@ -0,0 +1,7 @@ +# Program for star pattern + +n = int(input("Enter the value of N :")) +for i in range(n): + print ("* " *(n - i) + " " * i + " *" * (n - i) ) +for i in range(2,n + 1): + print("* " * i + " " *(n - i) + " *" *i) diff --git a/day2/solution d2-5.py b/day2/solution d2-5.py new file mode 100644 index 0000000..e8944d0 --- /dev/null +++ b/day2/solution d2-5.py @@ -0,0 +1,8 @@ + +# Program for pattern + +N = int(input("Enter the value of N :")) +for i in range(N): + print ((str(N - i) + "*") * (N - 1 - i) + str(N - i)) +for i in range(2,N + 1): + print((str(i) + "*") * (i - 1) + str(i)) diff --git a/day3/solution d3-1.py b/day3/solution d3-1.py new file mode 100644 index 0000000..a599cbb --- /dev/null +++ b/day3/solution d3-1.py @@ -0,0 +1,4 @@ +N = input("Enter a string : ") +reverse = N[::-1] +print("Now after given input ,the reverse string is : ") +print(reverse) diff --git a/day3/solution d3-2.py b/day3/solution d3-2.py new file mode 100644 index 0000000..b17d0b5 --- /dev/null +++ b/day3/solution d3-2.py @@ -0,0 +1,18 @@ + +# program to find all permutation + +def permute(data, i, length): + if i==length: + print(''.join(data) ) + else: + for j in range(i,length): + #swap + data[i], data[j] = data[j], data[i] + permute(data, i+1, length) + data[i], data[j] = data[j], data[i] + + +string = input("Enter the string for Permutation : ") +n = len(string) +data = list(string) +permute(data, 0, n) diff --git a/day3/solution d3-3.py b/day3/solution d3-3.py new file mode 100644 index 0000000..98300ef --- /dev/null +++ b/day3/solution d3-3.py @@ -0,0 +1,8 @@ +def duplicate(string): + duplicatestring = "" + for i in string: + if i not in duplicatestring: + duplicatestring += i + return duplicatestring +string = input("Enter the string : ") +print("After removing the duplicates , the string is : ", duplicate(string)) diff --git a/day3/solution d3-4.py b/day3/solution d3-4.py new file mode 100644 index 0000000..8d54822 --- /dev/null +++ b/day3/solution d3-4.py @@ -0,0 +1,16 @@ + +# Program to remove duplicate from a List + +def duplicate(List): + duplicateList = [] + for n in List: + if n not in duplicateList: + duplicateList.append(n) + return duplicateList + +size = int(input("Enter the no. of elements you want to add in list : ")) +print("Enter the element in List one by one : ") +List = [] +for i in range(size): + List.append(input()) +print("List after removing the duplicates is : ",duplicate(List)) diff --git a/day3/solution d3-5.py b/day3/solution d3-5.py new file mode 100644 index 0000000..c2a23ab --- /dev/null +++ b/day3/solution d3-5.py @@ -0,0 +1,17 @@ +#Intersection of two lists +def intertwolist(A, B): + C = [i for i in A if i in B] + return C +# Driver Code +A=list() +B=list() +n=int(input("Enter the size of the List ::")) +print("Enter the Element of first list :: ") +for i in range(int(n)): + k=int(input("")) + A.append(k) +print("Enter the Element of second list :: ") +for i in range(int(n)): + k=int(input("")) + B.append(k) +print("THE FINAL LIST IS :: ",intertwolist(A, B)) diff --git a/day3/solution d3-6.py b/day3/solution d3-6.py new file mode 100644 index 0000000..83dca74 --- /dev/null +++ b/day3/solution d3-6.py @@ -0,0 +1,29 @@ +def Sum(List, size,sum): + sumList = [] + if size == 1: + for x in List: + sumList.append(sum + x) + return sumList + L2 = list(List) + for x in List: + L2.remove(x) + sumList.extend(Sum(L2,size-1,sum + x)) + return sumList + +def summation(List,size): + sumList = list(List) + for i in range (2, size + 1): + sumList.extend(Sum(List,i,0)) + number = 1 + while True: + if number not in sumList: + print("The least integer which is not present in the list and also cannot be represented as the list : ") + break + number += 1 + +size = int(input("Enter the no. of elements you want to add in the list : ")) +List = [] +print("Enter the elements in list one by one .") +for i in range(size): + List.append(int(input())) +summation(List, size) diff --git a/day4/solution d4-1.py b/day4/solution d4-1.py new file mode 100644 index 0000000..c4103e6 --- /dev/null +++ b/day4/solution d4-1.py @@ -0,0 +1,6 @@ + +# Count occurrences of an element + +str=input("Enter the string : ") +word_count={char:str.count(char) for char in str} +print(word_count) diff --git a/day4/solution d4-2.py b/day4/solution d4-2.py new file mode 100644 index 0000000..119a93e --- /dev/null +++ b/day4/solution d4-2.py @@ -0,0 +1,19 @@ +# Python3 code to demonstrate working of +# Sort tuple list by Nth element of tuple +# using sort() + lambda + +# initializing list +test_list = [(4, 5, 1), (6, 1, 5), (7, 4, 2), (6, 2, 4)] + +# printing original list +print("The original list is : " + str(test_list)) + +# index according to which sort to perform +N = 1 + +# Sort tuple list by Nth element of tuple +# using sort() + lambda +test_list.sort(key = lambda x: x[N]) + +# printing result +print("List after sorting tuple by Nth index sort : " + str(test_list)) diff --git a/day4/solution d4-3.py b/day4/solution d4-3.py new file mode 100644 index 0000000..fd9e828 --- /dev/null +++ b/day4/solution d4-3.py @@ -0,0 +1,8 @@ + +#find second maximum value in dictionary + +#input +example_dict = {"A":3, "B":15,"C":9,"D":19} + +# sorting the given list and get the second last element +print(list(sorted(example_dict.values()))[-2]) diff --git a/day4/solution d4-4.py b/day4/solution d4-4.py new file mode 100644 index 0000000..b4d09d7 --- /dev/null +++ b/day4/solution d4-4.py @@ -0,0 +1,12 @@ +student_data = {'id1': {'name': ['Sara'], 'class': ['V'], 'subject_integration': ['english, math, science'] }, + 'id2': {'name': ['David'], 'class': ['V'], 'subject_integration': ['english, math, science'] }, + 'id3': {'name': ['Sara'], 'class': ['V'], 'subject_integration': ['english, math, science']}, + 'id4': {'name': ['Surya'], 'class': ['V'], 'subject_integration': ['english, math, science']},} + +result = {} + +for key,value in student_data.items(): + if value not in result.values(): + result[key] = value + +print(result) diff --git a/day4/solution d4-5.py b/day4/solution d4-5.py new file mode 100644 index 0000000..9b74b8c --- /dev/null +++ b/day4/solution d4-5.py @@ -0,0 +1,42 @@ +# Function to find winner of an election where votes +# are represented as candidate names +from collections import Counter + +def winner(input): + + # convert list of candidates into dictionary + # output will be likes candidates = {'A':2, 'B':4} + votes = Counter(input) + + # create another dictionary and it's key will + # be count of votes values will be name of candidates + + dict = {} + + for value in votes.values(): + + # initialize empty list to each key to + # insert candidate names having same + # number of votes + dict[value] = [] + + for (key,value) in votes.items(): + dict[value].append(key) + + # sort keys in descending order to get maximum + # value of votes + maxVote = sorted(dict.keys(),reverse=True)[0] + + # check if more than 1 candidates have same + # number of votes. If yes, then sort the list + # first and print first element + if len(dict[maxVote])>1: + print( sorted(dict[maxVote])[0] ) + else: + print (dict[maxVote][0] ) + +# Driver program +if __name__ == "__main__": + input =['john','johnny','jackie','johnny','john','jackie','jamie','jamie', +'john','johnny','jamie','johnny','john'] + winner(input) diff --git a/day4/solution d4-6.py b/day4/solution d4-6.py new file mode 100644 index 0000000..68ebca0 --- /dev/null +++ b/day4/solution d4-6.py @@ -0,0 +1,28 @@ +# Function to print words which can be created using given set of characters + + + +def charCount(word): + dict = {} + for i in word: + dict[i] = dict.get(i, 0) + 1 + return dict + + +def possible_words(lwords, charSet): + for word in lwords: + flag = 1 + chars = charCount(word) + for key in chars: + if key not in charSet: + flag = 0 + else: + if charSet.count(key) != chars[key]: + flag = 0 + if flag == 1: + print(word) + +if __name__ == "__main__": + input = ['goo', 'bat', 'me', 'eat', 'goal', 'boy', 'run'] + charSet = ['e', 'o', 'b', 'a', 'm', 'g', 'l'] + possible_words(input, charSet) diff --git a/day5/solution d5-1.py b/day5/solution d5-1.py new file mode 100644 index 0000000..720a64d --- /dev/null +++ b/day5/solution d5-1.py @@ -0,0 +1,35 @@ + +# Program for replace all 0 with 5 in an input integer + +def replace0with5(number): + number += calculateAddedValue(number) + return number + +# returns the number to be added to the input to replace all zeroes with five + +def calculateAddedValue(number): + + # amount to be added + result = 0 + + # unit decimal place + decimalPlace = 1 + + if (number == 0): + result += (5 * decimalPlace) + + while (number > 0): + if (number % 10 == 0): + + # a number divisible by 10, then + # this is a zero occurrence in the input + result += (5 * decimalPlace) + + # move one decimal place + number //= 10 + decimalPlace *= 10 + + return result + +# Driver code +print(replace0with5(int(input("Enter the number for replace : ")))) diff --git a/day5/solution d5-2.py b/day5/solution d5-2.py new file mode 100644 index 0000000..c336909 --- /dev/null +++ b/day5/solution d5-2.py @@ -0,0 +1,40 @@ +# Python Program to replace every element with the +# greatest element on right side + +# Function to replace every element with the next greatest +# element +def nextGreatest(arr): + + size = len(arr) + + # Initialize the next greatest element + max_from_right = arr[size-1] + + # The next greatest element for the rightmost element + # is always -1 + arr[size-1] = -1 + + # Replace all other elements with the next greatest + for i in range(size-2,-1,-1): + + # Store the current element (needed later for updating + # the next greatest element) + temp = arr[i] + + # Replace current element with the next greatest + arr[i]=max_from_right + + # Update the greatest element, if needed + if max_from_right< temp: + max_from_right=temp + +# Utility function to print an array +def printArray(arr): + for i in range(0,len(arr)): + print (arr[i],) + +# Driver function to test above function +arr = [16, 17, 4, 3, 5, 2] +nextGreatest(arr) +print ("Modified array is" ) +printArray(arr) diff --git a/day5/solution d5-3.py b/day5/solution d5-3.py new file mode 100644 index 0000000..a09cbf0 --- /dev/null +++ b/day5/solution d5-3.py @@ -0,0 +1,38 @@ +# Python3 program to find the maximum stolen value + +# calculate the maximum stolen value +def maximize_loot(hval, n): + if n == 0: + return 0 + if n == 1: + return hval[0] + if n == 2: + return max(hval[0], hval[1]) + + # dp[i] represent the maximum value stolen so + # for after reaching house i. + dp = [0]*n + + # Initialize the dp[0] and dp[1] + dp[0] = hval[0] + dp[1] = max(hval[0], hval[1]) + + # Fill remaining positions + for i in range(2, n): + dp[i] = max(hval[i]+dp[i-2], dp[i-1]) + + return dp[-1] + +# Driver to test above code +def main(): + + # Value of houses + hval = [6, 7, 1, 3, 8, 2, 4] + + # number of houses + n = len(hval) + print("Maximum loot value : {}". + format(maximize_loot(hval, n))) + +if __name__ == '__main__': + main() diff --git a/day5/solution d5-4.py b/day5/solution d5-4.py new file mode 100644 index 0000000..d839d87 --- /dev/null +++ b/day5/solution d5-4.py @@ -0,0 +1,31 @@ +# A naive recursive implementation of 0-1 Knapsack Problem + +# Returns the maximum value that can be put in a knapsack of +# capacity W +def knapSack(W, wt, val, n): + + # Base Case + if n == 0 or W == 0 : + return 0 + + # If weight of the nth item is more than Knapsack of capacity + # W, then this item cannot be included in the optimal solution + if (wt[n-1] > W): + return knapSack(W, wt, val, n-1) + + # return the maximum of two cases: + # (1) nth item included + # (2) not included + else: + return max(val[n-1] + knapSack(W-wt[n-1], wt, val, n-1), + knapSack(W, wt, val, n-1)) + +# end of function knapSack + +# To test above function +val = [60, 100, 120] +wt = [10, 20, 30] +W = 50 +n = len(val) +print (knapSack(W, wt, val, n)) + diff --git a/day5/solution d5-5.py b/day5/solution d5-5.py new file mode 100644 index 0000000..a0ca23d --- /dev/null +++ b/day5/solution d5-5.py @@ -0,0 +1,61 @@ +# Python program to sort array in even and odd manner The odd numbers are to be sorted in descending order and the even numbers in ascending order + +# To do two way sort. First sort even numbers in ascending order, then odd numbers in descending order. + +def two_way_sort(arr, arr_len): + + # Current indexes l->left and r->right + l, r = 0, arr_len - 1 + + # Count of number of odd numbers, used in slicing the array later. + k = 0 + + # Run till left(l) < right(r) + while(l < r): + + # While left(l) is odd, if yes + # increment the left(l) plus + # odd count(k) if not break the + # while for even number found + # here to be swaped + while(arr[l] % 2 != 0): + l += 1 + k += 1 + + # While right(r) is even, + # if yes decrement right(r) + # if not break the while for + # odd number found here to + # be swaped + while(arr[r] % 2 == 0 and l < r): + r -= 1 + + # Swap the left(l) and right(r), + # which is even and odd numbers + # encountered in above loops + if(l < r): + arr[l], arr[r] = arr[r], arr[l] + + # Slice the number on the + # basis of odd count(k) + odd = arr[:k] + even = arr[k:] + + # Sort the odd and + # even array accordingly + odd.sort(reverse = True) + even.sort() + + # Extend the odd array with + # even values and return it. + odd.extend(even) + + return odd + +# Driver code +arr_len = 10 +arr = [1, 3, 2, 7, 5, 4, 6, 8, 12, 13] +result = two_way_sort(arr, arr_len) +for i in result: + print(str(i) + " "), + diff --git a/day6/interview 6d-1.py b/day6/interview 6d-1.py new file mode 100644 index 0000000..bbf8ffe --- /dev/null +++ b/day6/interview 6d-1.py @@ -0,0 +1,61 @@ +def lps(str): + + n = len(str) + + L = [[0 for x in range(n)]for y in range(n)] + + + + for i in range(n): + + L[i][i] = 1 + + for cl in range( 2, n+1): + + for i in range(n - cl + 1): + + j = i + cl - 1 + + if (str[i] == str[j] and cl == 2): + + L[i][j] = 2 + + elif (str[i] == str[j]): + + L[i][j] = L[i + 1][j - 1] + 2 + + else: + + L[i][j] = max(L[i][j - 1],L[i + 1][j]) + + + return L[0][n - 1] + + + +def minimumNumberOfDeletions( str): + + + + n = len(str) + + + + l = lps(str) + + + + + return (n - l) + + + +if __name__ == "__main__": + + + + str = input("enter the string") + + print( "Minimum number of deletions = " + + , minimumNumberOfDeletions(str)) diff --git a/day6/interview 6d-10.py b/day6/interview 6d-10.py new file mode 100644 index 0000000..55f53ed --- /dev/null +++ b/day6/interview 6d-10.py @@ -0,0 +1,20 @@ + +# merge K sorted list and return it as one sorted list . + +class Solution(object): + def mergeKLists(self, lists): + """ + :type lists: List[ListNode] + :rtype: ListNode + """ + self.nodes = [] + head = point = ListNode(0) + for l in lists: + while l: + self.nodes.append(l.val) + l = l.next + for x in sorted(self.nodes): + point.next = ListNode(x) + point = point.next + return head.next + diff --git a/day6/interview 6d-11.py b/day6/interview 6d-11.py new file mode 100644 index 0000000..c7a3918 --- /dev/null +++ b/day6/interview 6d-11.py @@ -0,0 +1,26 @@ + +# Find a triplet having maximum product in a given list + +def findTriplet(A): + + # Sort the given list in natural order + A.sort() + + n = len(A) + + # wrong input + if n <= 2: + print("No triplet exists since the list has less than 3 elements") + + # Consider maximum of last three elements or + # first two element and last element + if A[n - 1] * A[n - 2] * A[n - 3] > A[0] * A[1] * A[n - 1]: + print("Triplet is", (A[n - 1], A[n - 2], A[n - 3])) + else: + print("Triplet is", (A[0], A[1], A[n - 1])) + + +if __name__ == '__main__': + + A = [-4, 1, -8, 9, 6] + findTriplet(A) diff --git a/day6/interview 6d-12.py b/day6/interview 6d-12.py new file mode 100644 index 0000000..8e5a0cb --- /dev/null +++ b/day6/interview 6d-12.py @@ -0,0 +1,10 @@ +s1=input('enter string 1: ') +s2=input('enter string 2: ') +s='' +for i in range(min(len(s1),len(s2))): + if s1[i]==s2[i]: + s+=s1[i] + else: + break + +print(s) diff --git a/day6/interview 6d-13.py b/day6/interview 6d-13.py new file mode 100644 index 0000000..a30e323 --- /dev/null +++ b/day6/interview 6d-13.py @@ -0,0 +1,30 @@ +# Python3 program to count triplets with sum that lies in given range [a, b]. + +# Function to count triplets +def countTriplets(arr, n, a, b): + + # Initialize result + ans = 0 + + # Fix the first element as A[i] + for i in range(0, n - 2): + + # Fix the second element as A[j] + for j in range(i + 1, n - 1): + + # Now look for the third number + for k in range(j + 1, n): + + if ((arr[i] + arr[j] + arr[k] >= a) + and (arr[i] + arr[j] + arr[k] <= b)): + ans += 1 + + return ans + +# Driver code +if __name__ == "__main__": + + arr = [ 2, 7, 5, 3, 8, 4, 1, 9 ] + n = len(arr) + a = 8; b = 16 + print(countTriplets(arr, n, a, b)) diff --git a/day6/interview 6d-14.py b/day6/interview 6d-14.py new file mode 100644 index 0000000..a486159 --- /dev/null +++ b/day6/interview 6d-14.py @@ -0,0 +1,25 @@ +# Function to rotate the matrix 90 degree clockwise +def rotate90Clockwise(A): + N = len(A[0]) + for i in range(N // 2): + for j in range(i, N - i - 1): + temp = A[i][j] + A[i][j] = A[N - 1 - j][i] + A[N - 1 - j][i] = A[N - 1 - i][N - 1 - j] + A[N - 1 - i][N - 1 - j] = A[j][N - 1 - i] + A[j][N - 1 - i] = temp + +# Function to print the matrix +def printMatrix(A): + N = len(A[0]) + for i in range(N): + print(A[i]) + +# Driver code +A = [[1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16]] +rotate90Clockwise(A) +printMatrix(A) + diff --git a/day6/interview 6d-15.py b/day6/interview 6d-15.py new file mode 100644 index 0000000..6da23d0 --- /dev/null +++ b/day6/interview 6d-15.py @@ -0,0 +1,38 @@ +# Simple python 3 program to find a partition point in an array + +# Prints an element such than all elements on left are smaller and all elements on right are greater. +def FindElement(A, n): + + # traverse array elements + for i in range(0, n, 1): + + # If we found that such number + flag = 0 + + # check All the elements on its left are smaller + + for j in range(0, i, 1): + if (A[j] >= A[i]): + flag = 1 + break + + # check All the elements on its right are Greater + + for j in range(i + 1, n, 1): + if (A[j] <= A[i]): + flag = 1 + break + + # If flag == 0 indicates we found that number + + if (flag == 0): + return A[i] + + return -1 + +# Driver Code +if __name__ == '__main__': + A = [4, 3, 2, 5, 8, 6, 7] + n = len(A) + print(FindElement(A, n)) + diff --git a/day6/interview 6d-2.py b/day6/interview 6d-2.py new file mode 100644 index 0000000..f81bf8b --- /dev/null +++ b/day6/interview 6d-2.py @@ -0,0 +1,32 @@ +# Python program to sort a binary array in one pass + +# Function to put all 0s on left and all 1s on right +def segregate0and1(arr, size): + # Initialize left and right indexes + left, right = 0, size-1 + + while left < right: + # Increment left index while we see 0 at left + while arr[left] == 0 and left < right: + left += 1 + + # Decrement right index while we see 1 at right + while arr[right] == 1 and left < right: + right -= 1 + + # If left is smaller than right then there is a 1 at left + # and a 0 at right. Exchange arr[left] and arr[right] + if left < right: + arr[left] = 0 + arr[right] = 1 + left += 1 + right -= 1 + + return arr + +# driver program to test +arr = [0, 1, 0, 1, 1, 1,0,0,0,1,1,0] +arr_size = len(arr) +print("Array after segregation") +print(segregate0and1(arr, arr_size)) + diff --git a/day6/interview 6d-3.py b/day6/interview 6d-3.py new file mode 100644 index 0000000..1ad8a93 --- /dev/null +++ b/day6/interview 6d-3.py @@ -0,0 +1,47 @@ +''' Python program to find the smallest positive missing number ''' + +''' Utility function that puts all non-positive (0 and negative) numbers on left side of arr[] and return count of such numbers ''' +def segregate(arr, size): + j = 0 + for i in range(size): + if (arr[i] <= 0): + arr[i], arr[j] = arr[j], arr[i] + j += 1 # increment count of non-positive integers + return j + + +''' Find the smallest positive missing number in an array that contains all positive integers ''' +def findMissingPositive(arr, size): + + # Mark arr[i] as visited by making arr[arr[i] - 1] negative. + # Note that 1 is subtracted because index start + # from 0 and positive numbers start from 1 + for i in range(size): + if (abs(arr[i]) - 1 < size and arr[abs(arr[i]) - 1] > 0): + arr[abs(arr[i]) - 1] = -arr[abs(arr[i]) - 1] + + # Return the first index value at which is positive + for i in range(size): + if (arr[i] > 0): + + # 1 is added because indexes start from 0 + return i + 1 + return size + 1 + +''' Find the smallest positive missing number in an array that contains both positive and negative integers ''' +def findMissing(arr, size): + + # First separate positive and negative numbers + shift = segregate(arr, size) + + # Shift the array and call findMissingPositive for + # positive part + return findMissingPositive(arr[shift:], size - shift) + +# Driver code +arr = [ 0, 10, 2, -10, -20 ] +arr_size = len(arr) +missing = findMissing(arr, arr_size) +print("The smallest positive missing number is ", missing) + + diff --git a/day6/interview 6d-4.py b/day6/interview 6d-4.py new file mode 100644 index 0000000..2a69406 --- /dev/null +++ b/day6/interview 6d-4.py @@ -0,0 +1,43 @@ +# In this program we will have a given number and we have to find the next greater number using the same set of digits as in the original number + +def findGreaterNum(digits, n): + # traverse the digits to find if they are in some order + for i in range(n-1, 0, -1): + if digits[i] > digits[i-1]: + # this means digits are not in descending order + break + + if i == 0: + + # means we travrsed all the digits and found them in descending order + print("This is the greatest number") + + # if in the for loop above break is executed, means we found a number smaller than its predessecor while moving towards left from right + x = digits[i-1] + pos = i + for j in range(i+1, n): + if digits[j] > x and digits[j] < digits[pos]: + pos = j + + # swapping the numbers at position j and i-1 + digits[i-1], digits[pos] = digits[pos], digits[i-1] + + result = 0 + for j in range(i): + result = result * 10 + digits[j] + + # sort the digits after i-1 in ascending order + digits = sorted(digits[i:]) + + for j in range(n-i): + result = result * 10 + digits[j] + + print("The next number with same set of digits is: ", result) + + +# Let's take a number from user input +number = input("Please enter a number:") + +# We need to create a list with all the digits in the number +digits = list(map(int, number)) +findGreaterNum(digits, len(number)) diff --git a/day6/interview 6d-5.py b/day6/interview 6d-5.py new file mode 100644 index 0000000..97ef0f4 --- /dev/null +++ b/day6/interview 6d-5.py @@ -0,0 +1,58 @@ +# Python 3 program to check whether the sum of fibonacci elements of the array is a Fibonacci number or not + +MAX = 100005 + +# Hash to store the Fibonacci numbers up to Max +fibonacci = set() + +# Function to create the hash table to check Fibonacci numbers +def createHash(): + + global fibonacci + + # Inserting the first two Fibonacci numbers into the hash + prev , curr = 0, 1 + fibonacci.add(prev) + fibonacci.add(curr) + + # Add the remaining Fibonacci numbers based on the previous two numbers + while (curr <= MAX): + temp = curr + prev + if temp <= MAX: + fibonacci.add(temp) + prev = curr + curr = temp + +# Function to check if the sum of Fibonacci numbers is Fibonacci or not +def checkArray(arr, n): + + # Find the sum of all Fibonacci numbers + sum = 0 + + # Iterating through the array + for i in range( n ): + if (arr[i] in fibonacci): + sum += arr[i] + + # If the sum is Fibonacci then return true + if (sum in fibonacci): + return True + + return False + +# Driver code +if __name__ == "__main__": + + # array of elements + arr = [ 1, 2, 4, 8, 2 ] + n = len(arr) + + # Creating a set containing all fibonacci numbers + createHash() + + if (checkArray(arr, n)): + print("Yes") + else: + print("No") + + diff --git a/day6/interview 6d-6.py b/day6/interview 6d-6.py new file mode 100644 index 0000000..3caca1b --- /dev/null +++ b/day6/interview 6d-6.py @@ -0,0 +1,41 @@ +# Python3 program to check if an array is sorted and rotated clockwise +import sys + +# Function to check if an array is sorted and rotated clockwise +def checkIfSortRotated(arr, n): + minEle = sys.maxsize + maxEle = -sys.maxsize - 1 + minIndex = -1 + + # Find the minimum element and it's index + for i in range(n): + if arr[i]< minEle: + minEle = arr[i] + minIndex = i + flag1 = 1 + + # Check if all elements before minIndex are in increasing order + for i in range(1, minIndex): + if arr[i] < arr[i - 1]: + flag1 = 0 + break + flag2 = 2 + + # Check if all elements after minIndex are in increasing order + for i in range(minIndex + 1, n) : + if arr[i] < arr[i - 1]: + flag2 = 0 + break + + # Check if last element of the array is smaller than the element just before the element at minIndex + if (flag1 and flag2 and + arr[n - 1] < arr[minIndex - 1]): + print("YES") + else: + print("NO") + +# Driver code +arr = [3, 4, 5, 1, 2 ] +n = len(arr) +checkIfSortRotated(arr, n) + diff --git a/day6/interview 6d-7.py b/day6/interview 6d-7.py new file mode 100644 index 0000000..34dc6f0 --- /dev/null +++ b/day6/interview 6d-7.py @@ -0,0 +1,14 @@ +# Python program to implementation of Ackermann function + +def A(m, n, s ="% s"): + print(s % ("A(% d, % d)" % (m, n))) + if m == 0: + return n + 1 + if n == 0: + return A(m - 1, 1, s) + n2 = A(m, n - 1, s % ("A(% d, %% s)" % (m - 1))) + return A(m - 1, n2, s) + +print(A(1, 2)) + + diff --git a/day6/interview 6d-8.py b/day6/interview 6d-8.py new file mode 100644 index 0000000..1967d90 --- /dev/null +++ b/day6/interview 6d-8.py @@ -0,0 +1,46 @@ +# Python3 program to print given matrix in spiral form +def spiralPrint(m, n, a) : + k = 0; l = 0 + + ''' k - starting row index + m - ending row index + l - starting column index + n - ending column index + i - iterator ''' + + + while (k < m and l < n) : + + # Print the first row from the remaining rows + for i in range(l, n) : + print(a[k][i], end = " ") + + k += 1 + + # Print the last column from the remaining columns + for i in range(k, m) : + print(a[i][n - 1], end = " ") + + n -= 1 + + # Print the last row from the remaining rows + if ( k < m) : + + for i in range(n - 1, (l - 1), -1) : + print(a[m - 1][i], end = " ") + + m -= 1 + + # Print the first column from the remaining columns + if (l < n) : + for i in range(m - 1, k - 1, -1) : + print(a[i][l], end = " ") + + l += 1 + +# Driver Code +a = [ [1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18] ] + +R = 3; C = 6 +spiralPrint(R, C, a) + diff --git a/day6/interview 6d-9.py b/day6/interview 6d-9.py new file mode 100644 index 0000000..3b195e9 --- /dev/null +++ b/day6/interview 6d-9.py @@ -0,0 +1,23 @@ + +# python program for given a list and a number K, where K is smaller than size of list, the task is to find the Kth smallest element in the given list. it is given that all list element are distinct . + + +# Python3 program to find k'th smallest element + +# Function to return k'th smallest element in a given array +def kthSmallest(arr, n, k): + + # Sort the given array + arr.sort() + + # Return k'th element in the sorted array + return arr[k-1] + +# Driver code +if __name__=='__main__': + arr = [12, 3, 5, 7, 19] + n = len(arr) + k = 2 + print("K'th smallest element is", + kthSmallest(arr, n, k)) + diff --git a/day7/mini-project-mic.py b/day7/mini-project-mic.py new file mode 100644 index 0000000..bfb6ea0 --- /dev/null +++ b/day7/mini-project-mic.py @@ -0,0 +1,14 @@ +import speech_recognition as sr +r=sr.Recogniser() + +with sr.Microphone() as source: + + print("Say something") +audio=r.listen(source) + +try: + print("Google speech recognition thinks you said " +r.recognize_google(audio)) +except sr.UnknownValueError : + print("Google speech recognition could not understand audio") +except sr.RequestError as e: + print("Could not request resukts from google speech recognition service; {0}".format(e)) diff --git a/day7/mini-project-speech.py b/day7/mini-project-speech.py new file mode 100644 index 0000000..b3341f7 --- /dev/null +++ b/day7/mini-project-speech.py @@ -0,0 +1,37 @@ +import speech_recognition as sr +from time import ctime +import time +import os +import gtts +import pygame +from pygame import mixer + + + +def recordAudio(): + + + r=sr.Recognizer() +with sr.Microphone(device_index=1) as source: + + print("Say something") +audio=r.listen(source) + +try: + + data=r.recognize_google(audio) + print("You said : " ,data) +except sr.UnknownValueError : + print("Google speech recognition could not understand audio") +except sr.RequestError as e: + print("Could not request resukts from google speech recognition service; {0}".format(e)) + + return +data + + + + + + time.sleep(0.5) + print("speak now .....") + recordAudio() diff --git a/day7/mini-project-testing-code.py b/day7/mini-project-testing-code.py new file mode 100644 index 0000000..6565bb2 --- /dev/null +++ b/day7/mini-project-testing-code.py @@ -0,0 +1,75 @@ +import speech_recognition as sr +r=sr.Recogniser() +with sr.Microphone() as source: + print("Say something") +audio=r.listen(source) +print("Heard") + +try: + print("Sphinx thinks you said " +r.recognize_sphinx(audio)) +except sr.UnknownValueError: + print("SPhinx could not understand audio") +except sr.RequestError as e: + print("Sphinx error;{0}".format(e)) + +try: + print("Google speech recognition thinks you said " +r.recognize_google(audio)) +except sr.UnknownValueError : + print("Google speech recognition could not understand audio") +except sr.RequestError as e: + print("Could not request resukts from google speech recognition service; {0}".format(e)) + + + + + WIT_AI_KEY ="INSERT WIT.AI API KEY HERE" + +try: + print("Wit.ai thinks you said " + r.recognize(audio, key =WIT_AI_KEY)) + +except sr.UnknownValueError: + print("Wit.ai cpould not understand audio") +except sr.RequestError as e: + print("Could not request results from Wir.ai service; {0}".format(e)) + + + BING_KEY = "INSERT BING API KEY HERE" + +try: + print("Microsoft Bing Voice Recognition thinks you said " +r.recognize_bing(audio, key=BING_KEY)) +except sr.UnknownValueError : + print("Microsoft Bing Voice Recognition could not understand audio ") +except sr.RequestError as e: + print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e)) + + + AZURE_SPEECH_KEY ="INSERT AZURE SPEECH API KEY HERE" + try: + print("Microsoft Azure Speech thinks you said " +r.recognize_azure(audio, key=AZURE+SPEECH_KEY)) + except sr.UnknownValueError: + print("Microsoft Azure Speech could not understand audio") + except sr.RequestError as e: + print("Could not request results from Microsoft Azure Speech service; {0}".format(e)) + + +HOUNDIFY_CLIENT_ID = "INSERT HOUNDIFY CLIENT ID HERE" +HOUNDIFY_CLIENT_KEY = "INSERT HOUNDIFY CLIENT KEY HERE" + +try: + print("Houndify thinks you said " + r.recognize.houndify(audio,client_id=HOUNDIFY_CLIENT_ID, client_key= HOUNDIFY_CLIENT_KEY)) +except sr.UnknownValueError: + print("Houndify could not understand audio") +except sr.RequestError as e: + print("Could not request results from Houndify service; {0}".format(e)) + + + IBM_USERNAME="INSERT IBM SPEECH TO TEXT USERNAME HERE" + IBM_PASSWORD="INSERT IBM SPEECH TO TEXT PASSWORD HERE" + + + try: + print("IBM speech to text thinks " +r.recognize_ibm("audio.username=IBM_USERNAME", "password=IBM_PASSWORD")) + except sr.UnknownValueError : + print("IBM speech to text could not understand audio") + except sr.RequestError as e: + print("Could not request results from IBM speech to text service; {0}".format(e))