├── .gitignore ├── Alphabet_Rangoli.py ├── Arithmetic_Operators.py ├── Array_Mathematics.py ├── Arrays.py ├── Collections_Counter_Alternative.py ├── CompanyLogo.py ├── Concatenate.py ├── Dot_and_Cross.py ├── Eye_and_Identity.py ├── Find_Angle_MBC.py ├── Find_The_RunnerUp_Score.py ├── Find_The_Torsional_Angle.py ├── Find_a_String.py ├── Finding_The_Percentage.py ├── Floor_Ceil_Rint.py ├── HTMLParser_Part1.py ├── HTMLParser_Part2.py ├── HelloWorld.py ├── Hex_Color_Code.py ├── Inner_and_Outer.py ├── Iterable_and_Iterators_probability.py ├── Linear_Algebra.py ├── List_Comprehensions.py ├── Lists.py ├── Loops.py ├── Maximize_It.py ├── Mean_Var_StandardDeviation.py ├── Min_and_Max.py ├── Mutations.py ├── Nested_Lists.py ├── PilingUp_production.py ├── Polynomials.py ├── Print_Function.py ├── Python_Division.py ├── Python_IfElse.py ├── Python_String_Formatting.py ├── README.md ├── Shape_and_Reshape.py ├── Standardize_Mobile_Numbers_Decorators.py ├── String_Capitalize.py ├── String_Split_and_Join.py ├── String_Validators.py ├── Sum_and_Prod.py ├── Text_Alignment.py ├── Text_Wrap.py ├── Transpose_and_Flatten.py ├── Triangle_Quest.py ├── Triangle_Quest2.py ├── Tuples.py ├── Validate_Postal_Codes.py ├── Validating_CreditCard_Numbers.py ├── Validating_UID.py ├── Word_Order.py ├── Write_a_Function.py ├── Zeros_and_Ones.py ├── check_Subset.py ├── collections_counter.py ├── collections_defaultdict.py ├── collections_defaultdict_Production.py ├── collections_deque.py ├── detect_html_tags_attributes.py ├── ginortS.py ├── itertools_combinations.py ├── itertools_combinationsWithReplacement.py ├── itertools_groupby.py ├── itertools_permutations.py ├── itertools_product.py ├── matrixScript.py ├── mergeTools.py ├── minion_Game.py ├── name_Directory_Decorators.py ├── name_Directory_Decorators_solution2.py ├── polar_coordinates.py ├── py_check_StrictSuperSet.py ├── py_collections_OrderedDict.py ├── py_collections_namedtuple.py ├── py_set_mutations.py ├── python_integers_come_in_all_sizes.py ├── python_mod_divmod.py ├── python_power_modpower.py ├── regex_groups.py ├── regex_iter_finditer.py ├── set_Difference.py ├── set_SymmetricDifference.py ├── swap_case.py ├── the_Captains_Room.py └── whats_your_name.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /Alphabet_Rangoli.py: -------------------------------------------------------------------------------- 1 | def print_rangoli(size): 2 | alp = 'abcdefghijklmnopqrstuvwxyz' 3 | for i in range(size-1,-size,-1): 4 | temp = '-'.join(alp[size-1:abs(i):-1]+alp[abs(i):size]) 5 | print(temp.center(4*size-3,'-')) -------------------------------------------------------------------------------- /Arithmetic_Operators.py: -------------------------------------------------------------------------------- 1 | a = int(input()) 2 | b = int(input()) 3 | 4 | print(a+b) 5 | print(a-b) 6 | print(a*b) 7 | -------------------------------------------------------------------------------- /Array_Mathematics.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | elements = raw_input().split() 3 | int_elements = list(map(int,elements)) 4 | n_A = [] 5 | for i in range(int_elements[0]): 6 | N_ = raw_input().split() 7 | n = list(map(int,N_)) 8 | n_A.append(n) 9 | m_B = [] 10 | for i in range(int_elements[0]): 11 | M_ = raw_input().split() 12 | m = list(map(int,M_)) 13 | m_B.append(m) 14 | a = np.array(n_A) 15 | b = np.array(m_B) 16 | 17 | print np.add(a,b) 18 | print np.subtract(a,b) 19 | print np.multiply(a,b) 20 | print np.divide(a,b) 21 | print np.mod(a,b) 22 | print np.power(a,b) 23 | -------------------------------------------------------------------------------- /Arrays.py: -------------------------------------------------------------------------------- 1 | def arrays(arr): 2 | import numpy as np 3 | arr.reverse() 4 | fl = list(map(float,arr)) 5 | res = np.array(fl) 6 | return(res) -------------------------------------------------------------------------------- /Collections_Counter_Alternative.py: -------------------------------------------------------------------------------- 1 | import collections 2 | 3 | numShoes = int(input()) 4 | shoes = collections.Counter(map(int, input().split())) 5 | numCust = int(input()) 6 | 7 | income = 0 8 | 9 | for i in range(numCust): 10 | size, price = map(int, input().split()) 11 | if shoes[size]: 12 | income += price 13 | shoes[size] -= 1 14 | 15 | print (income) -------------------------------------------------------------------------------- /CompanyLogo.py: -------------------------------------------------------------------------------- 1 | def companyLogo(s): 2 | literal_cnts = {} 3 | dist_letters = set(s) 4 | for char in dist_letters: 5 | literal_cnts[char] = s.count(char) 6 | sorted_kv = sorted(literal_cnts.items(),key=lambda x: (x[1],-ord(x[0])),reverse=True) 7 | top_3 = sorted_kv[:3] 8 | if top_3[0][1] == top_3[1][1] and top_3[0][1] > top_3[2][1]: 9 | top_3[:2] = sorted(top_3[:2]) 10 | elif top_3[1][1] == top_3[2][1]: 11 | top_3[1:] = sorted(top_3[1:]) 12 | elif top_3[0][1] == top_3[1][1] == top_3[2][1]: 13 | top_3 = sorted(top_3) 14 | for k,v in top_3: 15 | print (k,v) 16 | 17 | if __name__ == '__main__': 18 | 19 | S = input() 20 | companyLogo(S) -------------------------------------------------------------------------------- /Concatenate.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | elements = raw_input().split() 3 | N = int(elements[0]) 4 | M = int(elements[1]) 5 | P = int(elements[2]) 6 | lt = [] 7 | for i in range(N): 8 | N_ = raw_input().split() 9 | n = list(map(int,N_)) 10 | lt.append(n) 11 | mt = [] 12 | for i in range(M): 13 | M_ = raw_input().split() 14 | m = list(map(int,M_)) 15 | mt.append(m) 16 | 17 | result = np.concatenate((lt,mt)).reshape(N+M,P) 18 | print(result) 19 | -------------------------------------------------------------------------------- /Dot_and_Cross.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | N = int(raw_input()) 3 | arr_A = [] 4 | for i in range(N): 5 | inp = raw_input().split() 6 | user_inp = list(map(int,inp)) 7 | arr_A.append(user_inp) 8 | arr_B = [] 9 | for i in range(N): 10 | inp_ = raw_input().split() 11 | user_inp_ = list(map(int,inp_)) 12 | arr_B.append(user_inp_) 13 | 14 | print np.dot(arr_A,arr_B) 15 | -------------------------------------------------------------------------------- /Eye_and_Identity.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | elements = raw_input().split() 3 | int_elements = list(map(int,elements)) 4 | result = np.eye(int_elements[0],int_elements[1]) 5 | print result 6 | -------------------------------------------------------------------------------- /Find_Angle_MBC.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | # -*- coding: utf-8 -*- 3 | import math 4 | AB = int(input()) 5 | BC = int(input()) 6 | 7 | if AB == BC: 8 | print (round(math.degrees(math.asin(1/math.sqrt(2)))),chr(176),sep='') 9 | 10 | else: 11 | x = AB/BC 12 | print (round(math.degrees(math.atan(x))),chr(176),sep='') 13 | -------------------------------------------------------------------------------- /Find_The_RunnerUp_Score.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | n = int(input()) 3 | arr = map(int, input().split()) 4 | ar = list(arr) 5 | ar = list(set(ar)) 6 | m = max(ar) 7 | ar.remove(m) 8 | l = [] 9 | for i in range(len(ar)): 10 | l.append(m-ar[i]) 11 | least_dif = min(l) 12 | for i in range(len(l)): 13 | if(l[i] == least_dif): 14 | print (ar[i]) -------------------------------------------------------------------------------- /Find_The_Torsional_Angle.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | class Points(object): 4 | def __init__(self,x,y,z): 5 | self.x = x 6 | self.y = y 7 | self.z = z 8 | def __sub__(self, no): 9 | return Points(self.x - no.x,self.y - no.y,self.z - no.z) 10 | def dot(self,no): 11 | return (sum((self.x * no.x,self.y * no.y,self.z * no.z))) 12 | def cross(self,no): 13 | c = [self.y*no.z - self.z*no.y, 14 | self.z*no.x - self.x*no.z, 15 | self.x*no.y - self.y*no.x] 16 | return Points(*c) 17 | def absolute(self): 18 | return (pow((self.x ** 2 + self.y ** 2 + self.z ** 2), 0.5)) 19 | 20 | 21 | if __name__ == '__main__': 22 | points = list() 23 | for i in range(4): 24 | a = list(map(float, input().split())) 25 | points.append(a) 26 | 27 | a, b, c, d = Points(*points[0]), Points(*points[1]), Points(*points[2]), Points(*points[3]) 28 | x = (b - a).cross(c - b) 29 | y = (c - b).cross(d - c) 30 | angle = math.acos(x.dot(y) / (x.absolute() * y.absolute())) 31 | 32 | print("%.2f" % math.degrees(angle)) 33 | -------------------------------------------------------------------------------- /Find_a_String.py: -------------------------------------------------------------------------------- 1 | def count_substring(string, sub_st): 2 | ans=[1 for i in range(len(string)-len(sub_st)+1) if string[i:i+len(sub_st)] == sub_st] 3 | ans = sum(ans) 4 | return ans 5 | 6 | -------------------------------------------------------------------------------- /Finding_The_Percentage.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | n = int(input()) 3 | student_marks = {} 4 | for _ in range(n): 5 | name, *line = input().split() 6 | scores = list(map(float, line)) 7 | student_marks[name] = scores 8 | query_name = str(input()) 9 | print ("%.2f" %(round(float(sum(student_marks[query_name]))/float(len(student_marks[query_name])),2))) 10 | -------------------------------------------------------------------------------- /Floor_Ceil_Rint.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | elements = raw_input().split() 3 | int_elements = list(map(float,elements)) 4 | print np.floor(int_elements) 5 | print np.ceil(int_elements) 6 | print np.rint(int_elements) -------------------------------------------------------------------------------- /HTMLParser_Part1.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | from html.parser import HTMLParser 4 | class MyHTMLParser(HTMLParser): 5 | def handle_starttag(self, tag, attrs): 6 | print ('Start :',tag) 7 | for element in attrs: 8 | print ('->',element[0],'>',element[1]) 9 | 10 | def handle_endtag(self, tag): 11 | print ('End :',tag) 12 | 13 | def handle_startendtag(self, tag, attrs): 14 | print ('Empty :',tag) 15 | for element in attrs: 16 | print ('->',element[0],'>',element[1]) 17 | 18 | MyParser = MyHTMLParser() 19 | MyParser.feed(''.join([input().strip() for _ in range(int(input()))])) 20 | 21 | -------------------------------------------------------------------------------- /HTMLParser_Part2.py: -------------------------------------------------------------------------------- 1 | from html.parser import HTMLParser 2 | 3 | class MyHTMLParser(HTMLParser): 4 | def handle_comment(self, data): 5 | number_of_line = len(data.split('\n')) 6 | if number_of_line>1: 7 | print('>>> Multi-line Comment') 8 | else: 9 | print('>>> Single-line Comment') 10 | if data.strip(): 11 | print(data) 12 | 13 | def handle_data(self, data): 14 | if data.strip(): 15 | print(">>> Data") 16 | print(data) 17 | 18 | 19 | html = "" 20 | for i in range(int(input())): 21 | html += input().rstrip() 22 | html += '\n' 23 | 24 | parser = MyHTMLParser() 25 | parser.feed(html) 26 | parser.close() 27 | 28 | -------------------------------------------------------------------------------- /HelloWorld.py: -------------------------------------------------------------------------------- 1 | string = "Hello, World!" 2 | print (string) 3 | -------------------------------------------------------------------------------- /Hex_Color_Code.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | import re, sys 4 | [print(j) for i in sys.stdin for j in re.findall('[\s:](#[a-f0-9]{6}|#[a-f0-9]{3})', i, re.I)] 5 | 6 | -------------------------------------------------------------------------------- /Inner_and_Outer.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | inp = raw_input().split() 3 | inp_ = raw_input().split() 4 | inp_A = list(map(int,inp)) 5 | inp_B = list(map(int,inp_)) 6 | print np.inner(inp_A,inp_B) 7 | print np.outer(inp_A,inp_B) -------------------------------------------------------------------------------- /Iterable_and_Iterators_probability.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | 3 | N = int(input()) 4 | letters = ''.join(input().strip().split()) 5 | k = int(input()) 6 | 7 | combos = list(itertools.combinations(letters,k)) 8 | sample_space = len(combos) 9 | 10 | contains_a_sum = 0 11 | for combination in combos: 12 | if 'a' in combination: 13 | contains_a_sum+=1 14 | 15 | print ('%.3f'%(contains_a_sum/sample_space)) 16 | -------------------------------------------------------------------------------- /Linear_Algebra.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | N = int(raw_input()) 3 | arr = [] 4 | for i in range(N): 5 | inputs = raw_input().split() 6 | user_inp = list(map(float,inputs)) 7 | arr.append(user_inp) 8 | array = np.array(arr).reshape(N,N) 9 | print np.linalg.det(array) -------------------------------------------------------------------------------- /List_Comprehensions.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | x = int(input()) 3 | y = int(input()) 4 | z = int(input()) 5 | n = int(input()) 6 | 7 | print ([ [i, j, k] for i in range(x + 1) for j in range(y + 1) for k in range(z + 1) if ((i + j + k) != n)]) 8 | -------------------------------------------------------------------------------- /Lists.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | N = int(input()) 3 | l = [] 4 | for i in range(N): 5 | name = input() 6 | spl = str.split(name) 7 | if len(spl) == 3: 8 | com = str(spl[0]) 9 | ind = int(spl[1]) 10 | obj = int(spl[2]) 11 | elif len(spl) == 2: 12 | com = str(spl[0]) 13 | obj = int(spl[1]) 14 | elif len(spl) == 1: 15 | com = str(spl[0]) 16 | if com == "insert": 17 | l.insert(ind,obj) 18 | elif com == "print": 19 | print (l) 20 | elif com == "remove": 21 | l.remove(obj) 22 | elif com == "append": 23 | l.append(obj) 24 | elif com == "sort": 25 | l.sort() 26 | elif com == "pop": 27 | l.pop() 28 | else: 29 | l.reverse() 30 | -------------------------------------------------------------------------------- /Loops.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | for i in range(N): 3 | print (i**2) 4 | -------------------------------------------------------------------------------- /Maximize_It.py: -------------------------------------------------------------------------------- 1 | 2 | # Enter your code here. Read input from STDIN. Print output to STDOUT 3 | import itertools 4 | 5 | K, M = list(map(int,input().strip().split())) 6 | X = [] 7 | for i in range(K): 8 | X.append(list(map(int,input().strip().split()))) 9 | 10 | X = [i[1:] for i in X] 11 | cartesian_product = itertools.product(*X) 12 | function_results = list(map(lambda z: sum(p**2 for p in z)%(M),cartesian_product)) 13 | print (max(function_results)) -------------------------------------------------------------------------------- /Mean_Var_StandardDeviation.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | inp = raw_input().split() 3 | N,M = list(map(int,inp)) 4 | arr = [] 5 | for i in range(N): 6 | user_inp = raw_input().split() 7 | user_int = list(map(int,user_inp)) 8 | arr.append(user_int) 9 | array = np.array(arr).reshape(N,M) 10 | print np.mean(array,axis = 1) 11 | print np.var(array,axis = 0) 12 | print np.std(array) -------------------------------------------------------------------------------- /Min_and_Max.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | inp = raw_input().split() 3 | N,M = list(map(int,inp)) 4 | arr = [] 5 | for i in range(N): 6 | user_inp = raw_input().split() 7 | user_int = list(map(int,user_inp)) 8 | arr.append(user_int) 9 | arr_trans = np.array(arr).reshape(N,M) 10 | array = np.min(arr_trans,axis = 1) 11 | result = max(array) 12 | print result 13 | -------------------------------------------------------------------------------- /Mutations.py: -------------------------------------------------------------------------------- 1 | def mutate_string(string, position, character): 2 | l = list(string) 3 | l[position] = character 4 | string = ''.join(l) 5 | return string 6 | -------------------------------------------------------------------------------- /Nested_Lists.py: -------------------------------------------------------------------------------- 1 | N = int(raw_input()) 2 | nested_list = [] 3 | for i in range(N): 4 | name = str(raw_input()) 5 | score = float(raw_input()) 6 | nested_list.append([name,score]) 7 | marks = [] 8 | for i in range(N): 9 | marks.append(nested_list[i][1]) 10 | marks = list(set(marks)) 11 | marks.sort() 12 | second_lowest = marks[1] 13 | name_list = [] 14 | for i in range(N): 15 | if(nested_list[i][1] == second_lowest): 16 | name_list.append(nested_list[i][0]) 17 | name_list.sort() 18 | print "\n".join(name_list) 19 | -------------------------------------------------------------------------------- /PilingUp_production.py: -------------------------------------------------------------------------------- 1 | T = int(input()) 2 | for t in range(T): 3 | N = int(input()) 4 | lst = list(map(int, input().strip().split())) 5 | L = len(lst) 6 | i = 0 7 | while i < L - 1 and lst[i] >= lst[i+1]: 8 | i += 1 9 | while i < L - 1 and lst[i] <= lst[i+1]: 10 | i += 1 11 | print ("Yes" if i == L - 1 else "No") -------------------------------------------------------------------------------- /Polynomials.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | Coef = raw_input().split() 3 | X = float(raw_input()) 4 | coef = list(map(float,Coef)) 5 | print np.polyval(coef,X) -------------------------------------------------------------------------------- /Print_Function.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | m = str(N) 3 | for i in range(1,N,1): 4 | j = N-i 5 | m = str(j) + m 6 | print(m) -------------------------------------------------------------------------------- /Python_Division.py: -------------------------------------------------------------------------------- 1 | a = int(input()) 2 | b = int(input()) 3 | 4 | print(a+b) 5 | print(a-b) 6 | print(a*b) 7 | -------------------------------------------------------------------------------- /Python_IfElse.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | if(n%2 != 0): 3 | print("Weird") 4 | else: 5 | if(n>=2 and n<=5): 6 | print("Not Weird") 7 | elif(n>=6 and n<=20): 8 | print("Weird") 9 | else: 10 | print("Not Weird") -------------------------------------------------------------------------------- /Python_String_Formatting.py: -------------------------------------------------------------------------------- 1 | def print_formatted(number): 2 | w = len(bin(number)[2:]) 3 | for n in range(1,number+1): 4 | print (str(n).rjust(w,' '),str(oct(n)[2:]).rjust(w,' '), 5 | str(hex(n)[2:].upper()).rjust(w,' '),str(bin(n)[2:]).rjust(w,' '),sep=' ') 6 | 7 | if __name__ == '__main__': 8 | n = int(input()) 9 | print_formatted(n) 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Hackerrank-Solutions 2 | 3 | #### Code files: 4 | 5 | + [Say Hello, World! With Python](HelloWorld.py) 6 | + [Python If-Else](Python_IfElse.py) 7 | + [Arithmetic Operators](Arithmetic_Operators.py) 8 | + [Python: Division](Python_Division.py) 9 | + [Loops](Loops.py) 10 | + [Write a function](Write_a_Function.py) 11 | + [Print Function](Print_Function.py) 12 | + [List Comprehensions](List_Comprehensions.py) 13 | + [Find the Runner Up Score](Find_The_RunnerUp_Score.py) 14 | + [Nested Lists](Nested_Lists.py) 15 | + [Finding the Percentage](Finding_The_Percentage.py) 16 | + [Lists](Lists.py) 17 | + [Tuples](Tuples.py) 18 | + [sWAP cASE](swap_case.py) 19 | + [String Split and Join](String_Split_and_Join.py) 20 | + [What's your name](whats_your_name.py) 21 | + [Mutations](Mutations.py) 22 | + [Find a String](Find_a_String.py) 23 | + [String Validators](String_Validators.py) 24 | + [Text Alignment](Text_Alignment.py) 25 | + [Text Wrap](Text_Wrap.py) 26 | + [Alphabet Rangoli](Alphabet_Rangoli.py) 27 | + [Arrays](Arrays.py) 28 | + [Shape and Reshape](Shape_and_Reshape.py) 29 | + [Transpose and Flatten](Transpose_and_Flatten.py) 30 | + [Concatenate](Concatenate.py) 31 | + [Zeros and Ones](Zeros_and_Ones.py) 32 | + [Eye and Identity](Eye_and_Identity.py) 33 | + [Array Mathematics](Array_Mathematics.py) 34 | + [Floor,Ceil and Rint](Floor_Ceil_Rint.py) 35 | + [Sum and Prod](Sum_and_Prod.py) 36 | + [Min and Max](Min_and_Max.py) 37 | + [Mean, Variance and Standard Deviation](Mean_Var_StandardDeviation.py) 38 | + [Dot and Cross](Dot_and_Cross.py) 39 | + [Inner and Outer](Inner_and_Outer.py) 40 | + [Polynomials](Polynomials.py) 41 | + [Linear Algebra](Linear_Algebra.py) 42 | + [String Formatting](Python_String_Formatting.py) 43 | + [Capitalize!](String_Capitalize.py) 44 | + [The Minion Game](minion_Game.py) 45 | + [Merge the Tools](mergeTools.py) 46 | + [itertools.product()](itertools_product.py) 47 | + [collections.counter()](collections_counter.py) 48 | + [itertools.permutations()](itertools_permutations.py) 49 | + [Polar Coordinates](polar_coordinates.py) 50 | + [DefaultDict Tutorial](collections_defaultdict_Production.py) 51 | + [Collections.namedtuple()](py_collections_namedtuple.py) 52 | + [Find Angle MBC](Find_Angle_MBC.py) 53 | + [Collections.OrderedDict()](py_collections_OrderedDict.py) 54 | + [itertools.combinations()](itertools_combinations.py) 55 | + [itertools.combinations_with_replacement()](itertools_combinationsWithReplacement.py) 56 | + [Word Order](Word_Order.py) 57 | + [Collections.deque()](collections_deque.py) 58 | + [Compress the string!](itertools_groupby.py) 59 | + [CompanyLogo](CompanyLogo.py) 60 | + [Piling Up!](PilingUp_production.py) 61 | + [Triangle Quest 2](Triangle_Quest2.py) 62 | + [iterables and iterators](Iterable_and_Iterators_probability.py) 63 | + [Mod Divmod](python_mod_divmod.py) 64 | + [Power - Mod Power](python_power_modpower.py) 65 | + [Maximize it!](Maximize_It.py) 66 | + [set.difference() Operation](set_Difference.py) 67 | + [Integers Come in All Sizes](python_integers_come_in_all_sizes.py) 68 | + [Set.symmetric_difference() Operation](set_SymmetricDifference.py) 69 | + [Set Mutations](py_set_mutations.py) 70 | + [Triangle Quest](Triangle_Quest.py) 71 | + [The Captain's Room](the_Captains_Room.py) 72 | + [Check Subset](check_Subset.py) 73 | + [Check Strict Superset](py_check_StrictSuperSet.py) 74 | + [Find the Torsional Angle](Find_The_Torsional_Angle.py) 75 | + [ginortS](ginortS.py) 76 | + [Group(),Groups() and GroupDict()](regex_groups.py) 77 | + [re.findall() & re.finditer()](regex_iter_finditer.py) 78 | + [Hex Color Code](Hex_Color_Code.py) 79 | + [HTML Parser - Part 1](HTMLParser_Part1.py) 80 | + [HTML Parser - Part 2](HTMLParser_Part2.py) 81 | + [Detect HTML Tags, Attributes and Attribute Values](detect_html_tags_attributes.py) 82 | 83 | -------------------------------------------------------------------------------- /Shape_and_Reshape.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | arr = input().strip().split(' ') 3 | my_array = np.array(list(map(int,arr))) 4 | new_array = my_array.reshape(3,3) 5 | print (new_array) -------------------------------------------------------------------------------- /Standardize_Mobile_Numbers_Decorators.py: -------------------------------------------------------------------------------- 1 | def wrapper(f): 2 | def fun(l): 3 | len_numbers = [len(phone_num) for phone_num in l] 4 | for i in range(len(l)): 5 | if len_numbers[i]==10: 6 | l[i] = '+91' + ' ' + l[i][:5] + ' ' + l[i][5:] 7 | elif len_numbers[i]==11: 8 | l[i] = '+91' + ' ' + l[i][1:][:5] + ' ' + l[i][1:][5:] 9 | elif len_numbers[i]==12: 10 | l[i] = '+91' + ' ' + l[i][2:][:5] + ' ' + l[i][2:][5:] 11 | elif len_numbers[i]==13: 12 | l[i] = '+91' + ' ' + l[i][3:][:5] + ' ' + l[i][3:][5:] 13 | return f(l) 14 | return fun 15 | 16 | @wrapper 17 | def sort_phone(l): 18 | print(*sorted(l), sep='\n') 19 | 20 | if __name__ == '__main__': 21 | l = [input() for _ in range(int(input()))] 22 | sort_phone(l) 23 | -------------------------------------------------------------------------------- /String_Capitalize.py: -------------------------------------------------------------------------------- 1 | import os 2 | # Complete the solve function below. 3 | def solve(s): 4 | splitted_s = s.split(" ") 5 | return (" ".join(i.capitalize() for i in splitted_s)) 6 | 7 | if __name__ == '__main__': 8 | fptr = open(os.environ['OUTPUT_PATH'], 'w') 9 | 10 | s = input() 11 | 12 | result = solve(s) 13 | 14 | fptr.write(result + '\n') 15 | 16 | fptr.close() 17 | -------------------------------------------------------------------------------- /String_Split_and_Join.py: -------------------------------------------------------------------------------- 1 | def split_and_join(line): 2 | s = line.split(" ") 3 | result = '-'.join(s) 4 | return result 5 | -------------------------------------------------------------------------------- /String_Validators.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | s = input() 3 | sl = list(s) 4 | print (any(c.isalnum() for c in sl)) 5 | print (any(c.isalpha() for c in sl)) 6 | print (any(c.isdigit() for c in sl)) 7 | print (any(c.islower() for c in sl)) 8 | print (any(c.isupper() for c in sl)) -------------------------------------------------------------------------------- /Sum_and_Prod.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | elements = raw_input().split() 3 | N,M = list(map(int,elements)) 4 | n = [] 5 | for i in range(N): 6 | N_ = raw_input().split() 7 | n_ = list(map(int,N_)) 8 | n.append(n_) 9 | sm = np.sum(n,axis = 0) 10 | print np.prod(sm) -------------------------------------------------------------------------------- /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 | #Top Cone 7 | for i in range(thickness): 8 | print((c*i).rjust(thickness-1)+c+(c*i).ljust(thickness-1)) 9 | 10 | #Top Pillars 11 | for i in range(thickness+1): 12 | print((c*thickness).center(thickness*2)+(c*thickness).center(thickness*6)) 13 | 14 | #Middle Belt 15 | for i in range((thickness+1)//2): 16 | print((c*thickness*5).center(thickness*6)) 17 | 18 | #Bottom Pillars 19 | for i in range(thickness+1): 20 | print((c*thickness).center(thickness*2)+(c*thickness).center(thickness*6)) 21 | 22 | #Bottom Cone 23 | for i in range(thickness): 24 | print(((c*(thickness-i-1)).rjust(thickness)+c+(c*(thickness-i-1)).ljust(thickness)).rjust(thickness*6)) -------------------------------------------------------------------------------- /Text_Wrap.py: -------------------------------------------------------------------------------- 1 | def wrap(string, max_width): 2 | return '\n'.join(textwrap.wrap(string, max_width)) 3 | -------------------------------------------------------------------------------- /Transpose_and_Flatten.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | dim = input().strip().split(' ') 3 | dim_ = list(map(int,dim)) 4 | _ent_ = [] 5 | for i in range(dim_[0]): 6 | ent = input().strip().split(' ') 7 | ent_ = list(map(int,ent)) 8 | _ent_.append(ent_) 9 | result = np.array(_ent_).reshape(dim_[0],dim_[1]) 10 | print (result.T) 11 | print (result.flatten()) -------------------------------------------------------------------------------- /Triangle_Quest.py: -------------------------------------------------------------------------------- 1 | for i in range(1,int(input())): #More than 2 lines will result in 0 score. 2 | print (int(i * (pow(10,i)-1)/9)) -------------------------------------------------------------------------------- /Triangle_Quest2.py: -------------------------------------------------------------------------------- 1 | 2 | for i in range(1,int(input())+1): #More than 2 lines will result in 0 score. 3 | print (((10**i-1)//9)**2) 4 | 5 | 6 | -------------------------------------------------------------------------------- /Tuples.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | n = int(input()) 3 | integer_list = map(int, input().split()) 4 | tup = tuple(integer_list) 5 | print(hash(tup)) 6 | -------------------------------------------------------------------------------- /Validate_Postal_Codes.py: -------------------------------------------------------------------------------- 1 | regex_integer_in_range = r'^[1-9][\d]{5}$' 2 | regex_alternating_repetitive_digit_pair = r'(\d)(?=\d\1)' 3 | 4 | 5 | import re 6 | P = input() 7 | 8 | print (bool(re.match(regex_integer_in_range, P)) 9 | and len(re.findall(regex_alternating_repetitive_digit_pair, P)) < 2) 10 | -------------------------------------------------------------------------------- /Validating_CreditCard_Numbers.py: -------------------------------------------------------------------------------- 1 | import re 2 | TESTER = re.compile( 3 | r"^" 4 | r"(?!.*(\d)(-?\1){3})" 5 | r"[456]" 6 | r"\d{3}" 7 | r"(?:-?\d{4}){3}" 8 | r"$") 9 | for _ in range(int(input().strip())): 10 | print("Valid" if TESTER.search(input().strip()) else "Invalid") -------------------------------------------------------------------------------- /Validating_UID.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | import re 4 | T = int(input()) 5 | for tcase in range(T): 6 | UID = input() 7 | UID = ''.join(sorted(UID)) 8 | try: 9 | assert re.search(r'[A-Z]{2}', UID) 10 | assert re.search(r'\d\d\d', UID) 11 | assert not re.search(r'[^a-zA-Z0-9]', UID) 12 | assert not re.search(r'(.)\1',UID) 13 | assert len(UID) == 10 14 | except: 15 | print('Invalid') 16 | else: 17 | print('Valid') -------------------------------------------------------------------------------- /Word_Order.py: -------------------------------------------------------------------------------- 1 | from collections import OrderedDict 2 | n = int(input()) 3 | distinct_words = OrderedDict() 4 | for m in range(n): 5 | word = input() 6 | if word not in distinct_words: 7 | distinct_words[word] = word 8 | else: 9 | distinct_words[word] = distinct_words[word] + ' ' + word 10 | 11 | distinct_keys = len(distinct_words.keys()) 12 | distOccurrences = ' '.join(str(len(v.split(' '))) for k,v in distinct_words.items()) 13 | print (distinct_keys) 14 | print (distOccurrences) -------------------------------------------------------------------------------- /Write_a_Function.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | if(year%100 == 0): 4 | if(year%400 == 0): 5 | leap = True 6 | else: 7 | if(year%4 == 0): 8 | leap = True 9 | return leap 10 | 11 | -------------------------------------------------------------------------------- /Zeros_and_Ones.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | elements = raw_input().split() 3 | int_elements = list(map(int,elements)) 4 | List = tuple(int_elements) 5 | zeroes = np.zeros(List,dtype = np.int) 6 | print zeroes 7 | Ones = np.ones(List,dtype = np.int) 8 | print Ones -------------------------------------------------------------------------------- /check_Subset.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | T = int(input()) 4 | for tcase in range(T): 5 | nA = int(input()) 6 | nA_elements = set(map(int,input().strip().split())) 7 | nB = int(input()) 8 | nB_elements = set(map(int,input().strip().split())) 9 | if nA_elements.issubset(nB_elements): 10 | print (True) 11 | else: 12 | print (False) 13 | -------------------------------------------------------------------------------- /collections_counter.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | X = int(input()) 4 | shoe_sizes = list(map(int,input().strip().split())) 5 | 6 | N = int(input()) 7 | 8 | earned_amount = [] 9 | for n in range(N): 10 | desired_size,price = list(map(int,input().strip().split())) 11 | if desired_size in shoe_sizes: 12 | earned_amount.append(price) 13 | shoe_sizes.remove(desired_size) 14 | 15 | print (sum(earned_amount)) 16 | -------------------------------------------------------------------------------- /collections_defaultdict.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | from collections import defaultdict 3 | n,m = list(map(int,input().strip().split())) 4 | 5 | wordsA = [] 6 | wordsB = [] 7 | for A in range(n): 8 | wordsA.append(input()) 9 | for B in range(m): 10 | wordsB.append(input()) 11 | 12 | inds = defaultdict(list) 13 | for b in wordsB: 14 | if b in wordsA: 15 | inds[b].extend([str(k+1) for k,v in enumerate(wordsA) if v == b]) 16 | else: 17 | inds[b].append(str(-1)) 18 | for key in inds: 19 | print (' '.join(inds[key])) 20 | -------------------------------------------------------------------------------- /collections_defaultdict_Production.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | d = defaultdict(list) 3 | list1=[] 4 | 5 | n, m = map(int,raw_input().split()) 6 | 7 | for i in range(0,n): 8 | d[raw_input()].append(i+1) 9 | 10 | for i in range(0,m): 11 | list1=list1+[raw_input()] 12 | 13 | for i in list1: 14 | if i in d: 15 | print " ".join( map(str,d[i]) ) 16 | else: 17 | print -1 18 | -------------------------------------------------------------------------------- /collections_deque.py: -------------------------------------------------------------------------------- 1 | 2 | from collections import deque 3 | N = int(input()) 4 | dequed = deque() 5 | for ops in range(N): 6 | objs = input().strip().split() 7 | if len(objs) == 2: 8 | operation_name,data = objs[0],objs[1] 9 | else: 10 | operation_name = objs[0] 11 | if operation_name=='append': 12 | dequed.append(data) 13 | elif operation_name == 'pop': 14 | dequed.pop() 15 | elif operation_name == 'popleft': 16 | dequed.popleft() 17 | elif operation_name == 'appendleft': 18 | dequed.appendleft(data) 19 | 20 | print (' '.join(str(i) for i in dequed)) -------------------------------------------------------------------------------- /detect_html_tags_attributes.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | 3 | from html.parser import HTMLParser 4 | class MyHTMLParser(HTMLParser): 5 | def handle_starttag(self, tag, attrs): 6 | print(tag) 7 | [print('-> {} > {}'.format(*attr)) for attr in attrs] 8 | 9 | html = '\n'.join([input() for _ in range(int(input()))]) 10 | parser = MyHTMLParser() 11 | parser.feed(html) 12 | parser.close() 13 | -------------------------------------------------------------------------------- /ginortS.py: -------------------------------------------------------------------------------- 1 | def sort_String(s): 2 | lower_case = [] 3 | upper_case = [] 4 | odd_digits = [] 5 | even_digits = [] 6 | for char in s: 7 | if char.islower(): 8 | lower_case.append(char) 9 | elif char.isupper(): 10 | upper_case.append(char) 11 | elif char.isdigit(): 12 | if int(char)%2==0: 13 | even_digits.append(char) 14 | else: 15 | odd_digits.append(char) 16 | return (''.join(sorted(lower_case)+sorted(upper_case)+sorted(odd_digits)+sorted(even_digits))) 17 | 18 | if __name__=='__main__': 19 | 20 | S = input() 21 | print (sort_String(S)) -------------------------------------------------------------------------------- /itertools_combinations.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | 3 | strg,k = input().strip().split() 4 | strg = ''.join(sorted(strg)) 5 | combs = [] 6 | for c in range(1,int(k)+1): 7 | combs.extend(list(itertools.combinations(strg,c))) 8 | for combination in combs: 9 | print (''.join(combination)) 10 | -------------------------------------------------------------------------------- /itertools_combinationsWithReplacement.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | from itertools import combinations_with_replacement 3 | 4 | s, k = input().strip().split() 5 | 6 | for c in combinations_with_replacement(sorted(s), int(k)): 7 | print("".join(c)) 8 | 9 | 10 | -------------------------------------------------------------------------------- /itertools_groupby.py: -------------------------------------------------------------------------------- 1 | from itertools import groupby 2 | 3 | S = input() 4 | compress_str = [list(v) for k,v in groupby(S)] 5 | ans = [] 6 | for C in compress_str: 7 | ans.append(str((len(C),int(C[0])))) 8 | print (' '.join(ans)) -------------------------------------------------------------------------------- /itertools_permutations.py: -------------------------------------------------------------------------------- 1 | # Enter your code here. Read input from STDIN. Print output to STDOUT 2 | import itertools 3 | 4 | strg,k = input().strip().split() 5 | strg = ''.join(sorted(strg)) 6 | perms = list(itertools.permutations(strg,int(k))) 7 | for p in perms: 8 | print (''.join(p)) -------------------------------------------------------------------------------- /itertools_product.py: -------------------------------------------------------------------------------- 1 | from itertools import product 2 | 3 | A = list(map(int,input().strip().split())) 4 | B = list(map(int,input().strip().split())) 5 | 6 | ans = [str(i) for i in list(product(A,B))] 7 | print (' '.join(ans)) 8 | 9 | -------------------------------------------------------------------------------- /matrixScript.py: -------------------------------------------------------------------------------- 1 | 2 | #!/bin/python3 3 | import re 4 | 5 | def matrix_Script(matrix): 6 | matrix = [list(i) for i in matrix] 7 | col_wise = [] 8 | for ind in range(len(matrix[0])): 9 | col_wise.append([row[ind] for row in matrix]) 10 | final_string = '' 11 | for ind in range(len(matrix[0])): 12 | final_string = final_string + ''.join(col_wise[ind]) 13 | final_string = re.sub(r"(?<=\w)([^\w]+)(?=\w)"," ",final_string) 14 | return (final_string) 15 | 16 | if __name__ == '__main__': 17 | nm = input().split() 18 | 19 | n = int(nm[0]) 20 | 21 | m = int(nm[1]) 22 | 23 | matrix = [] 24 | 25 | for _ in range(n): 26 | matrix_item = input() 27 | matrix.append(matrix_item) 28 | print (matrix_Script(matrix)) -------------------------------------------------------------------------------- /mergeTools.py: -------------------------------------------------------------------------------- 1 | from collections import OrderedDict 2 | 3 | def merge_tools(STR,K): 4 | len_S = len(STR) 5 | segment_list = [] 6 | C=0 7 | while (C < len_S): 8 | segment_list.append(STR[C:C+K]) 9 | C+=K 10 | for seg in segment_list: 11 | print (''.join(OrderedDict.fromkeys(seg))) 12 | 13 | 14 | if __name__ == '__main__': 15 | string, k = input(), int(input()) 16 | merge_tools(string, k) -------------------------------------------------------------------------------- /minion_Game.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | import string 3 | def minionGame(STR): 4 | stuart_score = 0 5 | kevin_score = 0 6 | combs_list = [] 7 | len_S = len(STR) 8 | for v in range(1,len_S+1): 9 | combins = list(itertools.combinations(STR,v)) 10 | combins = [''.join(r) for r in combins] 11 | combs_list.extend(combins) 12 | for c in combs_list: 13 | if c[0].isupper(): 14 | if c[0] in set(string.ascii_uppercase)-set('AEIOU'): 15 | stuart_score+=1 16 | else: 17 | kevin_score+=1 18 | if stuart_score>kevin_score: 19 | return ('Stuart', stuart_score) 20 | elif stuart_score