├── The Minion Game ├── README.md └── The_Minion_Game.py ├── Text Wrap ├── Text Wrap.py └── README.md ├── Multiply By 11 ├── README.md └── Multiply-by-11.py ├── String Split and Join ├── String_Split_and_Join.py └── README.md ├── Tuples ├── Tuples.py └── README.md ├── Introduction to set ├── Introduction_to_set.py └── README.md ├── Mutations ├── Mutations.py └── README.md ├── What's Your Name ├── What's_Your_Name.py └── README.md ├── Product is even or odd ├── product-is-even-or-odd.py └── README.md ├── The Great Kian ├── The_Great_Kian.py └── README.md ├── Combine Two Tables ├── Combine Two Tables.py └── README.md ├── ishaans-weight ├── ishaans-weight.py └── README.md ├── itertools.product() ├── itertools.product().py └── README.md ├── Designer Door Mat ├── Designer_Door_Mat.py └── README.md ├── Mod Divmod ├── Mod Divmod.py └── README.md ├── Self Powers ├── problem └── code.py ├── Complicated Sum ├── ComplicatedSum.py └── README.md ├── Remove vowels from string ├── remove-vowels-from-string.py └── README.md ├── Find the Runner-Up Score! ├── Find the Runner-Up Score!.py └── README.md ├── SAP Interview Question ├── Shift-0s-to-end.py └── README.md ├── List_comprehensions ├── List comprehensions.py └── README.md ├── Second Highest Salary ├── Second Highest Salary.py └── README.md ├── Search_Small_Ones ├── Search_Small_Ones.py └── README.md ├── Find_a_string ├── Find_a_string.py └── README.md ├── String Formatting ├── String_Formatting.py └── README.md ├── README.md ├── itertools.permutations() ├── itertools.permutations().py └── README.md ├── The Penalty Shootout ├── the-penalty-shootout.py └── README.md ├── Min and Max - Numpy ├── Min Max.py └── README.md ├── Nth Highest Salary ├── Nth Highest Salary.py └── README.md ├── Collection_named tuple ├── collection_namedtuple.py └── README.md ├── Count_Bigger ├── Count_Bigger.py └── README.md ├── Alphabet Rangoli ├── alphabet_rangoli.py └── README.md ├── Geek and his Tricky Series ├── geek-and-his-tricky-series.py └── README.md ├── Two_Strings ├── Two_Strings.py └── README.md ├── kth_smallest_element ├── kth_smallest_element.py └── README.md ├── Iterables and Iterators ├── Iterables and Iterators.py └── README.md ├── Monk_and_Welcome_Problem ├── Monk_and_Welcome_Problem.py └── README.md ├── Multiply left and right array sum ├── multiply-left-and-right-array-sum.py └── README.md ├── Merge the Tools ├── Merge_the_tools.py └── README.md ├── Swap Case ├── Swap_Case.py └── README.md ├── Count Of 1's ├── Count Of 1's.py └── README.md ├── Micro and Array ├── Micro_and_Array.py └── README.md ├── Reduce the Fraction ├── README.md └── Reduce_the_Fraction.py ├── itertools.combinations() ├── itertools.combinations().py └── README.md ├── itertools.combinations_with_replacement() ├── itertools.combinations_with_replacement().py └── README.md ├── pairs_equal_to_k ├── pairs_equal_to_k.py └── README.md ├── Finding_the_percentage ├── Finding_the_percentage.py └── README.md ├── Remove all characters other than alphabets ├── Remove-all-characters-other-than-alphabets.py └── README.md ├── Johny and the integer ├── Johny and the integer.py └── README.md ├── First repeating element ├── First repeating element.py └── README.md ├── Fitting-The-Array ├── fitting-the-array.py └── README.md ├── calendar module ├── calendar_module.py └── README.md ├── Mini-Max Sum ├── Mini-Max Sum.py └── README.md ├── collections.Counter() ├── collections.Counter().py └── README.md ├── Lonely Integer ├── Lonely Integer.py └── README.md ├── Convert-a-list-of-characters-into-string.py ├── Convert-a-list-of-characters-into-string.py └── README.md ├── First and last occurrence of x ├── First-and-last-occurrence-of-x.py └── README.md ├── Longest Common Prefix ├── Longest Common Prefix.py └── README.md ├── Diagonal Difference ├── Diagonal_Difference.py └── README.md ├── Counting Sort 1 ├── Time_Conversion.py └── README.md ├── Time Conversion ├── Time_Conversion.py └── README.md ├── Product-of-max-and-min-of-two-arrays.py ├── Product-of-max--and-min-of-two-arrays.py └── README.md ├── String-Validators ├── String_Validators.py └── README.md ├── Petr and a calendar ├── Petr and a calendar.py └── README.md ├── Minimum number of jumps ├── Minimum_number_of_jumps.py └── README.md ├── Nested_Lists ├── Nested_Lists.py └── README.md ├── Point In a Rectangle ├── Points_in_a_rectangle.py └── README.md ├── All A's Well ├── README.md └── All A's Well.py ├── Generate Prime Numbers In A Interval with Python ├── Generate_Prime_Numbers_In_A_Interval_with_Python.py └── README.md ├── Tower Breakers ├── Tower Breakers.py └── README.md ├── Plus Minus ├── Plus Minus.py └── README.md ├── Zig Zag Sequence ├── Zig Zag Sequence.py └── README.md ├── Recursive Digit Sum ├── Recursive Digit Sum.py └── README.md ├── Playing with matches ├── solution_code.py └── Problem ├── Lists ├── Lists.py └── README.md ├── Caesar Cipher ├── Caesar Cipher.py └── readme.md ├── Jumping on the Clouds ├── Jumping_on_the_Clouds.py └── README.md ├── Text Alignment ├── Text_Alignment.py └── README.md ├── Sock Merchant ├── Sock Merchant.py └── README.md ├── 2D Array - DS ├── 2D Array - DS.py └── README.md ├── Grid Challenge ├── Grid Challenge.py └── README.md ├── Repeated String ├── Repeated String.py └── README.md └── Counting Valleys ├── counting_valleys.py └── README.md /The Minion Game/README.md: -------------------------------------------------------------------------------- 1 | ## The Minion Game 2 | 3 | -------------------------------------------------------------------------------- /Text Wrap/Text Wrap.py: -------------------------------------------------------------------------------- 1 | def wrap(string, max_width): 2 | return textwrap.fill(string, max_width) 3 | -------------------------------------------------------------------------------- /Multiply By 11/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ranajoy-dutta/Competitive-Programmming/HEAD/Multiply By 11/README.md -------------------------------------------------------------------------------- /String Split and Join/String_Split_and_Join.py: -------------------------------------------------------------------------------- 1 | def split_and_join(line): 2 | return "-".join(line.split()) 3 | 4 | -------------------------------------------------------------------------------- /Tuples/Tuples.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | n = int(input()) 3 | print(hash(tuple(map(int, input().split())))) 4 | -------------------------------------------------------------------------------- /Introduction to set/Introduction_to_set.py: -------------------------------------------------------------------------------- 1 | def average(array): 2 | array = set(array) 3 | return sum(array)/len(array) 4 | -------------------------------------------------------------------------------- /Mutations/Mutations.py: -------------------------------------------------------------------------------- 1 | def mutate_string(string, position, character): 2 | return string[:position] + character + string[position+1:] 3 | -------------------------------------------------------------------------------- /What's Your Name/What's_Your_Name.py: -------------------------------------------------------------------------------- 1 | def print_full_name(a, b): 2 | print("Hello {0} {1}! You just delved into python.".format(a,b)) 3 | -------------------------------------------------------------------------------- /Product is even or odd/product-is-even-or-odd.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | while t>0: 3 | print( 1 if int(input())*int(input())%2==0 else 0) 4 | t-=1 5 | -------------------------------------------------------------------------------- /The Great Kian/The_Great_Kian.py: -------------------------------------------------------------------------------- 1 | n = int(input().strip()) 2 | a = [int(x) for x in input().strip().split()] 3 | print("{} {} {}".format(sum(a[::3]),sum(a[1::3]),sum(a[2::3]))) 4 | -------------------------------------------------------------------------------- /Combine Two Tables/Combine Two Tables.py: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT P.FirstName, P.LastName, A.City, A.State from Person as P left join Address as A on P.PersonId = A.PersonId; -------------------------------------------------------------------------------- /Multiply By 11/Multiply-by-11.py: -------------------------------------------------------------------------------- 1 | t = int(input()) # Number of test cases 2 | while t > 0: 3 | n = int(input()) # Taking the number 4 | print(n * 11) # Multiply the number by 11 5 | t -= 1 6 | -------------------------------------------------------------------------------- /ishaans-weight/ishaans-weight.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | while t>0: 3 | n,k=map(int,input().split()) 4 | amount=0 5 | for i in range(k,n+1,k): 6 | amount+=i 7 | print(amount) 8 | t-=1 9 | -------------------------------------------------------------------------------- /itertools.product()/itertools.product().py: -------------------------------------------------------------------------------- 1 | from itertools import product 2 | result = list(product(tuple(map(int,input().split())),tuple(map(int,input().split())))) 3 | for i in result: 4 | print(i, end=" ") 5 | -------------------------------------------------------------------------------- /Designer Door Mat/Designer_Door_Mat.py: -------------------------------------------------------------------------------- 1 | n, m = map(int,input().split()) 2 | pattern = [('.|.'*(2*i + 1)).center(m, '-') for i in range(n//2)] 3 | print('\n'.join(pattern + ['WELCOME'.center(m, '-')] + pattern[::-1])) 4 | -------------------------------------------------------------------------------- /Mod Divmod/Mod Divmod.py: -------------------------------------------------------------------------------- 1 | x = int(input()) 2 | y = int(input()) 3 | z = int(input()) 4 | N = int(input()) 5 | arr = [[X, Y, Z] for X in range(x+1) for Y in range(y+1) for Z in range(z+1) if X + Y + Z != N] 6 | print(arr) 7 | -------------------------------------------------------------------------------- /Self Powers/problem: -------------------------------------------------------------------------------- 1 | The problem can be found here: 2 | https://www.hackerrank.com/contests/projecteuler/challenges/euler048/problem 3 | Problem#48 4 | 5 | It is question from projecteuler contest of Hackerrank. 6 | -------------------------------------------------------------------------------- /Complicated Sum/ComplicatedSum.py: -------------------------------------------------------------------------------- 1 | t=int(input()) # test cases 2 | mod=1000000007 3 | while t>0: 4 | n=int(input()) 5 | res=(n*(n+1)*(3*n*n+7*n+2))//24 6 | print(res%mod) 7 | t=t-1 8 | -------------------------------------------------------------------------------- /Remove vowels from string/remove-vowels-from-string.py: -------------------------------------------------------------------------------- 1 | t = int(input()) # Number of test cases 2 | while t > 0: 3 | n = int(input()) # Taking the number 4 | print(n * 11) # Multiply the number by 11 5 | t -= 1 6 | -------------------------------------------------------------------------------- /Find the Runner-Up Score!/Find the Runner-Up Score!.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | n = int(input()) 3 | arr = list(map(int, input().split())) 4 | arr = [i for i in arr if i != max(arr)] 5 | print(max(arr)) 6 | -------------------------------------------------------------------------------- /SAP Interview Question/Shift-0s-to-end.py: -------------------------------------------------------------------------------- 1 | arr = list(map(int,input().split())) 2 | pos = 0 3 | for i in range(len(arr)): 4 | if arr[i] != 0: 5 | arr[i],arr[pos] = arr[pos],arr[i] 6 | pos+=1 7 | print(*arr) 8 | -------------------------------------------------------------------------------- /List_comprehensions/List comprehensions.py: -------------------------------------------------------------------------------- 1 | x = int(input()) 2 | y = int(input()) 3 | z = int(input()) 4 | N = int(input()) 5 | arr = [[X, Y, Z] for X in range(x+1) for Y in range(y+1) for Z in range(z+1) if X + Y + Z != N] 6 | print(arr) 7 | -------------------------------------------------------------------------------- /Second Highest Salary/Second Highest Salary.py: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT IFNULL( 3 | (SELECT distinct Salary 4 | FROM Employee 5 | ORDER BY Salary desc limit 1,1),NULL) 6 | As SecondHighestSalary; 7 | -------------------------------------------------------------------------------- /Search_Small_Ones/Search_Small_Ones.py: -------------------------------------------------------------------------------- 1 | # from TECHGIG 2 | def main(): 3 | n = int(input()) 4 | arr = list(map(int,input().split())) 5 | min1 = min(arr) 6 | arr.remove(min1) 7 | print(min(arr) + min1) 8 | 9 | main() 10 | 11 | -------------------------------------------------------------------------------- /Find_a_string/Find_a_string.py: -------------------------------------------------------------------------------- 1 | def count_substring(string, sub_string): 2 | count = 0 3 | for i in range(len(string)-len(sub_string)+1): 4 | if string[i:i+len(sub_string)] == sub_string: 5 | count += 1 6 | return count 7 | 8 | -------------------------------------------------------------------------------- /String Formatting/String_Formatting.py: -------------------------------------------------------------------------------- 1 | def print_formatted(number): 2 | w =len(bin(number)[2:]) 3 | for i in range(1,number+1): 4 | print (str(i).rjust(w,' '),oct(i)[2:].rjust(w,' '),hex(i)[2:].upper().rjust(w,' '),bin(i)[2:].rjust(w,' ')) 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive-Programmming 2 | 3 | Questions solved from Various Coding websites viz. **HackerRank**, **HackerEarth**, **CodeChef**, **CodingNinja** and other websites. 4 | This repository also contains Questions from various offline and onsite competitions. 5 | -------------------------------------------------------------------------------- /itertools.permutations()/itertools.permutations().py: -------------------------------------------------------------------------------- 1 | from itertools import permutations 2 | string, n = map(str,input().split()) 3 | listt = sorted(list(permutations(string,int(n)))) 4 | for i in listt: 5 | for j in i: 6 | print(j,end="") 7 | print() 8 | -------------------------------------------------------------------------------- /The Penalty Shootout/the-penalty-shootout.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | while t>0: 3 | num = input() 4 | arr=[] 5 | score=0 6 | for i in range(len(num)): 7 | if num[i]=='2': 8 | score+=int(num[i+1]) 9 | print(score) 10 | t -= 1 11 | -------------------------------------------------------------------------------- /Min and Max - Numpy/Min Max.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | n = list(map(int,input().split())) 3 | n = n[0] 4 | for i in range(n): 5 | temp = list(map(int, input().split())) 6 | arr.append(temp) 7 | mins = numpy.min(arr, axis = 1) 8 | print(max(mins)) 9 | 10 | 11 | -------------------------------------------------------------------------------- /Nth Highest Salary/Nth Highest Salary.py: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT 2 | BEGIN 3 | SET N=N-1; 4 | RETURN ( 5 | # Write your MySQL query statement below. 6 | SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT N, 1 7 | ); 8 | END -------------------------------------------------------------------------------- /Collection_named tuple/collection_namedtuple.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | n = int(input()) 3 | input() 4 | summ = 0 5 | for i in range(n): 6 | marks,name = input().split() 7 | summ+=int(marks) 8 | print("{0:.2f}".format(summ/n)) 9 | main() 10 | 11 | -------------------------------------------------------------------------------- /Count_Bigger/Count_Bigger.py: -------------------------------------------------------------------------------- 1 | # from TECHGIG 2 | def main(): 3 | n = int(input()) 4 | arr = list(map(int,input().split())) 5 | v = int(input()) 6 | count=0 7 | for i in arr: 8 | if i>v: 9 | count+=1 10 | return count 11 | print(main()) 12 | -------------------------------------------------------------------------------- /Alphabet Rangoli/alphabet_rangoli.py: -------------------------------------------------------------------------------- 1 | import string 2 | def print_rangoli(size): 3 | a = string.ascii_lowercase 4 | r = [] 5 | for i in range(size): 6 | s = "-".join(a[i:size]) 7 | r.append((s[::-1]+s[1:]).center(4*size-3, "-")) 8 | print('\n'.join(r[:0:-1]+r)) 9 | -------------------------------------------------------------------------------- /Geek and his Tricky Series/geek-and-his-tricky-series.py: -------------------------------------------------------------------------------- 1 | t=int(input()) 2 | while t>0: 3 | n = int(input("")) 4 | counter=7 5 | term=1 6 | while term0: 3 | a,b=map(str,input().split()) 4 | flag=1 5 | for i in b: 6 | if i in a: 7 | a=a.replace(i,"") 8 | if a=="": 9 | print("YES") 10 | else: 11 | print("NO") 12 | t-=1 13 | -------------------------------------------------------------------------------- /kth_smallest_element/kth_smallest_element.py: -------------------------------------------------------------------------------- 1 | # from TECHGIG 2 | def main(): 3 | n = int(input()) 4 | arr=[] 5 | for i in range(n): 6 | arr.append(int(input())) 7 | v = int(input()) 8 | arr = sorted(arr) 9 | print(arr[v-1]) 10 | 11 | main() 12 | 13 | 14 | -------------------------------------------------------------------------------- /Iterables and Iterators/Iterables and Iterators.py: -------------------------------------------------------------------------------- 1 | from itertools import combinations 2 | input() 3 | arr = input().split() 4 | k = int(input()) 5 | total,count = 0,0 6 | for i in combinations(arr,k): 7 | total += 1 8 | for j in i: 9 | if j == 'a': 10 | count += 1 11 | break 12 | print(count/total) 13 | -------------------------------------------------------------------------------- /Monk_and_Welcome_Problem/Monk_and_Welcome_Problem.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | while t>0: 3 | try: 4 | arr1 = list(map(int,input().split())) 5 | arr2 = list(map(int,input().split())) 6 | for i in range(len(arr1)): 7 | print(arr1[i]+arr2[i], end=" ") 8 | except: 9 | print() 10 | t-=1 11 | 12 | -------------------------------------------------------------------------------- /Multiply left and right array sum/multiply-left-and-right-array-sum.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | while t>0: 3 | n=int(input()) 4 | arr = list(map(int,input().split())) 5 | sum1,sum2 =0,0 6 | for i in range(0,n//2): 7 | sum1 += arr[i] 8 | for i in range(n//2,n): 9 | sum2 += arr[i] 10 | print(sum1*sum2) 11 | t -= 1 12 | -------------------------------------------------------------------------------- /Merge the Tools/Merge_the_tools.py: -------------------------------------------------------------------------------- 1 | def rem_duplicate(string): 2 | result = [] 3 | for c in string: 4 | if c not in result: 5 | result.append(c) 6 | return ''.join(result) 7 | 8 | def merge_the_tools(string, k): 9 | a=0 10 | for i in range(len(string)//k): 11 | print(rem_duplicate(string[a:a+k])) 12 | a+=k 13 | -------------------------------------------------------------------------------- /Swap Case/Swap_Case.py: -------------------------------------------------------------------------------- 1 | def swap_case(s): 2 | s=list(s) 3 | for i in range(len(s)): 4 | if s[i].isupper(): 5 | s[i]= s[i].lower() 6 | elif s[i].islower(): 7 | s[i] = s[i].upper() 8 | s="".join(s) 9 | return (s) 10 | 11 | if __name__ == '__main__': 12 | s = input() 13 | result = swap_case(s) 14 | print(result) 15 | -------------------------------------------------------------------------------- /Count Of 1's/Count Of 1's.py: -------------------------------------------------------------------------------- 1 | ''' Read input from STDIN. Print your output to STDOUT ''' 2 | #Use input() to read input from STDIN and use print to write your output to STDOUT 3 | 4 | def main(): 5 | counter = 0 6 | for _ in range(int(input())): 7 | if int(input())==0: 8 | break 9 | counter+=1 10 | 11 | print(counter) 12 | main() 13 | 14 | -------------------------------------------------------------------------------- /Micro and Array/Micro_and_Array.py: -------------------------------------------------------------------------------- 1 | for i in range(int(input())): #For test cases 2 | n, k = map(int, input().split(' ')) #input N and K 3 | l = min(map(int, input().split(' '))) #input array and finding least in array 4 | if l < k: #finding least number of seconds 5 | print(k - l) 6 | else: 7 | print(0) 8 | -------------------------------------------------------------------------------- /Reduce the Fraction/README.md: -------------------------------------------------------------------------------- 1 | ## Reduce the Fraction 2 | 3 | **You are given a fraction - A/B.
4 | Reduce the fraction to the lowest possible.**
5 | 6 | **Input**
7 | First line consist of T - the number of test cases. T lines follows - each consisting of 2 numbers A B.
8 | 9 | **Output**
10 | Output T lines - In each line print the answer in the form P/Q. -------------------------------------------------------------------------------- /Self Powers/code.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | lst = [] 3 | for i in range(1,n+1): 4 | lst.append(i**i) 5 | sum = 0 6 | for j in range(0,n): 7 | sum = sum + lst[j] 8 | res = [int(x) for x in str(sum)] 9 | 10 | k = 0 11 | for i in range(0,len(res)): 12 | if (i!=0): 13 | k = i 14 | break 15 | 16 | ans = res[k+1:] 17 | for i in ans: 18 | print(i, end = '') 19 | 20 | -------------------------------------------------------------------------------- /itertools.combinations()/itertools.combinations().py: -------------------------------------------------------------------------------- 1 | from itertools import combinations 2 | string, n = map(str,input().split()) 3 | 4 | for i in range(1,int(n)+1): 5 | listt = list(combinations(string,i)) 6 | for j in range(len(listt)): 7 | listt[j] = sorted(listt[j]) 8 | for j in sorted(listt): 9 | for k in j: 10 | print(k, end="") 11 | print() -------------------------------------------------------------------------------- /itertools.combinations_with_replacement()/itertools.combinations_with_replacement().py: -------------------------------------------------------------------------------- 1 | from itertools import combinations_with_replacement 2 | string, n = map(str, input().split()) 3 | 4 | listt = list(combinations_with_replacement(string,int(n))) 5 | for i in range(len(listt)): 6 | listt[i] = sorted(listt[i]) 7 | 8 | for i in sorted(listt): 9 | for j in i: 10 | print(j, end="") 11 | print() -------------------------------------------------------------------------------- /pairs_equal_to_k/pairs_equal_to_k.py: -------------------------------------------------------------------------------- 1 | # from TECHGIG 2 | def main(): 3 | n = int(input()) 4 | arr = [] 5 | for i in range(n): 6 | arr.append(int(input())) 7 | v = int(input()) 8 | arr = sorted(arr) 9 | count=0 10 | for i in range(n-1): 11 | for j in range(i+1,n): 12 | if (arr[j]-arr[i])==v: 13 | count+=1 14 | print(count) 15 | main() 16 | -------------------------------------------------------------------------------- /Finding_the_percentage/Finding_the_percentage.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | big_arr=[] 3 | while n>0: 4 | arr = list(map(str, input().split())) 5 | big_arr.append(arr) 6 | n-=1 7 | name = input() 8 | lst = [] 9 | for i in range(len(big_arr)): 10 | if big_arr[i][0]==name: 11 | lst=big_arr[i] 12 | sum=0 13 | for i in range(1,len(lst)): 14 | sum = sum + float(lst[i]) 15 | print("%.2f"%(sum/3)) 16 | -------------------------------------------------------------------------------- /Remove all characters other than alphabets/Remove-all-characters-other-than-alphabets.py: -------------------------------------------------------------------------------- 1 | import string 2 | 3 | t = int(input()) 4 | while t > 0: 5 | str1 = input() 6 | str2 = '' 7 | for i in str1: 8 | if i in string.ascii_lowercase or i in string.ascii_uppercase: 9 | str2 = str2 + i 10 | if str2!='': 11 | print(str2) 12 | else: 13 | print('-1') 14 | t -= 1 15 | -------------------------------------------------------------------------------- /Reduce the Fraction/Reduce_the_Fraction.py: -------------------------------------------------------------------------------- 1 | def gcd(x, y): #function to find GCD 2 | while y != 0: 3 | (x, y) = (y, x % y) 4 | return x 5 | 6 | n = int(input()) #number of test cases 7 | while n>0 : 8 | a,b=map(int,input().split()) 9 | gcd = gcd(a,b) 10 | print(gcd) 11 | r1 = int(a/gcd) 12 | r2 = int(b/gcd) 13 | print("%d/%d"%(r1,r2)) 14 | n-=1 15 | -------------------------------------------------------------------------------- /Johny and the integer/Johny and the integer.py: -------------------------------------------------------------------------------- 1 | ''' Read input from STDIN. Print your output to STDOUT ''' 2 | #Use input() to read input from STDIN and use print to write your output to STDOUT 3 | 4 | def main(): 5 | n = int(input()) 6 | k = int(input()) 7 | if n%k==0: 8 | print(n+k) 9 | else: 10 | mid = int(-(-(n/k) // 1)) # you can use math.ceil 11 | print(k*mid) 12 | 13 | main() 14 | 15 | -------------------------------------------------------------------------------- /First repeating element/First repeating element.py: -------------------------------------------------------------------------------- 1 | ''' Read input from STDIN. Print your output to STDOUT ''' 2 | #Use input() to read input from STDIN and use print to write your output to STDOUT 3 | 4 | def main(): 5 | n = int(input()) 6 | k = int(input()) 7 | if n%k==0: 8 | print(n+k) 9 | else: 10 | mid = int(-(-(n/k) // 1)) # you can use math.ceil 11 | print(k*mid) 12 | 13 | main() 14 | 15 | -------------------------------------------------------------------------------- /Fitting-The-Array/fitting-the-array.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | while t>0: 3 | input() 4 | arr1 = list(map(int,input().split())) 5 | arr2 = list(map(int,input().split())) 6 | arr1.sort() 7 | arr2.sort() 8 | flag = 0 9 | for i in range(len(arr1)): 10 | if arr1[i]>arr2[i]: 11 | flag = 1 12 | if flag==1: 13 | print("NO") 14 | else: 15 | print("YES") 16 | 17 | t-=1 18 | -------------------------------------------------------------------------------- /calendar module/calendar_module.py: -------------------------------------------------------------------------------- 1 | import calendar 2 | month, date, year = map(int, input().split()) 3 | day = calendar.weekday(year, month, date) 4 | if day == 0: 5 | print("MONDAY") 6 | elif day == 1: 7 | print("TUESDAY") 8 | elif day == 2: 9 | print("WEDNESDAY") 10 | elif day == 3: 11 | print("THURSDAY") 12 | elif day == 4: 13 | print("FRIDAY") 14 | elif day == 5: 15 | print("SATURDAY") 16 | else: 17 | print("SUNDAY") 18 | -------------------------------------------------------------------------------- /Mini-Max Sum/Mini-Max Sum.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | def miniMaxSum(arr): 4 | v_min, v_max, v_sum = arr[0], arr[0], 0 5 | for ele in arr: 6 | if ele < v_min: 7 | v_min = ele 8 | if ele > v_max: 9 | v_max = ele 10 | v_sum += ele 11 | print(v_sum-v_max, v_sum-v_min) 12 | 13 | 14 | if __name__ == '__main__': 15 | 16 | arr = list(map(int, input().rstrip().split())) 17 | 18 | miniMaxSum(arr) 19 | -------------------------------------------------------------------------------- /collections.Counter()/collections.Counter().py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | n = int(input()) 3 | mylist = list(map(int, input().split())) 4 | counter = Counter(mylist) 5 | q = int(input()) 6 | total=0 7 | while q>0: 8 | val = map(int, input().split())) 9 | size_qty = counter.get(val[0]) 10 | if size_qty != 0 and size_qty != None: 11 | counter[val[0]]=(counter.get(val[0])-1) 12 | total+=val[1] 13 | q -= 1 14 | print(total) 15 | -------------------------------------------------------------------------------- /String Split and Join/README.md: -------------------------------------------------------------------------------- 1 | ## String Split and Join 2 | > **You are given a string. Split the string on a " " (space) delimiter and join using a - hyphen.
** 3 | 4 | **Input Format**
5 | The first line contains a string consisting of space separated words.
6 | 7 | **Output Format**
8 | Print the formatted string as explained above.
9 | 10 | **Sample Input**
11 | this is a string
12 | 13 | **Sample Output**
14 | this-is-a-string -------------------------------------------------------------------------------- /calendar module/README.md: -------------------------------------------------------------------------------- 1 | ## Calendar Module 2 | > **You are given a date. Your task is to find what the day is on that date.** 3 | 4 | **Input Format**
5 | A single line of input containing the space separated month, day and year, respectively, in MM DD YYYY format. 6 |
7 | 8 | **Output Format**
9 | Output the correct day in capital letters.
10 | 11 | **Sample Input**
12 | 08 05 2015

