Home / Development / Software Development / Python / HackerRank Challenges
HackerRank Python Challenges
In this article I show how I solved HackerRank Python challenges. The main purpose of the content is to present my acquired experiences primarily for learning purposes.
Other categories:
Introduction
Basic Data Types
Strings
Sets
Itertools
Collections
Built-Ins
Numpy
Others
Collections
collections.Counter()
Task:
Raghu is a shoe shop owner. His shop has X number of shoes.
He has a list containing the size of each shoe he has in his shop.
There are N number of customers who are willing to pay x amount of money only if they get the shoe of their desired size.
Your task is to compute how much money Raghu earned.
Solution:
number_of_shoes = input() shoe_sizes = input().split() num_of_customers = input() total_earned = 0 for i in range(int(num_of_customers)): buy = input() size_price = buy.split() if size_price[0] in shoe_sizes: total_earned += int(size_price[1]) shoe_sizes.remove(size_price[0]) print(total_earned)
DefaultDict Tutorial
Task:
In this challenge, you will be given 2 integers, n and m.
There are n words, which might repeat, in word group A.
There are m words belonging to word group B.
For each m words, check whether the word has appeared in group A or not.
Print the indices of each occurrence of m in group A.
If it does not appear, print -1.
Solution:
nm = input() nm_split = nm.split() n = nm_split[0] m = nm_split[1] a = dict() b = dict() for i in range(1, int(n) + 1): a[i] = input() for i in range(1, int(m) + 1): b[i] = input() for i in b: positions = -1 for j in a: if b[i] == a[j]: if positions == -1: positions = '' positions += str(j) + ' ' print(positions)
Collections.namedtuple()
Task:
Dr. John Wesley has a spreadsheet containing a list of student's IDs, marks, class and name.
Your task is to help Dr. Wesley calculate the average marks of the students.
Solution:
n = input() columns = input() columns_list = columns.split() marks_index = columns_list.index("MARKS") l = [] marks_sum = 0 amount = 0 for x in range(int(n)): inp = input() value_list = inp.split() marks_sum += int(value_list[marks_index]) amount += 1 print(marks_sum / amount)
Collections.OrderedDict()
Task:
You are the manager of a supermarket.
You have a list of N items together with their prices that consumers bought on a particular day.
Your task is to print each item_name and net_price in order of its first occurrence.
item_name = Name of the item.
net_price = Quantity of the item sold multiplied by the price of each item.
Solution:
n = int(input()) resultDict = dict() for i in range(n): item = input() arr = item.rpartition(' ') name = arr[0] price = arr[2] if resultDict.get(name) is None: resultDict[name] = price else: originalPrice = resultDict[name] resultDict[name] = int(originalPrice) + int(price) for i in resultDict: print(i + ' ' + str(resultDict[i]))
Word Order
Task:
You are given n words.
Some words may repeat.
For each word, output its number of occurrences.
The output order should correspond with the input order of appearance of the word.
See the sample input/output for clarification.
Solution:
n = input() word_list = [] for i in range(int(n)): word_list.append(input()) word_list_dict = dict() for i in word_list: try: count = word_list_dict[i] word_list_dict[i] = count + 1 except: word_list_dict[i] = 1 print(len(word_list_dict)) count = '' for i in word_list_dict: count += str(word_list_dict.get(i)) + ' ' print(count)
Collections.deque()
Task:
Perform append, pop, popleft and appendleft methods on an empty deque d.
Solution:
from collections import deque n = int(input()) d = deque() for i in range(n): methods = input() split_method = methods.split() method = split_method[0] if method == 'append': d.append(int(split_method[1])) elif method == 'appendleft': d.appendleft(int(split_method[1])) elif method == 'pop': d.pop() elif method == 'popleft': d.popleft() d_str = '' for i in d: d_str += str(i) + ' ' print(d_str)
Company Logo
Task:
A newly opened multinational brand has decided to base their company logo on the three most common characters in the company name.
They are now trying out various combinations of company names and logos based on this condition.
Given a string s, which is the company name in lowercase letters, your task is to find the top three most common characters in the string.
- Print the three most common characters along with their occurrence count.
- Sort in descending order of occurrence count.
- If the occurrence count is the same, sort the characters in alphabetical order.
Solution:
import math import os import random import re import sys s = input() letter_list = [] letter_list[:0] = s letter_set = set(letter_list) occurrences = dict() for i in letter_list: value = occurrences.get(i) if value is None: occurrences[i] = 1 else: occurrences[i] = value + 1 occurrences = {val[0] : val[1] for val in sorted(occurrences.items(), key = lambda x: (-x[1], x[0]))} printed_lines = 0 for i in occurrences: if printed_lines < 3: print(str(i) + ' ' + str(occurrences.get(i))) printed_lines += 1
Piling Up!
Task:
There is a horizontal row of n cubes.
The length of each cube is given.
You need to create a new vertical pile of cubes.
The new pile should follow these directions: if cube[i] is on top of cube[j] then sideLength[j] >= sideLength[i].
When stacking the cubes, you can only pick up either the leftmost or the rightmost cube each time.
Print 'Yes' if it is possible to stack the cubes.
Otherwise, print 'No'.
Solution:
num_of_test_cases = int(input()) for i in range(1, num_of_test_cases + 1): blocks_size = int(input()) blocks = input() blocks = blocks.split(' ') top_of_pile = pow(2, 32) while len(blocks) > 0: blocks_length = len(blocks) if int(blocks[0]) > top_of_pile and int(blocks[-1]) > top_of_pile: break elif int(blocks[-1]) == top_of_pile: top_of_pile = int(blocks[-1]) del blocks[-1] continue elif int(blocks[0]) == top_of_pile: top_of_pile = int(blocks[0]) del blocks[0] continue if int(blocks[0]) < int(blocks[-1]): if int(blocks[-1]) < top_of_pile: top_of_pile = int(blocks[-1]) del blocks[-1] else: if int(blocks[0]) < top_of_pile: top_of_pile = int(blocks[0]) del blocks[0] if blocks_length == len(blocks): break if len(blocks) == 0: print('Yes') else: print('No')