tokarevms

hackerrank

Aug 18th, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. https://www.hackerrank.com/challenges/validating-postalcode/problem
  2. ____________________________________________________
  3. import re
  4. post_code = raw_input()
  5. pattern = re.compile(r'([0-9]).\1')
  6. result = []
  7. pos = 0
  8. for _ in xrange(4):
  9.     found = pattern.search(post_code, pos=(pos or 0))
  10.     try:
  11.         pos = found.start() + 1
  12.     except AttributeError:
  13.         break
  14.     result.append(pos)
  15. print bool(re.search(r'^[1-9][0-9]{5}$', post_code) and len(result) < 2)
  16.  
  17.  
  18.  
  19.  
  20. https://www.hackerrank.com/challenges/detect-html-tags-attributes-and-attribute-values/problem
  21. ____________________________________________________
  22. from __future__ import print_function
  23. from HTMLParser import HTMLParser
  24.  
  25. class MyParserHTML(HTMLParser):
  26.    
  27.     def print_attrib(self, attribs=None):
  28.         if attribs:
  29.             print(*('-> %s > %s' % (attrib[0], attrib[1]) for attrib in attribs), sep = '\n')
  30.    
  31.     def handle_starttag(self, tag, attrs):
  32.         print(tag)
  33.         self.print_attrib(attrs)
  34.        
  35.     def handle_startendtag(self, tag, attrs):
  36.         print(tag)
  37.         self.print_attrib(attrs)
  38.  
  39. if __name__ == '__main__':
  40.     parser = MyParserHTML()
  41.     for _ in xrange(input()):
  42.         parser.feed(raw_input())
  43.     parser.close()        
  44.  
  45.  
  46.  
  47.  
  48. https://www.hackerrank.com/challenges/ginorts/problem
  49. ____________________________________________________
  50. from __future__ import print_function
  51. result = sorted(raw_input(),
  52.                 key = lambda x: (not x.islower(), not x.isupper(), x.isdigit() and ord(x)%2==0, ord(x)))
  53. print(*result, sep = '')
  54.  
  55.  
  56.  
  57.  
  58. https://www.hackerrank.com/challenges/python-time-delta/problem
  59. ____________________________________________________
  60. from time import *
  61.  
  62. def time_in_sec(tstr):
  63.     sec1 = mktime(strptime(tstr[:-6], '%a %d %b %Y %H:%M:%S'))
  64.     pm, hour, min = tstr[-5:-4], tstr[-4:-2], tstr[-2:]
  65.     sec2 = - int(hour) * 3600 - int(min) * 60
  66.     return eval('sec1%ssec2' % pm)
  67.  
  68. for _ in xrange(input()):
  69.     print '%i' % (abs(time_in_sec(raw_input()) - time_in_sec(raw_input())))
  70.    
  71.  
  72.  
  73.  
  74. https://www.hackerrank.com/challenges/merge-the-tools/problem
  75. ____________________________________________________
  76. def merge_the_tools(string, k):
  77.     shorts = len(string)/k
  78.     blocks = [string[i*k:(i+1)*k] for i in range(shorts)]
  79.     result = []
  80.     for sub in blocks:
  81.         bet = []
  82.         for j in sub:
  83.             if j not in bet:
  84.                 bet.append(j)
  85.         result.append(''.join(bet))
  86.     for res in result:
  87.         print res
  88.  
  89.  
  90.  
  91.  
  92. https://www.hackerrank.com/challenges/alphabet-rangoli/problem
  93. ____________________________________________________
  94. def print_rangoli(size):
  95.     if size == 1:
  96.         print 'a'
  97.     else:
  98.         colms = (2*size-1) * 2 - 1
  99.         row = []
  100.         res = []
  101.         for i in xrange(96 + size, 96, -1):
  102.             row.append(chr(i))
  103.             string = '-'.join(row)+'-'+'-'.join(row[-2::-1])
  104.             print string.center(colms, '-')
  105.         for _ in xrange(size-1):
  106.             row.pop()
  107.             string = '-'.join(row)+'-'+'-'.join(row[-2::-1])
  108.             print string.center(colms, '-')
  109.          
  110.  
  111.  
  112.  
  113. https://www.hackerrank.com/challenges/iterables-and-iterators/problem
  114. ____________________________________________________
  115. from __future__ import division
  116. from itertools import *
  117. input()
  118. string = raw_input().split()
  119. k = input()
  120. combinated = [char for char in combinations(string, k)]
  121. included_a = [char for char in combinated if 'a' in char]
  122. print len(included_a)/len(combinated)        
  123.  
  124.  
  125.  
  126.  
  127. https://www.hackerrank.com/challenges/most-commons/problem
  128. ____________________________________________________
  129. from collections import Counter
  130. x = Counter(raw_input())
  131. result = sorted(x.most_common(3), key=lambda x: (-x[1], x[0]))
  132. for item, value in result:
  133.     print item, value
  134.  
  135.  
  136.  
  137.  
  138.  
  139. https://www.hackerrank.com/challenges/xml-1-find-the-score/problem
  140. ____________________________________________________
  141. def get_attr_number(node):
  142.     sum = len(node.attrib)
  143.     for child in node:
  144.         sum += get_attr_number(child)
  145.     return sum
  146.  
  147.  
  148.  
  149.  
  150. https://www.hackerrank.com/challenges/matrix-script/problem
  151. ____________________________________________________
  152. import re
  153. N, M = map(int,raw_input().split())
  154. matrix = []
  155. for _ in xrange(N):
  156.     matrix.append([char for char in re.sub(r'(^.{%s}$)' % (M - 1), r'\1 ', raw_input())])
  157. string = ''.join([matrix[j][i] for i in xrange(M) for j in xrange(N)])
  158. result = re.sub(r'(?<=[A-Za-z])[!@#$%& ]+(?=[A-Za-z])' ,' ', string)
  159. print result
Add Comment
Please, Sign In to add comment