├── .idea ├── HackerRank Python.iml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── vcs.xml └── workspace.xml ├── 1_1.txt ├── Built-Ins ├── eval.py ├── input.py ├── sortData.py └── zipped.py ├── CTCI ├── DetectLoop.java ├── LeftRotation.java ├── LinkedListNode.java ├── bracket-validator.py ├── leftrotation.py ├── lonelyinteger.py ├── making_anagrams.py └── ransomnote.py ├── Capitalize.py ├── Collections ├── collectionsDeque.py ├── counter.py ├── namedTuple.py └── orderedDictionary.py ├── Math ├── integersInAllSizes.py ├── modDivMod.py ├── polarCoordinates.py ├── powerModPower.py ├── triangleQuest.py └── triangleQuest2.py ├── README.md ├── Sets ├── IntroductionToSets.py ├── checkSubset.py ├── discardRemovePop.py ├── noIdea.py ├── setDifference.py ├── setDotAdd.py ├── setIntersection.py ├── setMutations.py ├── setUnion.py ├── strictSuperSet.py └── symmetricDiff.py ├── angle_clock_hands.py ├── buzzfizz.py ├── challenges └── hacker_rank_challenge_1.py ├── christmas_tree.py ├── findString.py ├── findingThePercentage.py ├── findingThePercentageUsingDict.py ├── itertools ├── combinations.py ├── combinationsWithRep.py ├── itertoolsProduct.py ├── permutations.py └── stringCompress.py ├── printFunction.py ├── pythonLists.py ├── pythonNestedLists.py ├── pythonSets.py ├── pythontuples.py ├── sWAPcASE.py ├── secondLargestNumber.py ├── shift.py ├── stringFormatting.py ├── stringMutations.py ├── stringSplitAndJoin.py ├── stringValidators.py ├── textAlignment.py └── textFillAndWrap.py /.idea/HackerRank Python.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 15 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 59 | 60 | 61 | 63 | 64 | 110 | 111 | 112 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | Python 124 | 125 | 126 | 127 | 128 | PyCompatibilityInspection 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 168 | 169 | 170 | 171 | 174 | 175 | 178 | 179 | 180 | 181 | 184 | 185 | 188 | 189 | 192 | 193 | 194 | 195 | 198 | 199 | 202 | 203 | 206 | 207 | 208 | 209 | 212 | 213 | 216 | 217 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 249 | 250 | 266 | 267 | 283 | 284 | 300 | 301 | 317 | 318 | 334 | 335 | 346 | 347 | 365 | 366 | 384 | 385 | 405 | 406 | 427 | 428 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 1466891390766 478 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 511 | 514 | 515 | 516 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | -------------------------------------------------------------------------------- /1_1.txt: -------------------------------------------------------------------------------- 1 | 4 2 | Krishna 67 68 69 3 | Arjun 70 98 63 4 | Malika 52 56 60 5 | Kyrie 98 89 90 6 | Malika -------------------------------------------------------------------------------- /Built-Ins/eval.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | data = raw_input() 3 | eval(data) -------------------------------------------------------------------------------- /Built-Ins/input.py: -------------------------------------------------------------------------------- 1 | x, k = map(int, raw_input().split()) 2 | #print x,k 3 | print(input() == k) -------------------------------------------------------------------------------- /Built-Ins/sortData.py: -------------------------------------------------------------------------------- 1 | #use of sorted, key and lambda 2 | rows, cols = map(int, raw_input().split()) 3 | lst = list() 4 | sortedList = list() 5 | for iterator in range(rows): 6 | lst.append(map(int, raw_input().split())) 7 | k = int(raw_input()) 8 | sortedList = sorted(lst, key=lambda x: x[k]) 9 | for item in sortedList: 10 | print " ".join(map(str, item)) -------------------------------------------------------------------------------- /Built-Ins/zipped.py: -------------------------------------------------------------------------------- 1 | #use of zip 2 | data = raw_input() 3 | sumOfScores = 0 4 | finalList = list() 5 | anotherList = list() 6 | avgList = list() 7 | 8 | first = list(int(i) for i in data.split()) 9 | numCourses = first[1] 10 | numStudents = first[0] 11 | 12 | for i in range(numCourses): 13 | finalList.append(raw_input().split()) 14 | anotherList = zip(*finalList) 15 | 16 | for i in range(numStudents): 17 | for item in anotherList[i]: 18 | sumOfScores += float(item) 19 | avg = sumOfScores/numCourses 20 | avgList.append(avg) 21 | sumOfScores = 0 22 | for item in avgList: 23 | print item 24 | -------------------------------------------------------------------------------- /CTCI/DetectLoop.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Gautam on 7/3/16. 3 | */ 4 | public class CTCI2_6 5 | { 6 | private static LinkedListNode FindBeginning(LinkedListNode head) 7 | { 8 | LinkedListNode slow = head; 9 | LinkedListNode fast = head; 10 | 11 | //while fast.next!=null #GoodShit 12 | while (fast != null && fast.next != null) 13 | { 14 | slow = slow.next; 15 | fast = fast.next.next; 16 | if (slow == fast) 17 | { 18 | System.out.println("HERE WE MEET AT " + slow.data); 19 | break; 20 | } 21 | } 22 | //Check if no loop exists 23 | if (fast == null || fast.next == null) 24 | { 25 | return null; 26 | } 27 | 28 | slow = head; 29 | while (slow != fast) 30 | { 31 | slow = slow.next; 32 | fast = fast.next; 33 | } 34 | // At this point slow=fast=starting of the loop 35 | return fast; 36 | } 37 | 38 | public static void main(String[] args) 39 | { 40 | int list_length = 100; 41 | int k = 45; 42 | //Generate List 43 | LinkedListNode[] nodes = new LinkedListNode[list_length]; 44 | for (int i = 0; i < list_length; i++) 45 | { 46 | nodes[i] = new LinkedListNode(i, null, i > 0 ? nodes[i - 1] : null); 47 | } 48 | 49 | //Force loop 50 | nodes[list_length - 1].next = nodes[list_length - k]; 51 | 52 | LinkedListNode loop = FindBeginning(nodes[0]); 53 | if (loop == null) 54 | { 55 | System.out.println("Lucky to survive. No loop found"); 56 | } 57 | else 58 | { 59 | System.out.println("Starting point of loop: " + loop.data); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /CTCI/LeftRotation.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Gautam on 10/2/16. 3 | */ 4 | public class LeftRotation 5 | { 6 | public static int[] rotate(int a[], int elements, int rotationParameter) 7 | { 8 | int temp[] = new int[elements]; 9 | 10 | for(int i=0; i" + next.printForward(); 45 | } else { 46 | return ((Integer) data).toString(); 47 | } 48 | } 49 | 50 | public LinkedListNode clone() { 51 | LinkedListNode next2 = null; 52 | if (next != null) { 53 | next2 = next.clone(); 54 | } 55 | LinkedListNode head2 = new LinkedListNode(data, next2, null); 56 | return head2; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /CTCI/bracket-validator.py: -------------------------------------------------------------------------------- 1 | def Evaluate(str): 2 | stack = [] 3 | set1, set2 = "<({[", ">)}]" 4 | for c in str : 5 | if c in set1: 6 | #print "c in pushChars, APPENDING: ", c 7 | stack.append(c) 8 | elif c in set2: 9 | #print "c in popChars: ", c 10 | if len(stack)==0: 11 | return False 12 | else: 13 | stackTop = stack.pop() 14 | #print "POPPING" , stackTop 15 | balancingBracket = set1[set2.index(c)] 16 | if stackTop != balancingBracket: 17 | #print "THIS NOW" 18 | return False 19 | #finally everything should be popped 20 | if len(stack)==0: 21 | return True 22 | else: 23 | return False 24 | 25 | result = Evaluate("

