HackerRank - 30 Days Python Challenge
My goal is to commit to 2 challenges a day for this 30 day HackerRank python challenge, while simultaneously studying for OSCP and doing boxes (Target : 1hr per day)
Day 6 : Lets Review
So this challenge mainly reviews knowledge of strings, loops and accessing indexes
Task
Given a string, , of length that is indexed from to , print its even-indexed and odd-indexed characters as space-separated strings on a single line (see the Sample below for more detail).
Note: is considered to be an even index.
Example
s = adbecf
Print abc def
Solution
https://thispointer.com/python-reverse-a-list-sub-list-or-list-of-list-in-place-or-copy/
# Enter your code here. Read input from STDIN. Print output to STDOUT
# First input is number of test cases, T
T = int(input())
#Now iterate exactly T times for per input string S
for i in range (0,T):
#take twice input string
S=input()
# Now this is the function for slicing indexes of even + odd
print(S[0::2] + " " + S[1::2])
# first [0::2] takes index zero of string S then jumps by twos to the end and so on..
Day 7 : Arrays
Task
Given an array, , of integers, print 's elements in reverse order as a single line of space-separated numbers.
Example
A = [1,2,3,4]
Print 4 3 2 1. Each integer is separated by one space.
Solution 1 - use slicing with negative step then join
Solution 2 - for loop
Day 8 : Dictionaries & Key-value map
Task
Given n names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for is not found, print Not found instead.
The criteria was for the phone book to be of: Dictionary, Map or HashMap data structure
Input format:
Solution
Used first to map to dictionary structure, next perform while True if loop for input in dict (is the name query a key in the dictionary?)
Tip: items() method for looping through dictionaries
dict = {...}
for key, value in dict.items():
print(key, value)
Recap: Looping over a list and appending filtered values (To filter out only numbers)
Day 9 : Recursion & Factorial of Number
Day 10 : Converting Base10 to Base2
Given a base10 number, convert to base2 and find the maximum number of consecutive ones '1'.
Plan 1:
counter of current no. of consecutive 1 (if current counter > maximum counter, set it to maximum counter value)
counter of maximum consecutive 1
once hit zero, reset counter to zero
Plan 2 : Convert > bin > max > len
Plan 3: Use Bin first to convert then count 1s
Plan 4: Use append the remainders to a list, count number of 1s
Last updated