13 | 14 | **Sample Output**
15 | WEDNESDAY -------------------------------------------------------------------------------- /Lonely Integer/Lonely Integer.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | 5 | def lonelyinteger(a): 6 | for i in a: 7 | if a.count(i) == 1: 8 | return i 9 | 10 | if __name__ == '__main__': 11 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 12 | 13 | n = int(input().strip()) 14 | 15 | a = list(map(int, input().rstrip().split())) 16 | 17 | result = lonelyinteger(a) 18 | 19 | fptr.write(str(result) + '\n') 20 | 21 | fptr.close() 22 | -------------------------------------------------------------------------------- /Convert-a-list-of-characters-into-string.py/Convert-a-list-of-characters-into-string.py: -------------------------------------------------------------------------------- 1 | t = int(input()) # Number of test cases 2 | while t > 0: 3 | input() # satisfying input format 4 | val = list(map(str, input().split())) # Input Multiple characters in one line 5 | res = '' 6 | for i in range(len(val)): # for loop to create string from list of characters 7 | res = res + str(val[i]) 8 | print(res) # Printing the resultant string 9 | t -= 1 10 | -------------------------------------------------------------------------------- /First and last occurrence of x/First-and-last-occurrence-of-x.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | while t>0: 3 | input() 4 | arr = list(map(int,input().split())) 5 | x = int(input()) 6 | first,last = -1,-1 7 | for i in range(len(arr)): 8 | if arr[i] == x: 9 | if first == -1: 10 | first,last=i,i 11 | else: 12 | last = i 13 | if first == -1: 14 | print(first) 15 | else: 16 | print(first,last) 17 | t -= 1 -------------------------------------------------------------------------------- /SAP Interview Question/README.md: -------------------------------------------------------------------------------- 1 | ## Shift 0s to end 2 | 3 | > **You are given an array. Your task is to shift all the 0s to the end. 4 | 5 | **Input Format**
6 | Only one line of input containing numbers of array
7 | 8 | **Output Format**
9 | Print the shifted array. 10 | 11 | **Constraints**
12 | Time complexity should be O(n) 13 | Do not use any other array. 14 | 15 | **Sample Input**
16 | 1 3 2 0 5 0 6 0
17 | 18 | **Sample Output**
19 | 1 3 2 5 6 0 0 0 20 | -------------------------------------------------------------------------------- /Longest Common Prefix/Longest Common Prefix.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def longestCommonPrefix(self, strs): 3 | """ 4 | :type strs: List[str] 5 | :rtype: str 6 | """ 7 | prefix="" 8 | if len(strs)==0: return prefix 9 | 10 | for i in range(len(min(strs))): 11 | c=strs[0][i] 12 | if all(a[i]==c for a in strs): 13 | prefix+=c 14 | else: 15 | break 16 | return prefix 17 | -------------------------------------------------------------------------------- /The Minion Game/The_Minion_Game.py: -------------------------------------------------------------------------------- 1 | def minion_game(string): 2 | S=string 3 | n = len(S) 4 | # consonents 5 | stuart = 0 6 | # vowels 7 | kevin = 0 8 | for i in range(n): 9 | if S[i] in ('A', 'E', 'I', 'O', 'U'): 10 | kevin += n - i 11 | else: 12 | stuart += n - i 13 | if kevin > stuart: 14 | print ('Kevin', kevin) 15 | elif stuart > kevin: 16 | print ('Stuart', stuart) 17 | else: 18 | print ('Draw') 19 | -------------------------------------------------------------------------------- /Diagonal Difference/Diagonal_Difference.py: -------------------------------------------------------------------------------- 1 | def diagonalDifference(arr): 2 | v_sum = 0 3 | lcounter = 0 4 | rcounter = len(arr[0])-1 5 | for i in arr: 6 | v_sum = v_sum + i[lcounter] - i[rcounter] 7 | lcounter+=1 8 | rcounter-=1 9 | return abs(v_sum) 10 | 11 | 12 | n = int(input().strip()) 13 | a = [] 14 | for a_i in range(n): 15 | a_t = [int(a_temp) for a_temp in input().strip().split(' ')] 16 | a.append(a_t) 17 | result = diagonalDifference(a) 18 | print(result) 19 | -------------------------------------------------------------------------------- /Counting Sort 1/Time_Conversion.py: -------------------------------------------------------------------------------- 1 | def timeConversion(time): 2 | if time[-2:] == "AM" and time[:2] == "12": #12 at Night 3 | return "00"+time[2:-2] 4 | elif time[-2:] == "AM": 5 | return time[:-2] 6 | elif time[-2:] == "PM" and time[:2] == "12": #12 at Noon 7 | return time[:-2] 8 | else: 9 | return str(int(time[:2]) + 12) + time[2:8] #converting to 24 hrs by adding 12 hrs 10 | 11 | s = input().strip() 12 | print(timeConversion(s)) 13 | 14 | -------------------------------------------------------------------------------- /Time Conversion/Time_Conversion.py: -------------------------------------------------------------------------------- 1 | def timeConversion(time): 2 | if time[-2:] == "AM" and time[:2] == "12": #12 at Night 3 | return "00"+time[2:-2] 4 | elif time[-2:] == "AM": 5 | return time[:-2] 6 | elif time[-2:] == "PM" and time[:2] == "12": #12 at Noon 7 | return time[:-2] 8 | else: 9 | return str(int(time[:2]) + 12) + time[2:8] #converting to 24 hrs by adding 12 hrs 10 | 11 | s = input().strip() 12 | print(timeConversion(s)) 13 | 14 | -------------------------------------------------------------------------------- /Product-of-max-and-min-of-two-arrays.py/Product-of-max--and-min-of-two-arrays.py: -------------------------------------------------------------------------------- 1 | t = int(input()) # Number of test cases 2 | while t > 0: # while loop for test cases 3 | input() # satisfying input format 4 | arr1 = list(map(int, input().split())) # input first array 5 | input() # satisfying input format 6 | arr2 = list(map(int, input().split())) # input second array 7 | # printing result of product of maximum of first array and minimum of second array 8 | print(max(arr1) * min(arr2)) 9 | t -= 1 10 | -------------------------------------------------------------------------------- /String-Validators/String_Validators.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | s = input() 3 | alnum,num,cap,low,al = 'False','False','False','False','False' 4 | for i in s: 5 | if i.isalnum(): 6 | alnum = "True" 7 | if i.isalpha(): 8 | al = "True" 9 | if i.isdigit(): 10 | num = "True" 11 | if i.islower(): 12 | low = "True" 13 | if i.isupper(): 14 | cap = "True" 15 | print(alnum) 16 | print(al) 17 | print(num) 18 | print(low) 19 | print(cap) 20 | -------------------------------------------------------------------------------- /Petr and a calendar/Petr and a calendar.py: -------------------------------------------------------------------------------- 1 | ''' Read input from STDIN. Print your output to STDOUT ''' 2 | #Use input() to read input from STDIN and use print to write your output to STDOUT 3 | 4 | def main(): 5 | m = int(input()) 6 | d = int(input()) 7 | counter, col = 1, 1 8 | days = [0,31,28,31,30,31,30,31,31,30,31,30,31] 9 | 10 | while (counter7): 14 | d = 1 15 | col += 1 16 | print(col) 17 | 18 | main() 19 | 20 | 21 | -------------------------------------------------------------------------------- /Text Wrap/README.md: -------------------------------------------------------------------------------- 1 | ## Text Wrap 2 | > **You are given a string S and width w.
Your task is to wrap the string into a paragraph of width w.
** 3 | 4 | **Input Format**
5 | The first line contains a string, S.
The second line contains the width, w.
6 | 7 | **Output Format**
8 | Print the text wrapped paragraph.
9 | 10 | **Sample Input**
11 | ABCDEFGHIJKLIMNOQRSTUVWXYZ
12 | 4
13 | 14 | **Sample Output**
15 | ABCD
16 | EFGH
17 | IJKL
18 | IMNO
19 | QRST
20 | UVWX
21 | YZ -------------------------------------------------------------------------------- /Count Of 1's/README.md: -------------------------------------------------------------------------------- 1 | ## Count Of 1's 2 | Given a binary array sorted in non-increasing order, count the number of 1's in it. 3 |