Still working on it

") 26 | print "Bracket Validator: ", result 27 | result = Evaluate("

Still working on it") 28 | print "Bracket Validator: ", result 29 | -------------------------------------------------------------------------------- /CTCI/leftrotation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | def array_left_rotation(a, n, k): 3 | temp = [] 4 | for item in range(n): 5 | temp.append(a[(item+k)%n]) 6 | return temp 7 | 8 | 9 | n, k = map(int, raw_input().strip().split(' ')) 10 | a = map(int, raw_input().strip().split(' ')) 11 | answer = array_left_rotation(a, n, k); 12 | print ' '.join(map(str,answer)) 13 | -------------------------------------------------------------------------------- /CTCI/lonelyinteger.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | def missing_drone(drones): 3 | unique_id = 0 4 | for drone in drones: 5 | unique_id ^= int(drone) 6 | return unique_id 7 | total_num = int(raw_input()) 8 | drones = raw_input().split() 9 | print missing_drone(drones) 10 | -------------------------------------------------------------------------------- /CTCI/making_anagrams.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from collections import Counter 3 | def diff(a, b): 4 | count_a = Counter(a) 5 | count_b = Counter(b) 6 | count_a.subtract(count_b) 7 | return sum(abs(k) for k in count_a.values()) 8 | 9 | a = raw_input('Enter first string: ').strip() 10 | b = raw_input('Enter second string: ').strip() 11 | 12 | number = diff(a, b) 13 | if number == 0: 14 | print 'Stupid. You entered two anagrams' 15 | else: 16 | print number, 'chars need to deleted' 17 | -------------------------------------------------------------------------------- /CTCI/ransomnote.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | def validate(mag_words, ransome_words, mag, ran): 3 | count_mag = {} 4 | count_ran = {} 5 | for item in mag: 6 | if item not in count_mag: 7 | count_mag[item] = 1 8 | else: 9 | count_mag[item] = count_mag[item] + 1 10 | print 'MAG: ', count_mag 11 | for item in ran: 12 | if item not in count_ran: 13 | count_ran[item] = 1 14 | else: 15 | count_ran[item] = count_ran[item] + 1 16 | print 'RAN ', count_ran 17 | for key,value in count_ran.items(): 18 | if key not in count_mag or count_mag[key] < value: 19 | return False 20 | else: 21 | return True 22 | 23 | 24 | mag_words, ransome_words = map(int, raw_input().split(' ')) 25 | mag_string = "give me one grand tonight or tomorrow".split() 26 | ransome_string = "give me one grand tonight or give me tomorrow".split() 27 | print validate(mag_words, ransome_words, mag_string, ransome_string) 28 | -------------------------------------------------------------------------------- /Capitalize.py: -------------------------------------------------------------------------------- 1 | input = raw_input().split(' ') 2 | #print input 3 | #print type(input) 4 | #countWord = 0 5 | #countNotWord = 0 6 | output = [] 7 | 8 | for word in input: 9 | if word: 10 | #countWord = countWord + 1 11 | #print 'word: ', countWord 12 | output.append(word[0].upper() + word[1:].lower()) 13 | else: 14 | #countNotWord = countNotWord +1 15 | #print 'not word: ', countNotWord 16 | output.append('') 17 | 18 | print ' '.join(output) -------------------------------------------------------------------------------- /Collections/collectionsDeque.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | d = deque() 3 | for i in range(int(raw_input())): 4 | command = raw_input().split() 5 | if command[0] == 'pop': 6 | d.pop() 7 | elif command[0] == 'append': 8 | d.append(command[1]) 9 | elif command[0] == 'appendleft': 10 | d.appendleft(command[1]) 11 | elif command[0] == 'popleft': 12 | d.popleft() 13 | print ' '.join(d) 14 | -------------------------------------------------------------------------------- /Collections/counter.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | items = int(raw_input()) 3 | sizes = Counter(map(int, raw_input().split())) 4 | #print sizes 5 | customerCount = int(raw_input()) 6 | rate = [] 7 | for i in range(customerCount): 8 | rate.append(map(int, raw_input().split())) 9 | #print rate 10 | earnings = 0 11 | for item in rate: 12 | if sizes[item[0]] > 0: 13 | earnings += item[1] 14 | sizes[item[0]] -= 1 15 | print earnings 16 | 17 | -------------------------------------------------------------------------------- /Collections/namedTuple.py: -------------------------------------------------------------------------------- 1 | #using namedtuple(STU) 2 | from collections import namedtuple 3 | count, STU = int(raw_input()), namedtuple('Student', raw_input().split()) 4 | students = [] 5 | for i in range(count): 6 | data = raw_input().split() 7 | students.append(STU(data[0], data[1], data[2], data[3])) 8 | sum = 0 9 | #print students 10 | for iterator in students: 11 | #print iterator 12 | sum += float(iterator.MARKS) 13 | print (sum / float(count)) -------------------------------------------------------------------------------- /Collections/orderedDictionary.py: -------------------------------------------------------------------------------- 1 | #using orderedDictionaries 2 | from collections import OrderedDict 3 | items = int(raw_input()) 4 | d = OrderedDict() 5 | for i in range(items): 6 | itemName, itemPrice = raw_input().rsplit(' ', 1) 7 | if itemName not in d: 8 | d[itemName] = int(itemPrice) 9 | else: 10 | d[itemName] += int(itemPrice) 11 | for i, j in d.items(): 12 | print str(i), str(j) 13 | -------------------------------------------------------------------------------- /Math/integersInAllSizes.py: -------------------------------------------------------------------------------- 1 | one = raw_input() 2 | two = raw_input() 3 | three = raw_input() 4 | four = raw_input() 5 | power1 = pow(int(one), int(two)) 6 | power2 = pow(int(three),int(four)) 7 | print power1 + power2 -------------------------------------------------------------------------------- /Math/modDivMod.py: -------------------------------------------------------------------------------- 1 | first = raw_input() 2 | second = raw_input() 3 | f = int(first) 4 | s = int (second) 5 | print f/s 6 | print f%s 7 | print (f/s,f%s) -------------------------------------------------------------------------------- /Math/polarCoordinates.py: -------------------------------------------------------------------------------- 1 | import cmath 2 | data = eval(raw_input()) 3 | print abs(data) 4 | print cmath.phase(data) -------------------------------------------------------------------------------- /Math/powerModPower.py: -------------------------------------------------------------------------------- 1 | f = raw_input() 2 | s = raw_input() 3 | t = raw_input() 4 | f1 = int(f) 5 | s1 = int(s) 6 | t1 = int(t) 7 | p = pow(f1,s1) 8 | print p 9 | mod = pow(f1,s1,t1) 10 | print mod 11 | -------------------------------------------------------------------------------- /Math/triangleQuest.py: -------------------------------------------------------------------------------- 1 | for i in range(1, int(raw_input())): 2 | print (i*(10**i -1)//9) -------------------------------------------------------------------------------- /Math/triangleQuest2.py: -------------------------------------------------------------------------------- 1 | for i in range(1,int(raw_input())+1): print((10**i-1)**2//81) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HackerRank-Python 2 | 3 | Solutions to Python Challenges from HackerRank 4 | Code for the following subdomains lie in the root directory: 5 | 6 | 1. Introduction 7 | 2. Data Types 8 | 3. Strings 9 | 10 | Code for all other subdomains lie in their respective directories: 11 | 12 | 1. Sets 13 | 2. Math 14 | 3. Itertools 15 | 4. Collections 16 | 5. Built-Ins 17 | 18 | Fork and study. Happy learning! 19 | -------------------------------------------------------------------------------- /Sets/IntroductionToSets.py: -------------------------------------------------------------------------------- 1 | knapsack = set() 2 | numPlants = int(raw_input()) 3 | sumOfLengths = 0 4 | n = raw_input().split() 5 | for item in n: 6 | knapsack.add(int(item)) 7 | #print knapsack 8 | for plantLengths in knapsack: 9 | sumOfLengths = sumOfLengths + plantLengths 10 | print "%.3f" % float(float(sumOfLengths)/float(len(knapsack))) 11 | 12 | 13 | -------------------------------------------------------------------------------- /Sets/checkSubset.py: -------------------------------------------------------------------------------- 1 | for i in range(int(raw_input())): 2 | a = int(raw_input()); A = set(raw_input().split()); b = int(raw_input()); B = set(raw_input().split()) 3 | if A.issubset(B): print True 4 | else: print False 5 | 6 | # t = int(raw_input()) 7 | # for i in range(t): 8 | # num_a = int(raw_input()) 9 | # a = set(raw_input().split()) 10 | # num_b = int(raw_input()) 11 | # b = set(raw_input().split()) 12 | # if a.issubset(b): 13 | # print True 14 | # else: 15 | # print False -------------------------------------------------------------------------------- /Sets/discardRemovePop.py: -------------------------------------------------------------------------------- 1 | s = set() 2 | numberOfElements = int(raw_input()) 3 | elements = raw_input().split() 4 | for item in elements: 5 | s.add(int(item)) 6 | #print s 7 | commands = int(raw_input()) 8 | for x in range(commands): 9 | command = raw_input().split() 10 | if command[0] == 'pop': 11 | s.pop() 12 | #print s 13 | #print 'done popping' 14 | elif command[0] == 'remove': 15 | s.remove(int(command[1])) 16 | #print s 17 | #print 'done removing' 18 | elif command[0] == 'discard': 19 | s.discard(int(command[1])) 20 | #print s 21 | #print 'done discarding' 22 | print sum(s) 23 | -------------------------------------------------------------------------------- /Sets/noIdea.py: -------------------------------------------------------------------------------- 1 | # use of map and assertions 2 | (n, m) = map(int, raw_input().split()) 3 | array = map(int, raw_input().split()) 4 | assert len(array) == n 5 | A = set(map(int, raw_input().split())) 6 | assert len(A) == m 7 | B = set(map(int, raw_input().split())) 8 | assert len(B) == m 9 | happiness = 0 10 | for i in array: 11 | if i in A: 12 | happiness += 1 13 | elif i in B: 14 | happiness -= 1 15 | 16 | print happiness -------------------------------------------------------------------------------- /Sets/setDifference.py: -------------------------------------------------------------------------------- 1 | sEng = set() 2 | sFr = set() 3 | count = 0 4 | subscribersEng = int(raw_input()) 5 | rollNumbersEng = raw_input().split() 6 | subscribersFr = int(raw_input()) 7 | rollNumbersFr = raw_input().split() 8 | #print rollNumbersFr 9 | for engReaders in rollNumbersEng: 10 | sEng.add(int(engReaders)) 11 | for frReadrers in rollNumbersFr: 12 | sFr.add(int(frReadrers)) 13 | for iterator in (sEng.difference(sFr)): 14 | count = count + 1 15 | print count -------------------------------------------------------------------------------- /Sets/setDotAdd.py: -------------------------------------------------------------------------------- 1 | stamps = set() 2 | numberOfStamps = int(raw_input()) 3 | for x in range(numberOfStamps): 4 | country = raw_input().rstrip() 5 | stamps.add(country) 6 | print len(stamps) -------------------------------------------------------------------------------- /Sets/setIntersection.py: -------------------------------------------------------------------------------- 1 | sEng = set() 2 | sFr = set() 3 | count = 0 4 | subscribersEng = int(raw_input()) 5 | rollNumbersEng = raw_input().split() 6 | subscribersFr = int(raw_input()) 7 | rollNumbersFr = raw_input().split() 8 | #print rollNumbersFr 9 | for engReaders in rollNumbersEng: 10 | sEng.add(int(engReaders)) 11 | for frReadrers in rollNumbersFr: 12 | sFr.add(int(frReadrers)) 13 | for iterator in (sEng.intersection(sFr)): 14 | count = count + 1 15 | print count -------------------------------------------------------------------------------- /Sets/setMutations.py: -------------------------------------------------------------------------------- 1 | num_a = raw_input() 2 | a = set(map(int, raw_input().split())) 3 | n = int(raw_input()) 4 | 5 | for i in range(n): 6 | command = raw_input().split() 7 | b = set(map(int, raw_input().split())) 8 | 9 | if command[0] == "update": 10 | a.update(b) 11 | elif command[0] == "intersection_update": 12 | a.intersection_update(b) 13 | elif command[0] == "difference_update": 14 | a.difference_update(b) 15 | elif command[0] == "symmetric_difference_update": 16 | a.symmetric_difference_update(b) 17 | 18 | print sum(a) -------------------------------------------------------------------------------- /Sets/setUnion.py: -------------------------------------------------------------------------------- 1 | sEng = set() 2 | sFr = set() 3 | count = 0 4 | subscribersEng = int(raw_input()) 5 | rollNumbersEng = raw_input().split() 6 | subscribersFr = int(raw_input()) 7 | rollNumbersFr = raw_input().split() 8 | #print rollNumbersFr 9 | for engReaders in rollNumbersEng: 10 | sEng.add(int(engReaders)) 11 | for frReadrers in rollNumbersFr: 12 | sFr.add(int(frReadrers)) 13 | for iterator in (set(sEng.union(sFr))): 14 | count = count + 1 15 | print count -------------------------------------------------------------------------------- /Sets/strictSuperSet.py: -------------------------------------------------------------------------------- 1 | #add all the sets to one big list and check for strict super set element by element 2 | bigList = list() 3 | result = True 4 | a = set(map(int, raw_input().split())) 5 | numberOfSetsToCheck = int(raw_input()) 6 | for x in range(numberOfSetsToCheck): 7 | b = set(map(int, raw_input().split())) 8 | bigList.append(b) 9 | for item in bigList: 10 | if not a.issuperset(item): 11 | result = False 12 | print result 13 | -------------------------------------------------------------------------------- /Sets/symmetricDiff.py: -------------------------------------------------------------------------------- 1 | sEng = set() 2 | sFr = set() 3 | count = 0 4 | subscribersEng = int(raw_input()) 5 | rollNumbersEng = raw_input().split() 6 | subscribersFr = int(raw_input()) 7 | rollNumbersFr = raw_input().split() 8 | #print rollNumbersFr 9 | for engReaders in rollNumbersEng: 10 | sEng.add(int(engReaders)) 11 | for frReadrers in rollNumbersFr: 12 | sFr.add(int(frReadrers)) 13 | for iterator in (sEng.symmetric_difference(sFr)): 14 | count = count + 1 15 | print count -------------------------------------------------------------------------------- /angle_clock_hands.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #angle between the hands of a 12 hour analog clock at any given time in hours and minutes 3 | #Minute hand turns 6 degrees every minute. Hour hand turns 30 degrees every hour (0.5 degrees every minute) 4 | def angle(hour, minute): 5 | angle = abs((hour * 30 + minute * 0.5)- (minute * 6)) 6 | return min(angle, 360-angle) 7 | hour = int(raw_input('Enter Hours ')) 8 | minute = int(raw_input('Enter Minutes ')) 9 | print str(angle(hour, minute)) + ' degrees' 10 | -------------------------------------------------------------------------------- /buzzfizz.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | def bark(): 3 | for x in range(0, 100): 4 | if(x % 5 == 0): 5 | print str(x) + ' Buzz' 6 | if(x % 3 ==0): 7 | print str(x) + ' Fizz' 8 | bark() 9 | -------------------------------------------------------------------------------- /challenges/hacker_rank_challenge_1.py: -------------------------------------------------------------------------------- 1 | def findFraudolentTraders(datafeed): 2 | flagged_trades = set() 3 | trades = {} 4 | current_price = None 5 | for feed in datafeed: 6 | vals = feed.split("|") 7 | day = int(vals[0]) 8 | if len(vals) == 2: 9 | #CASE - NO DATA EXISTS IN FEED TO POPULATE 10 | current_price = int(vals[1]) 11 | for x in range(day - 3, day): 12 | if x in trades: 13 | for (trader_name, isBuy, price, amount) in trades[x]: 14 | if (x, trader_name) in flagged_trades: 15 | continue 16 | if isBuy: 17 | fraudolent = (current_price - price) * amount >= 5000000 18 | else: 19 | fraudolent = (price - current_price) * amount >= 5000000 20 | if fraudolent: 21 | flagged_trades.add((x, trader_name)) 22 | else: 23 | #CASE - DATA EXISTS IN FEED TO POPULATE (pull name, amont and buy/sell clause) 24 | trader_name = vals[1] 25 | isBuy = len(vals[2]) == 3 26 | amount = int(vals[3]) 27 | if day not in trades: 28 | trades[day] = [] 29 | trades[day].append((trader_name, isBuy, current_price, amount)) 30 | flagged_trades = sorted(list(flagged_trades)) 31 | return list(map(lambda x: str(x[0]) + "|" + str(x[1]), flagged_trades)) 32 | 33 | 34 | #test data 35 | 36 | feed1 = """0|1000 37 | 0|Shilpa|BUY|30000 38 | 0|Will|BUY|50000 39 | 0|Tom|BUY|40000 40 | 0|Kristi|BUY|15000 41 | 1|Kristi|BUY|11000 42 | 1|Tom|BUY|1000 43 | 1|Will|BUY|19000 44 | 1|Shilpa|BUY|25000 45 | 2|1500 46 | 2|Will|SELL|7000 47 | 2|Shilpa|SELL|8000 48 | 2|Kristi|SELL|6000 49 | 2|Tom|SELL|9000 50 | 3|500 51 | 38|1000 52 | 78|Shilpa|BUY|30000 53 | 79|Kristi|BUY|60000 54 | 80|1100 55 | 81|1200""" 56 | datafeed1 = feed1.split("\n") 57 | #print findFraudolentTraders(datafeed1) 58 | 59 | feed2 = """0|20 60 | 0|Kristi|SELL|3000 61 | 0|Will|BUY|5000 62 | 0|Tom|BUY|50000 63 | 0|Shilpa|BUY|1500 64 | 1|Tom|BUY|1500000 65 | 3|25 66 | 5|Shilpa|SELL|1500 67 | 8|Kristi|SELL|600000 68 | 9|Shilpa|BUY|500 69 | 10|15 70 | 11|5 71 | 14|Will|BUY|100000 72 | 15|Will|BUY|100000 73 | 16|Will|BUY|100000 74 | 17|25""" 75 | 76 | datafeed2 = feed2.split("\n") 77 | var = findFraudolentTraders(datafeed2) 78 | print ' '.join(var[0:2]) 79 | -------------------------------------------------------------------------------- /christmas_tree.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import time 3 | def painttree(n): 4 | z = n - 1 5 | x = 1 6 | for i in range(n): 7 | print(' ' * z + '+' * x + ' ' * z) 8 | x+=2 9 | z-=1 10 | time.sleep(0.2) 11 | painttree(50) 12 | -------------------------------------------------------------------------------- /findString.py: -------------------------------------------------------------------------------- 1 | #IMP - number of comparisons are equal to big len - small len + 1 (here it is - lenusable) 2 | big = raw_input() 3 | small = raw_input() 4 | len1 = len(big) 5 | len2 = len(small) 6 | count = 0 7 | lenusable = len1-len2+1 8 | #print lenusable 9 | for x in range(0, lenusable): 10 | if big[x:x+len2] == small: 11 | count = count+1 12 | print count 13 | -------------------------------------------------------------------------------- /findingThePercentage.py: -------------------------------------------------------------------------------- 1 | #done using text parsing 2 | import sys 3 | longString = str() 4 | for line in sys.stdin: 5 | longString = longString + line.rstrip() + " " 6 | lst = longString.split() 7 | print lst 8 | # #print 'Number of students', lst[0] 9 | # numberOfStudents = lst[0] 10 | posOfStudentOfInterest = 4*int(lst[0]) + 1 11 | #print 'Student of interest', lst[posOfStudentOfInterest] 12 | for item in lst: 13 | if item==lst[posOfStudentOfInterest]: 14 | score1 = lst[lst.index(item) + 1] 15 | score2 = lst[lst.index(item) + 2] 16 | score3 = lst[lst.index(item) + 3] 17 | break 18 | avg = (float(score1)+float(score2)+float(score3))/3 19 | print '{0:.2f}'.format(avg) -------------------------------------------------------------------------------- /findingThePercentageUsingDict.py: -------------------------------------------------------------------------------- 1 | #done using dictionaries taking raw_input line by line 2 | numberOfStudents = int(raw_input()) 3 | total = dict() 4 | for i in range(0, numberOfStudents): 5 | tokens = raw_input().split() 6 | name = tokens[0] 7 | total[name] = float(tokens[1]) + float(tokens[2]) + float(tokens[3]) 8 | student = raw_input() 9 | print "{0:.2f}".format(total[student] / 3) -------------------------------------------------------------------------------- /itertools/combinations.py: -------------------------------------------------------------------------------- 1 | #from __future__ import print_function 2 | from itertools import combinations 3 | data = raw_input().split() 4 | word = data[0] 5 | word = sorted(word) 6 | parameter = int(data[1]) 7 | #print word 8 | #print parameter 9 | for i in range(1, parameter+1): 10 | l = map("".join, list(combinations(word, i))) 11 | for item in sorted(l): 12 | print item -------------------------------------------------------------------------------- /itertools/combinationsWithRep.py: -------------------------------------------------------------------------------- 1 | #from __future__ import print_function 2 | from itertools import combinations_with_replacement 3 | data = raw_input().split() 4 | word = data[0] 5 | word = sorted(word) 6 | parameter = int(data[1]) 7 | #print word 8 | #print parameter 9 | l = map("".join, list(combinations_with_replacement(word, parameter))) 10 | for item in sorted(l): 11 | print item -------------------------------------------------------------------------------- /itertools/itertoolsProduct.py: -------------------------------------------------------------------------------- 1 | str1 = str() 2 | from itertools import product 3 | lstA = map(int, raw_input().split()) 4 | lstB = map(int, raw_input().split()) 5 | print ' '.join(map(str,list(product(lstA, lstB)))) -------------------------------------------------------------------------------- /itertools/permutations.py: -------------------------------------------------------------------------------- 1 | #from __future__ import print_function 2 | from itertools import permutations 3 | data = raw_input().split() 4 | word = data[0] 5 | parameter = int(data[1]) 6 | print word 7 | print parameter 8 | b = list(permutations(word, parameter)) 9 | for item in sorted(b): 10 | print("".join(item)) -------------------------------------------------------------------------------- /itertools/stringCompress.py: -------------------------------------------------------------------------------- 1 | from itertools import groupby 2 | s = raw_input() 3 | for key, group in groupby(s): 4 | print (len(list(group)), int(key)) 5 | -------------------------------------------------------------------------------- /printFunction.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | number = raw_input('enter number: ') 3 | n=int(number) 4 | for x in range(1,n+1): 5 | print (x, end='') 6 | -------------------------------------------------------------------------------- /pythonLists.py: -------------------------------------------------------------------------------- 1 | lstC = list() 2 | lst = list() 3 | commands = int(raw_input()) 4 | for x in range (0,commands): 5 | command = raw_input() 6 | lstC = command.split() 7 | if lstC[0] == 'insert': 8 | lst.insert(int(lstC[1]), int(lstC[2])) 9 | elif lstC[0] == 'print': 10 | print lst 11 | elif lstC[0] == 'remove': 12 | lst.remove(int(lstC[1])) 13 | elif lstC[0] == 'append': 14 | lst.append(int(lstC[1])) 15 | elif lstC[0] == 'pop': 16 | lst.pop() 17 | elif lstC[0] == 'sort': 18 | lst.sort() 19 | elif lstC[0] == 'reverse': 20 | lst.reverse() 21 | 22 | -------------------------------------------------------------------------------- /pythonNestedLists.py: -------------------------------------------------------------------------------- 1 | lst = list() 2 | uniqueListOfScores = list() 3 | tempListOfScores = list() 4 | pos = None 5 | studentCount = int(raw_input()) 6 | lowest = None 7 | for x in range(0,studentCount): 8 | lst.append([raw_input(), float(raw_input())]) 9 | #print lst 10 | #print lst[0][1], lst[1][1], lst[2][1], lst[3][1], lst[4][1] 11 | 12 | tempListOfScores = [lst[x][1] for x in range(0, studentCount)] 13 | uniqueListOfScores = list(set(tempListOfScores)) 14 | minimumOfAll = min(uniqueListOfScores) 15 | uniqueListOfScores.remove(minimumOfAll) 16 | #print uniqueListOfScores 17 | secondMinimumOfAll = min(uniqueListOfScores) 18 | lst = [x[0] for x in lst if x[1] == secondMinimumOfAll] 19 | #print lst 20 | lst.sort() 21 | if len(lst) > 1: 22 | print lst[0] 23 | print lst[1] 24 | else: 25 | print lst[0] 26 | -------------------------------------------------------------------------------- /pythonSets.py: -------------------------------------------------------------------------------- 1 | M = int(raw_input()) 2 | setA = set(map(int, raw_input().split())) 3 | 4 | N = int(raw_input()) 5 | setB = set(map(int, raw_input().split())) 6 | 7 | for x in sorted(setA.symmetric_difference(setB)): 8 | print x -------------------------------------------------------------------------------- /pythontuples.py: -------------------------------------------------------------------------------- 1 | # *IMP* - can't append to tuple, so append to a list instead and later cast into a tuple 2 | lst = list() 3 | n = int(raw_input()) 4 | l = raw_input() 5 | for x in l.split(): 6 | lst.append(int(x)) 7 | t = tuple(lst) 8 | print hash(t) -------------------------------------------------------------------------------- /sWAPcASE.py: -------------------------------------------------------------------------------- 1 | input = raw_input() 2 | outputString = "" 3 | for alphabet in input: 4 | if alphabet.isupper() is True: 5 | alphabet=alphabet.lower() 6 | #print alphabet 7 | outputString=outputString+alphabet 8 | else: 9 | alphabet=alphabet.upper() 10 | outputString=outputString+alphabet 11 | print outputString -------------------------------------------------------------------------------- /secondLargestNumber.py: -------------------------------------------------------------------------------- 1 | lst = list() 2 | unqlst = list() 3 | n = int(raw_input()) 4 | l = raw_input() 5 | for x in l.split(): 6 | lst.append(int(x)) 7 | lst.sort() 8 | unqlst = list(set(lst)) 9 | maxOfAll = max(unqlst) 10 | unqlst.remove(maxOfAll) 11 | print max(unqlst) 12 | 13 | -------------------------------------------------------------------------------- /shift.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | def shift(input, left, right): 3 | if len(input) == 1: 4 | return input 5 | return input[left:] + input[0:left] 6 | return input[-right:] + input[0:-right] 7 | 8 | print shift("A", 0, 0) 9 | -------------------------------------------------------------------------------- /stringFormatting.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | def num_lines(num): 4 | lines = list() 5 | width = len(bin(num)) - 2 6 | 7 | for n in range(1, num+1): 8 | line = '{1: >{0}d} {1: >{0}o} {1: >{0}X} {1: >{0}b}'.format(width, n) 9 | lines.append(line) 10 | 11 | return lines 12 | 13 | def main(): 14 | T = int(input()) 15 | for line in num_lines(T): 16 | print(line) 17 | main() -------------------------------------------------------------------------------- /stringMutations.py: -------------------------------------------------------------------------------- 1 | input = raw_input() 2 | l = list() 3 | outputStr = str() 4 | for alphabet in input: 5 | l.append(alphabet) 6 | nextInput = raw_input() 7 | nextInputSplit = nextInput.split() 8 | l[int(nextInputSplit[0])]=nextInputSplit[1] 9 | for iterator in l: 10 | outputStr = outputStr + iterator 11 | print outputStr 12 | -------------------------------------------------------------------------------- /stringSplitAndJoin.py: -------------------------------------------------------------------------------- 1 | input = raw_input() 2 | splittedString = input.split() 3 | splittedString = '-'.join(splittedString) 4 | #print type(splittedString) 5 | print splittedString -------------------------------------------------------------------------------- /stringValidators.py: -------------------------------------------------------------------------------- 1 | #use of inbuilt function - any 2 | input = raw_input() 3 | if any(alphabet.isalnum() for alphabet in input): 4 | print 'True' 5 | else: 6 | print 'False' 7 | if any(alphabet.isalpha() for alphabet in input): 8 | print 'True' 9 | else: 10 | print 'False' 11 | if any(alphabet.isdigit() for alphabet in input): 12 | print 'True' 13 | else: 14 | print 'False' 15 | if any(alphabet.islower() for alphabet in input): 16 | print 'True' 17 | else: 18 | print 'False' 19 | if any(alphabet.isupper() for alphabet in input): 20 | print 'True' 21 | else: 22 | print 'False' 23 | -------------------------------------------------------------------------------- /textAlignment.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function, division 2 | 3 | char = 'H' 4 | space = ' ' 5 | 6 | def text_align(width): 7 | lines = [] 8 | for n in range(width): 9 | lines.append((char*(n*2+1)).center(width*2-1, space)) 10 | 11 | offset = space*(width*4 - (width*2-1)) 12 | 13 | for n in range(width+1): 14 | lines.append((char*width).center(width*2-1, space) + offset + (char*width).center(width*2-1, space)) 15 | 16 | for n in range((width+1)//2): 17 | lines.append((char*width*5).center(width*6, space)) 18 | 19 | for n in range(width+1): 20 | lines.append((char*width).center(width*2-1, space) + offset + (char*width).center(width*2-1, space)) 21 | 22 | for n in reversed(range(width)): 23 | lines.append(space*(width*4) + (char*(n*2+1)).center(width*2-1, space)) 24 | 25 | return lines 26 | 27 | def main(): 28 | T = int(input()) 29 | print('\n'.join(text_align(T))) 30 | 31 | main() -------------------------------------------------------------------------------- /textFillAndWrap.py: -------------------------------------------------------------------------------- 1 | import textwrap 2 | data = raw_input() 3 | width = int(raw_input()) 4 | lst = textwrap.wrap(data, width) 5 | for item in lst: 6 | print item --------------------------------------------------------------------------------