30 days of code https://www.hackerrank.com
Day 6
Task
Given a string,S , of length N that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail).
Note: is considered to be an even index.
Sample Input
2
Hacker
Rank
Sample Output
Hce akr
Rn ak
My code
# Enter your code here. Read input from STDIN. Print output to STDOUT x=int(raw_input()) for i in range(x): d=raw_input() a='';b='' for i in range(0,len(d),2): a+=d[i] print a, for i in range(1,len(d),2): b+=d[i] print b, print ''
official answer:
t = int(raw_input()) for _ in range(t): line = raw_input() first = "" second = "" for i, c in enumerate(line): if (i & 1) == 0: first += c else: second += c print first, second
Day 8
Sample Input
3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
Sample Output
sam=99912222
Not found
harry=12299933
my answer
# Enter your code here. Read input from STDIN. Print output to STDOUT x=raw_input() y={} for i in range(int(x)): k=raw_input() y[k.split(' ')[0]]=k.split(' ')[-1] while True: try: c=raw_input() except: c=0 if c: try: print c+'='+y[c] except: print 'Not found' else: break
official
Python3 import sys # Read input and assemble Phone Book n = int(input()) phoneBook = {} for i in range(n): contact = input().split(' ') phoneBook[contact[0]] = contact[1] # Process Queries lines = sys.stdin.readlines() for i in lines: name = i.strip() if name in phoneBook: print(name + '=' + str( phoneBook[name] )) else: print('Not found')
Day 12: Inheritance
class Person: def __init__(self, firstName, lastName, idNumber): self.firstName = firstName self.lastName = lastName self.idNumber = idNumber def printPerson(self): print "Name:", self.lastName + ",", self.firstName print "ID:", self.idNumber class Student(Person): def __init__(self, firstName, lastName, idNum ,scores): Person.__init__(self, firstName, lastName, idNum) self.scores=scores def calculate(self): avg = sum(scores) / len(scores) grade = '' if (90 <= avg <= 100): grade = 'O' if (80 <= avg < 90): grade = 'E' if (70 <= avg < 80): grade = 'A' if (55 <= avg < 70): grade = 'P' if (40 <= avg <= 55): grade = 'D' if (avg < 40): grade = 'T' return grade line = raw_input().split() firstName = line[0] lastName = line[1] idNum = line[2] numScores = int(raw_input()) # not needed for Python scores = map(int, raw_input().split()) s = Student(firstName, lastName, idNum, scores) s.printPerson() print "Grade:", s.calculate()
Day 14: Scope
The absolute difference between two integers
class Difference: def __init__(self, a): self.__elements = a # Add your code here def computeDifference(self): self.maximumDifference = max([a-b for a in self.__elements for b in self.__elements]) # End of Difference class _ = raw_input() a = [int(e) for e in raw_input().split(' ')] d = Difference(a) d.computeDifference() print d.maximumDifference
Day 15: Linked List
class Node: def __init__(self,data): self.data = data self.next = None class Solution: def display(self,head): current = head while current: print current.data, current = current.next def insert(self,head,data): if (head == None): head = Node(data) else: current = head while True: if(current.next == None): current.next = Node(data) break current = current.next return head mylist= Solution() T=int(input()) head=None for i in range(T): data=int(input()) head=mylist.insert(head,data) mylist.display(head);