4 | 5 | Input Format
You will be given an integer array of size N. 6 |

7 | Constraints
8 | 1 < N < 10^5
0< = A[i]< = 1

9 | Output Format
You need to print the count of 1's.

10 | Sample Input 11 | 12 | 7 13 | 1 14 | 1 15 | 0 16 | 0 17 | 0 18 | 0 19 | 0 20 |
21 | Sample Output 22 | 23 | 2 24 | -------------------------------------------------------------------------------- /Minimum number of jumps/Minimum_number_of_jumps.py: -------------------------------------------------------------------------------- 1 | def minstep(a,a_size): 2 | x=[10000 for _ in range(a_size)] 3 | if(a[0]==0): 4 | return -1 5 | else: 6 | x[0]=0 7 | for i in range(0,a_size): 8 | z=min(i+a[i],a_size-1) 9 | for j in range(i+1,z+1): 10 | if(x[i]+10: 4 | name = input() 5 | marks = float(input()) 6 | big_arr.append([marks,name]) 7 | big_arr = sorted(big_arr) 8 | n-=1 9 | smallest = big_arr[0][0] 10 | 11 | pos=[] 12 | for i in range(len(big_arr)): 13 | if big_arr[i][0]==smallest: 14 | pos.append(i) 15 | 16 | for index in sorted(pos, reverse=True): 17 | del big_arr[index] 18 | 19 | smallest = big_arr[0][0] 20 | 21 | for i in range(len(big_arr)): 22 | if big_arr[i][0]==smallest: 23 | print(big_arr[i][1]) 24 | 25 | 26 | -------------------------------------------------------------------------------- /Point In a Rectangle/Points_in_a_rectangle.py: -------------------------------------------------------------------------------- 1 | t = int(input()) #test cases 2 | while t > 0: 3 | x1,y1,x2,y2 = map(int,input().split()) #input coordinates 4 | maxx=max(x1,x2) 5 | minx=min(x1,x2) 6 | maxy=max(y1,y2) 7 | miny=min(y1,y2) 8 | count=0 9 | cases = int(input()) 10 | while cases > 0: 11 | a,b = map(int,input().split()) 12 | if ((a>minx) and (aminy)): 13 | count += 1 14 | cases -= 1 15 | print(count) 16 | t -= 1 17 | -------------------------------------------------------------------------------- /Swap Case/README.md: -------------------------------------------------------------------------------- 1 | ## You are given a string and your task is to swap cases. In other words, convert all lowercase letters to uppercase letters and vice versa. 2 | 3 | ### For Example: 4 | >**Www.HackerRank.com ? wWW.hACKERrANK.COM** 5 | >**Pythonist 2 ? pYTHONIST 2** 6 | 7 | >**Input Format** 8 | >A single line containing a string 'S'. 9 | 10 | >**Constraints** 11 | >0 < len(S) <= 1000 12 | 13 | >**Output Format** 14 | >Print the modified string S 15 | 16 | >**Sample Input 0** 17 | >HackerRank.com presents 'Pythonist 2" 18 | 19 | >**Sample Output 0** 20 | >hACKERrANK.COM PRESENTS "pYTHONIST 2". -------------------------------------------------------------------------------- /All A's Well/README.md: -------------------------------------------------------------------------------- 1 | ## All A's Well 2 | Given an input string with duplicate characters generate a string which does have all A's together in the front and other characters maintain their respective position. 3 |

4 | 5 | Input Format
You will be given a function with String as argument. 6 |

7 | Constraints
8 | 1 < |S| < 10^6

9 | Output Format
You need to return the string from the given function. 10 |

11 | Sample Input 12 | 13 | ababacada 14 |
15 | Sample Output 16 | 17 | aaaaabbcd 18 | 19 | -------------------------------------------------------------------------------- /pairs_equal_to_k/README.md: -------------------------------------------------------------------------------- 1 | ## Pairs Equal to K 2 | > **Given an integer array and a positive integer k, count all distinct pairs with difference equal to k. ** 3 | 4 | **Input Format**
5 | You will be given an integer array of size N and an integer k.
6 | 7 | **Output Format**
8 | You need to print the count of pairs.
9 | 10 | **Constraints**
11 | 1 < N < 10^5
12 | 1 < A[i] < 10^5
13 | 1 < k < 10^5

14 | 15 | **Sample Input**
16 | 5
17 | 1
18 | 5
19 | 3
20 | 4
21 | 2
22 | 3

23 | 24 | **Sample Output**
25 | 2 -------------------------------------------------------------------------------- /Johny and the integer/README.md: -------------------------------------------------------------------------------- 1 | ## Johny and the integer 2 | Johny likes numbers n and k very much. Now Johny wants to find the smallest integer x greater than n, so it is divisible by the number k.

3 | 4 | Input Format
You will be given a function with two integers n and k as arguments. 5 |

6 | Constraints
7 | 1 <= n, k <=10^9

8 | Output Format
Return the smallest integer x > n, so it is divisible by the number k. 9 |

10 | Sample Input 11 | 12 | 5 13 | 3 14 |
15 | Sample Output 16 | 17 | 6 18 | -------------------------------------------------------------------------------- /Min and Max - Numpy/README.md: -------------------------------------------------------------------------------- 1 | ## Min Max 2 | > **You are given a 2-D array with dimensions N X M. 3 | Your task is to perform the min function over axis 1 and then find the max of that.** 4 | 5 | **Input Format**
6 | The first line of input contains the space separated values of N and M. 7 | The next N lines contains M space separated integers.
8 | 9 | **Output Format**
10 | Compute the min along axis 1 and then print the max of that result.
11 | 12 | **Sample Input**
13 | 4 2
14 | 2 5
15 | 3 7
16 | 1 3
17 | 4 0

18 | 19 | **Sample Output**
20 | 3 -------------------------------------------------------------------------------- /Nth Highest Salary/README.md: -------------------------------------------------------------------------------- 1 | ## Nth Highest Salary 2 | Write a SQL query to get the nth highest salary from the Employee table. 3 |
4 | 5 | ``` 6 | +----+--------+ 7 | | Id | Salary | 8 | +----+--------+ 9 | | 1 | 100 | 10 | | 2 | 200 | 11 | | 3 | 300 | 12 | +----+--------+ 13 | ``` 14 | For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null. 15 | ``` 16 | +------------------------+ 17 | | getNthHighestSalary(2) | 18 | +------------------------+ 19 | | 200 | 20 | +------------------------+ 21 | ``` 22 | -------------------------------------------------------------------------------- /Generate Prime Numbers In A Interval with Python/Generate_Prime_Numbers_In_A_Interval_with_Python.py: -------------------------------------------------------------------------------- 1 | ''' Read input from STDIN. Print your output to STDOUT ''' 2 | #Use input() to read input from STDIN and use print to write your output to STDOUT 3 | 4 | def main(): 5 | n1 = int(input()) 6 | n2 = int(input()) 7 | for i in range(n1, n2): 8 | if i>1: 9 | prime = True 10 | for j in range(2, i): 11 | if i%j == 0: 12 | prime = False 13 | break 14 | if prime: 15 | print(i) 16 | return True 17 | 18 | main() 19 | 20 | -------------------------------------------------------------------------------- /Introduction to set/README.md: -------------------------------------------------------------------------------- 1 | ## Introduction to set 2 | >**Ms. Gabriel Williams is a botany professor at District College. One day, she asked her student Mickey to compute the average of all the plants with distinct heights in her greenhouse.** 3 | 4 | **Input Format**
5 | The first line contains the integer, N, the total number of plants.
6 | The second line contains the N space separated heights of the plants.
7 | 8 | **Output Format**
9 | Output the average height value on a single line. 10 | 11 | **Sample Input**
12 | 10
13 | 161 182 161 154 176 170 167 171 170 174

14 | **Sample Output**
15 | 169.375 16 | -------------------------------------------------------------------------------- /Mutations/README.md: -------------------------------------------------------------------------------- 1 | ## Mutations 2 | > **Read a given string, change the character at a given index and then print the modified string.
** 3 | 4 | **Input Format**
5 | The first line contains a string consisting of space separated words.The first line contains a string, S.
6 | The next line contains an integer i, denoting the index location and a character c separated by a space.
7 | 8 | **Output Format**
9 | Using any of the methods explained above, replace the character at index i with character c.
10 | 11 | **Sample Input**
12 | abracadabra
13 | 5 k
14 | 15 | **Sample Output**
16 | abrackdabra -------------------------------------------------------------------------------- /Tower Breakers/Tower Breakers.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | 5 | 6 | def towerBreakers(n, m): 7 | if m > 1 and n % 2: 8 | return 1 9 | else: 10 | return 2 11 | 12 | if __name__ == '__main__': 13 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 14 | 15 | t = int(input().strip()) 16 | 17 | for t_itr in range(t): 18 | first_multiple_input = input().rstrip().split() 19 | 20 | n = int(first_multiple_input[0]) 21 | 22 | m = int(first_multiple_input[1]) 23 | 24 | result = towerBreakers(n, m) 25 | 26 | fptr.write(str(result) + '\n') 27 | 28 | fptr.close() 29 | -------------------------------------------------------------------------------- /Plus Minus/Plus Minus.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | 4 | def plusMinus(arr): 5 | n = len(arr) 6 | positives, negetives, zeroes = 0, 0, 0 7 | for ele in arr: 8 | if ele > 0: 9 | positives += 1 10 | elif ele < 0: 11 | negetives += 1 12 | else: 13 | zeroes += 1 14 | print("{:.6f}".format(round(positives/n, 6))) 15 | print("{:.6f}".format(round(negetives/n, 6))) 16 | print("{:.6f}".format(round(zeroes/n, 6))) 17 | 18 | 19 | if __name__ == '__main__': 20 | n = int(input().strip()) 21 | 22 | arr = list(map(int, input().rstrip().split())) 23 | 24 | plusMinus(arr) 25 | -------------------------------------------------------------------------------- /Second Highest Salary/README.md: -------------------------------------------------------------------------------- 1 | ## Second Highest Salary 2 | Write a SQL query to get the second highest salary from the Employee table. 3 |
4 | ``` 5 | +----+--------+ 6 | | Id | Salary | 7 | +----+--------+ 8 | | 1 | 100 | 9 | | 2 | 200 | 10 | | 3 | 300 | 11 | +----+--------+ 12 | ``` 13 | For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null. 14 | 15 | ``` 16 | +---------------------+ 17 | | SecondHighestSalary | 18 | +---------------------+ 19 | | 200 | 20 | +---------------------+ 21 | ``` 22 | -------------------------------------------------------------------------------- /itertools.combinations()/README.md: -------------------------------------------------------------------------------- 1 | ## itertools.combinations() 2 | > **You are given a string S.
3 | Your task is to print all possible combinations, up to size k, of the string in
4 | lexicographic sorted order.**
5 | 6 | **Input Format**
7 | A single line containing the string S and integer value k separated by a space.
8 | 9 | **Output Format**
10 | Print the different combinations of string S on separate lines.
11 | 12 | **Sample Input**
13 | HACK 2
14 | 15 | **Sample Output**
16 | A
17 | C
18 | H
19 | K
20 | AC
21 | AH
22 | AK
23 | CH
24 | CK
25 | HK -------------------------------------------------------------------------------- /kth_smallest_element/README.md: -------------------------------------------------------------------------------- 1 | ## Kth Smallest Element 2 | > **Given an array and a number k where k is smaller than size of array, we need to find the k th smallest element in the given array.** 3 | 4 | **Input Format**
5 | You will be given an array of integers and integer k.
6 | 7 | **Output Format**
8 | You need to print the K th Smallest Element.
9 | 10 | **Constraints**
11 | 1 < n < 10^5
12 | 1 < a[i] < 10^5
13 | 1 < k < 10^5

14 | 15 | **Sample Input**
16 | 6
17 | 7
18 | 10
19 | 4
20 | 3
21 | 20
22 | 15
23 | 3

24 | 25 | **Sample Output**
26 | 7 -------------------------------------------------------------------------------- /Time Conversion/README.md: -------------------------------------------------------------------------------- 1 | ## Time Conversion 2 | 3 | Given a time in 12-hour AM/PM format, convert it to military (24-hour) time. 4 | 5 | **Note : **Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock. 6 | 7 | **Input Format** 8 | A single string containing a time in 12-hour clock format (i.e.:hh:mm:ssAM or hh:mm:ssPM), where 01 <= _hh_ <= 12 and 00<= _mm_, _ss_<= 59. 9 | 10 | **Output Format** 11 | Convert and print the given time in 24-hour format, where 00 <= _hh_ <= 23. 12 | 13 | >**Sample Input** 14 | >07:05:45PM 15 | 16 | >**Sample Output** 17 | >19:05:45 -------------------------------------------------------------------------------- /itertools.permutations()/README.md: -------------------------------------------------------------------------------- 1 | ## itertools.permutations() 2 | > **You are given a string S.
3 | Your task is to print all possible permutations of size k of the string in lexicographic sorted order.
** 4 | 5 | **Input Format**
6 | A single line containing the space separated string S and the integer value k.
7 | 8 | **Output Format**
9 | Print the permutations of the string S on separate lines.
10 | 11 | **Sample Input**
12 | HACK 2
13 | 14 | **Sample Output**
15 | AC
16 | AH
17 | AK
18 | CA
19 | CH
20 | CK
21 | HA
22 | HC
23 | HK
24 | KA
25 | KC
26 | KH -------------------------------------------------------------------------------- /Remove vowels from string/README.md: -------------------------------------------------------------------------------- 1 | ## Remove Vowels From String 2 | > **Given a string, remove the vowels from the string and print the string without vowels.** 3 | 4 | **Input Format**
5 | The first line consists of an integer T i.e number of test cases. The first and only line of each test case consists of a string s. 6 | 7 | **Output Format**
8 | Print the required output. 9 | 10 | **Constraints**
11 | 1<=T<=100
12 | 1<=|Length of string|<=1000
13 | 14 | **Sample Input**
15 | 2
16 | welcome to geeksforgeeks
17 | what is your name ?
18 | 19 | **Sample Output**
20 | wlcm t gksfrgks
21 | wht s yr nm ? 22 | -------------------------------------------------------------------------------- /Zig Zag Sequence/Zig Zag Sequence.py: -------------------------------------------------------------------------------- 1 | def findZigZagSequence(a, n): 2 | a.sort() 3 | mid = int((n + 1)/2) 4 | a[mid], a[n-1] = a[n-1], a[mid] 5 | 6 | st = mid + 1 7 | ed = n - 1 8 | while(st <= ed): 9 | a[st], a[ed] = a[ed], a[st] 10 | st = st + 1 11 | ed = ed + 1 12 | 13 | for i in range (n): 14 | if i == n-1: 15 | print(a[i]) 16 | else: 17 | print(a[i], end = ' ') 18 | return 19 | 20 | test_cases = int(input()) 21 | for cs in range (test_cases): 22 | n = int(input()) 23 | a = list(map(int, input().split())) 24 | findZigZagSequence(a, n) 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Longest Common Prefix/README.md: -------------------------------------------------------------------------------- 1 | ## Longest Common Prefix 2 | > **Write a function to find the longest common prefix string amongst an array of strings.
3 | If there is no common prefix, return an empty string "". 4 |
5 | 6 | **Example 1:**
7 | Input: strs = ["flower","flow","flight"]
8 | Output: "fl"

9 | 10 | **Example 2:**
11 | Input: strs = ["dog","racecar","car"]
12 | Output: ""
13 | Explanation: There is no common prefix among the input strings.

14 | 15 | **Constraints**
16 | 0 <= strs.length <= 200
17 | 0 <= strs[i].length <= 200
18 | strs[i] consists of only lower-case English letters.

19 | -------------------------------------------------------------------------------- /Complicated Sum/README.md: -------------------------------------------------------------------------------- 1 | ## Complicated Sum 2 | > **Considering the code below, Calculate the final value of sum if n is provided as input.** 3 | 4 | M = 1000000007
5 | long long int sum = 0;
6 | for (long long int i = 1;i<=n;i++) {
7 | for (long long int j = i;j<=n;j++) {
8 | long long int iTemp = i%M;
9 | long long int jTemp = j%M;
10 | sum = (sum + (iTemp*jTemp) % M ) % M; }}
11 | 12 | **Input**
13 | The first Line of input consist of T - the number of test cases. T lines follows - each consisting of single integer n
14 | 15 | **Output**
16 | Output in T lines the answer to each test case. 17 | -------------------------------------------------------------------------------- /Find the Runner-Up Score!/README.md: -------------------------------------------------------------------------------- 1 | ## Find the Runner Up! 2 | > Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.

3 | 4 | **Input Format**
5 | The first line contains n. The second line contains an array A[] of n integers each separated by a space. 6 |

7 | 8 | **Output Format**
9 | Print the runner-up score.

10 | 11 | **Constraints**
12 | 2 <= n <= 10
13 | -100 <= A[i] <= 100 14 |

15 | 16 | **Sample Input**
17 | 5
18 | 2 3 6 6 5

19 | 20 | **Sample Output**
21 | 5 -------------------------------------------------------------------------------- /Point In a Rectangle/README.md: -------------------------------------------------------------------------------- 1 | ## Points in a rectangle 2 | 3 | **Given a rectangle with sides parallel to X and Y axis and N points in X-Y co-ordinates, print the total number of points that lie strictly inside the rectangle.** 4 | 5 | **Input**
6 | The first line consist of a single integer T - the number of test cases.
7 | For every test case the input is as follows -
8 | First Line consists of x1 y1 x2 y2 - (x1, y1) and (x2, y2) are opposite corner points of the rectangle.
9 | Next line consists of N
10 | N lines follows - each line containing - X Y coordinate of the point.
11 | 12 | **Output**
13 | Output in T lines, the answer to each test case. 14 | 15 | -------------------------------------------------------------------------------- /Tuples/README.md: -------------------------------------------------------------------------------- 1 | ## Tuples 2 | > **Given an integer, n, and n space-separated integers as input, create a tuple, t, of those n integers. Then compute and print the result of hash(t). 3 |
Note: hash() is one of the functions in the __builtins__ module, so it need not be imported.** 4 | 5 | **Input Format**
6 | The first line contains an integer, n, denoting the number of elements in the tuple.
7 | The second line contains n space-separated integers describing the elements in tuple t.
8 | 9 | **Output Format**
10 | Print the result of hash(t).
11 | 12 | **Sample Input**
13 | 2
14 | 1 2
15 | 16 | **Sample Output**
17 | 3713081631934410656 18 | -------------------------------------------------------------------------------- /Diagonal Difference/README.md: -------------------------------------------------------------------------------- 1 | ## Diagonal Difference 2 | > **Find the difference between sum of diagonals of square matrix** 3 |
4 | Given a square matrix of size , calculate the absolute difference between the sums of its diagonals. 5 | 6 | **Input Format**
7 | The first line contains a single integer, . The next lines denote the matrix's rows, with each line containing space-separated integers describing the columns.
8 | 9 | **Output Format**
10 | Print the absolute difference between the two sums of the matrix's diagonals as a single integer.
11 | 12 | **Sample Input**
13 | 3
14 | 11 2 4
15 | 4 5 6
16 | 10 8 -12
17 | 18 | **Sample Output**
19 | 15
-------------------------------------------------------------------------------- /What's Your Name/README.md: -------------------------------------------------------------------------------- 1 | ## What's Your Name 2 | 3 | > You are given the firstname and lastname of a person on two different lines. Your task is to read them and print the following: 4 |
Hello firstname lastname! You just delved into python.

5 | 6 | **Input Format**
7 | The first line contains the first name, and the second line contains the last name.

8 | 9 | **Output Format**
10 | Print the output as mentioned above.

11 | 12 | **Constraints**
13 | The length of the first and last name = 10.

14 | 15 | **Sample Input**
16 | Ross
17 | Taylor

18 | 19 | **Sample Output**
20 | Hello Ross Taylor! You just delved into python. -------------------------------------------------------------------------------- /itertools.combinations_with_replacement()/README.md: -------------------------------------------------------------------------------- 1 | ## itertools.combinations_with_replacement() 2 | > **You are given a string S.
3 | Your task is to print all possible size k replacement combinations of the string in 4 | lexicographic sorted order.**
5 | 6 | **Input Format**
7 | A single line containing the string S and integer value k separated by a space.
8 | 9 | **Output Format**
10 | Print the combinations with their replacements of string S on separate lines.
11 | 12 | **Sample Input**
13 | HACK 2
14 | 15 | **Sample Output**
16 | AA
17 | AC
18 | AH
19 | AK
20 | CC
21 | CH
22 | CK
23 | HH
24 | HK
25 | KK
26 | HK -------------------------------------------------------------------------------- /First repeating element/README.md: -------------------------------------------------------------------------------- 1 | ## First repeating element 2 | Given an array of integers, find the first repeating element in it. We need to find the element that occurs more than once and whose index of first occurrence is smallest.

3 | 4 | Input Format
You will be given a function with an integer array as arguments. 5 |

6 | Constraints
7 | 1 < N < 10^5
1 < a[i] < 10^5 8 |

9 | Output Format
You need to return the first repeating element from the function. 10 |

11 | Sample Input 12 | 13 | 7 14 | 10 15 | 5 16 | 3 17 | 4 18 | 3 19 | 5 20 | 6 21 |
22 | Sample Output 23 | 24 | 5 25 | -------------------------------------------------------------------------------- /itertools.product()/README.md: -------------------------------------------------------------------------------- 1 | ## itertools.product() 2 | > You are given a two lists A and B. Your task is to compute their cartesian product A X B.

3 | 4 | **Input Format**
5 | The first line contains the space separated elements of list A.
6 | The second line contains the space separated elements of list B.
7 | 8 | Both lists have no duplicate integer elements.

9 | 10 | **Output Format**
11 | Output the space separated tuples of the cartesian product.

12 | 13 | **Constraints**
14 | 0 < A < 30
15 | 0 < B < 30 16 |

17 | 18 | **Sample Input**
19 | 1 2
20 | 3 4

21 | 22 | **Sample Output**
23 | (1, 3) (1, 4) (2, 3) (2, 4) -------------------------------------------------------------------------------- /Recursive Digit Sum/Recursive Digit Sum.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | 5 | 6 | def superDigit_rec(n): 7 | if int(n)<10: 8 | return n 9 | else: 10 | s = sum([int(x) for x in str(n)]) 11 | return superDigit_rec(s) 12 | 13 | 14 | def superDigit(n, k): 15 | l = int(superDigit_rec(n)*k) 16 | return superDigit_rec(l) 17 | 18 | 19 | if __name__ == '__main__': 20 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 21 | 22 | first_multiple_input = input().rstrip().split() 23 | 24 | n = first_multiple_input[0] 25 | 26 | k = int(first_multiple_input[1]) 27 | 28 | result = superDigit(n, k) 29 | 30 | fptr.write(str(result) + '\n') 31 | 32 | fptr.close() 33 | -------------------------------------------------------------------------------- /Count_Bigger/README.md: -------------------------------------------------------------------------------- 1 | ## Count Bigger 2 | > **You will be given an array and a number and you need to count how many numbers are bigger than this number and print this count to the stdout. ** 3 | 4 | **Input Format**
5 | You will be taking a number as an input using stdin which tells about the length of the array. On another line, array elements should be there with single space between them. Another line will have the number for which you have to do the comparison. 6 |
7 | 8 | **Output Format**
9 | Print the count to the stdout.
10 | 11 | **Constraints**
12 | 1 <= L <= 1000
13 | 1 <= Ai <= 1000
14 | 15 | **Sample Input**
16 | 5
17 | 10 11 12 44 66
18 | 22

19 | 20 | **Sample Output**
21 | 2 -------------------------------------------------------------------------------- /Convert-a-list-of-characters-into-string.py/README.md: -------------------------------------------------------------------------------- 1 | ## Convert a list of characters into string 2 | 3 | > **Given a list of characters, merge all of them into a string.** 4 | 5 | **Input**
6 | First line of the input contains an integer T, denoting the number of testcases. Then T test case follows. Each testcase contains two lines:-
7 | The number of characters in the array N.
8 | The array of characters separated by space
9 | 10 | **Output**
11 | For each testcase, print the character array converted into a string.
12 | 13 | **Sample Input**
14 | 2
15 | 13
16 | g e e k s f o r g e e k s
17 | 11
18 | p r o g r a m m i n g
19 | 20 | **Sample Output**
21 | geeksforgeeks
22 | programming
23 | -------------------------------------------------------------------------------- /Playing with matches/solution_code.py: -------------------------------------------------------------------------------- 1 | 2 | def noofmatch(n): 3 | if n==0: 4 | return 6 5 | elif n==1: 6 | return 2 7 | elif n==2: 8 | return 5 9 | elif n==3: 10 | return 5 11 | elif n==4: 12 | return 4 13 | elif n==5: 14 | return 5 15 | elif n==6: 16 | return 6 17 | elif n==7: 18 | return 3 19 | elif n==8: 20 | return 7 21 | elif n==9: 22 | return 6 23 | 24 | for i in range(int(input())): 25 | a, b = map(int, input().split()) 26 | new = list(map(int, str(a+b))) 27 | 28 | output = 0 29 | for x in range(len(new)): 30 | output = output + noofmatch(new[x]) 31 | 32 | print (output) 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Search_Small_Ones/README.md: -------------------------------------------------------------------------------- 1 | ## Count Bigger 2 | > **You will be given an array and you need to find the smallest and second smallest numbers and add them.
The length of the array should not be less than 2.
Expected Time Complexity - O(n) ** 3 | 4 | **Input Format**
5 | You will be taking a number as an input stdin which tells about the length of the array. On another line, array elements should be there with single space between them.
6 | 7 | **Output Format**
8 | You need to print the addition of smallest and second smallest element to the stdout.
9 | 10 | **Constraints**
11 | 2 <= L <= 1000
12 | 1 <= Ai <= 1000
13 | 14 | **Sample Input**
15 | 7
16 | 48 77 7 11 49 99 78

17 | 18 | **Sample Output**
19 | 18 -------------------------------------------------------------------------------- /Mod Divmod/README.md: -------------------------------------------------------------------------------- 1 | ## Mod Divmod 2 | > **Read in two integers, a and b, and print three lines. 3 | The first line is the integer division a//b (While using Python2 remember to import division from __future__). 4 | The second line is the result of the modulo operator: a%b. 5 | The third line prints the divmod of a and b.** 6 | 7 | **Input Format**
8 | The first line contains the first integer, a, and the second line contains the second integer, b. 9 | 10 |
11 | 12 | **Output Format**
13 | Print the result as described above.
14 | 15 | **Constraints**
16 | Print the list in lexicographic increasing order.
17 | 18 | **Sample Input**
19 | 177
20 | 10
21 | 22 | **Sample Output**
23 | 17
24 | 7
25 | (17, 7) 26 | -------------------------------------------------------------------------------- /Remove all characters other than alphabets/README.md: -------------------------------------------------------------------------------- 1 | ## Remove all characters other than alphabets 2 | 3 | > **Given a string consisting of alphabets and others characters, the task is to remove all the characters other than alphabets and print the string so formed.**
4 | 5 | **Input**
6 | The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains a string S.
7 | 8 | **Output**
9 | For each test case, print the remaining string in new line, If no character remains in the string print "-1".
10 | 11 | **Sample Input**
12 | 2
13 | $Gee*k;s..fo, r'Ge^eks?
14 | P&ra+$BHa;;t*ku, ma$r@@s#in}gh
15 | 16 | **Sample Output**
17 | GeeksforGeeks
18 | PraBHatkumarsingh
-------------------------------------------------------------------------------- /All A's Well/All A's Well.py: -------------------------------------------------------------------------------- 1 | # Python3 code to print all possible subarrays 2 | # for given array using recursion 3 | 4 | # Recursive function to print all possible subarrays 5 | # for given array 6 | def printSubArrays(arr, start, end): 7 | 8 | # Stop if we have reached the end of the array 9 | if end == len(arr): 10 | return 11 | 12 | # Increment the end point and start from 0 13 | elif start > end: 14 | return printSubArrays(arr, 0, end + 1) 15 | 16 | # Print the subarray and increment the starting 17 | # point 18 | else: 19 | print(arr[start:end + 1]) 20 | return printSubArrays(arr, start + 1, end) 21 | 22 | # Driver code 23 | arr = [1, 2, 3] 24 | printSubArrays(arr, 0, 0) -------------------------------------------------------------------------------- /Lists/Lists.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | N = int(input()) 3 | lis = [] 4 | for i in range(N): 5 | command = list(map(str,input().split())) 6 | if command[0]=='insert': 7 | lis.insert(int(command[1]),int(command[2])) 8 | elif command[0]=='print': 9 | print(lis) 10 | elif command[0]=='append': 11 | lis.append(int(command[1])) 12 | elif command[0]=='sort': 13 | lis = sorted(lis) 14 | elif command[0]=='remove': 15 | lis.remove(int(command[1])) 16 | elif command[0]=='pop': 17 | lis.pop() 18 | elif command[0]=='reverse': 19 | lis.reverse() 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Geek and his Tricky Series/README.md: -------------------------------------------------------------------------------- 1 | ## Geek and his Tricky Series 2 | > **Given a series with starting 6 members of the series. Given an integer n find the nth term of this series.
3 | Series: 7, 15, 32, 67, 138, 281, ............** 4 | 5 | **Input Format**
6 | First line of the input contains an integer T, denoting the number of test cases. Then T test case follows. The only line of the test case contains an integer N.

7 | 8 | **Output Format**
9 | For each test case print the nth term of the series in a new line.Print the answer modulo 10^9 +7.

10 | 11 | **Constraints**
12 | 1<=T<=100
13 | 1<=N<=120

14 | 15 | **Sample Input**
16 | 3
17 | 2
18 | 9
19 | 16
20 | 21 | **Sample Output**
22 | 2294
23 | 294895 -------------------------------------------------------------------------------- /Caesar Cipher/Caesar Cipher.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import os 4 | 5 | def caesarCipher(s, k): 6 | encr_string = '' 7 | for letter in s: 8 | if letter.isalpha(): 9 | uni = ord(letter) 10 | base = 97 if letter.islower() else 65 11 | balance = (uni + k - base) % 26 12 | encr_string += chr(balance + base) # adding base to bring in alphabet range 13 | else: 14 | encr_string += letter 15 | return encr_string 16 | 17 | if __name__ == '__main__': 18 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 19 | 20 | n = int(input().strip()) 21 | 22 | s = input() 23 | 24 | k = int(input().strip()) 25 | 26 | result = caesarCipher(s, k) 27 | 28 | fptr.write(result + '\n') 29 | 30 | fptr.close() 31 | -------------------------------------------------------------------------------- /Generate Prime Numbers In A Interval with Python/README.md: -------------------------------------------------------------------------------- 1 | ## Generate Prime Numbers In A Interval 2 | You just need to take two number as input from stdin and you need to find prime numbers between those two numbers and print them. 3 | 4 | **Input Format**
5 | You will be taking two numbers as an input from stdin one on each line respectively.
6 | 7 | 8 | **Constraints**
9 | 1 <= N <= 10000
10 | 11 | **Output Format**
12 | You need to print the prime numbers one on each line to the stdout. 13 | 14 | **Sample Input**
15 | 900
16 | 1000
17 | 18 | **Sample Output**
19 | 907
20 | 911
21 | 919
22 | 929
23 | 937
24 | 941
25 | 947
26 | 953
27 | 967
28 | 971
29 | 977
30 | 983
31 | 991
32 | 997 33 | -------------------------------------------------------------------------------- /Jumping on the Clouds/Jumping_on_the_Clouds.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the jumpingOnClouds function below. 10 | def jumpingOnClouds(c): 11 | n=len(c) 12 | jumps, pos = 0, 0 13 | while pos < n - 1: 14 | if pos + 2 >= n or c[pos + 2] == 1: 15 | pos = pos + 1 16 | jumps = jumps + 1 17 | else: 18 | pos = pos + 2 19 | jumps = jumps + 1 20 | return jumps 21 | 22 | if __name__ == '__main__': 23 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 24 | 25 | n = int(input()) 26 | 27 | c = list(map(int, input().rstrip().split())) 28 | 29 | result = jumpingOnClouds(c) 30 | 31 | fptr.write(str(result) + '\n') 32 | 33 | fptr.close() 34 | -------------------------------------------------------------------------------- /Text Alignment/Text_Alignment.py: -------------------------------------------------------------------------------- 1 | #Replace all ______ with rjust, ljust or center. 2 | 3 | thickness = int(input()) #This must be an odd number 4 | c = 'H' 5 | 6 | for i in range(thickness): 7 | print((c*i).rjust(thickness-1)+c+(c*i).ljust(thickness-1)) 8 | 9 | #Top Pillars 10 | for i in range(thickness+1): 11 | print((c*thickness).center(thickness*2)+(c*thickness).center(thickness*6)) 12 | 13 | #Middle Belt 14 | for i in range((thickness+1)//2): 15 | print((c*thickness*5).center(thickness*6)) 16 | 17 | #Bottom Pillars 18 | for i in range(thickness+1): 19 | print((c*thickness).center(thickness*2)+(c*thickness).center(thickness*6)) 20 | 21 | #Bottom Cone 22 | for i in range(thickness): 23 | print(((c*(thickness-i-1)).rjust(thickness)+c+(c*(thickness-i-1)).ljust(thickness)).rjust(thickness*6)) 24 | -------------------------------------------------------------------------------- /List_comprehensions/README.md: -------------------------------------------------------------------------------- 1 | ## List Comprehensions 2 | > **Let's learn about list comprehensions! You are given three integers X,Y and Z representing the dimensions of a cuboid along with an integer N. You have to print a list of all possible coordinates given by (i,j,k) on a 3D grid where the sum of i+j+k is not equal to N. Here , 0<=i<=X; 0<=j<=Y; 0<=k<=Z ** 3 | 4 | **Input Format**
5 | Four integers X,Y,Z and N each on four separate lines, respectively.
6 | 7 | **Output Format**
8 | For each test case, print the required answer in a new line.
9 | 10 | **Constraints**
11 | Print the list in lexicographic increasing order.
12 | 13 | **Sample Input**
14 | 1
15 | 1
16 | 1
17 | 2
18 | 19 | **Sample Output**
20 | [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]] 21 | -------------------------------------------------------------------------------- /Product is even or odd/README.md: -------------------------------------------------------------------------------- 1 | ## Product is even or odd 2 | > **You are given two numbers N1 and N2. You need to find out if the product of these numbers generate an even number or an odd number.** 3 | 4 | **Input Format**
5 | The first line of the input contains a single integer T, denoting the number of test cases. Then T test cases follow. Each test case contains two lines of input:-
6 | The first line contains N1
7 | The second line contains N2
8 | 9 | **Output Format**
10 | For each test case, Print 1 or 0 according to the result. if even print 1 else print 0.
11 | 12 | **Constraints**
13 | 1<=T<=120
14 | 1<=N1,N2<=1099
15 | 16 | **Sample Input**
17 | 3
18 | 5
19 | 8
20 | 2
21 | 98
22 | 773
23 | 13
24 | 25 | **Sample Output** 26 | 1 27 | 1 28 | 0 -------------------------------------------------------------------------------- /Find_a_string/README.md: -------------------------------------------------------------------------------- 1 | ## Find A String 2 | > In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.
3 | NOTE: String letters are case-sensitive.

