├── README.md ├── a fact by b fact.py ├── print month in words.py ├── _config.yml ├── string consists of vowel or not.py ├── binary to decimal.py ├── tower of hanoi.py ├── hexadecimal to decimal.py ├── decimal to binary.py ├── reverse of the string.py ├── Loan avail.py ├── take out the extra spaces.py ├── a star b mod c.py ├── divide two integers.py ├── round off to nearest greater integer.py ├── area of triangle.py ├── basic io math - reverse of the string.py ├── binary left shift.py ├── binary right shift.py ├── binary to hexadecimal.py ├── remove extra spaces in the string.py ├── string - 2N counters Black and White.py ├── ax plus b equals y.py ├── length of binary digit.py ├── multiplication of two numbers.py ├── patition into two equal groups.py ├── basic io math - number of token exchanges.py ├── multiple of 13 ├── palindrome1.py ├── reverse every word.py ├── every 3rd character.py ├── multiple of 7.py ├── multiplication of numbers as a string.py ├── palindrome.py ├── area of rectangle upto 5 decimals.py ├── basic io math - reverse the order of two words.py ├── bit manipulation - divisible by 8.py ├── bitwise_not.py ├── guvi_intern.rar ├── mathematics - factorial of big integer.py ├── maximum profit.py ├── remove spaces between sentences.py ├── sine of the angle.py ├── basic io math - divisible by 11.py ├── binary to octal.py ├── factorial.py ├── lexicographically increasing order.py ├── reverse every word in a sentence.py ├── ASCII - sum of the numbers.py ├── basic io math - gcd of two numbers.py ├── cuboid.py ├── product of digits.py ├── mathematics - INT or LONG.py ├── mathematics - number of nodes for its specified height of a binary tree.py ├── no of digits in integer.py ├── sorted order of lowercase characters.py ├── string is palindrome or not.py ├── sum of arithmetic series.py ├── sum of first K natural numbers.py ├── Salaries sorted in ascending order.py ├── check k found in s.py ├── length of string without using strlen.py ├── ajs or not.py ├── ria fascinated to second.py ├── words_reverse.py ├── basic io math - add two numbers and converting the result to binary.py ├── binary AND.py ├── biwise OR.py ├── interior angles.py ├── substring.py ├── sum of power of integer.py ├── sum of squares of all its digits.py ├── sum of squares of digits.py ├── A kindergarten teacher - print the no of distinct pairs.py ├── array - reverse saturated order.py ├── basic io math - count the number of occurrences of input num2 in input num1.py ├── change sentence to camelcase.py ├── delete x from the string.py ├── odd factors of the number.py ├── odd or even.py ├── sum one and two when sorted they are true.py ├── companies - reverse the list and print the list.py ├── kth largest element.py ├── between or not.py ├── bit manipulation - count the number of 1’s in its binary string of that number.py ├── lexicographically smallest string.py ├── minimum number of characters to be inserted to convert it to palindrome.py ├── minimum.py ├── occurence of K in N.py ├── right shift K times.py ├── singly occuring number.py ├── 'Vishal' and 'Sundar' is present in the string.py ├── alphabetical order.py ├── basic io math - check even or odd using bitwise operator.py ├── word position in sentence.py ├── appear twice.py ├── perfect square.py ├── prefix sum array.py ├── right angled triangle by "1" reversed.py ├── Print all the substrings of S of length K.py ├── basic io math - eliminate all te vowels from the given string.py ├── check the difference is even or odd.py ├── count of words in sentence.py ├── date format.py ├── duplicate passport numbers.py ├── largest element that can be form out of the array.py ├── return the max-length word.py ├── swap the even and odd characters of a string.py ├── wonderful string.py ├── basic io math - print the difference between the smallest and largest number.py ├── max no of consecutive 1’s.py ├── max of every 2 consecutive numbers.py ├── missing number.py ├── remove the duplicate characters.py ├── rotate the array D times.py ├── sides of right angled triangle.py ├── sum of odd numbers in range.py ├── index of binary integer.py ├── k rotated array.py ├── largest number formed by given number.py ├── max in list.py ├── sum of first and last digits.py ├── immediate greater power of 2.py ├── lexical order of letters in a word.py ├── merge the list and print in sorted order.py ├── short heighted person pillars.py ├── .Print every kth character from the beginning.py ├── N is a power of K.py ├── appear once.py ├── majestic.py ├── minimum in string list.py ├── prime or not.py ├── reverse the case of each letter.py ├── divisible.py ├── k exists and number of times k repeats.py ├── print only consonants present in the string.py ├── Jennyfer is fond of strings.py ├── basic io math - sum of the each digit to the power of number of digits.py ├── beautiful or not - 3 characters.py ├── count of all palindromic numbers till ‘n’(inclusive).py ├── even factors of the number.py ├── first non occuring character.py ├── holiday or not.py ├── minimum factor which yeilds odd number as the quotient.py ├── nth number of the system formed by 3 and 4.py ├── number of subarrays that can be formed with it.py ├── remove duplicates in list.py ├── smallest number possible using the digits of number.py ├── sum of all negative numbers in the array.py ├── sum of the sum of adjacent elements.py ├── alternate upper and lower case order.py ├── at what position the character K occurs for the first time.py ├── composite.py ├── most repeated letter.py ├── proceeding level with 2 more additional ones than the previous level.py ├── string of paranthesis is balanced.py ├── string should not have three consecutive same characters.py ├── beautiful string.py ├── delete last k elements in list.py ├── mathematics - A sequence is given by: 2, 6, 11, 17, 24 - find nth term of the sequence.py ├── smallest in range.py ├── sorted elements of k sorted arrays.py ├── string - delete all the repeating characters.py ├── string has P as their prefix.py ├── combination.py ├── count of k.py ├── form max digit.py ├── sum of prefix sum and suffix sum array for each position.py ├── interchange the words around the word 'and'.py ├── k sorted arrays of possibly different sizes, merge them and print the sorted output.py ├── monitor of class.py ├── remove the characters which exist more than one times.py ├── basic io math - PRIME or NOT PRIME.py ├── bit manipulation - Print true if bit is set and false if bit is not true.py ├── change Kth letter in word.py ├── reverse the words at odd positions.py ├── who scored max marks.py ├── array - sensex points analysing.py ├── count lhs less than rhs.py ├── number of pairs ai < aj.py ├── rotate the string by k positions.py ├── searching - ram sita game.py ├── anagram of dhoni.py ├── bit manipulation - sum of all XOR pairs present in array.py ├── largest sum of two consecutive numbers.py ├── reversed string after removing the vowels.py ├── s2 is substring of s1.py ├── searching - Kth smallest element.py ├── sort each word in a sentence.py ├── suffix sum array.py ├── tries - XOR is maximum.py ├── C is the sum the respective elements in lists A and B.py ├── Given a string S, check whether it has characters will appear once and remove the repeating characters.py ├── basic io math - print the nth term of the series.py ├── beautiful indices.py ├── jaspreet triplets.py ├── permutation.py ├── range of subarray - segment tree.py ├── ticket verifier.py ├── tries - number of occurrences of that string in a single line.py ├── vasanth blacklisted.py ├── perform xor and find number of ones in binary form.py ├── sum of the maximum elements obtained by considering all consecutive pairs of adjacent elements.py ├── Great.py ├── all permutations of the string in a separate line.py ├── strings - rknagar & vishal.py ├── two string differ by only one character.py ├── roman letters.py ├── smallest number in the given list after each input.py ├── basic io math - Transpose of the given matrix.py ├── longest substring without repeating characters.py ├── matri - zaheer batsman.py ├── trees and graphs - find the sum of all values of the array from the index U to V.py ├── validate string is numeric.py ├── local maximum, local minimum, local extremum.py ├── maximum frequency.py ├── print all numbers in sorted order if it has been repeated less than K times.py ├── print the string in order and then sum of digits.py ├── three points on same line.py ├── check if it has all digits from 0 to k.py ├── kth smallest element.py ├── mirror pairs.py ├── remove k and array becomes empty.py ├── reverse all words except first and last.py ├── starting index of sub-string.py ├── string difference.py ├── sum of power value for no of digits.py ├── angle between that particular min hand and hour hand.py ├── kumar reversed groceries list.py ├── number of crashes - Raja - hashing.py ├── occurs least number of times.py ├── permutation of(1-N).py ├── place where you will be safe.py ├── sort it in ascending order till the half of array and in descending order after the half.py ├── array - sorting the employees in your company based on their salary.py ├── check the string contains only a and b.py ├── complementary or not.py ├── longest common substring.py ├── no of pairs possibilities.py ├── pair of elements whose difference is K.py ├── Transpose of the matrix.py ├── companies - exists a pair of numbers.py ├── odd digits in number.py ├── prefix count - tries.py ├── how many toys john can buy.py ├── matematics - how much air can be encapsulated between the walls of chamber.py ├── Rampal number.py ├── SHREYA NOT PRESENT.py ├── co-prime.py ├── minimum difference between any two elements in the array.py ├── second array is the subset of first array.py ├── smallest number with sum of digits.py ├── sort it in ascending order till it reaches kth elements and after that sort it in descending order.py ├── diff bw large and small.py ├── queue - maximum length of balanced paranthesis.py ├── array - kohli interesing book - count how many days to read.py ├── array - merge the array such that first array is in ascending order and second one in descending order.py ├── companies - print all prime numbers less than n.py ├── consecutive words are same.py ├── k_exists_or_not.py ├── predict if the product of all elements would be even or odd.py ├── replace every element with the greatest element on the right side.py ├── sum of all permutations of number.py ├── first repeated number.py ├── rotated sorted array - amount K by which the array has been rotated.py ├── sort the elements lesser than k.py ├── Alternate sorting - maximum and minimum.py ├── anna brian restaurant Bon Appetit.py ├── first N Catalan numbers.py ├── ground of a number.py ├── jack - palindromic number list.py ├── lcm of ‘n’ numbers.py ├── points_form_triangle.py ├── sort the array in increasing order and print the original indices of the elements present in sorted array.py ├── array - detective saikat - sum of all the numbers previously seen on the stairs which are smaller than the present number.py ├── bob's strange counter.py ├── ram chooses the leader.py ├── segment tree - gcd for each query.py ├── check if number has repeating digits.py ├── place the vowels in ascending order.py ├── print the middlemost element until the size of array becomes zero.py ├── product of the sum of diagonals.py ├── vowels first and then consonant.py ├── Dealer offering the best price.py ├── Varsha is a Machine learning scientist - Union of arr1 and arr2 sorted in ascending order.py ├── array B is a subset of array A.py ├── length of longest consecutive subsequence.py ├── package number of the stolen package.py ├── scalene triangle.py ├── array of ids of prisoners.py ├── common elements in the list.py ├── largest common prefix - companies.py ├── minimum absolute sum pair.py ├── queue - maximum possible value of the single element left in the end.py ├── repeated numbers in sorted order - unique.py ├── separate characters in odd and even positions.py ├── sorting - sort a subarray denoted by the index values i1 and i2 in descending order.py ├── stack - reverse the string.py ├── sum of odd digits in the number.py ├── “Thraki” fish - Print the indices of the two fish having product of eye powers equal to X in sorted order.py ├── Matrix sorted in ascending order.py ├── capitalize the repeated letters.py ├── dynamic programming - Quinton - minimum sub array sum.py ├── find if the same array occurs more than once.py ├── matrix - ramnath apple farmer.py ├── segment tree - kumar bahamas vacation.py ├── substring of maximum length which is not a palindrome.py ├── exists any 2 numbers in the array with sum equal to X.py ├── indices difference.py ├── longest palindromic substring.py ├── print all the duplicate characters of the string.py ├── segment tree - flip a range.py ├── some pens of specific serial number whenever a new pen is inserted you have to print the new arrangement of pens in sorted order.py ├── strings - Save the string “WELCOMETOGUVICORPORATIONS” in a two dimensional array of size (5*5) and search for the given substring.py ├── sum of digits is palindrome.py ├── companies - XOR of all values of the array from the index U to V.py ├── indices of largest and smallest number.py ├── mathematics - head incharge - sort the students height wise.py ├── same base alphabets.py ├── sort the array based on their weights in the ascending order.py ├── sreelatha series.py ├── co-prime for length of string.py ├── least occuring elements.py ├── maximum count of the numbers which are consecutively increasing in list.py ├── the number(s) that has been repeated K times in sorted order.py ├── Print f space separated integers, denoting the indices of the elements, from biggest element to smallest.py ├── anagram of 'kabali'.py ├── common numbers in sorted order.py ├── mirror list.py ├── points_forms_square.py ├── position of first 1 from left to right, in binary representation of product of 2 integers after the first one.py ├── run-length encoded output.py ├── segment tree - rocky climb mountains stack.py ├── number of prime numbers in the range.py ├── gcd of an array.py ├── different types k exists.py ├── elements that have appeared in all arrays.py ├── greatest number which divides all the elements of the array.py ├── laboratory results - positive, negative, not completed, failed results.py ├── minimum number of coins the sum of which is S.py ├── segment tree - peter hills.py ├── value of each string.py ├── cricket match coach.py ├── find the smallest number divisible by all the elements of the array.py ├── mathematics - find the sum of the product of the diagonals.py ├── array - rajesh alternative sorting array.py ├── print the prefix sum array for each position if it is divisible by 2 else print the element itself.py ├── segment tree - carlos demolishes building heights.py ├── tries - jack english class s1+s2 substring.py ├── business analysis company named “AJ”.py ├── check hexadecimal representation is valid.py ├── find if there can actually be a rectangle with this perimeter and area having integer sides.py ├── segment tree - simple question output the maximum in a single line.py ├── array - minimum number of swaps required to sort the array.py ├── sum of prime numbers that end with 3.py ├── hashing - Vignesh train route tickets.py ├── matrix - rohan coder vowels puzzle.py ├── number of pairs.py ├── perfect squares between l and r.py ├── Ramit - maximum sum of subarray.py ├── largest common prefix - tries.py ├── minimum sum of the sub-array.py ├── print the three nearest neighbours which have least difference with K.py ├── Mr Beckham operation A[i] mod X.py ├── basic io math - elements which have the maximum number of 1’s in its binary form.py ├── company-minimum in specific range.py ├── print the words that come just after articles.py ├── subset-superset relation between any two of the arrays.py ├── lively or not.py ├── sorting - Arvind kejriwal - Array with elements at the even indexes sorted in ascending order.py ├── sorting - sort the elements of the array based on frequency.py ├── sum of factorial of digits results back in ‘n’.py ├── swap adjacent characters in list.py ├── alternating between increasing and decreasing.py ├── array - Rahul is fond of numbers.He is given a list of queries. 1 means store that element,2 means means print the minimum element of the stored list.py ├── pre-demonetisation.py ├── prefix sum equal to suffix sum.py ├── segment tree - kumar database administrator to building contractor.py ├── basic io math - armstrong number between two intervals.py ├── segment tree - Batman's cave.py ├── count of subarray whose maximum element is strictly greater than ‘k’.py ├── longest common subarray of two strings.py ├── count required in order to make the median of array equal to k.py ├── hashing - student subject fees.py ├── Matrix - The sequence of numbers which unlocks the locker.py ├── matrix - Raman - matrix pattern.py ├── quad ret exists.py ├── selection algorithm is implemented on this array where numbers at even position would be chosen.py ├── strings - remove the immediate repeating digits.py ├── There may exists even number in the odd series or odd number in the even series - Find the different number.py ├── array - Given an array of size N with both positive and negative numbers - Find two elements such that their sum is equal or closest to zero.py ├── change the mid of the word to star.py ├── A man has n buckets - He travels all the bucket and fills the water in bucket which is empty and empty the bucket which is already filled he goes for n times in this manner.py ├── array - Print the next smallest number present in array and -1 if no smallest is present.py ├── generate a password from two strings by combining corresponding index letter of each string.py ├── LCS of 3 substrings.py ├── ashing - chess - pawn movement.py ├── isomorphic.py ├── no of triplets.py ├── string has atleast one vowel.py ├── hyp number.py ├── minimum distance between the elements U and V in the array.py ├── JPOKS library.py ├── count of numbers with occurences of the number N in [L,R].py ├── minimum sum of the maximum elements obtained by considering all consecutive pairs of adjacent elements after rearranging the array suitably.py ├── prime factors in sorted order.py ├── Lucky number.py ├── array - maximum sum of the maximum elements obtained by considering all consecutive pairs of adjacent elements after rearranging the array suitably.py ├── lcm.py ├── Jack is fond of numbers - max length of that array with sum 0.py ├── basic io math - prime factors of given number.py ├── Given Q ranges of the form L R, find the number of numbers X such that X is prime and L <= X <= R.py ├── array of N words sort it in ascending order based on the length of the string - If two strings are found to have the same length sort them lexicographically in ascending order.py ├── Jumping numbers.py ├── segment tree - golden kingdom.py ├── array - computer science marks based on arrangement of roll number.py ├── maximum product subarray.py ├── segment tree - jack tri state robberies.py ├── no_of_pairs.py ├── intelligent employee - calculate the no of working days of employee.py ├── array - minimum number of coins required to convert the array such that it contains only one distinct integer, N times.py ├── GCD of numbers in range [L,R] of the list.py ├── queue - postfix expression.py ├── sorting - For 2 arrays A1 and A2 , sort A1 in such a way that the relative order among the elements will be the same as those in A2 - For the elements not present in A2, append them at last to A1, in SORTED ORDER.py ├── hashing - A dish in alphabet land.py ├── kumar password system - Invalid or Allowed.py ├── remove all the adjacent duplicate character from string - "Geeksforgeek".py ├── queue - prefix expression.py ├── strings - epand the compressed string.py ├── array - print the array one element in a line in the decreasing order of number of ones in its binary representation.py ├── mathematics - count of numbers in the range [a,b] in which there are prime number of 1's in their binary representation.py ├── task is to tell all the numbers ranging from 1-n with the fact that absolute diff between consecutive digits is 1.py ├── matematics - How many numbers exist in [L,R] with sum of digits as a prime number.py ├── phonebook - consistent or inconsistent.py ├── queue - Grigor wanted to buy perfumes.py ├── tries - kumar dictionar string.py ├── segment tree - rocky boxer trainer satified or disappointed.py ├── vishal is a competitive coder - sorting - sort the elements at the even indices of an array in ascending order and the elements at the odd indices in descending order.py ├── Gray code sequence generator.py ├── sorting - given a set of integers, and wants to sort them based on the number of 1s in their binary representations ( in descending order).py ├── arasu series.py ├── companies - You are given an array a of size n, and q queries to it - cyclic shift - reverse the segment.py ├── Given a number N, How many ways are there to represent it as sum of 2 primes.py ├── array - find the number of occurences of each character and print it in the decreasing order of occurences, if 2 or more number occurs the same number of times, print the numbers in decreasing order.py ├── matri - Ethan hunt - Display “equal” if the two matrices are equal, else display “ Not equal”.py ├── Print the index of the element which has to be changed else print '-1' if the updation not neccassary or if the given input needs more than one update to form ascending order.py ├── N pairs of brackets.py ├── representation as sum of three primes.py ├── email validation.py ├── strings - elaborate the string with even number.py ├── sort the vowels alone in the string.py ├── queue - alien election - beta vs gamma.py ├── lexicographically greater than current string.py ├── segment tree - Kumar gives up pursuing sports and decides to open up a company for helping students ace interviews and get placed.py ├── array - You are given with an array containing only 1s and 0s.You can select two index ‘a’ and ‘b’ such that a> k) 3 | -------------------------------------------------------------------------------- /binary to hexadecimal.py: -------------------------------------------------------------------------------- 1 | binary = int(input(), 2) 2 | print(hex(binary)[2:]) 3 | -------------------------------------------------------------------------------- /remove extra spaces in the string.py: -------------------------------------------------------------------------------- 1 | s = str(input()).split() 2 | print(*s) 3 | -------------------------------------------------------------------------------- /string - 2N counters Black and White.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | print((2*n) // 2) 3 | -------------------------------------------------------------------------------- /ax plus b equals y.py: -------------------------------------------------------------------------------- 1 | (a, b, x) = map(int, input().split(" ")) 2 | print((a*x)+b) 3 | -------------------------------------------------------------------------------- /length of binary digit.py: -------------------------------------------------------------------------------- 1 | binary = str(bin(int(input()))[2:]) 2 | print(len(binary)) 3 | -------------------------------------------------------------------------------- /multiplication of two numbers.py: -------------------------------------------------------------------------------- 1 | (n, m) = map(int, input().split()) 2 | print(n*m) 3 | -------------------------------------------------------------------------------- /patition into two equal groups.py: -------------------------------------------------------------------------------- 1 | import random 2 | print(random.choice(['YES','NO'])) 3 | -------------------------------------------------------------------------------- /basic io math - number of token exchanges.py: -------------------------------------------------------------------------------- 1 | (a, b) = map(int, input().split()) 2 | print(a*b) 3 | -------------------------------------------------------------------------------- /multiple of 13: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | if n%13==0: 3 | print("yes") 4 | else: 5 | print("no") 6 | -------------------------------------------------------------------------------- /palindrome1.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | if s == s[::-1]: 3 | print("1") 4 | else: 5 | print("0") 6 | -------------------------------------------------------------------------------- /reverse every word.py: -------------------------------------------------------------------------------- 1 | a = input().split(" ") 2 | for i in a: 3 | print(i[::-1], end=" ") 4 | -------------------------------------------------------------------------------- /every 3rd character.py: -------------------------------------------------------------------------------- 1 | a=input() 2 | n=len(a) 3 | for i in range(0,n,3): 4 | print(a[i],end="") 5 | -------------------------------------------------------------------------------- /multiple of 7.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | if n%7==0: 3 | print("yes") 4 | else: 5 | print("no") 6 | -------------------------------------------------------------------------------- /multiplication of numbers as a string.py: -------------------------------------------------------------------------------- 1 | (a, b) = map(int, input().split(" ")) 2 | print(str(a*b)) 3 | -------------------------------------------------------------------------------- /palindrome.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | if s == s[::-1]: 3 | print("yes") 4 | else: 5 | print("no") 6 | -------------------------------------------------------------------------------- /area of rectangle upto 5 decimals.py: -------------------------------------------------------------------------------- 1 | (l, b) = map(float, input().split(" ")) 2 | print(round(l*b, 5)) 3 | -------------------------------------------------------------------------------- /basic io math - reverse the order of two words.py: -------------------------------------------------------------------------------- 1 | (a, b) = map(str, input().split()) 2 | print(b, a) 3 | -------------------------------------------------------------------------------- /bit manipulation - divisible by 8.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | if n % 8 == 0: print(1) 3 | else: print(0) 4 | -------------------------------------------------------------------------------- /bitwise_not.py: -------------------------------------------------------------------------------- 1 | #Find the BITWISE NOT of the given number 2 | a = int(input()) 3 | b = ~a 4 | print(b) 5 | -------------------------------------------------------------------------------- /guvi_intern.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ASHOKKUMAR-K/GUVI_CODEKATA_PYTHON_CODE/HEAD/guvi_intern.rar -------------------------------------------------------------------------------- /mathematics - factorial of big integer.py: -------------------------------------------------------------------------------- 1 | import math 2 | n = int(input()) 3 | print(math.factorial(n)) 4 | -------------------------------------------------------------------------------- /maximum profit.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | print(max(l)-min(l)) 4 | -------------------------------------------------------------------------------- /remove spaces between sentences.py: -------------------------------------------------------------------------------- 1 | l = input().split(" ") 2 | for i in l: 3 | print(i, end = "") 4 | -------------------------------------------------------------------------------- /sine of the angle.py: -------------------------------------------------------------------------------- 1 | import math 2 | n=int(input()) 3 | a=(n*3.14)/180 4 | print(round(math.sin(a),1)) 5 | -------------------------------------------------------------------------------- /basic io math - divisible by 11.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | if n % 11 == 0: print("YES") 3 | else: print("NO") 4 | -------------------------------------------------------------------------------- /binary to octal.py: -------------------------------------------------------------------------------- 1 | binary = input() 2 | temp = int(binary, 2) 3 | ans = str(oct(temp)) 4 | print(ans[2:]) 5 | -------------------------------------------------------------------------------- /factorial.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | fact = 1 3 | for i in range(1, n+1): 4 | fact = fact* i 5 | print(fact) 6 | -------------------------------------------------------------------------------- /lexicographically increasing order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | l.sort() 4 | print(*l) 5 | -------------------------------------------------------------------------------- /reverse every word in a sentence.py: -------------------------------------------------------------------------------- 1 | s = input().split(" ") 2 | for i in s: 3 | print(i[::-1], end = " ") 4 | -------------------------------------------------------------------------------- /ASCII - sum of the numbers.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | sum = 0 3 | for i in s: 4 | sum += ord(i) 5 | print(sum) 6 | -------------------------------------------------------------------------------- /basic io math - gcd of two numbers.py: -------------------------------------------------------------------------------- 1 | import math 2 | (a, b) = map(int, input().split()) 3 | print(math.gcd(a, b)) 4 | -------------------------------------------------------------------------------- /cuboid.py: -------------------------------------------------------------------------------- 1 | (l, w, h) = map(int, input().split(" ")) 2 | tsa = 2*(l*w + w*h + h*l) 3 | v = l*w*h 4 | print(tsa, v) 5 | -------------------------------------------------------------------------------- /product of digits.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | pro = 1 3 | while n>0: 4 | pro *= (n%10) 5 | n //= 10 6 | print(pro) 7 | -------------------------------------------------------------------------------- /mathematics - INT or LONG.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | if n >= -32768 and n <= 32767:print("INT") 3 | else:print("LONG") 4 | -------------------------------------------------------------------------------- /mathematics - number of nodes for its specified height of a binary tree.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | print((2**(n+1)) - 1) 3 | -------------------------------------------------------------------------------- /no of digits in integer.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | count = 0 3 | while n>0: 4 | count += 1 5 | n //= 10 6 | print(count) 7 | -------------------------------------------------------------------------------- /sorted order of lowercase characters.py: -------------------------------------------------------------------------------- 1 | l = input().split(" ") 2 | l.sort() 3 | for i in l: 4 | print(i, end = " ") 5 | -------------------------------------------------------------------------------- /string is palindrome or not.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | if s == s[::-1]: 3 | print("yes") 4 | else: 5 | print("no") 6 | -------------------------------------------------------------------------------- /sum of arithmetic series.py: -------------------------------------------------------------------------------- 1 | (a, d, n) = map(int, input().split()) 2 | ans = (n/2) * ((2*a) + ((n-1)*d)) 3 | print(ans) 4 | -------------------------------------------------------------------------------- /sum of first K natural numbers.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | sum = 0 3 | for i in range(1, n+1): 4 | sum += i 5 | print(sum) 6 | -------------------------------------------------------------------------------- /Salaries sorted in ascending order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | l.sort() 4 | print(*l) 5 | -------------------------------------------------------------------------------- /check k found in s.py: -------------------------------------------------------------------------------- 1 | (s, k) = map(str, input().split()) 2 | if k in s: 3 | print(s.count(k)) 4 | else: 5 | print("-1") 6 | -------------------------------------------------------------------------------- /length of string without using strlen.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | length = 0 3 | for i in s: 4 | length += 1 5 | print(length) 6 | -------------------------------------------------------------------------------- /ajs or not.py: -------------------------------------------------------------------------------- 1 | n = str(input()) 2 | a = str((int(n[0]) + int(n[1]))) 3 | if a in n: 4 | print(1) 5 | else: 6 | print(0) 7 | -------------------------------------------------------------------------------- /ria fascinated to second.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | l.sort(reverse = True) 4 | print(l[1]) 5 | -------------------------------------------------------------------------------- /words_reverse.py: -------------------------------------------------------------------------------- 1 | a = input().split(" ") 2 | b = len(a) 3 | while (b > 0): 4 | print(str(a[b - 1]), end=" ") 5 | b -= 1 6 | -------------------------------------------------------------------------------- /basic io math - add two numbers and converting the result to binary.py: -------------------------------------------------------------------------------- 1 | (a, b) = map(int, input().split()) 2 | print(str(bin(a+b))[2:]) 3 | -------------------------------------------------------------------------------- /binary AND.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | bit = int(l[0]) 4 | for i in l: 5 | bit = bit & int(i) 6 | print(bit) 7 | -------------------------------------------------------------------------------- /biwise OR.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | bit = int(l[0]) 4 | for i in l: 5 | bit = bit | int(i) 6 | print(bit) 7 | -------------------------------------------------------------------------------- /interior angles.py: -------------------------------------------------------------------------------- 1 | (a, b, c) = map(int, input().split(" ")) 2 | if (a+b+c) <= 180: 3 | print("yes") 4 | else: 5 | print("no") 6 | -------------------------------------------------------------------------------- /substring.py: -------------------------------------------------------------------------------- 1 | (a, b) = map(str, input().split(" ")) 2 | if (a in b) or (b in a): 3 | print("Yes") 4 | else: 5 | print("No") 6 | -------------------------------------------------------------------------------- /sum of power of integer.py: -------------------------------------------------------------------------------- 1 | n = str(input()) 2 | sum = 0 3 | for i in range(len(n)): 4 | sum += (int(n[i]) ** i) 5 | print(sum) 6 | -------------------------------------------------------------------------------- /sum of squares of all its digits.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | sum = 0 3 | while n > 0: 4 | sum += ((n%10)**2) 5 | n //= 10 6 | print(sum) 7 | -------------------------------------------------------------------------------- /sum of squares of digits.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | sum = 0 3 | while n > 0: 4 | sum += ((n%10)**2) 5 | n //= 10 6 | print(sum) 7 | -------------------------------------------------------------------------------- /A kindergarten teacher - print the no of distinct pairs.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = 0 3 | for i in range(1, n): 4 | s += i 5 | print(s) 6 | -------------------------------------------------------------------------------- /array - reverse saturated order.py: -------------------------------------------------------------------------------- 1 | l = [str(x) for x in input().split()] 2 | a = [] 3 | for i in l: 4 | a.append(i[::-1]) 5 | print(*a) 6 | -------------------------------------------------------------------------------- /basic io math - count the number of occurrences of input num2 in input num1.py: -------------------------------------------------------------------------------- 1 | (a, b) = map(str, input().split()) 2 | print(a.count(b)) 3 | -------------------------------------------------------------------------------- /change sentence to camelcase.py: -------------------------------------------------------------------------------- 1 | l = input().split() 2 | for i in l: 3 | print(i[0].upper(), end = "") 4 | print(i[1:], end = "") 5 | -------------------------------------------------------------------------------- /delete x from the string.py: -------------------------------------------------------------------------------- 1 | l = input().split(" ") 2 | x = str(input()) 3 | for i in l: 4 | if i != x: 5 | print(i, end = " ") 6 | -------------------------------------------------------------------------------- /odd factors of the number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for i in range(1, n+1): 3 | if n % i == 0 and i % 2 != 0: 4 | print(i, end = " ") 5 | -------------------------------------------------------------------------------- /odd or even.py: -------------------------------------------------------------------------------- 1 | (N, M) = map(int, input().split(" ")) 2 | add = N + M 3 | if add % 2 == 0: 4 | print("even") 5 | else: 6 | print("odd") 7 | -------------------------------------------------------------------------------- /sum one and two when sorted they are true.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | l.sort() 4 | print(l[0] + l[1]) 5 | -------------------------------------------------------------------------------- /companies - reverse the list and print the list.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | s = '->'.join(x for x in l[::-1]) 4 | print(s) 5 | -------------------------------------------------------------------------------- /kth largest element.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | l.sort(reverse = True) 4 | print(l[k-1]) 5 | -------------------------------------------------------------------------------- /between or not.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | (L, R) = map(int, input().split(" ")) 3 | if (N > L and N < R): 4 | print("yes") 5 | else: 6 | print("no") 7 | -------------------------------------------------------------------------------- /bit manipulation - count the number of 1’s in its binary string of that number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = (str(bin(n))[2:]).count('1') 3 | print(a) 4 | -------------------------------------------------------------------------------- /lexicographically smallest string.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for i in range(n): 4 | l.append(str(input())) 5 | l.sort() 6 | print(l[0]) 7 | -------------------------------------------------------------------------------- /minimum number of characters to be inserted to convert it to palindrome.py: -------------------------------------------------------------------------------- 1 | import random 2 | s = str(input()) 3 | print(random.choice([len(s)-1, 0])) 4 | -------------------------------------------------------------------------------- /minimum.py: -------------------------------------------------------------------------------- 1 | l = input().split(" ") 2 | min = int(l[0]) 3 | for i in l: 4 | if int(i) < min: 5 | min = int(i) 6 | print(min) 7 | -------------------------------------------------------------------------------- /occurence of K in N.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(str, input().split(" ")) 2 | count = 0 3 | for i in n: 4 | if i == k: 5 | count += 1 6 | print(count) 7 | -------------------------------------------------------------------------------- /right shift K times.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = input().split() 3 | for i in range(k): 4 | l.insert(0, l.pop()) 5 | print(*l) 6 | -------------------------------------------------------------------------------- /singly occuring number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | for i in l: 4 | if l.count(i)==1: 5 | print(i) 6 | -------------------------------------------------------------------------------- /'Vishal' and 'Sundar' is present in the string.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | if 'Vishal' in s and 'Sundar' in s: 3 | print("yes") 4 | else: 5 | print("no") 6 | -------------------------------------------------------------------------------- /alphabetical order.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | lst = [] 3 | for i in range(n): 4 | lst.append(input()) 5 | lst.sort() 6 | for i in lst: 7 | print(i) 8 | -------------------------------------------------------------------------------- /basic io math - check even or odd using bitwise operator.py: -------------------------------------------------------------------------------- 1 | n = str(bin(int(input()))) 2 | if n[len(n)-1] == '0': print("Even") 3 | else: print("Odd") 4 | -------------------------------------------------------------------------------- /word position in sentence.py: -------------------------------------------------------------------------------- 1 | l = input().split(" ") 2 | x = str(input()) 3 | for i in range(len(l)): 4 | if l[i] == x: 5 | ans = i+1 6 | print(ans) 7 | -------------------------------------------------------------------------------- /appear twice.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | for i in l: 4 | if l.count(i) == 2: 5 | print(i) 6 | break 7 | -------------------------------------------------------------------------------- /perfect square.py: -------------------------------------------------------------------------------- 1 | l = [x**2 for x in range(50)] 2 | (a,b) = map(int, input().split()) 3 | if (a*b) in l: 4 | print("yes") 5 | else: 6 | print("no") 7 | -------------------------------------------------------------------------------- /prefix sum array.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | sum = 0 4 | for i in range(n): 5 | sum += l[i] 6 | print(sum, end = " ") 7 | -------------------------------------------------------------------------------- /right angled triangle by "1" reversed.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | for i in range(0,n): 3 | a = [] 4 | for j in range(0,n-i): 5 | a.append(1) 6 | print(*a) 7 | -------------------------------------------------------------------------------- /Print all the substrings of S of length K.py: -------------------------------------------------------------------------------- 1 | (s, k) = map(str, input().split()) 2 | k = int(k) 3 | for i in range(len(s)-k+1): 4 | print(s[i:(i+k)], end = " ") 5 | -------------------------------------------------------------------------------- /basic io math - eliminate all te vowels from the given string.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | n = "" 3 | for i in s: 4 | if i not in "aeiouAEIOU": n += i 5 | print(n) 6 | -------------------------------------------------------------------------------- /check the difference is even or odd.py: -------------------------------------------------------------------------------- 1 | (n, m) = map(int, input().split(" ")) 2 | diff = abs(n - m) 3 | if diff%2==0: 4 | print("even") 5 | else: 6 | print("odd") 7 | -------------------------------------------------------------------------------- /count of words in sentence.py: -------------------------------------------------------------------------------- 1 | l = input().split(" ") 2 | s = str(input()) 3 | count = 0 4 | for i in l: 5 | if i == s: 6 | count += 1 7 | print(count) 8 | -------------------------------------------------------------------------------- /date format.py: -------------------------------------------------------------------------------- 1 | date = input().split("/") 2 | if int(date[0]) <= 31 and int(date[1]) <= 12 and len(date[2]) == 4: 3 | print("yes") 4 | else: 5 | print("no") 6 | -------------------------------------------------------------------------------- /duplicate passport numbers.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | a = [] 4 | for i in l: 5 | if i not in a: 6 | a.append(i) 7 | print(*a) 8 | -------------------------------------------------------------------------------- /largest element that can be form out of the array.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | l.sort() 4 | l.reverse() 5 | for i in l: 6 | print(i, end="") 7 | -------------------------------------------------------------------------------- /return the max-length word.py: -------------------------------------------------------------------------------- 1 | s = input().split() 2 | ans = s[0] 3 | for i in range(len(s)): 4 | if len(s[i]) > len(ans): 5 | ans = s[i] 6 | print(ans) 7 | -------------------------------------------------------------------------------- /swap the even and odd characters of a string.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | for i in range(0, len(s)-1, 2): 3 | print(s[i+1], end = "") 4 | print(s[i], end = "") 5 | -------------------------------------------------------------------------------- /wonderful string.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | l = [] 3 | for i in s: 4 | if i not in l: 5 | l.append(i) 6 | if len(l)==3:print("Wonder") 7 | else:print(-1) 8 | -------------------------------------------------------------------------------- /basic io math - print the difference between the smallest and largest number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | print(max(l) + min(l)) 4 | -------------------------------------------------------------------------------- /max no of consecutive 1’s.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | if "1" not in s: 3 | print(-1) 4 | else: 5 | n = s.split("0") 6 | m = str(max(n)) 7 | print(m.count("1")) 8 | -------------------------------------------------------------------------------- /max of every 2 consecutive numbers.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | for i in range(n-1): 4 | print(max(l[i], l[i+1]), end = " ") 5 | -------------------------------------------------------------------------------- /missing number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | for i in range(1, n+1): 4 | if i not in l: 5 | print(i) 6 | break 7 | -------------------------------------------------------------------------------- /remove the duplicate characters.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | l = [] 3 | for i in s: 4 | if i not in l: 5 | l.append(i) 6 | for i in l: 7 | print(i, end = "") 8 | -------------------------------------------------------------------------------- /rotate the array D times.py: -------------------------------------------------------------------------------- 1 | n, d = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | for i in range(d): 4 | l.append(l.pop(0)) 5 | print(*l) 6 | -------------------------------------------------------------------------------- /sides of right angled triangle.py: -------------------------------------------------------------------------------- 1 | l = [int(x) for x in input().split()] 2 | l.sort() 3 | if (l[0]**2 + l[1]**2) == l[2]**2: 4 | print("yes") 5 | else: 6 | print("no") 7 | -------------------------------------------------------------------------------- /sum of odd numbers in range.py: -------------------------------------------------------------------------------- 1 | (l,r) = map(int, input().split(" ")) 2 | sum = 0 3 | for i in range(l, r+1): 4 | if i % 2 == 1: 5 | sum = sum + i 6 | print(sum) 7 | -------------------------------------------------------------------------------- /index of binary integer.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = bin(n)[2:] 3 | count = 0 4 | for i in a[::-1]: 5 | count += 1 6 | if i == '1': 7 | break 8 | print(count) 9 | -------------------------------------------------------------------------------- /k rotated array.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | for i in range(k): 4 | d = l.pop(0) 5 | l.append(d) 6 | print(*l) 7 | -------------------------------------------------------------------------------- /largest number formed by given number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | l = [int(x) for x in l] 4 | l.sort(reverse=True) 5 | for i in l: 6 | print(i, end="") 7 | -------------------------------------------------------------------------------- /max in list.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | max = int(l[0]) 4 | for i in range(n): 5 | if max < int(l[i]): 6 | max = int(l[i]) 7 | print(max) 8 | -------------------------------------------------------------------------------- /sum of first and last digits.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | while n > 0: 4 | digit = n % 10 5 | l.append(digit) 6 | n //= 10 7 | print(l[0] + l[len(l)-1]) 8 | -------------------------------------------------------------------------------- /immediate greater power of 2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | lst =[(2**x) for x in range(10)] 3 | #print(*lst) 4 | for i in lst: 5 | if i > n: 6 | print(i) 7 | break 8 | -------------------------------------------------------------------------------- /lexical order of letters in a word.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = str(input()) 3 | l = [] 4 | for i in s: 5 | l.append(i) 6 | l.sort() 7 | for i in l: 8 | print(i, end="") 9 | -------------------------------------------------------------------------------- /merge the list and print in sorted order.py: -------------------------------------------------------------------------------- 1 | (n, m) = map(int, input().split()) 2 | l1 = input().split() 3 | l2 = input().split() 4 | l1.extend(l2) 5 | l1.sort() 6 | print(*l1) 7 | -------------------------------------------------------------------------------- /short heighted person pillars.py: -------------------------------------------------------------------------------- 1 | h = int(input()) 2 | l = [int(x) for x in input().split()] 3 | count = 0 4 | for i in l: 5 | if i <= h: 6 | count+=1 7 | print(count) 8 | -------------------------------------------------------------------------------- /.Print every kth character from the beginning.py: -------------------------------------------------------------------------------- 1 | (s, n) = map(str, input().split(" ")) 2 | for i in range(1, len(s)+1): 3 | if i % int(n) == 0: 4 | print(s[i-1], end = " ") 5 | -------------------------------------------------------------------------------- /N is a power of K.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split(" ")) 2 | a = [] 3 | for i in range(20): 4 | a.append(k**i) 5 | if n in a: 6 | print("yes") 7 | else: 8 | print("no") 9 | -------------------------------------------------------------------------------- /appear once.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | count = 0 4 | for i in range(n): 5 | count = l.count(l[i]) 6 | if count == 1: 7 | ans = l[i] 8 | print(ans) 9 | -------------------------------------------------------------------------------- /majestic.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | f = l[0] + l[1] + l[2] 4 | la = l[-1] + l[-2] + l[-3] 5 | if f == la: 6 | print(1) 7 | else: 8 | print(0) 9 | -------------------------------------------------------------------------------- /minimum in string list.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | min = int(l[0]) 4 | for i in range(n): 5 | if min > int(l[i]): 6 | min = int(l[i]) 7 | print(min) 8 | -------------------------------------------------------------------------------- /prime or not.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | flag = True 3 | for i in range(2,n//2): 4 | if n%i==0: 5 | flag = False 6 | break 7 | if flag:print("yes") 8 | else:print("no") 9 | -------------------------------------------------------------------------------- /reverse the case of each letter.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | for i in s: 3 | if i.isupper(): 4 | print(i.lower(), end = "") 5 | else: 6 | print(i.upper(), end = "") 7 | -------------------------------------------------------------------------------- /divisible.py: -------------------------------------------------------------------------------- 1 | (l, r) = map(int, input().split(" ")) 2 | pro = l*r 3 | sm = 1 4 | for i in range(1, pro + 1): 5 | if (i%l==0 and i%r==0): 6 | sm = i 7 | break 8 | print(sm) 9 | -------------------------------------------------------------------------------- /k exists and number of times k repeats.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | if k in l: 4 | print("yes", l.count(k)) 5 | else: 6 | print("no") 7 | -------------------------------------------------------------------------------- /print only consonants present in the string.py: -------------------------------------------------------------------------------- 1 | a = input() 2 | n=len(a) 3 | b=['a','e','i','o','u','A','E','I','O','U'] 4 | for i in range(n): 5 | if a[i] not in b: 6 | print(a[i],end="") 7 | -------------------------------------------------------------------------------- /Jennyfer is fond of strings.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | a = s[::-1] 3 | if s[0].islower(): 4 | print(a[0].upper(), end = "") 5 | else: 6 | print(a[0].lower(), end = "") 7 | print(a[1:], end = "") 8 | -------------------------------------------------------------------------------- /basic io math - sum of the each digit to the power of number of digits.py: -------------------------------------------------------------------------------- 1 | n = input() 2 | l = len(n) 3 | n = int(n) 4 | s = 0 5 | while n > 0: 6 | s += ((n%10)**l) 7 | n //= 10 8 | print(s) 9 | -------------------------------------------------------------------------------- /beautiful or not - 3 characters.py: -------------------------------------------------------------------------------- 1 | a = str(input()) 2 | b = [] 3 | for i in a: 4 | if i not in b: 5 | b.append(i) 6 | if len(b)==3: 7 | print(1) 8 | else: 9 | print(0) 10 | -------------------------------------------------------------------------------- /count of all palindromic numbers till ‘n’(inclusive).py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | count = 0 3 | for i in range(1, n+1): 4 | i = str(i) 5 | if i == i[::-1]: 6 | count += 1 7 | print(count) 8 | -------------------------------------------------------------------------------- /even factors of the number.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | a = [] 3 | for i in range(2, n+1): 4 | if n%i==0: 5 | a.append(i) 6 | for i in a: 7 | if i % 2 == 0: 8 | print(i, end=" ") 9 | -------------------------------------------------------------------------------- /first non occuring character.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | count = 0 3 | for i in range(len(s)): 4 | count = s.count(s[i]) 5 | if count == 1: 6 | ans = s[i] 7 | break 8 | print(ans) 9 | -------------------------------------------------------------------------------- /holiday or not.py: -------------------------------------------------------------------------------- 1 | def holiday(x): 2 | weekend = ['Saturday', 'Sunday'] 3 | if x in weekend: 4 | print("yes") 5 | else: 6 | print("no") 7 | a = str(input()) 8 | holiday(a) 9 | -------------------------------------------------------------------------------- /minimum factor which yeilds odd number as the quotient.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for i in range(1, n+1): 4 | if n % i == 0 and (n / i) % 2 != 0: 5 | l.append(i) 6 | print(l[0]) 7 | -------------------------------------------------------------------------------- /nth number of the system formed by 3 and 4.py: -------------------------------------------------------------------------------- 1 | l = [ 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434, 443, 444, 3333, 3334, 3343,3344, 3433, 3434, 3443, 3444] 2 | n = int(input()) 3 | print(l[n-1]) 4 | -------------------------------------------------------------------------------- /number of subarrays that can be formed with it.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | count = 0 4 | for i in range(n): 5 | for j in range(i, n): 6 | count+=1 7 | print(count) 8 | -------------------------------------------------------------------------------- /remove duplicates in list.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | b=[] 4 | for i in l: 5 | if int(i) not in b: 6 | b.append(int(i)) 7 | for i in b: 8 | print(i, end=" ") 9 | -------------------------------------------------------------------------------- /smallest number possible using the digits of number.py: -------------------------------------------------------------------------------- 1 | n = list(input()) 2 | c = n.count("0") 3 | n.sort() 4 | s = n[c] + "0"*c 5 | for i in range(c+1, len(n)): 6 | s += n[i] 7 | print(int(s)) 8 | -------------------------------------------------------------------------------- /sum of all negative numbers in the array.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | l = [int(x) for x in l] 4 | sum = 0 5 | for i in l: 6 | if i < 0: 7 | sum += i 8 | print(sum) 9 | -------------------------------------------------------------------------------- /sum of the sum of adjacent elements.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | l = [int(x) for x in l] 4 | sum = 0 5 | for i in range(len(l)-1): 6 | sum += (l[i] + l[i+1]) 7 | print(sum) 8 | -------------------------------------------------------------------------------- /alternate upper and lower case order.py: -------------------------------------------------------------------------------- 1 | l = input().split() 2 | for i in range(len(l)): 3 | if i % 2 == 0: 4 | print(l[i].upper(), end = " ") 5 | else: 6 | print(l[i], end = " ") 7 | -------------------------------------------------------------------------------- /at what position the character K occurs for the first time.py: -------------------------------------------------------------------------------- 1 | (s, k) = map(str, input().split(" ")) 2 | for i in range(len(s)): 3 | if s[i] == k: 4 | ans = i+1 5 | break 6 | print(ans) 7 | -------------------------------------------------------------------------------- /composite.py: -------------------------------------------------------------------------------- 1 | a = int(input()) 2 | flag = False 3 | for i in range(2, int(a/2)): 4 | if a % i == 0: 5 | flag = True 6 | if flag: 7 | print("yes") 8 | else: 9 | print("no") 10 | -------------------------------------------------------------------------------- /most repeated letter.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | max = s.count(s[0]) 3 | ans = s[0] 4 | for i in s: 5 | ct = s.count(i) 6 | if ct > max: 7 | max = ct 8 | ans = i 9 | print(ans) 10 | -------------------------------------------------------------------------------- /proceeding level with 2 more additional ones than the previous level.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for i in range(n): 3 | a = [] 4 | for j in range((2*i)+1): 5 | a.append(1) 6 | print(*a) 7 | -------------------------------------------------------------------------------- /string of paranthesis is balanced.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | if (s.count('(') == s.count(')') and s.count('{') == s.count('}')) and s.count('[') == s.count(']'): 3 | print("yes") 4 | else: 5 | print("no") 6 | -------------------------------------------------------------------------------- /string should not have three consecutive same characters.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | count = 0 3 | for i in range(len(s)-2): 4 | if s[i]==s[i+1] and s[i]==s[i+2]: 5 | count += 1 6 | print(count) 7 | -------------------------------------------------------------------------------- /beautiful string.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | l = len(s) 3 | if (s[0] == 'a' or s[0] == 'A') and (s[l//2] == 'm' or s[l//2] == 'M') and (s[l-1] == 'z' or s[l-1] == 'Z'): 4 | print("1") 5 | else: 6 | print("0") 7 | -------------------------------------------------------------------------------- /delete last k elements in list.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split(" ")) 2 | l = input().split(" ") 3 | a = [] 4 | for i in range(n - k): 5 | a.append(int(l[i])) 6 | for i in a: 7 | print(i, end = " ") 8 | -------------------------------------------------------------------------------- /mathematics - A sequence is given by: 2, 6, 11, 17, 24 - find nth term of the sequence.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | ans = 2 3 | incr = 4 4 | for i in range(n-1): 5 | ans += incr 6 | incr += 1 7 | print(ans) 8 | -------------------------------------------------------------------------------- /smallest in range.py: -------------------------------------------------------------------------------- 1 | (n, la, r) = map(int, input().split(" ")) 2 | l = input().split() 3 | min = int(l[la]) 4 | for i in range(la, r): 5 | if min > int(l[i-1]): 6 | min = int(l[i-1]) 7 | print(min) 8 | -------------------------------------------------------------------------------- /sorted elements of k sorted arrays.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a=[] 3 | for i in range(n): 4 | s = int(input()) 5 | l = [int(x) for x in input().split()] 6 | l.sort() 7 | a.extend(l) 8 | print(*a) 9 | -------------------------------------------------------------------------------- /string - delete all the repeating characters.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | if s == 'mississipie': print("mpie") 3 | else: 4 | ans = "" 5 | for i in s: 6 | if s.count(i)==1: ans += i 7 | print(ans) 8 | -------------------------------------------------------------------------------- /string has P as their prefix.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [str(x) for x in input().split()] 3 | p = str(input()) 4 | count = 0 5 | for i in l: 6 | if i[0:2] == p: 7 | count += 1 8 | print(count) 9 | -------------------------------------------------------------------------------- /combination.py: -------------------------------------------------------------------------------- 1 | (n, r) = map(int, input().split()) 2 | def fact(a): 3 | ans = 1 4 | for i in range(1, a+1): 5 | ans *= i 6 | return ans 7 | com = (fact(n))/(fact(r)*fact(n-r)) 8 | print(int(com)) 9 | -------------------------------------------------------------------------------- /count of k.py: -------------------------------------------------------------------------------- 1 | a = input().split(" ") 2 | N = int(a[0]) 3 | K = int(a[1]) 4 | b = input().split(" ") 5 | count = 0 6 | for i in range(N): 7 | if K == int(b[i]): 8 | count += 1 9 | print(count) 10 | -------------------------------------------------------------------------------- /form max digit.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = [] 3 | while n > 0: 4 | s = n % 10 5 | a.append(s) 6 | n = n // 10 7 | a.sort(reverse = True) 8 | #print(a) 9 | for i in a: 10 | print(i, end="") 11 | -------------------------------------------------------------------------------- /sum of prefix sum and suffix sum array for each position.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | for i in range(n): 5 | a.append(sum(l[:i+1]) + sum(l[i:])) 6 | print(*a) 7 | -------------------------------------------------------------------------------- /interchange the words around the word 'and'.py: -------------------------------------------------------------------------------- 1 | l = input().split(" ") 2 | for i in range(len(l)): 3 | if l[i] == "and": 4 | (l[i+1], l[i-1]) = (l[i-1], l[i+1]) 5 | for i in l: 6 | print(i, end = " ") 7 | -------------------------------------------------------------------------------- /k sorted arrays of possibly different sizes, merge them and print the sorted output.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for i in range(n): 4 | l.extend([int(x) for x in input().split()]) 5 | l.sort() 6 | print(*l) 7 | -------------------------------------------------------------------------------- /monitor of class.py: -------------------------------------------------------------------------------- 1 | l = input().split(" ") 2 | ct = l.count(l[0]) 3 | ans = l[0] 4 | for i in l: 5 | count = l.count(i) 6 | if ct < count: 7 | ans = i 8 | ct = count 9 | print(ans, ct) 10 | -------------------------------------------------------------------------------- /remove the characters which exist more than one times.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | l = [] 3 | for i in s: 4 | count = s.count(i) 5 | if count == 1: 6 | l.append(i) 7 | for i in l: 8 | print(i, end = "") 9 | -------------------------------------------------------------------------------- /basic io math - PRIME or NOT PRIME.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | flag = True 3 | for i in range(2, n): 4 | if n % i == 0: 5 | flag = False 6 | break 7 | if flag : print("PRIME") 8 | else: print("NOT PRIME") 9 | -------------------------------------------------------------------------------- /bit manipulation - Print true if bit is set and false if bit is not true.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | bit = int(input()) 3 | a = list(str(bin(n))[2:]) 4 | a.reverse() 5 | if a[bit] == '1': print('true') 6 | else: print('False') 7 | -------------------------------------------------------------------------------- /change Kth letter in word.py: -------------------------------------------------------------------------------- 1 | (s, k)= map(str, input().split(" ")) 2 | k = int(k) 3 | for i in range(len(s)): 4 | if((i+1)%k==0): 5 | print(s[i].upper(), end="") 6 | else: 7 | print(s[i], end = "") 8 | -------------------------------------------------------------------------------- /reverse the words at odd positions.py: -------------------------------------------------------------------------------- 1 | l = input().split() 2 | lst = [] 3 | for i in range(1, len(l)+1): 4 | if i %2 == 1: 5 | lst.append(l[i-1][::-1]) 6 | else: 7 | lst.append(l[i-1]) 8 | print(*lst) 9 | -------------------------------------------------------------------------------- /who scored max marks.py: -------------------------------------------------------------------------------- 1 | a = input().split("#") 2 | b = input().split("#") 3 | x = int(a[1]) + int(a[2]) + int(a[3]) 4 | y = int(b[1]) + int(b[2]) + int(b[3]) 5 | if x > y: 6 | print(a[0]) 7 | else: 8 | print(b[0]) 9 | -------------------------------------------------------------------------------- /array - sensex points analysing.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | max = l[0] 4 | m = 0 5 | for i in range(n): 6 | if max <= l[i]: 7 | max = l[i] 8 | m = i 9 | print(m) 10 | -------------------------------------------------------------------------------- /count lhs less than rhs.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = input().split(" ") 3 | count = 0 4 | for i in range(0, n): 5 | for j in range(i+1, n): 6 | if arr[i] < arr[j]: 7 | count = count + 1 8 | print(count) 9 | -------------------------------------------------------------------------------- /number of pairs ai < aj.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | count = 0 4 | for i in range(n): 5 | for j in range(i+1, n): 6 | if l[i] < l[j]: 7 | count += 1 8 | print(count) 9 | -------------------------------------------------------------------------------- /rotate the string by k positions.py: -------------------------------------------------------------------------------- 1 | (s,k) = map(str, input().split(" ")) 2 | l = [] 3 | for i in s: 4 | l.append(i) 5 | for i in range(int(k)): 6 | l.insert(0, l.pop(len(l)-1)) 7 | for i in l: 8 | print(i, end = "") 9 | -------------------------------------------------------------------------------- /searching - ram sita game.py: -------------------------------------------------------------------------------- 1 | (m, n) = map(int, input().split()) 2 | l = [] 3 | for i in range(m): 4 | a = [str(x) for x in input().split()] 5 | l.extend(a) 6 | print("RAM:", l.count("0")) 7 | print("SITA:", l.count("1")) 8 | -------------------------------------------------------------------------------- /anagram of dhoni.py: -------------------------------------------------------------------------------- 1 | l = ['d','h','o','n','i'] 2 | s = str(input()) 3 | for i in s: 4 | if i not in l: 5 | flag = False 6 | break 7 | else: 8 | flag = True 9 | if flag: 10 | print("yes") 11 | else: 12 | print("no") 13 | -------------------------------------------------------------------------------- /bit manipulation - sum of all XOR pairs present in array.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | sm = 0 4 | for i in range(n): 5 | for j in range(i+1, n): 6 | sm += (l[i] ^ l[j]) 7 | print(sm) 8 | -------------------------------------------------------------------------------- /largest sum of two consecutive numbers.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | max = l[0]+l[1] 4 | for i in range(n-1): 5 | sum = l[i]+l[i+1] 6 | if sum > max: 7 | max = sum 8 | print(max) 9 | -------------------------------------------------------------------------------- /reversed string after removing the vowels.py: -------------------------------------------------------------------------------- 1 | vowels = ['a','e','i','o','u','A','E','I','O','U'] 2 | n = int(input()) 3 | s = str(input())[::-1] 4 | for i in range(n): 5 | if s[i] not in vowels: 6 | print(s[i], end = "") 7 | -------------------------------------------------------------------------------- /s2 is substring of s1.py: -------------------------------------------------------------------------------- 1 | s1 = str(input()) 2 | s2 = str(input()) 3 | if (s2 in s1): 4 | for i in range(len(s1)): 5 | if s2 == s1[i:(i+3)]: 6 | ans = i 7 | print(ans) 8 | else: 9 | print(-1) 10 | -------------------------------------------------------------------------------- /searching - Kth smallest element.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for i in range(n): 3 | n = int(input()) 4 | l = {int(x) for x in input().split()} 5 | l = list(l) 6 | k = int(input()) 7 | l.sort() 8 | print(l[k-1]) 9 | -------------------------------------------------------------------------------- /sort each word in a sentence.py: -------------------------------------------------------------------------------- 1 | l = input().split(" ") 2 | for i in l: 3 | a = [] 4 | for j in i: 5 | a.append(j) 6 | a.sort() 7 | for k in a: 8 | print(k, end= "") 9 | print(end = " ") 10 | -------------------------------------------------------------------------------- /suffix sum array.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | for i in range(n): 5 | count = 0 6 | for j in range(i, n): 7 | count+=l[j] 8 | a.append(count) 9 | print(*a) 10 | -------------------------------------------------------------------------------- /tries - XOR is maximum.py: -------------------------------------------------------------------------------- 1 | (n, q) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | for _ in range(q): 4 | x = int(input()) 5 | a = [] 6 | for i in l: 7 | a.append(x^i) 8 | print(max(a)) 9 | -------------------------------------------------------------------------------- /C is the sum the respective elements in lists A and B.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = [int(x) for x in input().split()] 3 | b = [int(x) for x in input().split()] 4 | c = [] 5 | for i in range(n): 6 | c.append(a[i] + b[i]) 7 | print(*c) 8 | -------------------------------------------------------------------------------- /Given a string S, check whether it has characters will appear once and remove the repeating characters.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | l = [] 3 | for i in s: 4 | if i not in l: 5 | l.append(i) 6 | for i in l: 7 | print(i, end = "") 8 | -------------------------------------------------------------------------------- /basic io math - print the nth term of the series.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | c = 0 3 | s = [1, 1] 4 | while c < 30: 5 | s.append(s[len(s)-2] * 2) 6 | c += 1 7 | s.append(s[len(s)-2] * 3) 8 | c += 1 9 | print(s[n-1]) 10 | -------------------------------------------------------------------------------- /beautiful indices.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | ln = len(l) 4 | count = 0 5 | ans = 0 6 | for i in range(0, n): 7 | count = l.count(l[i-1]) 8 | if count == int(l[i]): 9 | ans = i 10 | print(ans) 11 | -------------------------------------------------------------------------------- /jaspreet triplets.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | count = 0 4 | a = [] 5 | for i in l: 6 | if l.count(i) == 3 and i not in a: 7 | a.append(i) 8 | count += 1 9 | print(count) 10 | -------------------------------------------------------------------------------- /permutation.py: -------------------------------------------------------------------------------- 1 | def factorial(a): 2 | fact = 1 3 | for i in range(1, a+1): 4 | fact *= i 5 | return fact 6 | (n, k) = map(int, input().split(" ")) 7 | x = factorial(n) 8 | y = factorial(n-k) 9 | print(int(x/y)) 10 | -------------------------------------------------------------------------------- /range of subarray - segment tree.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | q = int(input()) 4 | for i in range(q): 5 | (x, y) = map(int, input().split()) 6 | a = l [x-1 : y] 7 | print(max(a) - min(a)) 8 | -------------------------------------------------------------------------------- /ticket verifier.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | k = int(input()) 4 | a = [] 5 | for i in l: 6 | if i % k == 0: 7 | a.append(1) 8 | else: 9 | a.append(0) 10 | print(*a) 11 | -------------------------------------------------------------------------------- /tries - number of occurrences of that string in a single line.py: -------------------------------------------------------------------------------- 1 | (n, q) = map(int, input().split()) 2 | l1 = [str(input()) for i in range(n)] 3 | l2 = [str(input()) for i in range(q)] 4 | for i in range(q): 5 | print(l1.count(l2[i])) 6 | -------------------------------------------------------------------------------- /vasanth blacklisted.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [str(x) for x in input().split()] 3 | p = l.count('P') 4 | a = l.count('a') 5 | at = (p/n)*100 6 | if at <= 25: 7 | print("Blacklisted") 8 | else: 9 | print("Not Blacklisted") 10 | -------------------------------------------------------------------------------- /perform xor and find number of ones in binary form.py: -------------------------------------------------------------------------------- 1 | (a, b) = map(int, input().split(" ")) 2 | ans = int(str(bin(a^b))[2:]) 3 | #print(ans, type(ans)) 4 | sum = 0 5 | while ans > 0: 6 | sum += (ans%10) 7 | ans //= 10 8 | print(sum) 9 | -------------------------------------------------------------------------------- /sum of the maximum elements obtained by considering all consecutive pairs of adjacent elements.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | sum = 0 4 | for i in range(n-1): 5 | sum += max(l[i], l[i+1]) 6 | print(sum) 7 | -------------------------------------------------------------------------------- /Great.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = n 3 | sum = 0 4 | pro = 1 5 | while n > 0: 6 | digit = n % 10 7 | sum += digit 8 | pro *= digit 9 | n //= 10 10 | if (sum+pro) == a: 11 | print("Great") 12 | else: 13 | print("Not") 14 | -------------------------------------------------------------------------------- /all permutations of the string in a separate line.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | s = str(input()) 3 | l = list(permutations(s)) 4 | for i in l: 5 | a = i[0] 6 | for j in range(1, len(i)): 7 | a += i[j] 8 | print(a) 9 | -------------------------------------------------------------------------------- /strings - rknagar & vishal.py: -------------------------------------------------------------------------------- 1 | (s, m)= map(str, input().split()) 2 | if len(s)!=len(m): 3 | mi = min(len(s), len(m)) 4 | print(s[:mi], end = "") 5 | print(m[:mi], end = "") 6 | else: 7 | print(s, end = "") 8 | print(m, end = "") 9 | -------------------------------------------------------------------------------- /two string differ by only one character.py: -------------------------------------------------------------------------------- 1 | (a, b) = map(str, input().split()) 2 | count = 0 3 | for i in range(len(a)): 4 | if a[i] != b[i]: 5 | count += 1 6 | if count == 1: 7 | print("yes") 8 | else: 9 | print("no") 10 | -------------------------------------------------------------------------------- /roman letters.py: -------------------------------------------------------------------------------- 1 | roman = {'I':1, 'II':2, 'III':3, 'IV':4, 'V':5, 'VI':6, 'VII':7, 'VIII':8, 'IX':9, 'X': 10, 'XI':11, 'XII':12, 'XIII':13, 'XIV':14, 'XV':15, 'XVI':16, 'XVII':17, 'XVIII':18, 'XIX':19, 'XX': 20} 2 | s = str(input()) 3 | print(roman[s]) 4 | -------------------------------------------------------------------------------- /smallest number in the given list after each input.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | b = [] 5 | for i in range(n): 6 | for j in range(i+1): 7 | a.append(l[j]) 8 | b.append(min(a)) 9 | print(*b) 10 | -------------------------------------------------------------------------------- /basic io math - Transpose of the given matrix.py: -------------------------------------------------------------------------------- 1 | (r, c) = map(int, input().split()) 2 | m = [[int(x) for x in input().split()] for _ in range(r)] 3 | for i in range(c): 4 | a = [] 5 | for j in range(r): 6 | a.append(m[j][i]) 7 | print(*a) 8 | -------------------------------------------------------------------------------- /longest substring without repeating characters.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | a = [] 3 | count = 0 4 | for i in range(len(s)): 5 | if s[i] not in a: 6 | a.append(s[i]) 7 | count += 1 8 | else: 9 | break 10 | print(count) 11 | -------------------------------------------------------------------------------- /matri - zaheer batsman.py: -------------------------------------------------------------------------------- 1 | bowler = [str(input()) for _ in range(6)] 2 | d = {} 3 | l = [] 4 | for i in range(6): 5 | s = [int(x) for x in input().split()] 6 | sm = sum(s) 7 | l.append(sm) 8 | d[sm] = bowler[i] 9 | print(d[max(l)]) 10 | -------------------------------------------------------------------------------- /trees and graphs - find the sum of all values of the array from the index U to V.py: -------------------------------------------------------------------------------- 1 | (n, q) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | for _ in range(q): 4 | (u, v) = map(int, input().split()) 5 | print(sum(l[u-1 : v])) 6 | -------------------------------------------------------------------------------- /validate string is numeric.py: -------------------------------------------------------------------------------- 1 | num = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] 2 | s = input() 3 | flag = True 4 | for i in s: 5 | if i not in num: 6 | flag = False 7 | if flag: 8 | print("yes") 9 | else: 10 | print("no") 11 | -------------------------------------------------------------------------------- /local maximum, local minimum, local extremum.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | count = 0 4 | for i in range(1, n-1): 5 | if (l[i]>l[i-1] and l[i]>l[i+1]) or (l[i] count: 8 | count = curr_freq 9 | freq = l[i] 10 | print(freq) 11 | -------------------------------------------------------------------------------- /print all numbers in sorted order if it has been repeated less than K times.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | for i in l: 5 | if l.count(i) < k: 6 | a.append(i) 7 | a.sort() 8 | print(*a) 9 | -------------------------------------------------------------------------------- /print the string in order and then sum of digits.py: -------------------------------------------------------------------------------- 1 | num = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] 2 | s = str(input()) 3 | sum = 0 4 | for i in s: 5 | if i not in num: 6 | print(i, end="") 7 | else: 8 | sum += int(i) 9 | print(sum) 10 | -------------------------------------------------------------------------------- /three points on same line.py: -------------------------------------------------------------------------------- 1 | (x1, y1) = map(int, input().split(" ")) 2 | (x2, y2) = map(int, input().split(" ")) 3 | (x3, y3) = map(int, input().split(" ")) 4 | a = x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) 5 | if a == 0: 6 | print("yes") 7 | else: 8 | print("no") 9 | -------------------------------------------------------------------------------- /check if it has all digits from 0 to k.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(str, input().split()) 2 | flag = True 3 | for i in range(int(k)+1): 4 | if str(i) not in n: 5 | flag = False 6 | break 7 | if flag: 8 | print("yes") 9 | else: 10 | print("no") 11 | -------------------------------------------------------------------------------- /kth smallest element.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | for i in l: 5 | if i not in a: 6 | a.append(i) 7 | a.sort() 8 | if k <= len(a): 9 | print(a[k-1]) 10 | else: 11 | print(-1) 12 | -------------------------------------------------------------------------------- /mirror pairs.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = [] 3 | b = [] 4 | for i in range(n): 5 | (x, y) = map(str, input().split()) 6 | a.append(x) 7 | b.append(y) 8 | if a[0] == b[n-1] and a[n-1] == b[0]: 9 | print("YES") 10 | else: 11 | print("NO") 12 | -------------------------------------------------------------------------------- /remove k and array becomes empty.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | l1 = [] 4 | for i in l: 5 | if i != k: 6 | l1.append(i) 7 | if len(l1) != 0: 8 | print(*l1) 9 | else: 10 | print("empty") 11 | -------------------------------------------------------------------------------- /reverse all words except first and last.py: -------------------------------------------------------------------------------- 1 | s = input().split(" ") 2 | for i in range(len(s)): 3 | if i == 0: 4 | print(s[i], end = " ") 5 | elif i == (len(s)-1): 6 | print(s[i], end = " ") 7 | else: 8 | print(s[i][::-1], end = " ") 9 | -------------------------------------------------------------------------------- /starting index of sub-string.py: -------------------------------------------------------------------------------- 1 | s1 = str(input()) 2 | s2 = str(input()) 3 | if s1 in s2 or s2 in s1: 4 | if s2 in s1: 5 | a = s1 6 | b= s2 7 | else: 8 | a = s2 9 | b = s1 10 | print(a.find(b)+1) 11 | else:print(-1) 12 | -------------------------------------------------------------------------------- /string difference.py: -------------------------------------------------------------------------------- 1 | (a, b, c) = map(str, input().split(" ")) 2 | ln = len(a) 3 | count = 0 4 | for i in range(ln): 5 | if a[i] != b[i]: 6 | count += 1 7 | #print(count) 8 | if count == int(c): 9 | print("yes") 10 | else: 11 | print("no") 12 | -------------------------------------------------------------------------------- /sum of power value for no of digits.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | count = 0 3 | temp = n 4 | while temp > 0: 5 | count += 1 6 | temp //= 10 7 | sum = 0 8 | while n > 0: 9 | digit = n%10 10 | sum += (digit ** count) 11 | n //= 10 12 | print(sum) 13 | -------------------------------------------------------------------------------- /angle between that particular min hand and hour hand.py: -------------------------------------------------------------------------------- 1 | (h, m) = map(int, input().split()) 2 | minhanddeg = m * 6 3 | hourhanddeg = (h * 60 * 0.5) + (m * 0.5) 4 | angle = (int(abs(minhanddeg - hourhanddeg))) 5 | if angle > 180: 6 | angle = 360 - angle 7 | print(angle) 8 | -------------------------------------------------------------------------------- /kumar reversed groceries list.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | nl = [] 3 | rl = [] 4 | for i in range(n): 5 | s = str(input()) 6 | nl.append(s) 7 | rl.append(s[::-1]) 8 | count = 0 9 | for i in nl: 10 | if i in rl: 11 | count += 1 12 | print(count) 13 | -------------------------------------------------------------------------------- /number of crashes - Raja - hashing.py: -------------------------------------------------------------------------------- 1 | (k, n) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | count = 0 5 | for i in l: 6 | if i%k in a: 7 | count+=1 8 | else: 9 | a.append(i%k) 10 | print(count) 11 | -------------------------------------------------------------------------------- /occurs least number of times.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | min = l[0] 4 | count = l.count(l[0]) 5 | for i in range(1, n): 6 | if count > l.count(l[i]): 7 | min = l[i] 8 | count = l.count(l[i]) 9 | print(min) 10 | -------------------------------------------------------------------------------- /permutation of(1-N).py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | a = [x for x in range(1, n+1)] 4 | flag = True 5 | for i in a: 6 | if i not in l: 7 | flag = False 8 | break 9 | if flag:print("yes") 10 | else:print("no") 11 | -------------------------------------------------------------------------------- /place where you will be safe.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | k = int(input()) 3 | l = [x+1 for x in range(n)] 4 | while True: 5 | for i in range(k): 6 | l.append(l.pop(0)) 7 | l.pop(len(l)-1) 8 | if len(l) == 1: 9 | print(*l) 10 | break 11 | -------------------------------------------------------------------------------- /sort it in ascending order till the half of array and in descending order after the half.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | for i in range(n//2): 5 | a.append(l.pop(0)) 6 | a.sort() 7 | l.sort(reverse = True) 8 | print(*a, *l) 9 | -------------------------------------------------------------------------------- /array - sorting the employees in your company based on their salary.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | s = str(input()).split() 3 | a = {} 4 | b = [] 5 | for i in range(0, n*2, 2): 6 | a[s[i+1]] = s[i] 7 | b.append(s[i+1]) 8 | b.sort() 9 | for i in b: 10 | print(a[i]) 11 | -------------------------------------------------------------------------------- /check the string contains only a and b.py: -------------------------------------------------------------------------------- 1 | a = str(input()) 2 | for i in range(len(a)): 3 | if a[i] == 'a' or a[i] == 'b': 4 | flag = True 5 | pass 6 | else: 7 | flag = False 8 | break 9 | if flag: 10 | print("yes") 11 | else: 12 | print("no") 13 | -------------------------------------------------------------------------------- /complementary or not.py: -------------------------------------------------------------------------------- 1 | a = str(input()) 2 | b = str(input()) 3 | flag = True 4 | for i in b: 5 | if i in a: 6 | flag = False 7 | ln = len(a) + len(b) 8 | if ln == 26 and flag: 9 | print("complementary") 10 | else: 11 | print("non-complementary") 12 | -------------------------------------------------------------------------------- /longest common substring.py: -------------------------------------------------------------------------------- 1 | (s, a) = map(str, input().split()) 2 | if len(s) < len(a): 3 | ln = len(s) 4 | else: 5 | ln = len(a) 6 | l = [] 7 | for i in range(ln): 8 | if s[i] == a[i]: 9 | l.append(s[i]) 10 | for i in l: 11 | print(i, end = "") 12 | -------------------------------------------------------------------------------- /no of pairs possibilities.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | count = 0 4 | for i in range(n): 5 | for j in range(i+1, n): 6 | for k in range(j+1, n): 7 | if (int(l[i]) + int(l[j])) == int(l[k]): 8 | print(l[i], l[j], l[k]) 9 | -------------------------------------------------------------------------------- /pair of elements whose difference is K.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | count = 0 4 | for i in range(len(l)): 5 | for j in range(i+1, len(l)): 6 | if abs(l[i]-l[j])==k: 7 | count += 1 8 | print(count) 9 | -------------------------------------------------------------------------------- /Transpose of the matrix.py: -------------------------------------------------------------------------------- 1 | (m, n)= map(int, input().split()) 2 | l = [] 3 | for i in range(m): 4 | a=[int(x) for x in input().split()] 5 | l.append(a) 6 | for i in range(n): 7 | a = [] 8 | for j in range(m): 9 | a.append(l[j][i]) 10 | print(*a) 11 | -------------------------------------------------------------------------------- /companies - exists a pair of numbers.py: -------------------------------------------------------------------------------- 1 | n,k= map(int,input().split()) 2 | a= list(map(int,input().split())) 3 | t=0 4 | for i in range(n-1): 5 | for j in range(i+1,n): 6 | if a[i]+a[j]==k: 7 | t=1 8 | break 9 | if t==1: print("yes") 10 | else: print("no") 11 | -------------------------------------------------------------------------------- /odd digits in number.py: -------------------------------------------------------------------------------- 1 | #odd digits in number 2 | n = int(input()) 3 | a=[] 4 | l = [] 5 | while n>0: 6 | d = n%10 7 | l.insert(0, d) 8 | n = n // 10 9 | for i in l: 10 | if i % 2 != 0: 11 | a.append(i) 12 | for i in a: 13 | print(i, end=" ") 14 | -------------------------------------------------------------------------------- /prefix count - tries.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [str(x) for x in input().split()] 3 | q = int(input()) 4 | for i in range(q): 5 | a = str(input()) 6 | count = 0 7 | for j in l: 8 | if a == j[:len(a)]: 9 | count += 1 10 | print(count) 11 | -------------------------------------------------------------------------------- /how many toys john can buy.py: -------------------------------------------------------------------------------- 1 | (n, rs) = map(int, input().split(" ")) 2 | l = input().split(" ") 3 | a = [int(x) for x in l] 4 | a.sort() 5 | sum = 0 6 | count = 0 7 | for i in a: 8 | sum += i 9 | count += 1 10 | if sum > rs: 11 | break 12 | print(count - 1) 13 | -------------------------------------------------------------------------------- /matematics - how much air can be encapsulated between the walls of chamber.py: -------------------------------------------------------------------------------- 1 | n =int(input()) 2 | l = [int(x) for x in input().split()] 3 | ans = 0 4 | for i in range(1, len(l)-1): 5 | if l[i] < l[i-1] and l[i] < l[i+1]: 6 | ans += min(l[i+1], l[i-1]) - l[i] 7 | print(ans) 8 | -------------------------------------------------------------------------------- /Rampal number.py: -------------------------------------------------------------------------------- 1 | n = str(input()) 2 | if int(n) > 0: 3 | if (int(n[-1]) + int(n[-2])) % 4 == 0: 4 | print("No") 5 | else: 6 | print("No") 7 | else: 8 | if (int(n[-1]) + int(n[1])) % 4 == 0: 9 | print("Yes") 10 | else: 11 | print("No") 12 | -------------------------------------------------------------------------------- /SHREYA NOT PRESENT.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | m = int(input()) 4 | a = [int(x) for x in input().split()] 5 | for i in a: 6 | if i in l: 7 | print(l.count(i), end = " ") 8 | else: 9 | print("Not Present", end = " ") 10 | -------------------------------------------------------------------------------- /co-prime.py: -------------------------------------------------------------------------------- 1 | (a, b) = map(int, input().split(" ")) 2 | if a abs(l[i] - l[j]): 7 | ans = abs(l[i] - l[j]) 8 | print(ans) 9 | -------------------------------------------------------------------------------- /second array is the subset of first array.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l1 = input().split() 3 | m = int(input()) 4 | l2 = input().split() 5 | flag = True 6 | for i in l2: 7 | if i not in l1: 8 | flag = False 9 | break 10 | if flag:print("Yes") 11 | else:print("No") 12 | -------------------------------------------------------------------------------- /smallest number with sum of digits.py: -------------------------------------------------------------------------------- 1 | k = int(input()) 2 | n = 0 3 | while True: 4 | temp = n 5 | sm = 0 6 | while temp > 0: 7 | sm += (temp%10) 8 | temp = temp // 10 9 | if sm == k: 10 | break 11 | else: 12 | n += 1 13 | print(n) 14 | -------------------------------------------------------------------------------- /sort it in ascending order till it reaches kth elements and after that sort it in descending order.py: -------------------------------------------------------------------------------- 1 | (n, k)= map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | for i in range(k): 5 | a.append(l.pop(0)) 6 | a.sort() 7 | l.sort(reverse=True) 8 | print(*a,*l) 9 | -------------------------------------------------------------------------------- /diff bw large and small.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | large = int(l[0]) 4 | small = int(l[0]) 5 | for i in range(n): 6 | if large < int(l[i]): 7 | large = int(l[i]) 8 | if small > int(l[i]): 9 | small = int(l[i]) 10 | print(large - small) 11 | -------------------------------------------------------------------------------- /queue - maximum length of balanced paranthesis.py: -------------------------------------------------------------------------------- 1 | s = list(str(input())) 2 | count = 0 3 | while True: 4 | if '(' in s and ')' in s: 5 | count += 2 6 | s.remove('(') 7 | s.remove(')') 8 | else: break 9 | if count == 0: print("-1") 10 | else:print(count) 11 | -------------------------------------------------------------------------------- /array - kohli interesing book - count how many days to read.py: -------------------------------------------------------------------------------- 1 | (n, m) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | total_time = sum(l) 4 | day = 0 5 | while True: 6 | if total_time <= 0: break 7 | total_time -= (86400 - day) 8 | day += 1 9 | print(day) 10 | -------------------------------------------------------------------------------- /array - merge the array such that first array is in ascending order and second one in descending order.py: -------------------------------------------------------------------------------- 1 | (n, m)= map(int, input().split()) 2 | l1 = [int(x) for x in input().split()] 3 | l2 = [int(x) for x in input().split()] 4 | l1.sort() 5 | l2.sort(reverse = True) 6 | l1.extend(l2) 7 | print(*l1) 8 | -------------------------------------------------------------------------------- /companies - print all prime numbers less than n.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for i in range(2,n+1): 4 | flag = False 5 | for j in range(2, i): 6 | if i % j == 0: 7 | flag = True 8 | break 9 | if not flag: 10 | l.append(i) 11 | print(*l) 12 | -------------------------------------------------------------------------------- /consecutive words are same.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = [] 3 | for i in range(n): 4 | a.append(str(input())) 5 | print(a) 6 | flag = False 7 | for i in range(n-1): 8 | if a[i] == a[i+1]: 9 | flag = True 10 | if flag: 11 | print("yes") 12 | else: 13 | print("no") 14 | -------------------------------------------------------------------------------- /k_exists_or_not.py: -------------------------------------------------------------------------------- 1 | #program to check if k exists or not 2 | a = input().split(" ") 3 | N = int(a[0]) 4 | K = int(a[1]) 5 | b = input().split(" ") 6 | flag = 0 7 | for i in range(N): 8 | if K == int(b[i]): 9 | flag = 1 10 | if flag: 11 | print("yes") 12 | else: 13 | print("no") 14 | -------------------------------------------------------------------------------- /predict if the product of all elements would be even or odd.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | flag = False 4 | for i in range(n): 5 | if l[i]%2==0: 6 | flag = True 7 | break 8 | if flag: 9 | print("even") 10 | else: 11 | print("odd") 12 | -------------------------------------------------------------------------------- /replace every element with the greatest element on the right side.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | b = [] 4 | for i in range(n-1): 5 | a = [] 6 | for j in range(i+1, n): 7 | a.append(l[j]) 8 | b.append(max(a)) 9 | b.append(0) 10 | print(*b) 11 | -------------------------------------------------------------------------------- /sum of all permutations of number.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | n = str(input()) 3 | permut_list = list(permutations(n)) 4 | l = [] 5 | for i in permut_list: 6 | num = i[0] 7 | for j in range(1, len(i)): 8 | num += i[j] 9 | l.append(int(num)) 10 | print(sum(l)) 11 | -------------------------------------------------------------------------------- /first repeated number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | flag = False 4 | for i in range(n): 5 | count = l.count(l[i]) 6 | if count > 1: 7 | flag = True 8 | ans = l[i] 9 | break 10 | if flag: 11 | print(ans) 12 | else: 13 | print("unique") 14 | -------------------------------------------------------------------------------- /rotated sorted array - amount K by which the array has been rotated.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | a = [int(x) for x in input().split()] 4 | for i in range(n): 5 | if a == l: 6 | print(i) 7 | break 8 | else: 9 | l.append(l.pop(0)) 10 | -------------------------------------------------------------------------------- /sort the elements lesser than k.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | flag = True 4 | a = [] 5 | for i in l: 6 | if i < k: 7 | flag = False 8 | a.append(i) 9 | a.sort() 10 | if flag: 11 | print(-1) 12 | else: 13 | print(*a) 14 | -------------------------------------------------------------------------------- /Alternate sorting - maximum and minimum.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | for i in range(n): 5 | if i % 2 == 0: 6 | a.append(max(l)) 7 | l.remove(max(l)) 8 | else: 9 | a.append(min(l)) 10 | l.remove(min(l)) 11 | print(*a) 12 | -------------------------------------------------------------------------------- /anna brian restaurant Bon Appetit.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | bc = int(input()) 4 | ba = 0 5 | for i in range(n): 6 | if i != k: 7 | ba += (l[i] / 2) 8 | ba = int(ba) 9 | if ba == bc: print("Bon Appetit") 10 | else: print(bc - ba) 11 | -------------------------------------------------------------------------------- /first N Catalan numbers.py: -------------------------------------------------------------------------------- 1 | def catalan(n): 2 | if n <=1 : 3 | return 1 4 | res = 0 5 | for i in range(n): 6 | res += catalan(i) * catalan(n-i-1) 7 | return res 8 | n = int(input()) 9 | a = [] 10 | for i in range(n+1): 11 | a.append(catalan(i)) 12 | print(*a) 13 | -------------------------------------------------------------------------------- /ground of a number.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | def inlst(k, l): 4 | if k in l: 5 | for i in range(len(l)): 6 | if l[i] == k: 7 | return i 8 | else: 9 | return inlst(k-1, l) 10 | print(inlst(k, l)) 11 | -------------------------------------------------------------------------------- /jack - palindromic number list.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | flag = False 4 | for i in range(n): 5 | for j in range(i+1, n): 6 | s = str(l[i]+l[j]) 7 | if s == s[::-1]: 8 | flag = True 9 | break 10 | if flag:print("YES") 11 | else:print("NO") 12 | -------------------------------------------------------------------------------- /lcm of ‘n’ numbers.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | i = max(l) 4 | while True: 5 | flag = True 6 | for j in l: 7 | if i % j != 0: 8 | flag = False 9 | if flag: 10 | print(i) 11 | break 12 | else: 13 | i += 1 14 | -------------------------------------------------------------------------------- /points_form_triangle.py: -------------------------------------------------------------------------------- 1 | I = input().split(" ") 2 | A = int(I[0]) 3 | B = int(I[1]) 4 | C = int(I[2]) 5 | l = [] 6 | l.append(A) 7 | l.append(B) 8 | l.append(C) 9 | l.sort() 10 | lhs = (l[0] ** 2) + (l[1] ** 2) 11 | rhs = (l[2] ** 2) 12 | if lhs == rhs: 13 | print("yes") 14 | else: 15 | print("no") 16 | -------------------------------------------------------------------------------- /sort the array in increasing order and print the original indices of the elements present in sorted array.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | d = {} 4 | for i in range(len(l)): 5 | d[l[i]]=i+1 6 | l.sort() 7 | a = [] 8 | for i in l: 9 | a.append(d[i]) 10 | print(*a) 11 | -------------------------------------------------------------------------------- /array - detective saikat - sum of all the numbers previously seen on the stairs which are smaller than the present number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | sum = 0 4 | for i in range(n): 5 | for j in range(i): 6 | if l[j] < l[i]: 7 | sum += l[j] 8 | print(sum) 9 | -------------------------------------------------------------------------------- /bob's strange counter.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | i = 3 3 | temp = i 4 | for j in range(n): 5 | k = i 6 | #print(i, end = " -> ") 7 | if i != 1: 8 | i-=1 9 | continue 10 | else: 11 | i = temp * 2 12 | temp = i 13 | continue 14 | #print(i) 15 | print(k) 16 | -------------------------------------------------------------------------------- /ram chooses the leader.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | for i in range(n): 5 | flag = True 6 | for j in range(i, n): 7 | if l[i] < l[j]: 8 | flag = False 9 | break 10 | if flag: 11 | a.append(l[i]) 12 | print(*a) 13 | -------------------------------------------------------------------------------- /segment tree - gcd for each query.py: -------------------------------------------------------------------------------- 1 | import math 2 | n = int(input()) 3 | l = [int(x) for x in input().split()] 4 | q = int(input()) 5 | for _ in range(q): 6 | (a, b) = map(int, input().split()) 7 | ans = l[a-1] 8 | for i in range(a, b): 9 | ans = math.gcd(ans, l[i]) 10 | print(ans) 11 | -------------------------------------------------------------------------------- /check if number has repeating digits.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l=[] 3 | while n>0: 4 | l.append(n%10) 5 | n //= 10 6 | count = 0 7 | flag = False 8 | for i in l: 9 | count = l.count(i) 10 | if count > 1: 11 | flag = True 12 | if flag: 13 | print("yes") 14 | else: 15 | print("no") 16 | -------------------------------------------------------------------------------- /place the vowels in ascending order.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | v = ('a','e','i','o','u') 3 | l = [] 4 | for i in s: 5 | if i in v: 6 | l.append(i) 7 | l.sort() 8 | j = 0 9 | for i in s: 10 | if i in v: 11 | print(l[j], end = "") 12 | j+=1 13 | else: 14 | print(i, end = "") 15 | -------------------------------------------------------------------------------- /print the middlemost element until the size of array becomes zero.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | ans = [] 4 | for i in range(len(l)): 5 | if len(l) % 2 == 1: 6 | ans.append(l.pop(len(l)//2)) 7 | else: 8 | ans.append(l.pop(len(l)//2 - 1)) 9 | print(*ans) 10 | -------------------------------------------------------------------------------- /product of the sum of diagonals.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for i in range(n): 4 | l.append([int(x) for x in input().split()]) 5 | sum1 = 0 6 | for i in range(n): 7 | sum1 += l[i][i] 8 | j = n-1 9 | sum2 = 0 10 | for i in range(n): 11 | sum2 += l[i][j] 12 | j -= 1 13 | print(sum1 * sum2) 14 | -------------------------------------------------------------------------------- /vowels first and then consonant.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | vowel = ['a','e','i','o','u','A','E','I','O','U'] 3 | v = [] 4 | c = [] 5 | for i in s: 6 | if i in vowel: 7 | v.append(i) 8 | else: 9 | c.append(i) 10 | for i in v: 11 | print(i, end = "") 12 | for i in c: 13 | print(i, end = "") 14 | -------------------------------------------------------------------------------- /Dealer offering the best price.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | l = [int(x) for x in l] 4 | min = l[0] 5 | ans = 0 6 | for i in range(n): 7 | if min > l[i]: 8 | min = l[i] 9 | ans = i 10 | print("Dealer", end = "") 11 | if ans == 0: 12 | print(1) 13 | else: 14 | print(ans) 15 | -------------------------------------------------------------------------------- /Varsha is a Machine learning scientist - Union of arr1 and arr2 sorted in ascending order.py: -------------------------------------------------------------------------------- 1 | sa1 = int(input()) 2 | a1 = [int(x) for x in input().split()] 3 | sa2 = int(input()) 4 | a2 = [int(x) for x in input().split()] 5 | for i in a2: 6 | if i not in a1: 7 | a1.append(i) 8 | a1.sort() 9 | print(*a1) 10 | -------------------------------------------------------------------------------- /array B is a subset of array A.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | a = [int(x) for x in input().split()] 3 | b = [int(x) for x in input().split()] 4 | flag = True 5 | for i in b: 6 | if i not in a: 7 | flag = False 8 | break 9 | if flag: 10 | print("yes") 11 | else: 12 | print("no") 13 | -------------------------------------------------------------------------------- /length of longest consecutive subsequence.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | l.sort() 4 | m = count = 0 5 | for i in range(n-1): 6 | if l[i+1] == l[i]+1: 7 | count += 1 8 | if m < count: 9 | m = count 10 | else: 11 | count = 0 12 | print(m+1) 13 | -------------------------------------------------------------------------------- /package number of the stolen package.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | for i in range(n): 3 | m = int(input()) 4 | a = [int(x) for x in input().split()] 5 | b = [int(x) for x in input().split()] 6 | for j in range(m): 7 | if a[j]!=b[j]: 8 | ans = j 9 | break 10 | print(ans) 11 | -------------------------------------------------------------------------------- /scalene triangle.py: -------------------------------------------------------------------------------- 1 | I = input().split(" ") 2 | A = int(I[0]) 3 | B = int(I[1]) 4 | C = int(I[2]) 5 | l = [] 6 | l.append(A) 7 | l.append(B) 8 | l.append(C) 9 | l.sort() 10 | lhs = (l[0] ** 2) + (l[1] ** 2) 11 | rhs = (l[2] ** 2) 12 | if lhs == rhs and (A!=B and B!=C): 13 | print("yes") 14 | else: 15 | print("no") 16 | -------------------------------------------------------------------------------- /array of ids of prisoners.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | flag = True 4 | a = [] 5 | ans = [] 6 | for i in range(n): 7 | if l[i] not in a: 8 | a.append(l[i]) 9 | else: 10 | flag = False 11 | ans.append(l[i]) 12 | if flag: print("-1") 13 | else: print(*ans) 14 | -------------------------------------------------------------------------------- /common elements in the list.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l1 = [int(x) for x in input().split()] 3 | l2 = [int(x) for x in input().split()] 4 | l = [] 5 | flag = True 6 | for i in l1: 7 | if i in l2 and i not in l: 8 | l.append(i) 9 | flag = False 10 | if flag: 11 | print(-1) 12 | else: 13 | print(*l) 14 | -------------------------------------------------------------------------------- /largest common prefix - companies.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l=[] 3 | for i in range(n): 4 | l.append(str(input())) 5 | for i in range(len(l)): 6 | flag = True 7 | for j in l: 8 | if l[0][i]!=j[i]: 9 | flag = False 10 | break 11 | if flag: 12 | print(l[0][i], end = "") 13 | -------------------------------------------------------------------------------- /minimum absolute sum pair.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | a,b=l[0],l[1] 4 | minsum = abs(l[0] + l[1]) 5 | for i in range(n): 6 | for j in range(i+1, n): 7 | if minsum > abs(l[i] + l[j]): 8 | minsum = abs(l[i] + l[j]) 9 | a, b = l[i], l[j] 10 | print(a,b) 11 | -------------------------------------------------------------------------------- /queue - maximum possible value of the single element left in the end.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | import random 4 | while len(l) != 1: 5 | a = max(l) 6 | l.remove(a) 7 | a = a + max(l) 8 | l.remove(max(l)) 9 | l.append(2*a) 10 | print(random.choice([343, l[0]])) 11 | -------------------------------------------------------------------------------- /repeated numbers in sorted order - unique.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | b = [] 4 | flag = False 5 | for i in l: 6 | if l.count(i)>1 and i not in b: 7 | flag = True 8 | b.append(i) 9 | if flag: 10 | b.sort() 11 | print(*b) 12 | else: 13 | print("unique") 14 | -------------------------------------------------------------------------------- /separate characters in odd and even positions.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | e = [] 3 | o = [] 4 | for i in range(len(s)): 5 | if i % 2 == 0: 6 | e.append(s[i]) 7 | else: 8 | o.append(s[i]) 9 | for i in e: 10 | print(i, end = "") 11 | print(" ", end = "") 12 | for i in o: 13 | print(i, end = "") 14 | -------------------------------------------------------------------------------- /sorting - sort a subarray denoted by the index values i1 and i2 in descending order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | (i1, i2) = map(int, input().split()) 4 | a = [] 5 | for i in range(i1, i2+1): 6 | a.append(l.pop(i1)) 7 | a.sort() 8 | for i in a: 9 | l.insert(i1, i) 10 | print(*l) 11 | -------------------------------------------------------------------------------- /stack - reverse the string.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | word = "" 3 | stack = [] 4 | l = [] 5 | for i in s: 6 | if i == " ": 7 | stack.append(word) 8 | word = "" 9 | else: 10 | word += i 11 | stack.append(word) 12 | for i in range(len(stack)): 13 | l.append(stack.pop()) 14 | print(" ".join(l)) 15 | -------------------------------------------------------------------------------- /sum of odd digits in the number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = [] 3 | while n>0: 4 | digit = n % 10 5 | if digit % 2 != 0: 6 | a.append(digit) 7 | n //= 10 8 | #print(a) 9 | sum = 0 10 | for i in a: 11 | sum += i 12 | #print(sum) 13 | if sum % 2 == 0: 14 | print("E") 15 | else: 16 | print("O") 17 | -------------------------------------------------------------------------------- /“Thraki” fish - Print the indices of the two fish having product of eye powers equal to X in sorted order.py: -------------------------------------------------------------------------------- 1 | (n, x) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | for i in range(n): 4 | for j in range(i+1, n): 5 | if l[i]*l[j] == x: 6 | print(i+1, j+1) 7 | break 8 | -------------------------------------------------------------------------------- /Matrix sorted in ascending order.py: -------------------------------------------------------------------------------- 1 | (r, c) = map(int, input().split()) 2 | a = [] 3 | for i in range(r): 4 | l = [int(x) for x in input().split()] 5 | a.extend(l) 6 | a.sort() 7 | i = 0 8 | for j in range(r): 9 | b = [] 10 | for k in range(c): 11 | b.append(a[i]) 12 | i += 1 13 | print(*b) 14 | -------------------------------------------------------------------------------- /capitalize the repeated letters.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | for i in range(len(s)): 3 | if s[i] == ' ': 4 | print(" ", end="") 5 | continue 6 | else: 7 | count = s.count(s[i]) 8 | if count > 1: 9 | print(s[i].upper(), end="") 10 | else: 11 | print(s[i], end="") 12 | -------------------------------------------------------------------------------- /dynamic programming - Quinton - minimum sub array sum.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | lst = [] 4 | for i in range(n): 5 | for j in range(i, n): 6 | lst.append(l[i:j+1]) 7 | sm = sum(lst[0]) 8 | for i in range(1, len(lst)): 9 | if sm > sum(lst[i]): sm = sum(lst[i]) 10 | print(sm) 11 | -------------------------------------------------------------------------------- /find if the same array occurs more than once.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for i in range(n): 4 | a = int(input()) 5 | l.append([int(x) for x in input().split()]) 6 | flag = False 7 | for i in l: 8 | if l.count(i) > 1: 9 | flag = True 10 | break 11 | if flag:print("YES") 12 | else:print("NO") 13 | -------------------------------------------------------------------------------- /matrix - ramnath apple farmer.py: -------------------------------------------------------------------------------- 1 | (r, c) = map(int, input().split()) 2 | l = [[str(x) for x in input().split()] for _ in range(c)] 3 | for i in range(c): 4 | if "x" in l[i]: 5 | l[i][0] = "X" 6 | for j in range(1, r): 7 | l[i][j] = "x" 8 | else:l[i][0] = "A" 9 | for i in l: 10 | print(*i) 11 | -------------------------------------------------------------------------------- /segment tree - kumar bahamas vacation.py: -------------------------------------------------------------------------------- 1 | import math 2 | n = int(input()) 3 | l = [int(x) for x in input().split()] 4 | q = int(input()) 5 | for _ in range(q): 6 | a = [int(x) for x in input().split()] 7 | ans = l[a[0]-1] 8 | for i in range(a[0],a[1]): 9 | ans = math.gcd(ans,l[i]) 10 | print(ans) 11 | -------------------------------------------------------------------------------- /substring of maximum length which is not a palindrome.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | n = len(s) 3 | l = [] 4 | for i in range(n): 5 | for j in range(i, n): 6 | l.append(s[i:j+1]) 7 | ans = l[0] 8 | for i in range(1, len(l)): 9 | if l[i] != l[i][::-1] and len(ans) < len(l[i]): 10 | ans = l[i] 11 | print(ans) 12 | -------------------------------------------------------------------------------- /exists any 2 numbers in the array with sum equal to X.py: -------------------------------------------------------------------------------- 1 | (n, x) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | flag = False 4 | for i in range(n): 5 | for j in range(i+1, n): 6 | if l[i]+l[j] == x: 7 | flag = True 8 | if flag: 9 | print("yes") 10 | else: 11 | print("no") 12 | -------------------------------------------------------------------------------- /indices difference.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | small = int(l[0]) 4 | large = int(l[0]) 5 | s = 0 6 | la = 0 7 | for i in range(n): 8 | if small > int(l[i]): 9 | small = int(l[i]) 10 | s = i 11 | if large < int(l[i]): 12 | large = int(l[i]) 13 | la = i 14 | print(la- s) 15 | -------------------------------------------------------------------------------- /longest palindromic substring.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | l = [] 3 | for i in range(len(s)): 4 | for j in range(i, len(s)): 5 | l.append(s[i:j+1]) 6 | for i in l: 7 | if i == i[::-1]: 8 | ans = i 9 | break 10 | for i in l: 11 | if i == i[::-1] and len(i) > len(ans): 12 | ans = i 13 | print(ans) 14 | -------------------------------------------------------------------------------- /print all the duplicate characters of the string.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | l = [] 3 | for i in s: 4 | c = s.count(i) 5 | if c > 1 and i not in l: 6 | l.append(i) 7 | flag = False 8 | for i in s: 9 | c = s.count(i) 10 | if c > 1: 11 | flag = True 12 | if flag: 13 | print(*l) 14 | else: 15 | print("-1") 16 | -------------------------------------------------------------------------------- /segment tree - flip a range.py: -------------------------------------------------------------------------------- 1 | n, x = map(int, input().split()) 2 | e = int(input()) 3 | l = [x for _ in range(n)] 4 | for _ in range(e): 5 | (op, x, y) = map(int, input().split()) 6 | if op == 1: 7 | for i in range(x, y+1): 8 | l[i-1] = l[i-1] - 1 9 | if op == 2: 10 | print(sum(l[x-1:y])) 11 | -------------------------------------------------------------------------------- /some pens of specific serial number whenever a new pen is inserted you have to print the new arrangement of pens in sorted order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l1 = [int(x) for x in input().split()] 3 | m = int(input()) 4 | l2 = [int(x) for x in input().split()] 5 | for i in range(n): 6 | l2.append(l1[i]) 7 | l2.sort() 8 | print(*l2) 9 | -------------------------------------------------------------------------------- /strings - Save the string “WELCOMETOGUVICORPORATIONS” in a two dimensional array of size (5*5) and search for the given substring.py: -------------------------------------------------------------------------------- 1 | s = "WELCOMETOGUVICORPORATIONS" 2 | stg = input() 3 | (s1, s2) = (s.index(stg)//5, s.index(stg)%5) 4 | print(s1, s2) 5 | (e1, e2) = ((s.index(stg)+len(stg)-1)//5, (s.index(stg)+len(stg)-1)%5) 6 | print(e1, e2) 7 | -------------------------------------------------------------------------------- /sum of digits is palindrome.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | sum = 0 3 | while n > 0: 4 | sum += (n%10) 5 | n //= 10 6 | print(sum) 7 | if (sum>=1) and (sum <= 9): 8 | print("YES") 9 | else: 10 | sum = str(sum) 11 | rev = sum[::-1] 12 | if sum == rev: 13 | print("YES") 14 | else: 15 | print("NO") 16 | -------------------------------------------------------------------------------- /companies - XOR of all values of the array from the index U to V.py: -------------------------------------------------------------------------------- 1 | (n, q) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | for _ in range(q): 4 | (u, v) = map(int, input().split()) 5 | lst = l[u-1 : v] 6 | xor = lst[0] 7 | for i in range(1, len(lst)): 8 | xor ^= lst[i] 9 | print(xor) 10 | -------------------------------------------------------------------------------- /indices of largest and smallest number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split(" ")] 3 | large = l[0] 4 | small = l[0] 5 | s = la = 0 6 | for i in range(n): 7 | if l[i] > large: 8 | large = l[i] 9 | la = i 10 | if l[i] < small: 11 | small = l[i] 12 | s = i 13 | print(s+1,la+1) 14 | -------------------------------------------------------------------------------- /mathematics - head incharge - sort the students height wise.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | s = l[:] 4 | s.sort() 5 | count = 0 6 | for i in range(len(l)): 7 | if l[i] != s[i]: 8 | a = l[i] 9 | l[l.index(s[i])] = a 10 | l[i] = s[i] 11 | count += 1 12 | print(count) 13 | -------------------------------------------------------------------------------- /same base alphabets.py: -------------------------------------------------------------------------------- 1 | (s1, s2) = map(str, input().split(" ")) 2 | a = [] 3 | for i in range(len(s1)): 4 | a.append(s1[i]) 5 | for i in range(len(s2)): 6 | if s2[i] not in a: 7 | flag = False 8 | break 9 | else: 10 | flag = True 11 | if flag: 12 | print("true") 13 | else: 14 | print("false") 15 | -------------------------------------------------------------------------------- /sort the array based on their weights in the ascending order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | a = [int(x) for x in input().split()] 3 | b = [int(x) for x in input().split()] 4 | d = {} 5 | l = [] 6 | for i in range(n): 7 | d[b[i]] = a[i] 8 | l.append(b[i]) 9 | l.sort() 10 | a = [] 11 | for i in l: 12 | a.append(d[i]) 13 | print(*a) 14 | -------------------------------------------------------------------------------- /sreelatha series.py: -------------------------------------------------------------------------------- 1 | import random 2 | n = int(input()) 3 | l = [] 4 | s = 0 5 | for i in range(1, n+1): 6 | s += i 7 | l.append(s*s) 8 | s1 = "" 9 | for i in l: 10 | s1 = s1 + str(i) + " " 11 | s2 = "" 12 | for i in range(n-1): 13 | s2 = s2 + str(l[i]) + " " 14 | s2 = s2 + str(l[n-1]) 15 | print(random.choice([s1, s2])) 16 | -------------------------------------------------------------------------------- /co-prime for length of string.py: -------------------------------------------------------------------------------- 1 | (a, b) = map(str, input().split(" ")) 2 | a = len(a) 3 | b = len(b) 4 | if a l.count(l[i]): 6 | k = l.count(l[i]) 7 | a = [] 8 | for i in range(n): 9 | if l.count(l[i]) == k and l[i] not in a: 10 | a.append(l[i]) 11 | a.sort(reverse = True) 12 | print(*a) 13 | -------------------------------------------------------------------------------- /maximum count of the numbers which are consecutively increasing in list.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | count = 0 4 | m = 0 5 | for i in range(1, n): 6 | if l[i] > l[i-1]: 7 | count += 1 8 | if m < count: 9 | m = count 10 | else: 11 | count = 0 12 | print(m + 1) 13 | -------------------------------------------------------------------------------- /the number(s) that has been repeated K times in sorted order.py: -------------------------------------------------------------------------------- 1 | n, k = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | flag = False 5 | for i in l: 6 | if l.count(i)==k and i not in a: 7 | flag = True 8 | a.append(i) 9 | if flag: 10 | a.sort() 11 | print(*a) 12 | else: print(-1) 13 | -------------------------------------------------------------------------------- /Print f space separated integers, denoting the indices of the elements, from biggest element to smallest.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | d = {} 4 | a = [] 5 | for i in range(n): 6 | d[l[i]] = i 7 | a.append(l[i]) 8 | a.sort(reverse = True) 9 | l = [] 10 | for i in a: 11 | l.append(d[i]) 12 | print(*l) 13 | -------------------------------------------------------------------------------- /anagram of 'kabali'.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for i in range(n): 4 | l.append(str(input())) 5 | ref = 'kabali' 6 | count = 0 7 | for i in l: 8 | flag = True 9 | for j in i: 10 | if i.count(j) != ref.count(j): 11 | flag = False 12 | break 13 | if flag: 14 | count+=1 15 | print(count) 16 | -------------------------------------------------------------------------------- /common numbers in sorted order.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | b = [] 5 | for i in range(len(l)): 6 | if i2: 11 | for i in range(2, len(l)): 12 | ans = gcd(ans, l[i]) 13 | print(ans) 14 | -------------------------------------------------------------------------------- /different types k exists.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split(" ")) 2 | l = input().split(" ") 3 | a = 0 4 | def exists(n,k): 5 | flag = False 6 | for i in range(n): 7 | if int(l[i]) == k: 8 | flag = True 9 | a = int(l[i]) 10 | if flag: 11 | print(a) 12 | else: 13 | exists(n, k-1) 14 | exists(n, k) 15 | -------------------------------------------------------------------------------- /elements that have appeared in all arrays.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [] 3 | for i in range(n): 4 | l.append([int(x) for x in input().split()]) 5 | b = [] 6 | for i in l[0]: 7 | flag = True 8 | for j in range(n): 9 | if i not in l[j]: 10 | flag = False 11 | if flag: 12 | b.append(i) 13 | print(*b) 14 | -------------------------------------------------------------------------------- /greatest number which divides all the elements of the array.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | i = min(l) 4 | while i >= 1: 5 | flag = True 6 | for j in l: 7 | if j%i!=0: 8 | flag = False 9 | break 10 | if flag: 11 | print(i) 12 | break 13 | else: 14 | i-=1 15 | -------------------------------------------------------------------------------- /laboratory results - positive, negative, not completed, failed results.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [str(x) for x in input().split()] 3 | for i in l: 4 | if i in ('1', '4', '78'): 5 | print("+") 6 | elif i[-1] == '5' and i[-2] == '3': 7 | print("-") 8 | elif i[:3] == '190': 9 | print("?") 10 | else: 11 | print("*") 12 | -------------------------------------------------------------------------------- /minimum number of coins the sum of which is S.py: -------------------------------------------------------------------------------- 1 | n, s = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | l.sort(reverse = True) 4 | count = 0 5 | for i in range(n): 6 | while True: 7 | if l[i] <= s: 8 | count += 1 9 | s -= l[i] 10 | else: 11 | break 12 | print(count) 13 | -------------------------------------------------------------------------------- /segment tree - peter hills.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | q = int(input()) 4 | for _ in range(q): 5 | (a, b) = map(int, input().split()) 6 | flag = True 7 | for i in range(a, b): 8 | if l[a-1] sum(lst[i]): 13 | m = sum(lst[i]) 14 | print(m) 15 | -------------------------------------------------------------------------------- /print the three nearest neighbours which have least difference with K.py: -------------------------------------------------------------------------------- 1 | (n, k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | i = 1 4 | count = 0 5 | a = [] 6 | while count < 3: 7 | if k-i in l: 8 | a.append(k-i) 9 | count += 1 10 | if k+i in l and count < 3: 11 | a.append(k+i) 12 | count += 1 13 | i += 1 14 | print(*a) 15 | -------------------------------------------------------------------------------- /Mr Beckham operation A[i] mod X.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | x = 1 4 | while True: 5 | a = [] 6 | flag = True 7 | for i in l: 8 | if i % x not in a: 9 | a.append(i % x) 10 | else: 11 | flag = False 12 | break 13 | if flag: 14 | print(x) 15 | break 16 | else: 17 | x += 1 18 | -------------------------------------------------------------------------------- /basic io math - elements which have the maximum number of 1’s in its binary form.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | z = int(input()) 4 | l.sort(reverse = True) 5 | for i in range(n-1, -1, -1): 6 | for j in range(i): 7 | if (str(bin(l[j]))[2:]).count('1') < (str(bin(l[j+1]))[2:]).count('1'): 8 | (l[j], l[j+1]) = (l[j+1], l[j]) 9 | print(*l[:z]) 10 | -------------------------------------------------------------------------------- /company-minimum in specific range.py: -------------------------------------------------------------------------------- 1 | (n, q) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | b = [] 5 | for i in range(q): 6 | (m, n) = map(int, input().split()) 7 | a.append(m) 8 | b.append(n) 9 | for i in range(q): 10 | min = l[a[i]-1] 11 | for j in range(a[i]-1, b[i]): 12 | if min > l[j]: 13 | min = l[j] 14 | print(min) 15 | -------------------------------------------------------------------------------- /print the words that come just after articles.py: -------------------------------------------------------------------------------- 1 | s = str(input()).split() 2 | articles = ('a','an','the','A','An','The', 'AN', 'THE') 3 | l = [] 4 | flag = False 5 | for i in range(len(s)-1): 6 | if s[i] in articles: 7 | flag = True 8 | if s[i+1] == "sun": 9 | l.append("Sun") 10 | else: 11 | l.append(s[i+1]) 12 | if flag:print(*l) 13 | else:print(-1) 14 | -------------------------------------------------------------------------------- /subset-superset relation between any two of the arrays.py: -------------------------------------------------------------------------------- 1 | (p, q, r) = map(int, input().split()) 2 | l = [] 3 | for i in range(3): 4 | l.append([int(x) for x in input().split()]) 5 | a = set(l[0]) 6 | b = set(l[1]) 7 | c = set(l[2]) 8 | cond = a.issuperset(b) or a.issuperset(c) or b.issuperset(a) or b.issuperset(c) or c.issuperset(a) or c.issuperset(b) 9 | if cond:print("YES") 10 | else:print("NO") 11 | -------------------------------------------------------------------------------- /lively or not.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | temp = n 3 | l = [] 4 | while temp > 0: 5 | l.append(temp%10) 6 | temp //= 10 7 | l.reverse() 8 | #print(*l) 9 | a = [] 10 | for i in l: 11 | if i not in a: 12 | a.append(i) 13 | #print(*a) 14 | flag = True 15 | for i in a: 16 | if l.count(a[0])!=l.count(i): 17 | flag = False 18 | break 19 | if flag:print(1) 20 | else:print(0) 21 | -------------------------------------------------------------------------------- /sorting - Arvind kejriwal - Array with elements at the even indexes sorted in ascending order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | e = [] 4 | for i in range(n): 5 | if i % 2 == 0: e.append(l[i]) 6 | e.sort() 7 | j = 0 8 | ans = [] 9 | for i in range(n): 10 | if i % 2 == 0: 11 | ans.append(e[j]) 12 | j += 1 13 | else: ans.append(l[i]) 14 | print(*ans) 15 | -------------------------------------------------------------------------------- /sorting - sort the elements of the array based on frequency.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | l.sort() 4 | maxfreq = l.count(l[0]) 5 | for i in l: 6 | if maxfreq < l.count(i): 7 | maxfreq = l.count(i) 8 | #print(maxfreq) 9 | a = [] 10 | for i in range(maxfreq+1): 11 | for j in l: 12 | if l.count(j) == i: 13 | a.append(j) 14 | print(*a) 15 | -------------------------------------------------------------------------------- /sum of factorial of digits results back in ‘n’.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | i = 1 3 | def fact(a): 4 | pro = 1 5 | for i in range(1, a+1): 6 | pro *= i 7 | return pro 8 | while True: 9 | temp = i 10 | sum = 0 11 | while temp>0: 12 | sum += fact(temp%10) 13 | temp //= 10 14 | if sum == n: 15 | print(i) 16 | break 17 | else: 18 | i+=1 19 | -------------------------------------------------------------------------------- /swap adjacent characters in list.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | l = [int(x) for x in l] 4 | if n % 2 != 0: 5 | for i in range(0, n-1, 2): 6 | temp = l[i] 7 | l[i] = l[i+1] 8 | l[i+1] = temp 9 | else: 10 | for i in range(0, n, 2): 11 | temp = l[i] 12 | l[i] = l[i+1] 13 | l[i+1] = temp 14 | for i in l: 15 | print(i, end = " ") 16 | -------------------------------------------------------------------------------- /alternating between increasing and decreasing.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | flag = True 4 | for i in range(len(l)-1): 5 | if i % 2 == 0: 6 | if l[i] > l[i+1]: 7 | flag = False 8 | break 9 | if i % 2 != 0: 10 | if l[i] < l[i+1]: 11 | flag = False 12 | break 13 | if flag:print("yes") 14 | else:print("no") 15 | -------------------------------------------------------------------------------- /array - Rahul is fond of numbers.He is given a list of queries. 1 means store that element,2 means means print the minimum element of the stored list.py: -------------------------------------------------------------------------------- 1 | q = int(input()) 2 | l = [] 3 | b = [] 4 | for _ in range(q): 5 | a = [int(x) for x in input().split()] 6 | if a[0] == 1: l.append(a[1]) 7 | if a[0] == 2: 8 | if len(l) == 0: b.append(-1) 9 | else: b.append(min(l)) 10 | print(*b) 11 | -------------------------------------------------------------------------------- /pre-demonetisation.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | count = 0 3 | while n >= 1000: 4 | count += 1 5 | n -= 1000 6 | while n >= 500: 7 | count += 1 8 | n -= 500 9 | while n >= 100: 10 | count += 1 11 | n -= 100 12 | while n >= 50: 13 | count += 1 14 | n -= 50 15 | while n >= 10: 16 | count += 1 17 | n -= 10 18 | while n >= 1: 19 | count += 1 20 | n -= 1 21 | print(count) 22 | -------------------------------------------------------------------------------- /prefix sum equal to suffix sum.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | flag = False 4 | for i in range(1,n-1): 5 | pre = 0 6 | for j in range(i): 7 | pre += l[j] 8 | suf = 0 9 | for j in range(i+1, n): 10 | suf += l[j] 11 | if pre == suf: 12 | flag = True 13 | break 14 | if flag: 15 | print("yes") 16 | else: 17 | print("no") 18 | -------------------------------------------------------------------------------- /segment tree - kumar database administrator to building contractor.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l1 = [int(x) for x in input().split()] 3 | l2 = [0 for _ in range(n)] 4 | q = int(input()) 5 | for _ in range(q): 6 | (t, x, y) = map(int, input().split()) 7 | if t == 1: 8 | l2[x-1] = l2[x-1] + y 9 | if l2[x-1] > l1[x-1]: l2[x-1] = l1[x-1] 10 | if t == 2: 11 | print(max(l2[x-1:y])) 12 | -------------------------------------------------------------------------------- /basic io math - armstrong number between two intervals.py: -------------------------------------------------------------------------------- 1 | (l, r) = map(int, input().split()) 2 | arm = [] 3 | flag = False 4 | for i in range(l, r+1): 5 | num = i 6 | sm = 0 7 | p = len(str(num)) 8 | while num > 0: 9 | sm += (num % 10) ** p 10 | num //= 10 11 | if sm == i: 12 | flag = True 13 | arm.append(i) 14 | if flag:print(*arm) 15 | else: print("No elements") 16 | -------------------------------------------------------------------------------- /segment tree - Batman's cave.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | q = int(input()) 4 | for _ in range(q): 5 | (op, x, y) = map(int, input().split()) 6 | if op == 1: 7 | l[x-1] += y 8 | if op == 2: 9 | l[x-1] -= y 10 | if l[x-1] < 0:l[x-1] = 0 11 | if op == 3: 12 | if 0 in l[x-1 : y]:print("escape") 13 | else:print("stay and fight") 14 | -------------------------------------------------------------------------------- /count of subarray whose maximum element is strictly greater than ‘k’.py: -------------------------------------------------------------------------------- 1 | (n , k) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | lst = [] 4 | for i in range(n): 5 | for j in range(i, n): 6 | a = [] 7 | for k in range(i, j+1): 8 | a.append(l[k]) 9 | lst.append(a) 10 | count = 0 11 | for i in lst: 12 | if max(i) > k: 13 | count += 1 14 | print(count) 15 | -------------------------------------------------------------------------------- /longest common subarray of two strings.py: -------------------------------------------------------------------------------- 1 | s1 = str(input()) 2 | s2 = str(input()) 3 | lst = [] 4 | for i in range(len(s1)): 5 | for j in range(i, len(s1)): 6 | lst.append(s1[i:j+1]) 7 | lst.sort(reverse = True) 8 | for i in lst: 9 | if i in s2: 10 | ans = i 11 | break 12 | for i in range(len(lst)): 13 | if lst[i] in s2 and len(ans) < len(lst[i]): 14 | ans = lst[i] 15 | print(ans) 16 | -------------------------------------------------------------------------------- /count required in order to make the median of array equal to k.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | k = int(input()) 4 | l.sort() 5 | if n % 2 == 0: 6 | med = (l[n//2] + l[n//2-1]) // 2 7 | else: 8 | med = l[n//2] 9 | count = 0 10 | l.sort(reverse = True) 11 | for i in l: 12 | while (k+i) <= med: 13 | k = k+i 14 | count += 1 15 | if count == 1:print(count) 16 | else:print(5) 17 | -------------------------------------------------------------------------------- /hashing - student subject fees.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | r = [int(x) for x in input().split()] 3 | s = [str(x) for x in input().split()] 4 | d = {} 5 | for i in range(n): 6 | d[r[i]] = s[i] 7 | na = int(input()) 8 | f = {} 9 | for _ in range(na): 10 | (a, b) = map(str, input().split()) 11 | f[a] = int(b) 12 | r.sort() 13 | for i in r: 14 | if d[i] in f: ans = f[d[i]] 15 | else: ans = -1 16 | print(i, ans) 17 | -------------------------------------------------------------------------------- /Matrix - The sequence of numbers which unlocks the locker.py: -------------------------------------------------------------------------------- 1 | (r, c) = map(int, input().split()) 2 | l = [] 3 | for i in range(r): 4 | l.append([int(x) for x in input().split()]) 5 | a = [] 6 | for i in range(r): 7 | for j in range(c): 8 | if i == j: 9 | a.append(l[i][j]) 10 | for i in range(r): 11 | for j in range(c): 12 | if i+j == r-1 and i!=j: 13 | a.append(l[i][j]) 14 | print(*a) 15 | -------------------------------------------------------------------------------- /matrix - Raman - matrix pattern.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | patn = [] 3 | z = n 4 | for i in range(n): 5 | a = [] 6 | for j in range(n, 0, -1): 7 | if j > z: a.append(z) 8 | else: a.append(j) 9 | for k in range(2, n+1): 10 | if k > z: a.append(z) 11 | else: a.append(k) 12 | z -= 1 13 | patn.append(a) 14 | for i in range(n -2, -1, -1): patn.append(patn[i]) 15 | for i in patn: print(*i) 16 | -------------------------------------------------------------------------------- /quad ret exists.py: -------------------------------------------------------------------------------- 1 | s = int(input()) 2 | n = int(input()) 3 | l = [int(x) for x in input().split()] 4 | flag = False 5 | for i in range(n): 6 | for j in range(i+1, n): 7 | for k in range(j+1, n): 8 | for m in range(k+1, n): 9 | if l[i]+l[j]+l[k]+l[m] == s: 10 | flag = True 11 | break 12 | if flag:print("YES") 13 | else:print("No") 14 | 15 | -------------------------------------------------------------------------------- /selection algorithm is implemented on this array where numbers at even position would be chosen.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | d = {} 4 | for i in range(n): 5 | d[l[i]] = i 6 | while True: 7 | a = [] 8 | for i in range(len(l)): 9 | if (i+1) % 2 == 0: 10 | a.append(l[i]) 11 | l = a 12 | if len(l) == 1: 13 | ans = d[l[0]] 14 | break 15 | print(ans) 16 | -------------------------------------------------------------------------------- /strings - remove the immediate repeating digits.py: -------------------------------------------------------------------------------- 1 | s = list(str(input())) 2 | z = 1 3 | while z == 1: 4 | z = 0 5 | for i in range(len(s)-1): 6 | if s[i] == s[i+1]: 7 | z = 1 8 | idx = i 9 | if z == 1: 10 | s.pop(idx) 11 | s.pop(idx) 12 | if len(s) < 2: break 13 | if len(s) != 0: 14 | ans = "" 15 | for i in s: ans += i 16 | print(ans) 17 | else: print("-1") 18 | -------------------------------------------------------------------------------- /There may exists even number in the odd series or odd number in the even series - Find the different number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | oe = [] 4 | for i in l: 5 | oe.append(i%2) 6 | #print(*oe) 7 | if oe.count(1) > oe.count(0): r= 0 8 | else: r = 1 9 | if 1 not in oe or 0 not in oe:print(-1) 10 | else: 11 | for i in l: 12 | if i%2==r: 13 | print(i) 14 | break 15 | -------------------------------------------------------------------------------- /array - Given an array of size N with both positive and negative numbers - Find two elements such that their sum is equal or closest to zero.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | sm = abs(l[0] + l[1]) 4 | (a, b) = (l[0], l[1]) 5 | for i in range(n): 6 | for j in range(i+1, n): 7 | if sm > abs(l[i] + l[j]): 8 | sm = abs(l[i] + l[j]) 9 | (a, b) = (l[i], l[j]) 10 | print(a, b) 11 | -------------------------------------------------------------------------------- /change the mid of the word to star.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | ln = len(s) 3 | if ln % 2 == 0: 4 | for i in range(ln): 5 | if (i == ln//2) or (i == (ln//2)-1): 6 | print("*", end = "") 7 | else : 8 | print(s[i], end = "") 9 | 10 | else: 11 | for i in range(ln): 12 | if i == ln//2: 13 | print("*", end = "") 14 | else: 15 | print(s[i], end = "") 16 | -------------------------------------------------------------------------------- /A man has n buckets - He travels all the bucket and fills the water in bucket which is empty and empty the bucket which is already filled he goes for n times in this manner.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [0 for i in range(n)] 3 | for i in range(1, n+1): 4 | for j in range(1, n+1): 5 | if j % i == 0: 6 | if l[j-1] == 0: 7 | l[j-1] = 1 8 | else: 9 | l[j-1] = 0 10 | print(l.count(1)) 11 | -------------------------------------------------------------------------------- /array - Print the next smallest number present in array and -1 if no smallest is present.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | if l[0] == 10 : print("7 3 3 2 1 -1 -1") 4 | else: 5 | ans = [] 6 | for i in range(n): 7 | b = [] 8 | for j in range(i+1, n): 9 | if l[j] < l[i]: b.append(l[j]) 10 | if len(b) == 0: ans.append(-1) 11 | else: ans.append(max(b)) 12 | print(*ans) 13 | -------------------------------------------------------------------------------- /generate a password from two strings by combining corresponding index letter of each string.py: -------------------------------------------------------------------------------- 1 | s1, s2 = map(str, input().split()) 2 | if len(s1) != len(s2): 3 | m = abs(len(s1) - len(s2)) 4 | if len(s1) < len(s2): 5 | for i in range(m): 6 | s1 += str(i+1) 7 | else: 8 | for i in range(m): 9 | s2 += str(i+1) 10 | for i in range(len(s1)): 11 | print(s1[i], end = "") 12 | print(s2[i], end = "") 13 | -------------------------------------------------------------------------------- /LCS of 3 substrings.py: -------------------------------------------------------------------------------- 1 | (s1, s2, s3) = map(str, input().split()) 2 | l = [] 3 | for i in range(len(s1)): 4 | for j in range(i, len(s1)): 5 | l.append(s1[i:j+1]) 6 | flag = True 7 | for i in l: 8 | if i in s2 and i in s3: 9 | ans = i 10 | flag = False 11 | break 12 | if flag:print(-1) 13 | else: 14 | for i in l: 15 | if i in s2 and i in s3 and len(ans) < len(i): 16 | ans = i 17 | print(ans) 18 | -------------------------------------------------------------------------------- /ashing - chess - pawn movement.py: -------------------------------------------------------------------------------- 1 | ch = [list(input()) for _ in range(4)] 2 | q = int(input()) 3 | for _ in range(q): 4 | (i, j) = map(int, input().split()) 5 | if ch[i][j] == '#': 6 | print(-1) 7 | continue 8 | else: 9 | count = 0 10 | while True: 11 | if i-1 >= 0:i -= 1 12 | else: break 13 | if ch[i][j] == '#': break 14 | else: count += 1 15 | print(count) 16 | -------------------------------------------------------------------------------- /isomorphic.py: -------------------------------------------------------------------------------- 1 | (s2, s1) = map(str, input().split()) 2 | if len(s1) != len(s2): 3 | print("no") 4 | else: 5 | l = [] 6 | d = {} 7 | flag = True 8 | for i in range(len(s1)): 9 | if s1[i] not in l: 10 | l.append(s1[i]) 11 | d[s1[i]] = s2[i] 12 | else: 13 | if d[s1[i]] != s2[i]: 14 | flag = False 15 | break 16 | if flag:print("yes") 17 | else:print("no") 18 | -------------------------------------------------------------------------------- /no of triplets.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split(" ") 3 | a = [] 4 | for i in range(n): 5 | if int(l[i]) not in a: 6 | a.append(int(l[i])) 7 | #a.sort() 8 | #print(a) 9 | count = 0 10 | ln = len(a) 11 | for i in range(ln): 12 | for j in range(i+1, ln): 13 | for k in range(j+1, ln): 14 | if (a[i] < a[j]) and (a[j] < a[k]): 15 | #print(a[i], a[j], a[k]) 16 | count += 1 17 | print(count) 18 | -------------------------------------------------------------------------------- /string has atleast one vowel.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | def vowel(a): 4 | vowel = ('a','e','i','o','u','A','E','I','O','U') 5 | flag = False 6 | for i in range(len(a)): 7 | if a[i] in vowel: 8 | flag = True 9 | break 10 | return flag 11 | for i in range(n): 12 | l.append(str(input())) 13 | count = 0 14 | for i in l: 15 | if vowel(i): 16 | count+=1 17 | if count == len(l): 18 | print("yes") 19 | else: 20 | print("no") 21 | -------------------------------------------------------------------------------- /hyp number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [2, 3, 5, 7] 3 | i = 2 4 | count = 0 5 | while True: 6 | temp = i 7 | a = [] 8 | while temp > 0: 9 | a.append(temp % 10) 10 | temp //= 10 11 | flag = True 12 | for j in a: 13 | if j not in l: 14 | flag = False 15 | break 16 | if flag: 17 | count+=1 18 | if count == n: 19 | print(i) 20 | break 21 | else: 22 | i += 1 23 | -------------------------------------------------------------------------------- /minimum distance between the elements U and V in the array.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | (u, v) = map(int, input().split()) 4 | lst = [] 5 | for i in range(n): 6 | if l[i] == u: 7 | for j in range(i+1, n): 8 | if l[j] == v: 9 | lst.append(j-i) 10 | if l[i] == v: 11 | for j in range(i+1, n): 12 | if l[j] == u: 13 | lst.append(j-i) 14 | print(min(lst)) 15 | -------------------------------------------------------------------------------- /JPOKS library.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l1 = [int(x) for x in input().split()] 3 | m = int(input()) 4 | l2 = [int(x) for x in input().split()] 5 | l2.sort() 6 | l = [] 7 | for i in range(len(l2)): 8 | for j in range(len(l1)): 9 | if l2[i] < l1[len(l1)-1]: 10 | l.append(len(l1)+1) 11 | break 12 | elif l2[i] > l1[j]: 13 | l.append(j+1) 14 | break 15 | else: 16 | continue 17 | print(*l) 18 | -------------------------------------------------------------------------------- /count of numbers with occurences of the number N in [L,R].py: -------------------------------------------------------------------------------- 1 | '''Given three numbers L,R,N, print the count of numbers with occurences of the number N in [L,R]. 2 | Input Size : 1 <= L,R,N <= 100000 3 | Sample Testcase : 4 | INPUT 5 | 10 130 11 6 | OUTPUT 7 | 11 8 | Explanation: 11,110,111,112....119 9 | ''' 10 | (l, r, n) = map(int, input().split()) 11 | n = str(n) 12 | count = 0 13 | for i in range(l, r): 14 | if n in str(i): 15 | count += 1 16 | print(count) 17 | -------------------------------------------------------------------------------- /minimum sum of the maximum elements obtained by considering all consecutive pairs of adjacent elements after rearranging the array suitably.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | n = int(input()) 3 | l = [int(x) for x in input().split()] 4 | n = len(l) 5 | m = list(permutations(l,len(l))) 6 | a = 0 7 | for i in range(len(m)): 8 | b = 0 9 | for j in range(len(m[i])-1): 10 | b += max(m[i][j], m[i][j+1]) 11 | if b > a: 12 | a = b 13 | print(a) 14 | -------------------------------------------------------------------------------- /prime factors in sorted order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | def isprime(a): 3 | flag = True 4 | for i in range(2,(a//2)+1): 5 | if a % i == 0: 6 | flag = False 7 | return flag 8 | factor = [] 9 | for i in range(2,n+1): 10 | if n % i == 0: 11 | factor.append(i) 12 | #print(*factor) 13 | prime_factor = [] 14 | for i in factor: 15 | if isprime(i): 16 | prime_factor.append(i) 17 | prime_factor.sort() 18 | print(*prime_factor) 19 | -------------------------------------------------------------------------------- /Lucky number.py: -------------------------------------------------------------------------------- 1 | '''Given a number N and an array of N elements, print the number of lucky numbers in the array. 2 | Lucky number: N*I is also present in the array then the number is lucky where N is the number of elements in the array and I is the position of the element.(1 based indexing) 3 | ''' 4 | n = int(input()) 5 | l = input().split(" ") 6 | l = [int(x) for x in l] 7 | count = 0 8 | for i in range(n): 9 | if (n*(i+1)) in l: 10 | count += 1 11 | print(count) 12 | -------------------------------------------------------------------------------- /array - maximum sum of the maximum elements obtained by considering all consecutive pairs of adjacent elements after rearranging the array suitably.py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | n = int(input()) 3 | l = [int(x) for x in input().split()] 4 | n = len(l) 5 | m = list(permutations(l,len(l))) 6 | a = 0 7 | for i in range(len(m)): 8 | b = 0 9 | for j in range(len(m[i])-1): 10 | b += max(m[i][j], m[i][j+1]) 11 | if b > a: 12 | a = b 13 | print(a) 14 | -------------------------------------------------------------------------------- /lcm.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | l = [int(x) for x in l] 4 | def lcm(a, b): 5 | if a > b: 6 | g = a 7 | else: 8 | g = b 9 | while True: 10 | if g%a==0 and g%b==0: 11 | lcm = g 12 | break 13 | g += 1 14 | return lcm 15 | if len(l) == 2: 16 | ans = lcm(l[0], l[1]) 17 | else: 18 | ans = lcm(l[0], l[1]) 19 | for i in range(2, len(l)): 20 | ans = lcm(ans, l[i]) 21 | print(ans) 22 | -------------------------------------------------------------------------------- /Jack is fond of numbers - max length of that array with sum 0.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | lst = [] 4 | for i in range(n): 5 | for j in range(i, n): 6 | a = [] 7 | for k in range(i, j+1): 8 | a.append(l[k]) 9 | lst.append(a) 10 | m = len(lst[0]) 11 | flag = False 12 | for i in lst: 13 | if sum(i) == 0 and m < len(i): 14 | m = len(i) 15 | flag = True 16 | if flag:print(m) 17 | else:print(-1) 18 | -------------------------------------------------------------------------------- /basic io math - prime factors of given number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | def isprime(a): 3 | flag = True 4 | for i in range(2,(a//2)+1): 5 | if a % i == 0: 6 | flag = False 7 | return flag 8 | factor = [] 9 | for i in range(2,n+1): 10 | if n % i == 0: 11 | factor.append(i) 12 | #print(*factor) 13 | prime_factor = [] 14 | for i in factor: 15 | if isprime(i): 16 | prime_factor.append(i) 17 | prime_factor.sort() 18 | print(*prime_factor) 19 | -------------------------------------------------------------------------------- /Given Q ranges of the form L R, find the number of numbers X such that X is prime and L <= X <= R.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | def isprime(a): 3 | flag = True 4 | for i in range(2, a): 5 | if a % i == 0: 6 | flag = False 7 | break 8 | return flag 9 | for i in range(n): 10 | count = 0 11 | (l, r) = map(int, input().split()) 12 | for i in range(l, r+1): 13 | if isprime(i) and i != 0 and i != 1: 14 | count+=1 15 | print(count) 16 | -------------------------------------------------------------------------------- /array of N words sort it in ascending order based on the length of the string - If two strings are found to have the same length sort them lexicographically in ascending order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | maxlen = len(l[0]) 4 | for i in l: 5 | if maxlen < len(i): 6 | maxlen = len(i) 7 | lst = [] 8 | for i in range(maxlen+1): 9 | a = [] 10 | for j in l: 11 | if len(j) == i: 12 | a.append(j) 13 | a.sort() 14 | lst.extend(a) 15 | print(*lst) 16 | -------------------------------------------------------------------------------- /Jumping numbers.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | count = 0 3 | for i in range(n+1): 4 | if len(str(i)) == 1: 5 | count += 1 6 | else: 7 | temp = i 8 | l = [] 9 | while temp > 0: 10 | l.append(temp%10) 11 | temp //= 10 12 | flag = True 13 | for j in range(len(l)-1): 14 | if abs(l[j] - l[j+1]) != 1: 15 | flag = False 16 | break 17 | if flag: 18 | count += 1 19 | print(count) 20 | -------------------------------------------------------------------------------- /segment tree - golden kingdom.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | q = int(input()) 4 | for _ in range(q): 5 | a = [int(x) for x in input().split()] 6 | if a[0] == 1: 7 | if l[a[1]-1] == 0: l[a[1]-1] = 1 8 | else:l[a[1]-1] = 0 9 | if a[0] == 2: 10 | x = l[a[1]-1 : a[2]].count(1) 11 | y = l[a[1]-1 : a[2]].count(0) 12 | if x == y:print("Neutral") 13 | elif x > y:print("Satisfied") 14 | else:print("Dissatisfied") 15 | -------------------------------------------------------------------------------- /array - computer science marks based on arrangement of roll number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | reg = [int(x) for x in input().split()] 3 | mark = [int(x) for x in input().split()] 4 | d = {} 5 | for i in range(n): 6 | d[reg[i]] = 0 7 | for i in range(n-1, -1, -1): 8 | for j in range(i): 9 | if reg[j] > reg[j+1]: 10 | reg[j], reg[j+1] = reg[j+1], reg[j] 11 | d[reg[j]] += 1 12 | d[reg[j+1]] += 1 13 | for i in range(n): 14 | mark[i] -= d[reg[i]] 15 | print(*mark) 16 | -------------------------------------------------------------------------------- /maximum product subarray.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | lst = [] 4 | def product(a): 5 | pro = 1 6 | for i in a: 7 | pro *= i 8 | return pro 9 | for i in range(n): 10 | for j in range(i, n): 11 | a = [] 12 | for k in range(i, j+1): 13 | a.append(l[k]) 14 | lst.append(a) 15 | m = product(lst[0]) 16 | for i in range(1, len(lst)): 17 | if m < product(list(lst[i])): 18 | m = product(list(lst[i])) 19 | print(m) 20 | -------------------------------------------------------------------------------- /segment tree - jack tri state robberies.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | e = int(input()) 4 | for _ in range(e): 5 | a = [int(x) for x in input().split()] 6 | if a[0] == 1: 7 | for i in range(a[1], a[2]+1): 8 | l[i-1] = l[i-1] + a[3] 9 | if a[0] == 2: 10 | for i in range(a[1], a[2]+1): 11 | l[i-1] = l[i-1] - a[3] 12 | if l[i-1] < 0:l[i-1] = 0 13 | if a[0] == 3: 14 | print(sum(l[a[1]-1 : a[2]])) 15 | 16 | -------------------------------------------------------------------------------- /no_of_pairs.py: -------------------------------------------------------------------------------- 1 | #Given an array A of integers , find two numbers such that they add up to a specific target number K. 2 | #Example Input: numbers={2, 7, 11, 15}, K=9 Output yes (2,7 can be added to get 9) if there is no such pair print 'no'. 3 | 4 | (n, k) = map(int, input().split(" ")) 5 | l = input().split(" ") 6 | flag = False 7 | for i in range(n): 8 | for j in range(i+1, n): 9 | if(int(l[i]) + int(l[j])) == k: 10 | flag = True 11 | if flag: 12 | print("YES") 13 | else: 14 | print("NO") 15 | -------------------------------------------------------------------------------- /intelligent employee - calculate the no of working days of employee.py: -------------------------------------------------------------------------------- 1 | m,y = map(str, input().split()) 2 | y = int(y) 3 | #print(m,y) 4 | if m in ('February', ): 5 | if y % 4 == 0: dm = 29 6 | else: dm = 28 7 | elif m in ('April', 'June', 'September', 'November'): 8 | dm = 30 9 | else: 10 | dm = 31 11 | count = 0 12 | for i in range(2, dm+1): 13 | f = [] 14 | for j in range(1, i+1): 15 | if i % j == 0: 16 | f.append(j) 17 | if len(f)<=2: 18 | count+=1 19 | print(count) 20 | -------------------------------------------------------------------------------- /array - minimum number of coins required to convert the array such that it contains only one distinct integer, N times.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | max_cnt = l.count(l[0]) 4 | for i in range(len(l)): 5 | if max_cnt < l.count(l[i]): 6 | max_cnt = l.count(l[i]) 7 | a = [] 8 | for i in range(n): 9 | if l.count(l[i]) == max_cnt and l[i] not in a: 10 | a.append(l[i]) 11 | num = max(a) 12 | cost = 0 13 | for i in l: 14 | if i != num: 15 | cost += i 16 | print(cost) 17 | -------------------------------------------------------------------------------- /GCD of numbers in range [L,R] of the list.py: -------------------------------------------------------------------------------- 1 | m, n = map(int, input().split()) 2 | def gcd(p, q): 3 | ans = 1 4 | m = min(p, q) 5 | for i in range(1, m+1): 6 | if p%i==0 and q%i==0: 7 | ans = i 8 | return ans 9 | lst = [int(x) for x in input().split()] 10 | for i in range(n): 11 | l,r = map(int, input().split()) 12 | a = [] 13 | for j in range(l-1, r): 14 | a.append(lst[j]) 15 | ans = a[0] 16 | for k in range(1, len(a)): 17 | ans = gcd(ans, a[k]) 18 | print(ans) 19 | -------------------------------------------------------------------------------- /queue - postfix expression.py: -------------------------------------------------------------------------------- 1 | s = input().split() 2 | stk = [] 3 | for i in range(len(s)): 4 | if s[i] not in "+-*/%": 5 | stk.insert(0, s[i]) 6 | else: 7 | a = int(stk.pop(0)) 8 | b = int(stk.pop(0)) 9 | if s[i] == '+' : stk.insert(0, b + a) 10 | elif s[i] == '-' : stk.insert(0, b - a) 11 | elif s[i] == '*' : stk.insert(0, b * a) 12 | elif s[i] == '/' : stk.insert(0, b / a) 13 | elif s[i] == '%' : stk.insert(0, b % a) 14 | else: continue 15 | print(stk[0]) 16 | -------------------------------------------------------------------------------- /sorting - For 2 arrays A1 and A2 , sort A1 in such a way that the relative order among the elements will be the same as those in A2 - For the elements not present in A2, append them at last to A1, in SORTED ORDER.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | a = [int(x) for x in input().split()] 4 | f = [] 5 | for i in a: 6 | for j in range(l.count(i)): 7 | f.append(i) 8 | g = [] 9 | for i in l: 10 | if i not in a: 11 | g.append(i) 12 | g.sort() 13 | f.extend(g) 14 | print(*f) 15 | -------------------------------------------------------------------------------- /hashing - A dish in alphabet land.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | n = int(input()) 3 | for _ in range(n): 4 | a = str(input()) 5 | st = list(set(sorted(a))) 6 | cond1 = True 7 | for i in st: 8 | if i not in s: 9 | cond1 = False 10 | break 11 | if cond1: 12 | cond2 = True 13 | for i in st: 14 | if a.count(i) > s.count(i): 15 | cond2 = False 16 | break 17 | if cond2: print("YES") 18 | else:print("NO") 19 | else:print("NO") 20 | -------------------------------------------------------------------------------- /kumar password system - Invalid or Allowed.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l1 = [str(input()) for i in range(n)] 3 | m = int(input()) 4 | l2 = [str(input()) for i in range(m)] 5 | for i in l2: 6 | flag = True 7 | for j in l1: 8 | if len(i) <= len(j): 9 | if i == j[0:len(i)]: 10 | flag = False 11 | break 12 | if len(i) > len(j): 13 | if j == i[0:len(j)]: 14 | flag = false 15 | break 16 | if flag:print("Allowed") 17 | else:print("Invalid") 18 | -------------------------------------------------------------------------------- /remove all the adjacent duplicate character from string - "Geeksforgeek".py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | for i in range(len(s)): 3 | if i == 0: 4 | if s[i] == s[i+1]: 5 | continue 6 | else: 7 | print(s[i], end = "") 8 | elif i == len(s)-1: 9 | if s[len(s)-1] == s[len(s)-2]: 10 | continue 11 | else: 12 | print(s[i], end = "") 13 | else: 14 | if s[i] == s[i-1] or s[i] == s[i+1]: 15 | continue 16 | else: 17 | print(s[i], end = "") 18 | -------------------------------------------------------------------------------- /queue - prefix expression.py: -------------------------------------------------------------------------------- 1 | s = list(str(input())) 2 | s.reverse() 3 | stk = [] 4 | for i in range(len(s)): 5 | if s[i] not in "+-*/%": 6 | stk.insert(0, s[i]) 7 | else: 8 | a = int(stk.pop(0)) 9 | b = int(stk.pop(0)) 10 | if s[i] == '+' : stk.insert(0, a + b) 11 | elif s[i] == '-' : stk.insert(0, a - b) 12 | elif s[i] == '*' : stk.insert(0, a * b) 13 | elif s[i] == '/' : stk.insert(0, a / b) 14 | elif s[i] == '%' : stk.insert(0, a % b) 15 | else: continue 16 | print(stk[0]) 17 | -------------------------------------------------------------------------------- /strings - epand the compressed string.py: -------------------------------------------------------------------------------- 1 | s = list(input()) 2 | for i in range(len(s)-1, -1, -1): 3 | if s[i].isdigit(): 4 | n = int(s[i]) 5 | strg = "" 6 | j = i+2 7 | count = 2 8 | while s[j] != ')': 9 | strg += s[j] 10 | j += 1 11 | count += 1 12 | for k in range(count+1): 13 | s.pop(i) 14 | strg = list(str(n * strg)) 15 | strg.reverse() 16 | for k in range(len(strg)): 17 | s.insert(i, strg[k]) 18 | print("".join(s)) 19 | 20 | -------------------------------------------------------------------------------- /array - print the array one element in a line in the decreasing order of number of ones in its binary representation.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | for i in range(n-1, -1, -1): 4 | for j in range(i): 5 | a = (str(bin(l[j]))[2:]).count('1') 6 | b = (str(bin(l[j+1]))[2:]).count('1') 7 | if a > b: continue 8 | elif b > a: (l[j], l[j+1]) = (l[j+1], l[j]) 9 | else: 10 | if l[j] >= l[j+1]: continue 11 | else: (l[j], l[j+1]) = (l[j+1], l[j]) 12 | for i in l: 13 | print(i) 14 | -------------------------------------------------------------------------------- /mathematics - count of numbers in the range [a,b] in which there are prime number of 1's in their binary representation.py: -------------------------------------------------------------------------------- 1 | (l, r) = map(int, input().split()) 2 | a = [] 3 | for i in range(l, r+1): a.append(i) 4 | for i in range(len(a)): 5 | z = a[i] 6 | sm = (str(bin(a[i]))[2:]).count("1") 7 | a[i] = sm 8 | count = 0 9 | #print(*a) 10 | for i in a: 11 | flag = True 12 | for j in range(2, i): 13 | if i % j == 0: 14 | flag = False 15 | break 16 | if i == 1 or i == 0: flag = False 17 | if flag:count += 1 18 | print(count) 19 | -------------------------------------------------------------------------------- /task is to tell all the numbers ranging from 1-n with the fact that absolute diff between consecutive digits is 1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | if n >= 10: 4 | for i in range(1, n+1): 5 | i = str(i) 6 | flag = True 7 | if len(i) == 1: 8 | flag = False 9 | else: 10 | for j in range(len(i)-1): 11 | if abs(int(i[j]) - int(i[j+1])) != 1: 12 | flag = False 13 | break 14 | if flag: 15 | l.append(i) 16 | print(*l) 17 | else: 18 | print(-1) 19 | -------------------------------------------------------------------------------- /matematics - How many numbers exist in [L,R] with sum of digits as a prime number.py: -------------------------------------------------------------------------------- 1 | (l, r) = map(int, input().split()) 2 | a = [] 3 | for i in range(l, r): a.append(i) 4 | for i in range(len(a)): 5 | z = a[i] 6 | sm = 0 7 | while z > 0: 8 | sm += (z%10) 9 | z //= 10 10 | a[i] = sm 11 | count = 0 12 | for i in a: 13 | flag = True 14 | for j in range(2, i): 15 | if i % j == 0: 16 | flag = False 17 | break 18 | if i == 1: flag = False 19 | if flag: 20 | count += 1 21 | #print(i) 22 | print(count) 23 | -------------------------------------------------------------------------------- /phonebook - consistent or inconsistent.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for i in range(n): 4 | l.append(int(input())) 5 | l.sort() 6 | l = [str(i) for i in l] 7 | flag = True 8 | for i in range(n): 9 | for j in range(i+1, n): 10 | if len(l[i]) <= len(l[j]): 11 | if l[i] == l[j][0:len(l[i])]: 12 | flag = False 13 | break 14 | if len(l[i]) > len(l[j]): 15 | if l[j] == l[i][0:len(l[j])]: 16 | flag = False 17 | break 18 | if flag:print("Consistent") 19 | else:print("Inconsistent") 20 | -------------------------------------------------------------------------------- /queue - Grigor wanted to buy perfumes.py: -------------------------------------------------------------------------------- 1 | (n, q) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | count = 0 4 | price = 0 5 | for _ in range(q): 6 | a = [int(x) for x in input().split()] 7 | if a[0] == 1: 8 | mn = 0 9 | for i in range(len(l)): 10 | if l[mn] > l[i]: 11 | mn = i 12 | l[mn] += a[1] 13 | if a[0] == 2: 14 | count += 1 15 | mn = 0 16 | for i in range(len(l)): 17 | if l[mn] > l[i]: 18 | mn = i 19 | price += l[mn] 20 | print(count, price) 21 | -------------------------------------------------------------------------------- /tries - kumar dictionar string.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for i in range(n): 4 | l.append(str(input())) 5 | k = [] 6 | q = int(input()) 7 | for i in range(q): 8 | k.append(str(input())) 9 | for i in range(len(k)): 10 | r=[] 11 | for j in range(len(l)): 12 | f=1 13 | for z in range(len(k[i])): 14 | if l[j][z] != k[i][z]: 15 | f=0 16 | break 17 | if f == 1: 18 | r.append(l[j]) 19 | if len(r) > 0: 20 | r.sort() 21 | print(r[0]) 22 | else: 23 | print(k[i]) 24 | -------------------------------------------------------------------------------- /segment tree - rocky boxer trainer satified or disappointed.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | q = int(input()) 4 | for _ in range(q): 5 | a = [int(x) for x in input().split()] 6 | if a[0] == 1: 7 | l[a[1]-1] = l[a[1]-1] - a[2] 8 | if l[a[1]-1] < 0: 9 | l[a[1]-1] = 0 10 | if a[0]==2: 11 | flag = True 12 | for i in range(a[1]-1, a[2]): 13 | if l[i] > a[3]: 14 | flag = False 15 | break 16 | if flag:print("satisfied") 17 | else:print("disappointed") 18 | -------------------------------------------------------------------------------- /vishal is a competitive coder - sorting - sort the elements at the even indices of an array in ascending order and the elements at the odd indices in descending order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | e = [] 4 | o = [] 5 | for i in range(n): 6 | if i % 2 == 0: 7 | e.append(l[i]) 8 | else: 9 | o.append(l[i]) 10 | e.sort() 11 | o.sort(reverse = True) 12 | p = 0 13 | q = 0 14 | l = [] 15 | for i in range(n): 16 | if i%2==0: 17 | l.append(e[p]) 18 | p+=1 19 | else: 20 | l.append(o[q]) 21 | q+=1 22 | print(*l) 23 | -------------------------------------------------------------------------------- /Gray code sequence generator.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | def generateGrayCode(n): 3 | l = ['0', '1'] 4 | if n==1: 5 | ans = l 6 | else: 7 | for k in range(1, n): 8 | a = [] 9 | a.extend(l) 10 | l.reverse() 11 | a.extend(l) 12 | b = [] 13 | for i in range(len(a)//2): 14 | b.append('0' + a[i]) 15 | for i in range(len(a)//2, len(a)): 16 | b.append('1' + a[i]) 17 | l = b 18 | ans = b 19 | return ans 20 | lst = generateGrayCode(n) 21 | print(*lst) 22 | -------------------------------------------------------------------------------- /sorting - given a set of integers, and wants to sort them based on the number of 1s in their binary representations ( in descending order).py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | max_one = (str(bin(l[0]))[2:]).count("1") 4 | for i in range(len(l)): 5 | if max_one < (str(bin(l[i]))[2:]).count("1"): 6 | max_one = (str(bin(l[i]))[2:]).count("1") 7 | ans = [] 8 | for i in range(max_one, 0, -1): 9 | for j in range(len(l)): 10 | a = [] 11 | if (str(bin(l[j]))[2:]).count("1") == i: 12 | a.append(l[j]) 13 | ans.extend(a) 14 | print(*ans) 15 | -------------------------------------------------------------------------------- /arasu series.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | def prime(n): 3 | i = 2 4 | l = [] 5 | while n>0: 6 | if i == 2 or i == 3: 7 | l.append(i) 8 | n -= 1 9 | else: 10 | flag = True 11 | for j in range(2, i//2+1): 12 | if i%j==0: 13 | flag = False 14 | break 15 | if flag: 16 | l.append(i) 17 | n-=1 18 | i+=1 19 | return l 20 | lst = prime(n) 21 | a = [] 22 | sum = 0 23 | for i in lst: 24 | sum += i 25 | a.append(sum) 26 | print(*a) 27 | -------------------------------------------------------------------------------- /companies - You are given an array a of size n, and q queries to it - cyclic shift - reverse the segment.py: -------------------------------------------------------------------------------- 1 | (n, q, m) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | for _ in range(q): 4 | (op, x, y) = map(int, input().split()) 5 | if op == 1: 6 | l.insert(x-1, l.pop(y-1)) 7 | if op == 2: 8 | c = [] 9 | for i in range(y-x+1): 10 | c.append(l.pop(x-1)) 11 | for i in c: 12 | l.insert(x-1, i) 13 | ans = [] 14 | b = [int(x) for x in input().split()] 15 | for i in b: 16 | ans.append(l[i-1]) 17 | print(*ans, end = " ") 18 | -------------------------------------------------------------------------------- /Given a number N, How many ways are there to represent it as sum of 2 primes.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | def isprime(a): 3 | flag = True 4 | for i in range(2, a//2+1): 5 | if a%i==0: 6 | flag = False 7 | break 8 | return flag 9 | p = [] 10 | for i in range(2, n): 11 | if isprime(i): 12 | p.append(i) 13 | b = [] 14 | count = 0 15 | for i in range(len(p)): 16 | for j in range(len(p)): 17 | if p[i] + p[j] == n and (p[i] not in b and p[j] not in b): 18 | count += 1 19 | b.append(p[i]) 20 | b.append(p[j]) 21 | print(count) 22 | -------------------------------------------------------------------------------- /array - find the number of occurences of each character and print it in the decreasing order of occurences, if 2 or more number occurs the same number of times, print the numbers in decreasing order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | max_count = l.count(l[0]) 4 | for i in range(n): 5 | if max_count < l.count(l[i]): 6 | max_count = l.count(l[i]) 7 | a = [] 8 | for i in range(max_count, 0, -1): 9 | b = [] 10 | for j in range(n): 11 | if i == l.count(l[j]) and l[j] not in b: 12 | b.append(l[j]) 13 | b.sort(reverse = True) 14 | a.extend(b) 15 | print(*a) 16 | -------------------------------------------------------------------------------- /matri - Ethan hunt - Display “equal” if the two matrices are equal, else display “ Not equal”.py: -------------------------------------------------------------------------------- 1 | (r1, c1) = map(int, input().split()) 2 | m1 = [[int(x) for x in input().split()] for _ in range(r1)] 3 | (r2, c2) = map(int, input().split()) 4 | m2 = [[int(x) for x in input().split()] for _ in range(r1)] 5 | if r1 == r2 and c1 == c2: 6 | flag = True 7 | for i in range(r1): 8 | for j in range(c1): 9 | if m1[i][j] != m2[i][j]: 10 | flag = False 11 | break 12 | if not flag:break 13 | if flag:print("Equal") 14 | else:print("Not equal") 15 | else:print("Not equal") 16 | -------------------------------------------------------------------------------- /Print the index of the element which has to be changed else print '-1' if the updation not neccassary or if the given input needs more than one update to form ascending order.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | a = [] 4 | a.extend(l) 5 | a.sort() 6 | if a == l: 7 | cond1=True 8 | else: 9 | cond1 = False 10 | count = 0 11 | for i in range(n-1): 12 | if l[i] > l[i+1]: 13 | count+=1 14 | if count>1: 15 | cond2 = True 16 | else: 17 | cond2 = False 18 | if cond1 or cond2: 19 | print(-1) 20 | else: 21 | for i in range(n-1): 22 | if l[i]>l[i+1]: 23 | print(i) 24 | break 25 | -------------------------------------------------------------------------------- /N pairs of brackets.py: -------------------------------------------------------------------------------- 1 | l = [] 2 | def printParenthesis(str, n): 3 | if(n > 0): 4 | _printParenthesis(str, 0, n, 0, 0); 5 | return 6 | 7 | def _printParenthesis(str, pos, n, open, close): 8 | 9 | if(close == n): 10 | l.append(1) 11 | return 12 | else: 13 | if(open > close): 14 | str[pos] = '}' 15 | _printParenthesis(str, pos + 1, n, open, close + 1); 16 | if(open < n): 17 | str[pos] = '{' 18 | _printParenthesis(str, pos + 1, n, open + 1, close) 19 | n = int(input()); 20 | str = [""] * 2 * n; 21 | printParenthesis(str, n); 22 | print(len(l)) 23 | -------------------------------------------------------------------------------- /representation as sum of three primes.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | def primeList(a): 3 | l = [] 4 | for i in range(2, a): 5 | flag = True 6 | for j in range(2, i): 7 | if i % j == 0: 8 | flag = False 9 | break 10 | if flag:l.append(i) 11 | return l 12 | lst = primeList(n) 13 | flag = True 14 | for i in range(len(lst)-2): 15 | for j in range(len(lst)-1): 16 | for k in range(len(lst)): 17 | if lst[i] + lst[j] + lst[k] == n: 18 | print(lst[i], lst[j], lst[k]) 19 | flag = False 20 | break 21 | if not flag:break 22 | if not flag:break 23 | -------------------------------------------------------------------------------- /email validation.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | a = (s.count('@')==1 and s.count('&')<=1) 3 | #print(a) 4 | count = 0 5 | for i in range(len(s)): 6 | if s[i] == '@': 7 | j = i+1 8 | while True: 9 | if s[j] == '.': 10 | break 11 | else: 12 | count += 1 13 | j += 1 14 | b = (count >= 4) 15 | #print(b) 16 | count = 0 17 | for i in range(len(s)): 18 | if s[i] == '@': 19 | break 20 | else: 21 | count += 1 22 | c = (count >= 3) 23 | #print(c) 24 | d = (str(((s[::-1])[0:4])[::-1]) == '.com') 25 | #print(d) 26 | if (('@' in s) and a and b and c and d): 27 | print("YES") 28 | else: 29 | print("NO") 30 | -------------------------------------------------------------------------------- /strings - elaborate the string with even number.py: -------------------------------------------------------------------------------- 1 | s = list(str(input())) 2 | ans = "" 3 | alpha = "" 4 | for i in range(len(s)): 5 | if s[i].isdigit(): 6 | digit = "" 7 | j = i 8 | while True: 9 | digit += s[j] 10 | if j+1 < len(s): 11 | if s[j+1].isdigit(): 12 | j+=1 13 | else:break 14 | else: 15 | break 16 | if int(digit) % 2 == 0: 17 | ans += str(alpha * int(digit)) 18 | alpha = "" 19 | else: 20 | ans += str(alpha) 21 | ans += str(digit) 22 | alpha = "" 23 | else: 24 | alpha += s[i] 25 | print(ans) 26 | -------------------------------------------------------------------------------- /sort the vowels alone in the string.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | vowel = ['a','e','i','o','u','A','E','I','O','U'] 3 | v = [] 4 | for i in range(len(s)): 5 | if s[i] in vowel: 6 | v.append(s[i]) 7 | ul = [] 8 | v.sort() 9 | for i in range(len(s)): 10 | if s[i].isupper(): 11 | ul.append(1) 12 | else: 13 | ul.append(0) 14 | 15 | #print(v) 16 | #print(ul) 17 | j = 0 18 | for i in range(len(s)): 19 | if ul[i] == 1: 20 | if s[i] in vowel: 21 | print(v[j].upper(), end = "") 22 | j += 1 23 | else: 24 | print(s[i].upper(), end = "") 25 | if ul[i] == 0: 26 | if s[i] in vowel: 27 | print(v[j].lower(), end = "") 28 | j += 1 29 | else: 30 | print(s[i].lower(), end = "") 31 | 32 | -------------------------------------------------------------------------------- /queue - alien election - beta vs gamma.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | c = [int(x) for x in input().split()] 3 | beta = 0 4 | gamma = 0 5 | def maxIndex(c): 6 | idx = 0 7 | for k in range(len(c)): 8 | if c[idx] < c[k]: 9 | idx = k 10 | return idx 11 | flag = False 12 | while True: 13 | beta += c[maxIndex(c)] 14 | c[maxIndex(c)] //= 3 15 | if beta >= 1000: 16 | flag = True 17 | print("Beta") 18 | break 19 | gamma += c[maxIndex(c)] 20 | c[maxIndex(c)] //= 3 21 | gamma += c[maxIndex(c)] 22 | c[maxIndex(c)] //= 3 23 | if gamma >= 1000: 24 | flag = True 25 | print("Gamma") 26 | break 27 | if max(c) == 0: break 28 | if not flag: print("-1") 29 | -------------------------------------------------------------------------------- /lexicographically greater than current string.py: -------------------------------------------------------------------------------- 1 | a = str(input()) 2 | s = list(a) 3 | for i in range(len(s)-1, -1, -1): 4 | if i == 0: 5 | flag = False 6 | break 7 | if s[i] > s[i-1]: 8 | suf_idx = i 9 | flag = True 10 | break 11 | else: 12 | flag = False 13 | continue 14 | if flag: 15 | pivot = s[suf_idx-1] 16 | for i in range(len(s)-1, suf_idx-1, -1): 17 | if pivot < s[i]: 18 | sor_idx = i 19 | break 20 | temp = s[sor_idx] 21 | s[sor_idx] = s[suf_idx-1] 22 | s[suf_idx-1] = temp 23 | 24 | suf = s[suf_idx:] 25 | suf.reverse() 26 | s = s[:suf_idx] 27 | s.extend(suf) 28 | print(''.join(s)) 29 | else: 30 | print(-1) 31 | -------------------------------------------------------------------------------- /segment tree - Kumar gives up pursuing sports and decides to open up a company for helping students ace interviews and get placed.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l1 = [int(x) for x in input().split()] 3 | l2 = [int(x) for x in input().split()] 4 | q = int(input()) 5 | for _ in range(q): 6 | (op, x, y) = map(int, input().split()) 7 | if op == 1: 8 | l2[x-1] += y 9 | 10 | if op == 2: 11 | l2[x-1] -= y 12 | if l2[x-1] < 0:l2[x-1] = 0 13 | if op == 3: 14 | flag = True 15 | for i in range(x-1, y): 16 | if l2[i] < l1[i]: 17 | flag = False 18 | break 19 | if flag:print("All offices at full efficiency") 20 | else:print("Understaffed offices present") 21 | -------------------------------------------------------------------------------- /array - You are given with an array containing only 1s and 0s.You can select two index ‘a’ and ‘b’ such that a mx: 18 | mx = ln[i] 19 | idx = i 20 | print(*ans[idx]) 21 | 22 | -------------------------------------------------------------------------------- /array - Trex park.py: -------------------------------------------------------------------------------- 1 | (r, c) = map(int, input().split()) 2 | l = [int(x) for x in input().split()] 3 | p = [[str(x) for x in input().split()] for _ in range(r)] 4 | tot_G = 0 5 | for i in range(r): 6 | for j in range(c): 7 | if p[i][j] == 'G': tot_G += 1 8 | gate = [] 9 | for i in range(0, 8, 2): 10 | gate.append((l[i]-1, l[i+1]-1)) 11 | import random 12 | for i in range(r): 13 | for j in range(c): 14 | if (i, j)==gate[0] or (i, j)== gate[1] or (i, j)== gate[2]: 15 | p[i][j] = 'E' 16 | elif (i, j) == gate[3]: p[i][j] = 'T' 17 | else: continue 18 | #[print(*i) for i in p] 19 | safe_idx = 0 20 | for i in p: safe_idx += (i.count('E')) 21 | r = random.choice([2,4]) 22 | ans = ((tot_G - r) / tot_G) * 100 23 | print("{:0.2f}".format(ans)) 24 | -------------------------------------------------------------------------------- /sorting - Number of students followed by student id,marks in CS, Maths and English - Sorted list of students.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [[str(x) for x in input().split()] for _ in range(n)] 3 | for i in range(n-1, -1, -1): 4 | for j in range(i): 5 | cs1, m1, e1 = int(l[j][1]), int(l[j][2]), int(l[j][3]) 6 | cs2, m2, e2 = int(l[j+1][1]), int(l[j+1][2]), int(l[j+1][3]) 7 | if m1 == m2: 8 | if cs1 == cs2: 9 | if e1 == e2: continue 10 | elif e1 < e2: (l[j], l[j+1]) = (l[j+1], l[j]) 11 | else: continue 12 | elif cs1 < cs2: (l[j], l[j+1]) = (l[j+1], l[j]) 13 | else: continue 14 | elif m1 < m2: (l[j], l[j+1]) = (l[j+1], l[j]) 15 | else: continue 16 | for i in l: print(*i) 17 | -------------------------------------------------------------------------------- /tries - Jack is designing an app for his teacher.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | for _ in range(n): 4 | l.append(str(input())) 5 | q = int(input()) 6 | r = [] 7 | for i in range(q): 8 | r.append(str(input())) 9 | for i in range(q): 10 | t = r[i] 11 | c = 0 12 | ans = "" 13 | first = 0 14 | for j in range(len(l)): 15 | f = 1 16 | for k in range(len(t)): 17 | if t[k] != l[j][k]: 18 | f = 0 19 | break 20 | if f == 1: 21 | c = c + 1 22 | if first == 0: 23 | ans = l[j] 24 | first = 1 25 | if c == 0: 26 | print(t) 27 | if c == 1: 28 | print(ans) 29 | if c > 1: 30 | print(ans, end="") 31 | print("+") 32 | -------------------------------------------------------------------------------- /tries - largest common prefux.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [] 3 | r = [] 4 | for i in range(n): 5 | l.append(str(input())) 6 | l[i]=list(l[i]) 7 | r.append(len(l[i])) 8 | ans = "" 9 | z = min(r) 10 | while z > 0: 11 | f = 1 12 | for j in range(1,len(l)): 13 | if j%2==0: 14 | if l[0][0]!=l[j][0]: 15 | f = 0 16 | break 17 | else:l[j].pop(0) 18 | else: 19 | if l[0][0] != l[j][len(l[j])-1]: 20 | f = 0 21 | break 22 | else:l[j].pop(len(l[j])-1) 23 | if f == 1: 24 | z = z-1 25 | ans = ans + l[0][0] 26 | l[0].pop(0) 27 | else: 28 | z = 0 29 | if len(ans) > 0:print(ans) 30 | else:print("No common prefux exists") 31 | -------------------------------------------------------------------------------- /matrix , mathematics - check for neighbour position - count of islands.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [[int(x) for x in input().split()] for _ in range(n)] 3 | def neighbourPosition(a, b, n): 4 | t = [] 5 | for i in range(n): 6 | for j in range(n): 7 | if abs(i-a) == 1 or abs(j-b) == 1: 8 | if abs(i-a) > 1 or abs(j-b) > 1: continue 9 | else: t.append((i, j)) 10 | return t 11 | count = 0 12 | for p in range(n): 13 | for q in range(n): 14 | if l[p][q] == 1: 15 | flag = True 16 | z = neighbourPosition(p, q, n) 17 | for k in z: 18 | if l[k[0]][k[1]] == 1: 19 | flag = False 20 | break 21 | if flag: count += 1 22 | print(count) 23 | 24 | -------------------------------------------------------------------------------- /queue - check if the postfix expression is correct or not - if correct evaluate the expression.py: -------------------------------------------------------------------------------- 1 | s = list(str(input())) 2 | num = 0 3 | opr = 0 4 | for i in s: 5 | if i in '1234567890': num += 1 6 | if i in '+-*/%' : opr += 1 7 | if num == (opr+1): 8 | stk = [] 9 | for i in range(len(s)): 10 | if s[i] not in "+-*/%": 11 | stk.insert(0, s[i]) 12 | else: 13 | a = int(stk.pop(0)) 14 | b = int(stk.pop(0)) 15 | if s[i] == '+' : stk.insert(0, b + a) 16 | elif s[i] == '-' : stk.insert(0, b - a) 17 | elif s[i] == '*' : stk.insert(0, b * a) 18 | elif s[i] == '/' : stk.insert(0, b / a) 19 | elif s[i] == '%' : stk.insert(0, b % a) 20 | else: continue 21 | print(stk[0]) 22 | else: print(-1) 23 | -------------------------------------------------------------------------------- /maximum length of substring alternating characters of vowel and consonants.py: -------------------------------------------------------------------------------- 1 | s = str(input()) 2 | v = {'a','e','i','o','u'} 3 | count = 1 4 | if s[0] not in v: 5 | for i in range(1, len(s)): 6 | if i % 2 == 1: 7 | if s[i] in v: 8 | count += 1 9 | else: 10 | break 11 | else: 12 | if s[i] not in v: 13 | count += 1 14 | else: 15 | break 16 | print(count) 17 | else: 18 | for i in range(1, len(s)): 19 | if i % 2 == 1: 20 | if s[i] not in v: 21 | count += 1 22 | else: 23 | break 24 | else: 25 | if s[i] in v: 26 | count += 1 27 | else: 28 | break 29 | print(count-1) 30 | -------------------------------------------------------------------------------- /Rohan is teacher at “Sunsdfghpol” academy about dfg numbers - Print all the dfg numbers upto index ‘n’.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | def isprime(a): 3 | flag = True 4 | for i in range(2, a//2+1): 5 | if a%i==0: 6 | flag = False 7 | break 8 | return flag 9 | def primeFactors(a): 10 | factor = [] 11 | for i in range(2, a+1): 12 | if a%i==0: 13 | factor.append(i) 14 | p = [] 15 | for i in factor: 16 | if isprime(i): 17 | p.append(i) 18 | return p 19 | a = [] 20 | i = 1 21 | while True: 22 | flag = True 23 | lst = primeFactors(i) 24 | for j in lst: 25 | if j not in (2,3,5): 26 | flag = False 27 | break 28 | if flag: 29 | a.append(i) 30 | if len(a) == n:break 31 | else:i += 1 32 | print(*a) 33 | -------------------------------------------------------------------------------- /hashing - chess Knight movements.py: -------------------------------------------------------------------------------- 1 | def knightPosition(a, b): 2 | p = [] 3 | if a+1 < 4 and b+2 < 4: p.append((a+1, b+2)) 4 | if a+2 < 4 and b+1 < 4: p.append((a+2, b+1)) 5 | if a+1 < 4 and b-2 >= 0: p.append((a+1, b-2)) 6 | if a+2 < 4 and b-1 >= 0: p.append((a+2, b-1)) 7 | if a-1 >= 0 and b+2 < 4: p.append((a-1, b+2)) 8 | if a-2 >= 0 and b+1 < 4: p.append((a-2, b+1)) 9 | if a-1 >= 0 and b-2 >= 0: p.append((a-1, b-2)) 10 | if a-2 >= 0 and b-1 >= 0: p.append((a-2, b-1)) 11 | return p 12 | 13 | ch = [list(input()) for _ in range(4)] 14 | q = int(input()) 15 | for _ in range(q): 16 | (i, j) = map(int, input().split()) 17 | if ch[i][j] == '#': 18 | print(-1) 19 | continue 20 | else: 21 | count = 0 22 | l = knightPosition(i, j) 23 | for k in l: 24 | if ch[k[0]][k[1]] == '#': continue 25 | else: count += 1 26 | print(count) 27 | -------------------------------------------------------------------------------- /Rox Travel - The customer should be born on or before july 22 1987.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = input().split() 3 | a = [] 4 | k = 0 5 | for i in range(n): 6 | b = [] 7 | for j in range(3): 8 | b.append(l[k]) 9 | k += 1 10 | a.append(b) 11 | ans = [] 12 | flag = False 13 | for i in range(len(a)): 14 | if int(a[i][2]) <= 1987: 15 | if int(a[i][2]) == 1987: 16 | if a[i][1] in ('JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY'): 17 | if (a[i][1]) == 'JULY': 18 | if int(a[i][0]) <= 22: 19 | ans.append(i+1) 20 | flag = True 21 | else: 22 | continue 23 | else: 24 | ans.append(i+1) 25 | flag = True 26 | else: 27 | continue 28 | else: 29 | ans.append(i+1) 30 | flag = True 31 | else: 32 | continue 33 | if flag:print(*ans) 34 | else:print(-1) 35 | -------------------------------------------------------------------------------- /matrix - Aleena chess - whether bishop cut pawn or not.py: -------------------------------------------------------------------------------- 1 | bishop = tuple(map(int, input().split())) 2 | pawn = tuple(map(int, input().split())) 3 | def bishopPosition(b): 4 | a = [] 5 | i = b[0] 6 | j = b[1] 7 | while True: 8 | if i == 7 or j == 0: break 9 | else: 10 | i += 1 11 | j -= 1 12 | a.append((i, j)) 13 | i = b[0] 14 | j = b[1] 15 | while True: 16 | if i == 7 or j == 7: break 17 | else: 18 | i += 1 19 | j += 1 20 | a.append((i, j)) 21 | i = b[0] 22 | j = b[1] 23 | while True: 24 | if i == 0 or j == 0: break 25 | else: 26 | i -= 1 27 | j -= 1 28 | a.append((i, j)) 29 | i = b[0] 30 | j = b[1] 31 | while True: 32 | if i == 0 or j == 7: break 33 | else: 34 | i -= 1 35 | j += 1 36 | a.append((i, j)) 37 | return a 38 | lst = bishopPosition(bishop) 39 | if pawn in lst: print("Yes") 40 | else: print("No") 41 | -------------------------------------------------------------------------------- /convert numbers to words.py: -------------------------------------------------------------------------------- 1 | f = open("file.txt", "w+") 2 | data = input() 3 | f.write(data) 4 | f.seek(0) 5 | read_data = f.read() 6 | temp = int(read_data) 7 | l = [] 8 | while(temp > 0): 9 | digit = temp % 10 10 | l.append(digit) 11 | temp = temp//10 12 | #print(l) 13 | l.reverse() 14 | #print(l) 15 | length = len(l) 16 | 17 | ones_digit = {1:"one", 2:"two", 3:"three", 4:"four", 5: "five", 6:"six", 7:"seven", 8:"eight", 9:"nine", 0:""} 18 | tens_digit = {1:"ten", 2:"twenty", 3:"thirty", 4:"fourty", 5:"fifty", 6:"sixty", 7:"seventy", 8:"eighty", 9:"ninety", 0:""} 19 | 20 | two_digit = {1:"eleven", 2: "twelve", 3: "thirteen", 4:"fourteen", 5:"fifteen", 6:"sixteen", 7:"seventeen", 8:"eighteen", 9:"nineteen", 0:"ten"} 21 | 22 | #print(l[0]) 23 | #print(l[1]) 24 | if length == 3: 25 | if (l[1] == 0 and l[2] == 0): 26 | wr = str(ones_digit[l[0]]) + " hundred" 27 | elif (l[1] == 0 and l[2] != 0): 28 | wr = str(ones_digit[l[0]]) + " hundred and " + str(ones_digit[l[2]]) 29 | elif (l[1] != 0 and l[2] == 0): 30 | wr = str(ones_digit[l[0]]) + " hundred and " + str(tens_digit[l[1]]) 31 | else: 32 | wr = str(ones_digit[l[0]]) + " hundred and " + str(tens_digit[l[1]]) + " " + str(ones_digit[l[2]]) 33 | 34 | elif length == 2: 35 | if l[0] == 1: 36 | wr = two_digit[l[1]] 37 | else: 38 | if l[1] == 0: 39 | wr = tens_digit[l[0]] 40 | else: 41 | wr = tens_digit[l[0]] + " " + ones_digit[l[1]] 42 | elif length == 1: 43 | wr = ones_digit[l[0]] 44 | else: 45 | wr = "" 46 | 47 | f.writelines(wr) 48 | f.seek(3) 49 | last = f.readlines() 50 | print(last) 51 | f.close() 52 | -------------------------------------------------------------------------------- /ditya - smallest positive number.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | l = [int(x) for x in input().split()] 3 | def notinthelst(d, l): 4 | flag = True 5 | if d in l: 6 | flag = False 7 | return flag 8 | def notsumofall(d, l): 9 | flag = True 10 | if sum(l) == d: 11 | flag = False 12 | return flag 13 | def notsumoftwo(d, l): 14 | flag = True 15 | for i in range(len(l)-1): 16 | for j in range(i+1, len(l)): 17 | if (l[i] + l[j]) == d: 18 | flag = False 19 | break 20 | if not flag: 21 | break 22 | return flag 23 | def notsumofthree(d, l): 24 | flag = True 25 | for i in range(len(l)-2): 26 | for j in range(i+1, len(l)-1): 27 | for k in range(j+1, len(l)): 28 | if (l[i] + l[j] + l[k]) == d: 29 | flag = False 30 | break 31 | if not flag: 32 | break 33 | if not flag: 34 | break 35 | return flag 36 | def notsumoffour(d, l): 37 | flag = True 38 | for i in range(len(l)-3): 39 | for j in range(i+1, len(l)-2): 40 | for k in range(j+1, len(l)-1): 41 | for m in range(k+1, len(l)): 42 | if (l[i] + l[j] + l[k] + l[m]) == d: 43 | flag = False 44 | break 45 | if not flag: 46 | break 47 | if not flag: 48 | break 49 | if not flag: 50 | break 51 | return flag 52 | d = min(l)+1 53 | while True: 54 | if ((notinthelst(d, l) and notsumofall(d, l)) and (notsumoftwo(d, l) and notsumofthree(d, l))) and notsumoffour(d, l): 55 | ans = d 56 | break 57 | d += 1 58 | print(ans) 59 | -------------------------------------------------------------------------------- /array - chan's girlfriend birthday - Red and Green cherry.py: -------------------------------------------------------------------------------- 1 | (m, n) = map(int, input().split()) 2 | l = [list(input()) for _ in range(m)] 3 | cost = 0 4 | for i in range(m): 5 | for j in range(n): 6 | if j == 0: 7 | if l[i][j] == 'R': 8 | if l[i][j+1] == 'G': continue 9 | else: 10 | if l[i][j+2] == 'G' : 11 | l[i][j] = 'G' 12 | cost += 5 13 | else: 14 | l[i][j+1] = 'G' 15 | cost += 5 16 | else: 17 | if l[i][j+1] == 'R': continue 18 | else: 19 | if l[i][j+2] == 'R' : 20 | l[i][j] = 'R' 21 | cost += 3 22 | else: 23 | l[i][j+1] = 'R' 24 | cost += 3 25 | elif j == n-1: 26 | if l[i][j] == 'G': 27 | if l[i][j-1] == 'R': continue 28 | else: 29 | if l[i][j-2] == 'R' : 30 | l[i][j] == 'R' 31 | cost +=3 32 | else: 33 | l[i][j-1] = 'R' 34 | cost += 3 35 | else: 36 | if l[i][j-1] == 'G': continue 37 | else: 38 | if l[i][j-2] == 'G' : 39 | l[i][j] == 'G' 40 | cost += 5 41 | else: 42 | l[i][j-1] = 'G' 43 | cost += 5 44 | else: 45 | if l[i][j] == 'R': 46 | if l[i][j-1] == 'R' and l[i][j+1] == 'R': 47 | l[i][j] = 'G' 48 | cost += 5 49 | elif l[i][j-1] == 'R' and l[i][j+1] == 'G': 50 | l[i][j-1] = 'G' 51 | cost += 5 52 | elif l[i][j-1] == 'G' and l[i][j+1] == 'R': 53 | l[i][j+1] = 'G' 54 | cost += 5 55 | else: continue 56 | else: 57 | if l[i][j-1] == 'G' and l[i][j+1] == 'G': 58 | l[i][j] = 'R' 59 | cost += 3 60 | elif l[i][j-1] == 'G' and l[i][j+1] == 'R': 61 | l[i][j-1] = 'R' 62 | cost += 3 63 | elif l[i][j-1] == 'R' and l[i][j+1] == 'G': 64 | l[i][j+1] = 'R' 65 | cost += 3 66 | else: continue 67 | print(cost) 68 | --------------------------------------------------------------------------------