├── .gitignore ├── angrychildren.py ├── any-or-all.py ├── arithmetic-operators.py ├── basiccalc.py ├── dayornight.py ├── division.py ├── emailvalidfilter.py ├── findastring.py ├── findhackerrank.py ├── gemstones.py ├── hackerranklang.py ├── hackranktweets.py ├── halloween.py ├── integers-come-in-all-sizes.py ├── interchange.py ├── lists.py ├── loops.py ├── maplambda.py ├── mod-divmod.py ├── mod-power.py ├── mutations.py ├── percentages.py ├── py-function.py ├── py-set-union.py ├── pystrfmt.py ├── python-if-else.py ├── python-print.py ├── raw-input.py ├── readme.md ├── sayinghi.py ├── secondlargest.py ├── set-add.py ├── set-intro.py ├── sets.py ├── split-join.py ├── splitnumber.py ├── swapcase.py ├── triangle-quest.py ├── tuples.py ├── two-strings.py ├── utopian_id.py ├── validpan.py ├── validphone.py ├── whatsyourname.py └── xmlfindscore.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /angrychildren.py: -------------------------------------------------------------------------------- 1 | import math 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/angry-children 5 | 6 | n, k = int(sys.stdin.readline().strip()), int(sys.stdin.readline().strip()) 7 | 8 | packets = [] 9 | for x in xrange(n): 10 | packets.append(int(sys.stdin.readline().strip())) 11 | 12 | packets.sort() 13 | 14 | # NOTE: Not 100% clear on why this is, but was able to get it working; didn't fully 15 | # understand the instructions; reached solution after reading comments 16 | unfairness = max(packets[:k]) - min(packets[:k]) 17 | for y in xrange(1, n - k): 18 | if (packets[y + k - 1] - packets[y]) < unfairness: 19 | unfairness = packets[y + k -1] - packets[y] 20 | 21 | print unfairness 22 | -------------------------------------------------------------------------------- /any-or-all.py: -------------------------------------------------------------------------------- 1 | from string import split 2 | 3 | # https://www.hackerrank.com/challenges/any-or-all 4 | 5 | def is_palindrome(x): 6 | if int(str(x)[::-1]) is x: 7 | return True 8 | return False 9 | 10 | def is_positive(x): 11 | if x > 0: 12 | return True 13 | return False 14 | 15 | n = int(raw_input()) 16 | nums = map(lambda x: int(x), split(raw_input())) 17 | 18 | if all([is_positive(x) for x in nums]): 19 | if any([is_palindrome(x) for x in nums]): 20 | print True 21 | else: 22 | print False 23 | else: 24 | print False 25 | -------------------------------------------------------------------------------- /arithmetic-operators.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-arithmetic-operators 2 | 3 | a = int(raw_input()) 4 | b = int(raw_input()) 5 | 6 | print a + b 7 | print a - b 8 | print a * b 9 | -------------------------------------------------------------------------------- /basiccalc.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # https://www.hackerrank.com/challenges/basic-calculator 4 | 5 | x, y = float(sys.stdin.readline().strip()), float(sys.stdin.readline().strip()) 6 | 7 | print "%.2f" % (x + y) 8 | print "%.2f" % (x - y) 9 | print "%.2f" % (x * y) 10 | print "%.2f" % (x / y) 11 | print "%.2f" % (x // y) 12 | -------------------------------------------------------------------------------- /dayornight.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # https://www.hackerrank.com/challenges/digital-camera-day-or-night 4 | 5 | # Pixel color values are in the format of (B,R,G) 6 | raw_data = sys.stdin.readline().strip().split() 7 | pixels = [] 8 | lum_sum = 0 9 | for pixel in raw_data: 10 | pixel = map(float, pixel.split(",")) 11 | pixels.append(pixel) 12 | # Close enough calculation of luminance 13 | lum_sum += ((0.16 * pixel[0]) + (0.33 * pixel[1]) + (0.59 * pixel[2])) 14 | 15 | avg_luminance = lum_sum / len(pixels) 16 | # Fails test 15; marks as day but should be night 17 | # Haven't been able to find the magic threshold 18 | if avg_luminance > 46: 19 | print "day" 20 | else: 21 | print "night" 22 | -------------------------------------------------------------------------------- /division.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | # https://www.hackerrank.com/challenges/python-division 4 | 5 | a = int(raw_input()) 6 | b = int(raw_input()) 7 | 8 | print a // b 9 | print a / b 10 | -------------------------------------------------------------------------------- /emailvalidfilter.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/validate-list-of-email-address-with-filter 5 | 6 | count = int(sys.stdin.readline().strip()) 7 | emails = [] 8 | for x in xrange(count): 9 | emails.append(sys.stdin.readline().strip()) 10 | 11 | # not optimal; struggled with a few of the test addresses slipping through 12 | # that had @@ in the domain 13 | regex = re.compile(r"^[a-zA-Z0-9-_]+\@[^@_]*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$") 14 | 15 | validated_emails = filter(lambda e: regex.findall(e), emails) 16 | print sorted(validated_emails, key=str.lower) 17 | -------------------------------------------------------------------------------- /findastring.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/find-a-string 5 | 6 | haystack = sys.stdin.readline().strip() 7 | needle = sys.stdin.readline().strip() 8 | 9 | # finds overlapping matches 10 | print len(re.findall("(?=%s)" % needle, haystack)) 11 | -------------------------------------------------------------------------------- /findhackerrank.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # https://www.hackerrank.com/challenges/find-hackerrank 4 | 5 | # I cheated on this, the instructions said to use regex and 6 | # very few people actually submitted a regex example 7 | 8 | count = int(sys.stdin.readline().strip()) 9 | 10 | out = [] 11 | 12 | for x in xrange(count): 13 | 14 | line = sys.stdin.readline().strip() 15 | 16 | if line.startswith("hackerrank") and line.endswith("hackerrank"): 17 | out.append("0") 18 | elif line.startswith("hackerrank") and not line.endswith("hackerrank"): 19 | out.append("1") 20 | elif not line.startswith("hackerrank") and line.endswith("hackerrank"): 21 | out.append("2") 22 | else: 23 | out.append("-1") 24 | 25 | print "\n".join(out) 26 | -------------------------------------------------------------------------------- /gemstones.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/gem-stones 2 | 3 | # I'm unsure why this was worth 60 points when it was so simple and 4 | # also wonder why there were so few solutions submitted 5 | 6 | count = int(raw_input()) 7 | 8 | gemstones = [] 9 | 10 | for _ in xrange(count): 11 | gemstones.append( set( list( raw_input() ) ) ) 12 | 13 | print len(set.intersection(*gemstones)) 14 | -------------------------------------------------------------------------------- /hackerranklang.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # https://www.hackerrank.com/challenges/hackerrank-language 4 | 5 | count = int(sys.stdin.readline().strip()) 6 | requests = [] 7 | for x in xrange(count): 8 | requests.append(sys.stdin.readline().strip().split()) 9 | 10 | langs = ("C","CPP","JAVA","PYTHON","PERL","PHP","RUBY","CSHARP", 11 | "HASKELL","CLOJURE","BASH","SCALA","ERLANG","CLISP", 12 | "LUA","BRAINFUCK","JAVASCRIPT","GO","D","OCAML","PASCAL", 13 | "SBCL","DART","GROOVY","OBJECTIVEC","R") 14 | 15 | for request in requests: 16 | if request[1] in langs: 17 | print "VALID" 18 | else: 19 | print "INVALID" 20 | -------------------------------------------------------------------------------- /hackranktweets.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/hackerrank-tweets 5 | 6 | count = int(sys.stdin.readline().strip()) 7 | 8 | tweets = [] 9 | for x in xrange(count): 10 | tweets.append(sys.stdin.readline().strip()) 11 | 12 | regex = re.compile(r"hackerrank", re.IGNORECASE) 13 | 14 | tweet_count = 0 15 | for tweet in tweets: 16 | if regex.findall(tweet): 17 | tweet_count += 1 18 | 19 | print tweet_count 20 | -------------------------------------------------------------------------------- /halloween.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # https://www.hackerrank.com/challenges/halloween-party 4 | 5 | count = int(sys.stdin.readline().strip()) 6 | cuts= [] 7 | for x in xrange(count): 8 | cuts.append(int(sys.stdin.readline().strip())) 9 | 10 | # read editorial before coming to solution 11 | for k in cuts: 12 | if (k % 2) == 0: 13 | print (k / 2 ) * (k / 2) 14 | else: 15 | print (k /2 ) * (k / 2 + 1) 16 | -------------------------------------------------------------------------------- /integers-come-in-all-sizes.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-integers-come-in-all-sizes 2 | 3 | a = int(raw_input()) 4 | b = int(raw_input()) 5 | c = int(raw_input()) 6 | d = int(raw_input()) 7 | 8 | print (a ** b) + (c ** d) 9 | -------------------------------------------------------------------------------- /interchange.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # https://www.hackerrank.com/challenges/interchange-two-numbers 4 | 5 | a,b = sys.stdin.readline(), sys.stdin.readline() 6 | 7 | (a,b) = (b,a) 8 | 9 | print "%s\n%s" % (a,b) 10 | -------------------------------------------------------------------------------- /lists.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-lists 2 | 3 | # TODO: Rewrite using dict instead of if/elif 4 | 5 | cmd_count = int(raw_input()) 6 | 7 | L = [] 8 | 9 | for i in xrange(cmd_count): 10 | cmd = raw_input().split() 11 | if cmd[0] == "insert": 12 | L.insert(int(cmd[1]), int(cmd[2])) 13 | elif cmd[0] == "remove": 14 | L.remove(int(cmd[1])) 15 | elif cmd[0] == "append": 16 | L.append(int(cmd[1])) 17 | elif cmd[0] == "sort": 18 | L.sort() 19 | elif cmd[0] == "reverse": 20 | L.reverse() 21 | elif cmd[0] == "pop": 22 | L.pop() 23 | elif cmd[0] == "print": 24 | print L 25 | -------------------------------------------------------------------------------- /loops.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # https://www.hackerrank.com/challenges/python-loops 4 | 5 | for i in range(0, int(sys.stdin.readline().strip())): 6 | print i * i 7 | -------------------------------------------------------------------------------- /maplambda.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/map-and-lambda-expression 5 | 6 | # lazily borrowed a fast fib method from SO 7 | # http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python 8 | 9 | # Since this problem focuses on map and lambda I used them as much as possible 10 | 11 | fib = lambda n: int(((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))) 12 | cube = lambda n: n * n * n 13 | 14 | count = int(sys.stdin.readline().strip()) 15 | 16 | print map(cube, map(fib, xrange(count))) 17 | -------------------------------------------------------------------------------- /mod-divmod.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-mod-divmod 2 | 3 | a = int(raw_input()) 4 | b = int(raw_input()) 5 | 6 | print a // b 7 | print a % b 8 | print divmod(a, b) 9 | -------------------------------------------------------------------------------- /mod-power.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-power-mod-power 2 | 3 | a = int(raw_input()) 4 | b = int(raw_input()) 5 | m = int(raw_input()) 6 | 7 | print pow(a, b) 8 | print pow(a, b, m) 9 | -------------------------------------------------------------------------------- /mutations.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-mutations 2 | 3 | s = list(raw_input()) 4 | i, l = raw_input().split() 5 | 6 | s[int(i)] = l 7 | 8 | print "".join(s) 9 | -------------------------------------------------------------------------------- /percentages.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # https://www.hackerrank.com/challenges/finding-the-percentage 4 | 5 | student_count = int(sys.stdin.readline().strip()) 6 | 7 | scores = [] 8 | for x in xrange(student_count): 9 | temp = sys.stdin.readline().split() 10 | scores.append({ 11 | "name": temp[0], 12 | "score1": float(temp[1]), 13 | "score2": float(temp[2]), 14 | "score3": float(temp[3]) 15 | }) 16 | 17 | target_student = sys.stdin.readline().strip() 18 | 19 | t_scores = scores[next(index for (index, d) in enumerate(scores) if d["name"] == target_student)] 20 | 21 | print "%0.2f" % ((t_scores["score1"] + t_scores["score2"] + t_scores["score3"]) / 3.0) 22 | -------------------------------------------------------------------------------- /py-function.py: -------------------------------------------------------------------------------- 1 | def is_leap(year): 2 | leap = False 3 | 4 | if ((year % 4 == 0) and ((year % 100 != 0) or (year % 100 == 0) and (year % 400 == 0))): 5 | leap = True 6 | 7 | return leap 8 | 9 | year = int(raw_input()) 10 | print is_leap(year) 11 | -------------------------------------------------------------------------------- /py-set-union.py: -------------------------------------------------------------------------------- 1 | en_input_count = raw_input() 2 | en_subs = set([int(n) for n in raw_input().split()]) 3 | fr_input_count = raw_input() 4 | fr_subs = set([int(n) for n in raw_input().split()]) 5 | print(len(en_subs | fr_subs)) 6 | -------------------------------------------------------------------------------- /pystrfmt.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-string-formatting 2 | 3 | n = int(raw_input()) 4 | 5 | colwidth = len(bin(n)[2:]) 6 | 7 | for num in range(1, n+1): 8 | for base in 'doXb': 9 | print '{0:{width}{base}}'.format(num, base=base, width=colwidth), 10 | print 11 | -------------------------------------------------------------------------------- /python-if-else.py: -------------------------------------------------------------------------------- 1 | if __name__ == '__main__': 2 | n = int(raw_input()) 3 | 4 | if n & 1: 5 | print "Weird" 6 | if not n & 1: 7 | if n >= 2 and n <= 5: 8 | print "Not Weird" 9 | if n >= 6 and n <= 20: 10 | print "Weird" 11 | if n > 20: 12 | print "Not Weird" 13 | 14 | -------------------------------------------------------------------------------- /python-print.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | # https://www.hackerrank.com/challenges/python-print 4 | 5 | [print(x, end='') for x in xrange(1, int(raw_input()) + 1)] 6 | -------------------------------------------------------------------------------- /raw-input.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-raw-input 2 | 3 | print raw_input() 4 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | My solutions to various HackerRank.com challenges. Some of the code may not be optimal or the most pythonic. My goal was to quickly write a solution that solved the challenge and was easy for anyone to read and understand. Also, I know that there are other/better ways to handle taking the input but in order to make my code more unique I've done things via the sys module. Eventually all of the very similar solutions may fall under their plagiarism checker. 2 | -------------------------------------------------------------------------------- /sayinghi.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/saying-hi 5 | 6 | count = int(sys.stdin.readline().strip()) 7 | lines = [] 8 | for x in xrange(count): 9 | lines.append(sys.stdin.readline().strip()) 10 | 11 | regex = re.compile(r"^hi.[^d]", re.IGNORECASE) 12 | for line in lines: 13 | if regex.findall(line): 14 | print line 15 | -------------------------------------------------------------------------------- /secondlargest.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list 4 | 5 | count = int(sys.stdin.readline().strip()) 6 | 7 | numbers = [int(x) for x in sys.stdin.readline().strip().split()] 8 | numbers = list(set(numbers)) 9 | numbers.sort() 10 | 11 | print numbers[len(numbers) - 2] 12 | -------------------------------------------------------------------------------- /set-add.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/py-set-add 2 | 3 | n = int(raw_input()) 4 | 5 | countries = set() 6 | 7 | for _ in xrange(n): 8 | countries.add(raw_input()) 9 | 10 | print len(countries) 11 | -------------------------------------------------------------------------------- /set-intro.py: -------------------------------------------------------------------------------- 1 | from string import split 2 | 3 | # https://www.hackerrank.com/challenges/py-introduction-to-sets 4 | 5 | num_plants = int(raw_input()) 6 | heights = map(lambda x: int(x), set(split(raw_input()))) 7 | print sum(heights) / float(len(heights)) 8 | -------------------------------------------------------------------------------- /sets.py: -------------------------------------------------------------------------------- 1 | import sets 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/sets 5 | 6 | m = int(sys.stdin.readline().strip()) 7 | m_ints = set(sys.stdin.readline().strip().split()) 8 | 9 | n = int(sys.stdin.readline().strip()) 10 | n_ints = set(sys.stdin.readline().strip().split()) 11 | 12 | results = list(m_ints.symmetric_difference(n_ints)) 13 | results.sort(key=int) 14 | 15 | for num in results: 16 | print num 17 | -------------------------------------------------------------------------------- /split-join.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-string-split-and-join 2 | 3 | print "-".join(raw_input().split()) 4 | -------------------------------------------------------------------------------- /splitnumber.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/split-number 5 | 6 | count = int(sys.stdin.readline().strip()) 7 | 8 | raw = [] 9 | for x in xrange(count): 10 | raw.append(sys.stdin.readline().strip()) 11 | 12 | regex = re.compile(r"(\d{,3})[- ](\d{,3})[- ](\d{4,10})") 13 | 14 | for raw_phone in raw: 15 | phone_num = regex.findall(raw_phone)[0] 16 | print "CountryCode=%s,LocalAreaCode=%s,Number=%s" % (phone_num[0], phone_num[1], phone_num[2]) 17 | -------------------------------------------------------------------------------- /swapcase.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/swap-case 2 | 3 | s = list(raw_input()) 4 | 5 | def swap(s): 6 | if s.isupper(): 7 | return s.lower() 8 | else: 9 | return s.upper() 10 | 11 | print ''.join( [swap(l) for l in s] ) 12 | -------------------------------------------------------------------------------- /triangle-quest.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-quest-1 2 | 3 | # This article really helps make sense of repdigits and how to figure them out 4 | # http://www.fact-index.com/r/re/repunit.html 5 | 6 | for i in range(1, input()): 7 | print ((10 ** i - 1) / 9) * i 8 | -------------------------------------------------------------------------------- /tuples.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/python-tuples 2 | 3 | # NOTE: You must convert the inputs to int in order for the hash() call to return the proper outputs 4 | 5 | n = int(raw_input()) 6 | t = tuple(map(int, raw_input().split())) 7 | 8 | print hash(t) 9 | -------------------------------------------------------------------------------- /two-strings.py: -------------------------------------------------------------------------------- 1 | # https://www.hackerrank.com/challenges/two-strings 2 | 3 | t = int(raw_input()) 4 | 5 | for _ in xrange(t): 6 | 7 | a = set(raw_input()) 8 | b = set(raw_input()) 9 | 10 | if len(a.intersection(b)) > 0: 11 | print "YES" 12 | else: 13 | print "NO" 14 | -------------------------------------------------------------------------------- /utopian_id.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/utopian-identification-number 5 | 6 | count = int(sys.stdin.readline().strip()) 7 | ids = [] 8 | for x in xrange(count): 9 | ids.append(sys.stdin.readline().strip()) 10 | 11 | regex = re.compile(r"^[a-z]{,3}\d{2,8}[A-Z]{3,}$") 12 | 13 | for id in ids: 14 | if regex.findall(id): 15 | print "VALID" 16 | else: 17 | print "INVALID" 18 | -------------------------------------------------------------------------------- /validpan.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/valid-pan-format 5 | 6 | count = int(sys.stdin.readline().strip()) 7 | 8 | tests = [] 9 | for x in xrange(count): 10 | tests.append(sys.stdin.readline().strip()) 11 | 12 | regex = re.compile(r"[A-Z]{5}\d{4}[A-Z]") 13 | 14 | for test in tests: 15 | if len(test) == 10: 16 | if regex.findall(test): 17 | print "YES" 18 | else: 19 | print "NO" 20 | else: 21 | print "NO" 22 | -------------------------------------------------------------------------------- /validphone.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | 4 | # https://www.hackerrank.com/challenges/regex-1-validating-the-phone-number 5 | 6 | count = int(sys.stdin.readline().strip()) 7 | nums = [] 8 | for x in xrange(count): 9 | nums.append(sys.stdin.readline().strip()) 10 | 11 | regex = re.compile(r"^[789]\d{9}$") 12 | 13 | for num in nums: 14 | if regex.findall(num): 15 | print "YES" 16 | else: 17 | print "NO" 18 | -------------------------------------------------------------------------------- /whatsyourname.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | # https://www.hackerrank.com/challenges/whats-your-name 4 | 5 | fname, lname = sys.stdin.readline(), sys.stdin.readline() 6 | print "Hello %s %s! You just delved into python." % (fname.strip(), lname.strip()) 7 | -------------------------------------------------------------------------------- /xmlfindscore.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import xml.etree.ElementTree as etree 3 | 4 | # https://www.hackerrank.com/challenges/xml-1-find-the-score 5 | 6 | count = int(sys.stdin.readline().strip()) 7 | data = "" 8 | for x in xrange(count): 9 | data += sys.stdin.readline().strip() 10 | 11 | tree = etree.ElementTree(etree.fromstring(data)) 12 | 13 | score = 0 14 | 15 | for elem in tree.iter(): 16 | score += len(elem.attrib) 17 | 18 | print score 19 | --------------------------------------------------------------------------------