4 | 5 | **Input Format**
6 | The first line of input contains the original string. The next line contains the substring.

7 | 8 | **Output Format**
9 | Output the integer number indicating the total number of occurrences of the substring in the original string.

10 | 11 | **Constraints**
12 | 1 <= len(string) <= 200
13 | Each character in the string is an ascii character. 14 |

15 | 16 | **Sample Input**
17 | ABCDCDC
18 | CDC

19 | 20 | **Sample Output**
21 | 2 -------------------------------------------------------------------------------- /Multiply left and right array sum/README.md: -------------------------------------------------------------------------------- 1 | ## Multiply left and right array sum 2 | > **Pitsy needs help in the given task by her teacher. The task is to divide a array into two sub array (left and right) containing n/2 elements each and do the sum of the subarrays and then multiply both the subarrays.** 3 | 4 | **Input Format**
5 | First line consists of T test cases. Only line of every test case consists of an integer N.
6 | 7 | **Output Format**
8 | Print the answer by dividing array into left and right array and add their elements individually and then multiply both the array
9 | 10 | **Constraints**
11 | 0<=T<=100
12 | 0< Number of Digits in Integer <=1000000
13 | 14 | **Sample Input**
15 | 2
16 | 4
17 | 1 2 3 4
18 | 3
19 | 4 5 6
20 | 21 | **Sample Output**
22 | 21
23 | 44
-------------------------------------------------------------------------------- /Alphabet Rangoli/README.md: -------------------------------------------------------------------------------- 1 | ## Alphabet Rangoli 2 | 3 | > **You are given an integer, N. Your task is to print an alphabet rangoli of size N. (Rangoli is a form of Indian folk art based on creation of patterns.)
The center of the rangoli has the first alphabet letter a, and the boundary has the Nth alphabet letter (in alphabetical order).
** 4 | 5 | **Input Format**
6 | Only one line of input containing N, the size of the rangoli.
7 | 8 | **Output Format**
9 | Print the alphabet rangoli in the format explained above.
10 | 11 | **Sample Input**
12 | 5
13 | 14 | **Sample Output**
15 | --------e--------
16 | ------e-d-e------
17 | ----e-d-c-d-e----
18 | --e-d-c-b-c-d-e--
19 | e-d-c-b-a-b-c-d-e
20 | --e-d-c-b-c-d-e--
21 | ----e-d-c-d-e----
22 | ------e-d-e------
23 | --------e-------- -------------------------------------------------------------------------------- /Lonely Integer/README.md: -------------------------------------------------------------------------------- 1 | ## Lonely Integer 2 | 3 | Given an array of integers, where all elements but one occur twice, find the unique element. 4 | 5 | **Example** 6 | a = [1, 2, 3, 4, 3, 2, 1] 7 | The unique element is 4. 8 | 9 | **Function Description** 10 | 11 | Complete the lonelyinteger function in the editor below. 12 | 13 | lonelyinteger has the following parameter(s): 14 | 15 | - int a[n]: an array of integers 16 | 17 | **Returns** 18 | - int: the element that occurs only once 19 | **Input Format** 20 | 21 | The first line contains a single integer, , the number of integers in the array. 22 | The second line contains n space-separated integers that describe the values in a. 23 | 24 | **Constraints** 25 | - 1 <= n < 100 26 | - It is guaranteed that n is an odd number and that there is one unique element. 27 | - 0 <= a[i] <= 100, where 0 <= i < n. 28 | -------------------------------------------------------------------------------- /Sock Merchant/Sock Merchant.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the sockMerchant function below. 10 | def sockMerchant(n, ar): 11 | pairs, freq = 0, {} 12 | for item in ar: # create dictionary to count frequency of each sock color 13 | if (item in freq): 14 | freq[item] += 1 15 | else: 16 | freq[item] = 1 17 | 18 | for key, value in freq.items(): #count pair of socks 19 | pairs += value//2 20 | return pairs 21 | 22 | 23 | if __name__ == '__main__': 24 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 25 | 26 | n = int(input()) 27 | 28 | ar = list(map(int, input().rstrip().split())) 29 | 30 | result = sockMerchant(n, ar) 31 | 32 | fptr.write(str(result) + '\n') 33 | 34 | fptr.close() 35 | -------------------------------------------------------------------------------- /Micro and Array/README.md: -------------------------------------------------------------------------------- 1 | # Micro and Array Update 2 | ## Micro purchased an array A having N integer values. After playing it for a while, he got bored of it and decided to update value of its element. In one second he can increase value of each array element by 1. He wants each array element's value to become greater than or equal to K. Please help Micro to find out the minimum amount of time it will take, for him to do so. 3 | 4 | >**Input**
5 | >First line consists of a single integer, T, denoting the number of test cases.
6 | >First line of each test case consists of two space separated integers denoting N and K.
7 | >Second line of each test case consists of N space separated integers denoting the array A.
8 | 9 | >**Output**
10 | >For each test case, print the minimum time in which all array elements will become greater than or equal to K. Print a new line after each test case. -------------------------------------------------------------------------------- /2D Array - DS/2D Array - DS.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the hourglassSum function below. 10 | def hourglassSum(arr): 11 | total = 0 12 | max_total = -100 13 | 14 | for i in range(len(arr)): 15 | for j in range(len(arr[i])): 16 | if (j+2 < 6) and (i+2 < 6): 17 | total = arr[i][j] + arr[i][j+1] + arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2] 18 | if max_total < total: 19 | max_total = total 20 | return max_total 21 | 22 | 23 | if __name__ == '__main__': 24 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 25 | 26 | arr = [] 27 | 28 | for _ in xrange(6): 29 | arr.append(map(int, raw_input().rstrip().split())) 30 | 31 | result = hourglassSum(arr) 32 | 33 | fptr.write(str(result) + '\n') 34 | 35 | fptr.close() 36 | -------------------------------------------------------------------------------- /Grid Challenge/Grid Challenge.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | import os 3 | 4 | 5 | def gridChallenge(grid): 6 | grid = [''.join(sorted(row)) for row in grid] 7 | index_counter = 0 8 | while index_counter < len(grid[0]): 9 | last_val = 'a' 10 | for row in grid: 11 | if last_val > row[index_counter]: 12 | return 'NO' 13 | last_val = row[index_counter] 14 | index_counter += 1 15 | return 'YES' 16 | 17 | 18 | if __name__ == '__main__': 19 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 20 | 21 | t = int(input().strip()) 22 | 23 | for t_itr in range(t): 24 | n = int(input().strip()) 25 | 26 | grid = [] 27 | 28 | for _ in range(n): 29 | grid_item = input() 30 | grid.append(grid_item) 31 | 32 | result = gridChallenge(grid) 33 | 34 | fptr.write(result + '\n') 35 | 36 | fptr.close() 37 | -------------------------------------------------------------------------------- /Combine Two Tables/README.md: -------------------------------------------------------------------------------- 1 | ## Combine Two Tables 2 | **Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
** 3 | ``` 4 | FirstName, LastName, City, State 5 | ``` 6 |
7 | **Table:** Person
8 | ``` 9 | +-------------+---------+ 10 | | Column Name | Type | 11 | +-------------+---------+ 12 | | PersonId | int | 13 | | FirstName | varchar | 14 | | LastName | varchar | 15 | +-------------+---------+ 16 | PersonId is the primary key column for this table. 17 | ``` 18 | 19 | **Table:** Address
20 | ``` 21 | +-------------+---------+ 22 | | Column Name | Type | 23 | +-------------+---------+ 24 | | AddressId | int | 25 | | PersonId | int | 26 | | City | varchar | 27 | | State | varchar | 28 | +-------------+---------+ 29 | AddressId is the primary key column for this table. 30 | ``` 31 | -------------------------------------------------------------------------------- /Minimum number of jumps/README.md: -------------------------------------------------------------------------------- 1 | ## Minimum Number of Jumps 2 | 3 | **Given an array of integers where each element represents the max number of steps that can be made forward from that element. 4 | Write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). 5 | If an element is 0, then cannot move through that element.** 6 | 7 | **Input**
8 | The first line contains an integer T, depicting total number of test cases.
9 | Then following T lines contains a number n denoting the size of the array. Next line contains the sequence of integers a1,?a2,?...,?an. 10 | 11 | **Output**
12 | Each seperate line showing the minimum number of jumps. If answer is not possible print -1.
13 | 14 | **Constraints**
15 | 1 = T = 40
16 | 1 = N = 100
17 | 0<=a[N]<=100
18 | 19 | >Example 20 | >Input: 21 | >1 22 | >11 23 | >1 3 5 8 9 2 6 7 6 8 9 24 | 25 | >Output 26 | >3 27 | -------------------------------------------------------------------------------- /The Great Kian/README.md: -------------------------------------------------------------------------------- 1 | ## The Great Kian 2 | 3 | **The great Kian is looking for a smart prime minister. He's looking for a guy who can solve the OLP (Old Legendary Problem). 4 | OLP is an old problem (obviously) that no one was able to solve it yet (like P=NP). 5 | But still, you want to be the prime minister really bad. So here's the problem: 6 | Given the sequence a1, a2, ..., an find the three values a1 + a4 + a7 + ..., a2 + a5 + a8 + ... and a3 + a6 + a9 + ... (these summations go on while the indexes are valid).** 7 | 8 | **Input** 9 | The first line of input contains a single integer n (1 = n = 105). 10 | The second line contains n integers a1, a2, ..., an separated by space (1 = ai = 109). 11 | 12 | **Output** 13 | Print three values in one line (the answers). 14 | | SAMPLE INPUT | SAMPLE OUTPUT | 15 | | 5 | 5 7 3 | 16 | | 1 2 3 4 5 | | 17 | 18 | -------------------------------------------------------------------------------- /ishaans-weight/README.md: -------------------------------------------------------------------------------- 1 | ## Ishaans Weight 2 | > **Ishaan is getting really fat. He wants to lose his weight but can't get the motivation to workout. Seeing this, his friend Charul offers him a deal. 3 | For every K pushups Ishaan does, Charul will give him money equal to the number of pushups Ishaan has done till then (Refer Example for Explanation). 4 | Find out the amount of money he made if he does N pushups.** 5 | 6 | **Input Format**
7 | The first line of input contains a single integer T denoting the number of test cases.
8 | The only line of each test case contains 2 space-separated integers N and K. 9 | 10 | **Output Format**
11 | For each test case, print the required answer in a new line. 12 | 13 | **Constraints**
14 | 1 <= T <= 200
15 | 1 <= N <= 10^9
16 | 1 <= K <= 10^5
17 | 18 | **Sample Input**
19 | 3
20 | 5 2
21 | 5 3
22 | 5 6
23 | 24 | **Sample Output**
25 | 6
26 | 3
27 | 0
28 | -------------------------------------------------------------------------------- /Two_Strings/README.md: -------------------------------------------------------------------------------- 1 | ## Two Strings 2 | > **Given two strings of equal length, you have to tell whether they both strings are identical. 3 |
4 | Two strings S1 and S2 are said to be identical, if any of the permutation of string S1 is equal to the string S2. See Sample explanation for more details.** 5 | 6 | **Input Format**
7 | First line, contains an intger 'T' denoting no. of test cases.
8 | Each test consists of a single line, containing two space separated strings S1 and S2 of equal length.

9 | 10 | **Output Format**
11 | For each test case, if any of the permutation of string S1 is equal to the string S2 print YES else print NO.
12 | 13 | **Constraints**
14 | 1<= T <=100
15 | 1<= |S1| = |S2| <= 10^5
16 | String is made up of lower case letters only.

17 | 18 | **Sample Input**
19 | 3
20 | sumit mitsu
21 | ambuj jumba
22 | abhi hibb

23 | 24 | **Sample Output**
25 | YES
26 | YES
27 | NO -------------------------------------------------------------------------------- /First and last occurrence of x/README.md: -------------------------------------------------------------------------------- 1 | ## Find first and last occurrence of x 2 | > **Given a sorted array with possibly duplicate elements, the task is to find indexes of first and last occurrences of an element x in the given array.** 3 | 4 | **Input Format** 5 | The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N denoting the size of the array. Then in the next line are N space separated values of the array. The last line of each test case contains an integer x. 6 | 7 | 8 | **Output Format** 9 | For each test case in a new line print two integers separated by space denoting the first and last occurrence of the element x. If the element is not present in the array print -1. 10 | 11 | **Constraints** 12 | 0<=T<=100 13 | 0< Number of Digits in Integer <=1000000 14 | 15 | **Sample Input** 16 | 2 17 | 9 18 | 1 3 5 5 5 5 67 123 125 19 | 5 20 | 9 21 | 1 3 5 5 5 5 7 123 125 22 | 7 23 | 24 | **Sample Output** 25 | 2 5 26 | 6 6 27 | -------------------------------------------------------------------------------- /Product-of-max-and-min-of-two-arrays.py/README.md: -------------------------------------------------------------------------------- 1 | ## Product of maximum in first array and minimum in second 2 | 3 | > **Given two arrays of size N1 and N2 respectively, the task is to calculate the product of max element of first array and min element of second array.** 4 | 5 | **Input**
6 | The first line of the input contains a single integer T, denoting the number of test cases. Then T test case follows. Each testcase contains 4 lines:-
7 | Size of the first array N1
8 | Elements of the first array separated by spaces
9 | Size of the second array N2
10 | Elements of the second array separated by spaces
11 | 12 | **Output**
13 | For each testcase, print the product of the max element of the first array and the minimum element of the second array.
14 | 15 | **Sample Input**
16 | 2
17 | 6
18 | 5 7 9 3 6 2
19 | 6
20 | 1 2 6 -1 0 9
21 | 6
22 | 1 4 2 3 10 2
23 | 6
24 | 4 2 6 5 2 9
25 | 26 | **Sample Output**
27 | -9
28 | 20
-------------------------------------------------------------------------------- /String-Validators/README.md: -------------------------------------------------------------------------------- 1 | ## String Validators 2 | > You are given a string S.
Your task is to find out if the string S contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters.

3 | 4 | **Input Format**
5 | A single line containing a string S.

6 | 7 | **Output Format**
8 | In the first line, print True if S has any alphanumeric characters. Otherwise, print False. 9 | In the second line, print True if S has any alphabetical characters. Otherwise, print False. 10 | In the third line, print True if S has any digits. Otherwise, print False. 11 | In the fourth line, print True if S has any lowercase characters. Otherwise, print False. 12 | In the fifth line, print True if S has any uppercase characters. Otherwise, print False.

13 | 14 | **Constraints**
15 | 0 < len(S) < 1000 16 |

17 | 18 | **Sample Input**
19 | qA2

20 | 21 | **Sample Output**
22 | True
23 | True
24 | True
25 | True
26 | True -------------------------------------------------------------------------------- /Iterables and Iterators/README.md: -------------------------------------------------------------------------------- 1 | ## Iterables and Iterators 2 | > **You are given a list of N lowercase English letters. For a given integer K, you can select any K indices (assume 1-based indexing) with a uniform probability from the list. 3 |
Find the probability that at least one of the K indices selected will contain the letter: 'a'.
** 4 | 5 | **Input Format**
6 | The input consists of three lines. The first line contains the integer N, denoting the length of the list. The next line consists of N space-separated lowercase English letters, denoting the elements of the list.
The third and the last line of input contains the integer K, denoting the number of indices to be selected.
7 | 8 | **Output Format**
9 | Output a single line consisting of the probability that at least one of the K indices selected contains the letter:'a'.
Note: The answer must be correct up to 3 decimal places.
10 | 11 | **Sample Input**
12 | 4
13 | a a c d
14 | 2
15 | 16 | **Sample Output**
17 | 0.8333 18 | -------------------------------------------------------------------------------- /Merge the Tools/README.md: -------------------------------------------------------------------------------- 1 | ## Merge The Tools 2 | Consider the following:
3 | A string, s, of length n.
4 | An integer, k, where k is a factor of n. 5 | We can split s into n/k subsegments where each subsegment, t(i), consists of a contiguous block of k characters in s. 6 | Then, use each t(i) to create string u(i) such that: 7 | The characters in u(i) are a subsequence of the characters in t(i). 8 | Any repeat occurrence of a character is removed from the string such that each character in u(i) occurs exactly once. 9 | In other words, if the character at some index j in t(i) occurs at a previous index < j in t(i), then do not include the 10 | character in string u(i). 11 | Given s and k, print n/k lines where each line i denotes string u(i). 12 | Input Format 13 | The first line contains a single string denoting s. 14 | The second line contains an integer, k, denoting the length of each subsegment. 15 | Output Format 16 | Print n/k lines where each line i contains string u(i). 17 | Sample Input 18 | AABCAAADA 19 | 3 20 | Sample Output 21 | AB 22 | CA 23 | AD 24 | -------------------------------------------------------------------------------- /Repeated String/Repeated String.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | 10 | # logic used :: find number of a in original string, multiply that to n. If length of string is not a factor of n, then find number of a in its subtring. 11 | # Complete the repeatedString function below. 12 | def repeatedString(s, n): 13 | countA = 0 14 | for i in list(s): #counting 'a' in string 15 | if i == 'a': 16 | countA += 1 17 | total = (n//len(s))*countA #multiply number of 'a' in string to n 18 | sub = n%len(s) 19 | for i in range(0, sub): #find 'a' if its in substring of that string 20 | if s[i] == 'a': 21 | total += 1 22 | return total 23 | 24 | if __name__ == '__main__': 25 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 26 | 27 | s = input() 28 | 29 | n = int(input()) 30 | 31 | result = repeatedString(s, n) 32 | 33 | fptr.write(str(result) + '\n') 34 | 35 | fptr.close() 36 | -------------------------------------------------------------------------------- /collections.Counter()/README.md: -------------------------------------------------------------------------------- 1 | ## collections.Counter() 2 | > Raghu is a shoe shop owner. His shop has X number of shoes.
3 | He has a list containing the size of each shoe he has in his shop.
4 | There are N number of customers who are willing to pay x amount of money only if they get the shoe of their desired size. 5 |
Your task is to compute how much money Raghu earned.

6 | 7 | **Input Format**
8 | The first line contains X, the number of shoes.
9 | The second line contains the space separated list of all the shoe sizes in the shop.
10 | The third line contains N, the number of customers.
11 | The next N lines contain the space separated values of the shoe size desired by the customer and x, the price of the shoe.

12 | 13 | **Output Format**
14 | Print the amount of money earned by Raghu.

15 | 16 | **Sample Input**
17 | 10
18 | 2 3 4 5 6 8 7 6 5 18
19 | 6
20 | 6 55
21 | 6 45
22 | 6 55
23 | 4 40
24 | 18 60
25 | 10 50

26 | 27 | **Sample Output**
28 | 200 -------------------------------------------------------------------------------- /The Penalty Shootout/README.md: -------------------------------------------------------------------------------- 1 | ## The Penalty Shootout 2 | > **Ishaan loves playing football. He is a penalty master as he practices a lot. Now during a match he wants to know how many goals does he score during a penalty. 3 | Since he wants to focus on the match, he asks you to count the goals for him. You are given a string which consists of 3 different characters : 4 | "0" stands for "no goal". 5 | "1" stands for "goal". 6 | "2" stands for a foul which gives Ishaan a penalty.
7 | Note : You need to count only those goals which are scored on a penalty.** 8 | 9 | **Input Format**
10 | First line of input contains a single integer T denoting the number of test cases.
11 | The only line of each test case contains a string S.
12 | 13 | **Output Format**
14 | For each test case, print the required answer in a new line.
15 | 16 | **Constraints**
17 | 1 <= T <= 100
18 | 1 <= Length of String <= 500
19 | 20 | **Sample Input**
21 | 3
22 | 101201212110
23 | 10101
24 | 2120
25 | 26 | **Sample Output**
27 | 2
28 | 0
29 | 1
-------------------------------------------------------------------------------- /Monk_and_Welcome_Problem/README.md: -------------------------------------------------------------------------------- 1 | ## Monk and Welcome Problem 2 | > **Having a good previous year, Monk is back to teach algorithms and data structures. This year he welcomes the learners with a problem which he calls "Welcome Problem". The problem gives you two arrays A and B (each array of size N) and asks to print new array C such that: 3 | C[i] = A[i] + B[i]; 1<= i<= N 4 |
5 | Now, Monk will proceed further when you solve this one. So, go on and solve it :)** 6 | 7 | **Input Format**
8 | First line consists of an integer N, denoting the size of A and B.
9 | Next line consists of N space separated integers denoting the array A.
10 | Next line consists of N space separated integers denoting the array B.

11 | **Output Format**
12 | Print N space separated integers denoting the array C.

13 | 14 | **Constraints**
15 | 1<= N <=100000
16 | 1<= A[i] <= 100000; 1<= i<= N
17 | 1<= B[i] <= 100000; 1<= i<= N

18 | 19 | **Sample Input**
20 | 5
21 | 1 2 3 4 5
22 | 4 5 3 2 10

23 | 24 | **Sample Output**
25 | 5 7 6 6 15 -------------------------------------------------------------------------------- /Fitting-The-Array/README.md: -------------------------------------------------------------------------------- 1 | ## Fitting The Array 2 | > **Adobe is playing an array game. He is weak in the concepts of arrays. Adobe is given two arrays a[ ] and b[ ] of the same size. The array a[ ] will be said to fit in array b[ ] if by arranging the elements of both array, there exists a solution such that i_th element of a[ ] is less than or equal to an i_th element of b[ ]. Help Adobe find if the given arrays are fit or not.** 3 | 4 | **Input Format**
5 | The first line of input contains an integer T denoting the number of test cases. For each test case, the next subsequent line contains the integer N i.e. size of arrays followed by array a[ ] and then array b[ ]. 6 | 7 | **Output Format**
8 | Print "YES" if array a[ ] fit in array b[ ] otherwise print "NO". 9 | 10 | **Constraints**
11 | 1<= T<= 100
12 | 1<= N<= 100
13 | 0<=a[ i ]<=1000
14 | 0<= b[ i ]<=1000
15 | 16 | **Sample Input**
17 | 2
18 | 4
19 | 7 5 3 2
20 | 5 4 8 7
21 | 8
22 | 7 5 3 2 5 105 45 10
23 | 2 4 0 5 6 9 75 84
24 | 25 | **Sample Output**
26 | YES
27 | NO
-------------------------------------------------------------------------------- /Petr and a calendar/README.md: -------------------------------------------------------------------------------- 1 | ## Petr and a calendar 2 | Petr wants to make a calendar for current month. For this purpose he draws a table in which columns correspond to weeks (a week is seven consequent days from Monday to Sunday), rows correspond to weekdays, and cells contain dates. For example, a calendar for January 2017 should look like on the picture: 3 |
4 | 5 | ``` 6 | | | 2 | 8 | 14 | 20 | 26 | 7 | | | 3 | 9 | 15 | 21 | 27 | 8 | | | 4 | 10 | 16 | 22 | 28 | 9 | | | 5 | 11 | 17 | 23 | 29 | 10 | | | 6 | 12 | 18 | 24 | 30 | 11 | | 1 | 7 | 13 | 19 | 25 | 31 | 12 | ``` 13 | Petr wants to know how many columns his table should have given the month and the weekday of the first date of that month? Assume that the year is non-leap. 14 | 15 | Input Format
You will be given a function with two integers m and d as arguments.

16 | Constraints
17 | 1 <= m <= 12
1 <= d <= 7

18 | Output Format
You need to return single integer: the number of columns the table should have..

19 | Sample Input
1
7

20 | Sample Output
6 21 | -------------------------------------------------------------------------------- /Finding_the_percentage/README.md: -------------------------------------------------------------------------------- 1 | ## Finding the Percentage 2 | > You have a record of N students. Each record contains the student's name, and their percent marks in Maths, Physics and Chemistry. The marks can be floating values. The user enters some integer N followed by the names and marks for N students. You are required to save the record in a dictionary data type. The user then enters a student's name. Output the average percentage marks obtained by that student, correct to two decimal places.
3 | 4 | **Input Format**
5 | The first line contains the integer N, the number of students. The next N lines contains the name and marks obtained by that student separated by a space. The final line contains the name of a particular student previously listed.
6 | 7 | **Output Format**
8 | Print one line: The average of the marks obtained by the particular student correct to 2 decimal places.
9 | 10 | **Constraints**
11 | 2 <= N <= 10
12 | 0 <= Marks <= 100 13 |
14 | 15 | **Sample Input**
16 | 3
17 | Krishna 67 68 69
18 | Arjun 70 98 63
19 | Malika 52 56 60
20 | Malika
21 | 22 | **Sample Output**
23 | 56.00 24 | -------------------------------------------------------------------------------- /Counting Valleys/counting_valleys.py: -------------------------------------------------------------------------------- 1 | #!/bin/python3 2 | 3 | import math 4 | import os 5 | import random 6 | import re 7 | import sys 8 | 9 | # Complete the countingValleys function below. 10 | def countingValleys(n, s): 11 | #sea for sea level, valleyCount to keep count of valleys, flag to keep track if Gary came out of previous valley 12 | sea, valleyCount, flag = 0, 0, 0 13 | s = list(s) #converting string to list of letters (U, D) 14 | for i in s: #looping over list 15 | if i=='U': #check if its U(up) or D(down) 16 | sea += 1 17 | else: 18 | sea -= 1 19 | 20 | if sea < 0 and flag == 0: #if its first step below sealevel 21 | valleyCount += 1 22 | flag = 1 23 | elif sea == 0 and flag == 1: #reset flag if its first step above sealevel 24 | flag = 0 25 | return valleyCount 26 | 27 | if __name__ == '__main__': 28 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 29 | 30 | n = int(input()) 31 | 32 | s = input() 33 | 34 | result = countingValleys(n, s) 35 | 36 | fptr.write(str(result) + '\n') 37 | 38 | fptr.close() 39 | -------------------------------------------------------------------------------- /Collection_named tuple/README.md: -------------------------------------------------------------------------------- 1 | ## Collection Named-tuple 2 | > **Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code.
They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of position index. 3 | You are given a data containing test results of class. The dataset consists of two columns namely : 'marks' and 'name'.
These two Columns can be in any order i.e. ('name' followed by 'marks' or vice versa). 4 | You have to find the average marks for the whole class.** 5 | 6 | **Input Format**
7 | First line will contain an Integer N, denoting the number of students.
8 | Next line will contain two string denoting the column heading.
9 | Next N lines will contain marks and name of the students respective of column headings.
10 | 11 | 12 | **Constraints**
13 | 1 <= N <= 10^3
14 | 0 <= Marks <= 100
15 | 16 | **Output Format**
17 | Output the average marks of the class rounded off to two decimal places. 18 | 19 | **Sample Input**
20 | 3
21 | marks names
22 | 10 arpit
23 | 20 anushka
24 | 35 rakshita
25 | 26 | **Sample Output**
27 | 21.67 28 | -------------------------------------------------------------------------------- /Nested_Lists/README.md: -------------------------------------------------------------------------------- 1 | ## Nested Lists 2 | > **Given the names and grades for each student in a Physics class of students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade. 3 |
4 | Note: If there are multiple students with the same grade, order their names alphabetically and print each name on a new line.
5 | 6 | **Input Format**
7 | The first line contains an integer, N, the number of students.
8 | The 2N subsequent lines describe each student over 2 lines; the first line contains a student's name, and the second line contains their grade.
9 | 10 | **Output Format**
11 | Print the name(s) of any student(s) having the second lowest grade in Physics; if there are multiple students, order their names alphabetically and print each one on a new line.

12 | 13 | **Constraints**
14 | 2 <= N <= 5
15 | There will always be one or more students having the second lowest grade.

16 | 17 | **Sample Input**
18 | 5
19 | Harry
20 | 37.21
21 | Berry
22 | 37.21
23 | Tina
24 | 37.2
25 | Akriti
26 | 41
27 | Harsh
28 | 39

29 | 30 | **Sample Output**
31 | Berry
32 | Harry -------------------------------------------------------------------------------- /Playing with matches/Problem: -------------------------------------------------------------------------------- 1 | You can find the problem here: 2 | https://www.codechef.com/COOK110B/problems/MATCHES 3 | 4 | Contest: COOK110B 5 | 6 | 7 | 8 | 9 | Chef's son Chefu found some matches in the kitchen and he immediately starting playing with them. 10 | 11 | The first thing Chefu wanted to do was to calculate the result of his homework — the sum of A and B, and write it using matches. Help Chefu and tell him the number of matches needed to write the result. 12 | 13 | Input: 14 | 15 | The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. 16 | The first and only line of each test case contains two space-separated integers A and B. 17 | 18 | Output: 19 | 20 | For each test case, print a single line containing one integer — the number of matches needed to write the result (A+B). 21 | 22 | Example Input: 23 | 3 24 | 123 234 25 | 10101 1010 26 | 4 4 27 | Example Output: 28 | 13 29 | 10 30 | 7 31 | Explanation 32 | Example case 1: The result is 357. We need 5 matches to write the digit 3, 5 matches to write the digit 5 and 3 matches to write the digit 7. 33 | 34 | Example case 2: The result is 11111. We need 2 matches to write each digit 1, which is 2⋅5=10 matches in total. 35 | -------------------------------------------------------------------------------- /String Formatting/README.md: -------------------------------------------------------------------------------- 1 | ## String Formatting 2 | 3 | > **Given an integer, n, print the following values for each integer i from 1 to n: 4 | 1. Decimal 5 | 2. Octal 6 | 3. Hexadecimal (capitalized) 7 | 4. Binary 8 | 5. The four values must be printed on a single line in the order specified above for each from to . Each value should be space-padded to match the width of the binary value of n.
** 9 | 10 | **Input Format**
11 | A single integer denoting n.
12 | 13 | **Output Format**
14 | Print lines where each line i (in the range 1<=i<=n) contains the respective decimal, octal, capitalized hexadecimal, and binary values of i. Each printed value must be formatted to the width of the binary value of n.
15 | 16 | **Sample Input**
17 | 17
18 | 19 | **Sample Output**
20 | 1 1 1 1
21 | 2 2 2 10
22 | 3 3 3 11
23 | 4 4 4 100
24 | 5 5 5 101
25 | 6 6 6 110
26 | 7 7 7 111
27 | 8 10 8 1000
28 | 9 11 9 1001
29 | 10 12 A 1010
30 | 11 13 B 1011
31 | 12 14 C 1100
32 | 13 15 D 1101
33 | 14 16 E 1110
34 | 15 17 F 1111
35 | 16 20 10 10000
36 | 17 21 11 10001 -------------------------------------------------------------------------------- /Lists/README.md: -------------------------------------------------------------------------------- 1 | ## Lists 2 | > **Consider a list (list = []). You can perform the following commands:
3 | 4 | 1. insert i e: Insert integer at position .
5 | 2. print: Print the list.
6 | 3. remove e: Delete the first occurrence of integer .
7 | 4. append e: Insert integer at the end of the list.
8 | 5. sort: Sort the list.
9 | 6. pop: Pop the last element from the list.
10 | 7. reverse: Reverse the list.
11 | 12 | Initialize your list and read in the value of n followed by n lines of commands where each command will be of the 7 types listed above. Iterate through each command in order and perform the corresponding operation on your list.** 13 | 14 | **Input Format**
15 | The first line contains an integer, n, denoting the number of commands.
16 | Each line i of the n subsequent lines contains one of the commands described above.
17 | 18 | **Output Format**
19 | For each command of type print, print the list on a new line.
20 | 21 | **Constraints**
22 | The elements added to the list must be integers.
23 | 24 | **Sample Input**
25 | 12
26 | insert 0 5
27 | insert 1 10
28 | insert 0 6
29 | print
30 | remove 6
31 | append 9
32 | append 1
33 | sort
34 | print
35 | pop
36 | reverse
37 | print
38 | 39 | **Sample Output**
40 | [6, 5, 10]
41 | [1, 5, 9, 10]
42 | [9, 5, 1] -------------------------------------------------------------------------------- /Repeated String/README.md: -------------------------------------------------------------------------------- 1 | ## [Repeated String](https://www.hackerrank.com/challenges/repeated-string/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup) 2 | Lilah has a string, s, of lowercase English letters that she repeated infinitely many times.
3 | Given an integer, n, find and print the number of letter a's in the first letters of Lilah's infinite string.
4 | For example, if the string s = 'abcac' and n = 10, the substring we consider is abcacabcac, the first 10 characters of her infinite string. There are 4 occurrences of a in the substring.
5 | 6 | **Function Description**
7 | Complete the repeatedString function in the editor below. It should return an integer representing the number of occurrences of a in the prefix of length in the infinitely repeating string.
8 | repeatedString has the following parameter(s): 9 |
  • s: a string to repeat
  • 10 |
  • n: the number of characters to consider

11 | 12 | **Input Format**
13 | The first line contains a single string, s.
14 | The second line contains an integer, n.
15 | 16 | **Output Format**
17 | Print a single integer denoting the number of letter a's in the first n letters of the infinite string created by repeating s infinitely many times. 18 | 19 | **Sample Input**
20 | aba
21 | 10
22 | 23 | **Sample Output**
24 | 7 -------------------------------------------------------------------------------- /Text Alignment/README.md: -------------------------------------------------------------------------------- 1 | ## Text Alignment 2 | 3 | > **You are given a partial code that is used for generating the HackerRank Logo of variable thickness.
4 | Your task is to replace the blank (______) with rjust, ljust or center.
** 5 | 6 | **Input Format**
7 | A single line containing the thickness value for the logo.
8 | 9 | **Output Format**
10 | Output the desired logo.
11 | 12 | **Sample Input**
13 | 5
14 | 15 | **Sample Output**
16 | 17 | H
18 | HHH
19 | HHHHH
20 | HHHHHHH
21 | HHHHHHHHH
22 | HHHHH HHHHH
23 | HHHHH HHHHH
24 | HHHHH HHHHH
25 | HHHHH HHHHH
26 | HHHHH HHHHH
27 | HHHHH HHHHH
28 | HHHHHHHHHHHHHHHHHHHHHHHHH
29 | HHHHHHHHHHHHHHHHHHHHHHHHH
30 | HHHHHHHHHHHHHHHHHHHHHHHHH
31 | HHHHH HHHHH
32 | HHHHH HHHHH
33 | HHHHH HHHHH
34 | HHHHH HHHHH
35 | HHHHH HHHHH
36 | HHHHH HHHHH
37 | HHHHHHHHH
38 | HHHHHHH
39 | HHHHH
40 | HHH
41 | H -------------------------------------------------------------------------------- /Zig Zag Sequence/README.md: -------------------------------------------------------------------------------- 1 | ## Zig Zag Sequence 2 | > In this challenge, the task is to debug the existing code to successfully execute all provided test files. 3 | 4 | Given an array of n distinct integers, transform the array into a zig zag sequence by permuting the array elements. A sequence will be called a zig zag sequence if the first k elements in the sequence are in increasing order and the last k elements are in decreasing order, where k=(n+1)/2. You need to find the lexicographically smallest zig zag sequence of the given array. 5 | 6 | **Example** 7 | 8 | a = [2, 3, 5, 1, 4] 9 | 10 | Now if we permute the array as [1, 4, 5, 3, 2], the result is a zig zag sequence. 11 | 12 | Debug the given function findZigZagSequence to return the appropriate zig zag sequence for the given input array. 13 | 14 | Note: You can modify at most three lines in the given code. You cannot add or remove lines of code. 15 | 16 | To restore the original code, click on the icon to the right of the language selector. 17 | 18 | **Input Format** 19 | 20 | The first line contains t the number of test cases. The first line of each test case contains an integer n, denoting the number of array elements. The next line of the test case contains n elements of array a. 21 | 22 | **Constraints** 23 | 24 | 1 <= t <= 20
25 | 1 <= n <= 10000 (n is always odd)
26 | 1 <= ai <= 10^9 27 | 28 | **Output Format** 29 | 30 | For each test cases, print the elements of the transformed zig zag sequence in a single line. 31 | -------------------------------------------------------------------------------- /Sock Merchant/README.md: -------------------------------------------------------------------------------- 1 | ## [Sock Merchant](https://www.hackerrank.com/challenges/sock-merchant/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup) 2 | John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. 3 | 4 | For example, there are n = 7 socks with colors ar = [1, 2, 1, 2, 1, 3, 2]. There is one pair of color 1 and one of color 2. There are three odd socks left, one of each color. The number of pairs is 2.
5 | 6 | **Function Description**
7 | Complete the sockMerchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.
8 | sockMerchant has the following parameter(s): 9 |
  • n: the number of socks in the pile
  • 10 |
  • ar: the colors of each sock
11 | 12 | **Input Format**
13 | The first line contains an integer n, the number of socks represented in ar.
. 14 | The second line contains n space-separated integers describing the colors ar[i] of the socks in the pile.
15 | 16 | **Output Format**
17 | Return the total number of matching pairs of socks that John can sell. 18 | 19 | **Sample Input**
20 | 9
21 | 10 20 20 10 10 30 50 10 20
22 | 23 | **Sample Output**
24 | 3 -------------------------------------------------------------------------------- /Recursive Digit Sum/README.md: -------------------------------------------------------------------------------- 1 | ## Recursive Digit Sum 2 | 3 | We define super digit of an integer x using the following rules: 4 | 5 | Given an integer, we need to find the super digit of the integer. 6 | 7 | If x has only 1 digit, then its super digit is x. 8 | 9 | Otherwise, the super digit of x is equal to the super digit of the sum of the digits of x. 10 | 11 | For example, the super digit of will be calculated as: 12 | 13 | super_digit(9875) 9+8+7+5 = 29 14 | super_digit(29) 2 + 9 = 11 15 | super_digit(11) 1 + 1 = 2 16 | super_digit(2) = 2 17 | 18 | **Example** 19 | 20 | n = '9875' 21 | 22 | k = 4 23 | 24 | The number p is created by concatenating the string n k times so the initial p = 9875987598759875. 25 | 26 | superDigit(p) = superDigit(9875987598759875) 27 | 9+8+7+5+9+8+7+5+9+8+7+5+9+8+7+5 = 116 28 | superDigit(p) = superDigit(116) 29 | 1+1+6 = 8 30 | superDigit(p) = superDigit(8) 31 | 32 | All of the digits of p sum to 116. The digits of 116 sum to 8. 8 is only one digit, so it is the super digit. 33 | 34 | **Function Description** 35 | 36 | Complete the function superDigit in the editor below. It must return the calculated super digit as an integer. 37 | 38 | superDigit has the following parameter(s): 39 | 40 | - string n: a string representation of an integer 41 | - int k: the times to concatenate n to make p 42 | 43 | **Returns** 44 | 45 | - int: the super digit of n repeated k times 46 | 47 | **Input Format** 48 | 49 | The first line contains two space separated integers, n and k. 50 | 51 | -------------------------------------------------------------------------------- /Counting Valleys/README.md: -------------------------------------------------------------------------------- 1 | ## Counting Valleys 2 | Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly
    3 |
  • A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.
  • 4 |
  • A valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level.
  • 5 |

Given Gary's sequence of up and down steps during his last hike, find and print the number of valleys he walked through.

For example, if Gary's path is s=[DDUUUUDD], he first enters a valley 2 units deep. Then he climbs out an up onto a mountain 2 units high. Finally, he returns to sea level and ends his hike.

Function Description

Complete the countingValleys function in the editor below. It must return an integer that denotes the number of valleys Gary traversed.

countingValleys has the following parameter(s):

    6 |
  • n: the number of steps Gary takes
  • 7 |
  • s: a string describing his path
  • 8 |
9 | 10 | **Input Format**
11 | The first line contains an integer n, the number of steps in Gary's hike.
12 | The second line contains a single string s, of n characters that describe his path.
13 | 14 | **Output Format**
15 | Print a single integer that denotes the number of valleys Gary walked through during his hike.
16 | 17 | 18 | **Sample Input**
19 | 8
20 | UDDDUDUU
21 | 22 | **Sample Output**
23 | 1 24 | -------------------------------------------------------------------------------- /Mini-Max Sum/README.md: -------------------------------------------------------------------------------- 1 | ## Mini-Max Sum 2 | Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. 3 | 4 | **Example**
5 | The minimum sum is 1+3+5+7=16 and the maximum sum is 3+5+7+9=24. The function prints 6 | 7 | 16 24 8 | 9 | **Function Description**
10 | Complete the miniMaxSum function in the editor below. 11 | 12 | miniMaxSum has the following parameter(s): 13 | - arr: an array of 5 integers 14 | 15 | **Print**
16 | Print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements. 17 | 18 | **Input Format**
19 | A single line of five space-separated integers. 20 | 21 | **Constraints**
22 | ![1\leq arr[i]\leq 10^{9}](https://latex.codecogs.com/svg.image?1\leq&space;arr[i]\leq&space;10^{9}) 23 | 24 | **Output Format**
25 | Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.) 26 | 27 | **Sample Input**
28 | 29 | 1 2 3 4 5 30 | 31 | **Sample Output**
32 | 33 | 10 14 34 | 35 | **Explanation**
36 | The numbers are 1, 2, 3, 4, and 5. Calculate the following sums using four of the five integers: 37 | 38 | 1. Sum everything except 1, the sum is 2 + 3 + 4 + 5 = 14. 39 | 2. Sum everything except 2, the sum is 1 + 3 + 4 + 5 = 13. 40 | 3. Sum everything except 3, the sum is 1 + 2 + 4 + 5 = 12. 41 | 4. Sum everything except 4, the sum is 1 + 2 + 3 + 5 = 11. 42 | 5. Sum everything except 5, the sum is 1 + 2 + 3 + 4 = 10. 43 | 44 | Hints: Beware of integer overflow! Use 64-bit Integer. -------------------------------------------------------------------------------- /Designer Door Mat/README.md: -------------------------------------------------------------------------------- 1 | ## Designer Door Mat 2 | > **Mr. Vincent works in a door mat manufacturing company. One day, he designed a new door mat with the following specifications: 3 |
4 | Mat size must be N x M. (N is an odd natural number, and M is 3 times N.)
5 | The design should have 'WELCOME' written in the center.
6 | The design pattern should only use |, . and - characters.
** 7 | 8 | **Sample Design**
9 | 10 | Size: 7 x 21
11 | ---------.|.---------
12 | ------.|..|..|.------
13 | ---.|..|..|..|..|.---
14 | -------WELCOME-------
15 | ---.|..|..|..|..|.---
16 | ------.|..|..|.------
17 | ---------.|.---------
18 |
19 | Size: 11 x 33
20 | ---------------.|.---------------
21 | ------------.|..|..|.------------
22 | ---------.|..|..|..|..|.---------
23 | ------.|..|..|..|..|..|..|.------
24 | ---.|..|..|..|..|..|..|..|..|.---
25 | -------------WELCOME-------------
26 | ---.|..|..|..|..|..|..|..|..|.---
27 | ------.|..|..|..|..|..|..|.------
28 | ---------.|..|..|..|..|.---------
29 | ------------.|..|..|.------------
30 | ---------------.|.---------------
31 | **Input Format**
32 | A single line containing the space separated values of N and M.
33 | 34 | **Output Format**
35 | Output the design pattern.
36 | 37 | **Sample Input**
38 | 9 27 39 | 40 | **Sample Output**
41 | ------------.|.------------
42 | ---------.|..|..|.---------
43 | ------.|..|..|..|..|.------
44 | ---.|..|..|..|..|..|..|.---
45 | ----------WELCOME----------
46 | ---.|..|..|..|..|..|..|.---
47 | ------.|..|..|..|..|.------
48 | ---------.|..|..|.---------
49 | ------------.|.------------ 50 | -------------------------------------------------------------------------------- /Caesar Cipher/readme.md: -------------------------------------------------------------------------------- 1 | Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c. 2 | 3 | Original alphabet: abcdefghijklmnopqrstuvwxyz 4 | Alphabet rotated +3: defghijklmnopqrstuvwxyzabc 5 | 6 | **Example** 7 | 8 | s = There's-a-starman-waiting-in-the-sky 9 | k = 3 10 | 11 | The alphabet is rotated by 3, matching the mapping above. The encrypted string is Wkhuh'v-d-vwdupdq-zdlwlqj-lq-wkh-vnb. 12 | 13 | Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted. 14 | 15 | **Function Description** 16 | 17 | Complete the caesarCipher function in the editor below. 18 | 19 | caesarCipher has the following parameter(s): 20 | 21 | - string s: cleartext 22 | - int k: the alphabet rotation factor 23 | 24 | **Returns** 25 | 26 | - string: the encrypted string 27 | 28 | **Input Format** 29 | 30 | The first line contains the integer, n, the length of the unencrypted string. 31 | The second line contains the unencrypted string, s. 32 | The third line contains k, the number of letters to rotate the alphabet by. 33 | 34 | **Constraints** 35 | 36 | 1 <= n <= 100 37 | 0 <= k <= 100 38 | s is a valid ASCII string without any spaces. 39 | 40 | **Sample Input** 41 | 42 | 11 43 | middle-Outz 44 | 2 45 | 46 | **Sample Output** 47 | 48 | okffng-Qwvb 49 | 50 | **Explanation** 51 | 52 | Original alphabet: abcdefghijklmnopqrstuvwxyz 53 | Alphabet rotated +2: cdefghijklmnopqrstuvwxyzab 54 | 55 | m -> o 56 | i -> k 57 | d -> f 58 | d -> f 59 | l -> n 60 | e -> g 61 | - - 62 | O -> Q 63 | u -> w 64 | t -> v 65 | z -> b -------------------------------------------------------------------------------- /Jumping on the Clouds/README.md: -------------------------------------------------------------------------------- 1 | ## [Jumping on the Clouds](https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup) 2 | Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. She must avoid the thunderheads. Determine the minimum number of jumps it will take Emma to jump from her starting postion to the last cloud. It is always possible to win the game. 3 |
4 | For each game, Emma will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided. For example, c=[0,1,0,0,0,1,0] indexed from 0 . . . 6. The number on each cloud is its index in the list so she must avoid the clouds at indexes 1 and 5. She could follow the following two paths: 0 -> 2 -> 4 -> 6 or 0 -> 2 -> 3 -> 4 -> 6. The first path takes 3 jumps while the second takes 4. 5 | 6 | **Function Description**
7 | Complete the jumpingOnClouds function in the editor below. It should return the minimum number of jumps required, as an integer. 8 |
9 | jumpingOnClouds has the following parameter(s): 10 |
11 |
  • c: an array of binary integers
12 | 13 | **Input Format**
14 | The first line contains an integer n, the total number of clouds. The second line contains n space-separated binary integers describing clouds c[i] where 0 <= i <= n.
15 | 16 | **Output Format**
17 | Print the minimum number of jumps needed to win the game.
18 | 19 | 20 | **Sample Input**
21 | 7
22 | 0 0 1 0 0 1 0
23 | 24 | **Sample Output**
25 | 4 26 | -------------------------------------------------------------------------------- /2D Array - DS/README.md: -------------------------------------------------------------------------------- 1 | ## [2D Array - DS](https://www.hackerrank.com/challenges/2d-array/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays) 2 | Given a 6 X 6 2D Array, arr:
3 |
1 1 1 0 0 0
 4 | 0 1 0 0 0 0
 5 | 1 1 1 0 0 0
 6 | 0 0 0 0 0 0
 7 | 0 0 0 0 0 0
 8 | 0 0 0 0 0 0
 9 | 

10 | We define an hourglass in A to be a subset of values with indices falling in this pattern in arr's graphical representation:
11 |
a b c
12 |   d
13 | e f g
14 | 

15 | 16 | There are 16 hourglasses in arr, and an hourglass sum is the sum of an hourglass' values. Calculate the hourglass sum for every hourglass in arr, then print the maximum hourglass sum.
17 | For example, given the 2D array:
18 |
-9 -9 -9  1 1 1 
19 |  0 -9  0  4 3 2
20 | -9 -9 -9  1 2 3
21 |  0  0  8  6 6 0
22 |  0  0  0 -2 0 0
23 |  0  0  1  2 4 0
24 | 

25 | We calculate the following 16 hourglass values:
26 |
-63, -34, -9, 12, 
27 | -10, 0, 28, 23, 
28 | -27, -11, -2, 10, 
29 | 9, 17, 25, 18
30 | 

31 | Our highest hourglass value is 28 from the hourglass: 32 |
0 4 3
33 |   1
34 | 8 6 6
35 | 

36 | 37 | 38 | **Function Description**
39 | Complete the function hourglassSum in the editor below. It should return an integer, the maximum hourglass sum in the array.
40 | hourglassSum has the following parameter(s): 41 | 42 |
  • arr: an array of integers
43 | 44 | **Input Format**
45 | Each of the 6 lines of inputs arr[i] contains 6 space-separated integers arr[i][j].
46 | 47 | **Output Format**
48 | Print the largest (maximum) hourglass sum found in arr. 49 | 50 | **Sample Input**
51 |
1 1 1 0 0 0
52 | 0 1 0 0 0 0
53 | 1 1 1 0 0 0
54 | 0 0 2 4 4 0
55 | 0 0 0 2 0 0
56 | 0 0 1 2 4 0
57 | 

58 | 59 | **Sample Output**
60 | 19 -------------------------------------------------------------------------------- /Grid Challenge/README.md: -------------------------------------------------------------------------------- 1 | ## Grid Challenge 2 | Given a square grid of characters in the range ascii[a-z], rearrange elements of each row alphabetically, ascending. Determine if the columns are also in ascending alphabetical order, top to bottom. Return YES if they are or NO if they are not. 3 | 4 | **Example** 5 | 6 | grid = ['abc, 'ade', 'efg'] 7 | 8 | The grid is illustrated below. 9 | 10 | a b c 11 | a d e 12 | e f g 13 | 14 | The rows are already in alphabetical order. The columns a a e, b d f and c e g are also in alphabetical order, so the answer would be YES. Only elements within the same row can be rearranged. They cannot be moved to a different row. 15 | 16 | **Function Description** 17 | 18 | Complete the gridChallenge function in the editor below. 19 | 20 | gridChallenge has the following parameter(s): 21 | 22 | string grid[n]: an array of strings 23 | 24 | **Returns** 25 | 26 | string: either YES or NO 27 | 28 | **Input Format** 29 | 30 | The first line contains t, the number of testcases. 31 | 32 | Each of the next t sets of lines are described as follows: 33 | - The first line contains n, the number of rows and columns in the grid. 34 | - The next n lines contains a string of length n 35 | 36 | **Constraints** 37 | 38 | 1<=t<=100 39 | 40 | 1<=n<=100 41 | 42 | Each string consists of lowercase letters in the range ascii[a-z] 43 | 44 | **Output Format** 45 | 46 | For each test case, on a separate line print YES if it is possible to rearrange the grid alphabetically ascending in both its rows and columns, or NO otherwise. 47 | 48 | **Sample Input** 49 | 50 | STDIN Function 51 | ----- -------- 52 | 1 t = 1 53 | 5 n = 5 54 | ebacd grid = ['ebacd', 'fghij', 'olmkn', 'trpqs', 'xywuv'] 55 | fghij 56 | olmkn 57 | trpqs 58 | xywuv 59 | 60 | **Sample Output** 61 | 62 | YES 63 | 64 | **Explanation** 65 | 66 | The 5x5 grid in the test case can be reordered to 67 | 68 | abcde 69 | fghij 70 | klmno 71 | pqrst 72 | uvwxy 73 | 74 | This fulfills the condition since the rows 1, 2, ..., 5 and the columns 1, 2, ..., 5 are all alphabetically sorted. -------------------------------------------------------------------------------- /Plus Minus/README.md: -------------------------------------------------------------------------------- 1 | ## Plus Minus 2 | Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal. 3 | 4 | Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to 10^-4 are acceptable. 5 | 6 | **Example**
7 | arr = [1,1,0,-1,-1]
8 | There are n=5 elements, two positive, two negative and one zero. Their ratios are 9 |
10 | ![\Large \frac{2}{5}](https://latex.codecogs.com/svg.image?\frac{2}{5}=0.400000), ![\Large \frac{2}{5}](https://latex.codecogs.com/svg.image?\frac{2}{5}=0.400000) and ![\Large \frac{1}{5}](https://latex.codecogs.com/svg.image?\frac{1}{5}=0.200000). 11 | 12 | Results are printed as: 13 | 14 | 0.400000 15 | 0.400000 16 | 0.200000 17 | 18 | **Function Description**
19 | Complete the plusMinus function in the editor below. 20 | 21 | plusMinus has the following parameter(s): 22 | - int arr[n]: an array of integers 23 | 24 | **Print**
25 | Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate line with 6 digits after the decimal. The function should not return a value. 26 | 27 | **Input Format**
28 | The first line contains an integer, n, the size of the array. 29 | The second line contains n space-separated integers that describe arr[n]. 30 | 31 | **Constraints**
32 | ![0 33 | ![-100 36 | Print the following 3 lines, each to 6 decimals: 37 | 38 | - proportion of positive values 39 | - proportion of negative values 40 | - proportion of zeros 41 | 42 | **Sample Input**
43 | 44 | STDIN Function 45 | ----- -------- 46 | 6 arr[] size n = 6 47 | -4 3 -9 0 4 1 arr = [-4, 3, -9, 0, 4, 1] 48 | 49 | **Sample Output**
50 | 51 | 0.500000 52 | 0.333333 53 | 0.166667 54 | 55 | **Explanation**
56 | There are 3 positive numbers, 2 negative numbers, and 1 zero in the array. 57 | The proportions of occurrence are positive: ![\frac{3}{6}=0.500000](https://latex.codecogs.com/svg.image?\frac{3}{6}=0.500000), negative: ![\frac{2}{6}=0.333333](https://latex.codecogs.com/svg.image?\frac{2}{6}=0.333333) and zeros: ![\frac{1}{6}=0.166667](https://latex.codecogs.com/svg.image?\frac{1}{6}=0.166667). 58 | -------------------------------------------------------------------------------- /Tower Breakers/README.md: -------------------------------------------------------------------------------- 1 | ## Tower Breakers 2 | 3 | Two players are playing a game of Tower Breakers! Player 1 always moves first, and both players always play optimally.The rules of the game are as follows: 4 | 5 | - Initially there are n towers. 6 | - Each tower is of height m. 7 | - The players move in alternating turns. 8 | - In each turn, a player can choose a tower of height x and reduce its height to y, where and 1 <= y < x evenly divides x. 9 | 10 | If the current player is unable to make a move, they lose the game. 11 | Given the values of n and m, determine which player will win. If the first player wins, return 1. Otherwise, return 2. 12 | 13 | **Example.** 14 | 15 | n=2 16 | 17 | m=6 18 | 19 | There are 2 towers, each 6 units tall. Player 1 has a choice of two moves: 20 | - remove 3 pieces from a tower to leave 3 as 6 modullo 3 = 0 21 | - remove 5 pieces to leave 1 22 | 23 | Let Player 1 remove 3. Now the towers are 3 and 6 units tall. 24 | 25 | Player 2 matches the move. Now the towers are both 3 units tall. 26 | 27 | Now Player 1 has only one move. 28 | 29 | Player 1 removes 2 pieces leaving 1. 30 | 31 | Towers are 1 and 2 units tall. 32 | Player 2 matches again. 33 | 34 | Towers are both 1 unit tall. 35 | 36 | Player 1 has no move and loses. Return 2. 37 | 38 | **Function Description** 39 | 40 | Complete the towerBreakers function in the editor below. 41 | 42 | towerBreakers has the following paramter(s): 43 | 44 | - int n: the number of towers 45 | - int m: the height of each tower 46 | 47 | **Returns** 48 | 49 | int: the winner of the game 50 | 51 | **Input Format** 52 | 53 | The first line contains a single integer t, the number of test cases. 54 | Each of the next t lines describes a test case in the form of 2 space-separated integers, n and m. 55 | 56 | **Constraints** 57 | 58 | - 1 <= t <= 100 59 | - 1 <= n,m <= 10^6 60 | 61 | **Sample Input** 62 | 63 | STDIN Function 64 | ----- -------- 65 | 2 t = 2 66 | 2 2 n = 2, m = 2 67 | 1 4 n = 1, m = 4 68 | 69 | **Sample Output** 70 | 71 | 2 72 | 1 73 | 74 | **Explanation** 75 | 76 | We'll refer to player 1 as P1 and player 2 as P2 77 | 78 | In the first test case, P1 chooses one of the two towers and reduces it to 1. Then P2 reduces the remaining tower to a height of 1. As both towers now have height 1, P1 cannot make a move so P2 is the winner. 79 | 80 | In the second test case, there is only one tower of height 4. P1 can reduce it to a height of either 2 or 1. P1 chooses 1 as both players always choose optimally. Because P2 has no possible move, P1 wins. 81 | -------------------------------------------------------------------------------- /Counting Sort 1/README.md: -------------------------------------------------------------------------------- 1 | ## Counting Sort 1 2 | 3 | **Comparison Sorting** 4 | 5 | Quicksort usually has a running time of n x log(n), but is there an algorithm that can sort even faster? In general, this is not possible. Most sorting algorithms are comparison sorts, i.e. they sort a list just by comparing the elements to one another. A comparison sort algorithm cannot beat n x log(n) (worst-case) running time, since n x log(n) represents the minimum number of comparisons needed to know where to place each element. For more details, you can see these notes (PDF). 6 | 7 | **Alternative Sorting** 8 | 9 | Another sorting method, the counting sort, does not require comparison. Instead, you create an integer array whose index range covers the entire range of values in your array to sort. Each time a value occurs in the original array, you increment the counter at that index. At the end, run through your counting array, printing the value of each non-zero valued index that number of times. 10 | 11 | **Example** 12 | 13 | arr = [1, 1, 3, 2, 1] 14 | All of the values are in the range [0...3], so create an array of zeros, result = [0, 0, 0, 0]. The results of each iteration follow: 15 | 16 | i arr[i] result 17 | 0 1 [0, 1, 0, 0] 18 | 1 1 [0, 2, 0, 0] 19 | 2 3 [0, 2, 0, 1] 20 | 3 2 [0, 2, 1, 1] 21 | 4 1 [0, 3, 1, 1] 22 | 23 | The frequency array is [0, 3, 1, 1]. These values can be used to create the sorted array as well: sorted = [1, 1, 1, 2, 3]. 24 | 25 | **Note** 26 | 27 | For this exercise, always return a frequency array with 100 elements. The example above shows only the first 4 elements, the remainder being zeros. 28 | 29 | **Challenge** 30 | 31 | Given a list of integers, count and return the number of times each value appears as an array of integers. 32 | 33 | **Function Description** 34 | 35 | Complete the countingSort function in the editor below.
36 | countingSort has the following parameter(s): 37 | - arr[n]: an array of integers 38 | 39 | **Returns** 40 | - int[100]: a frequency array 41 | 42 | **Input Format** 43 | 44 | The first line contains an integer n, the number of items in arr. 45 | Each of the next n lines contains an integer arr[i] where 0 <= i < n. 46 | 47 | **Constraints** 48 | 49 | 100 <= n <= 10^6 50 | 0 <= arr[i] <= 100 51 | 52 | **Sample Input** 53 | 54 | 100 55 | 63 25 73 1 98 73 56 84 86 57 16 83 8 25 81 56 9 53 98 67 99 12 83 89 80 91 39 86 76 85 74 39 25 90 59 10 94 32 44 3 89 30 27 79 46 96 27 32 18 21 92 69 81 40 40 34 68 78 24 87 42 69 23 41 78 22 6 90 99 89 50 30 20 1 43 3 70 95 33 46 44 9 69 48 33 60 65 16 82 67 61 32 21 79 75 75 13 87 70 33 56 | 57 | **Sample Output** 58 | 59 | 0 2 0 2 0 0 1 0 1 2 1 0 1 1 0 0 2 0 1 0 1 2 1 1 1 3 0 2 0 0 2 0 3 3 1 0 0 0 0 2 2 1 1 1 2 0 2 0 1 0 1 0 0 1 0 0 2 1 0 1 1 1 0 1 0 1 0 2 1 3 2 0 0 2 1 2 1 0 2 2 1 2 1 2 1 1 2 2 0 3 2 1 1 0 1 1 1 0 2 2 60 | 61 | **Explanation** 62 | 63 | Each of the resulting values result[i] represents the number of times i appeared in arr. 64 | 65 | 66 | 67 | --------------------------------------------------------------------------------