HackerRank Python Solution - Closures and Decorators - Name Directory

  • Let's use decorators to build a name directory! You are given some information about N people. Each person has a first name, last name, age, and sex. Print their names in a specific format sorted by their age in ascending order i.e. the youngest person's name should be printed first. For two people of the same age, print them in the order of their input.
  • For Henry Davids, the output should be:
Mr. Henry Davids
  • For Mary George, the output should be:
Ms. Mary George

HackerRank Python Solution - Closures and Decorators - Standardize Mobile Number Using Decorators

  • Let's dive into decorators! You are given N mobile numbers. Sort them in ascending order then print them in the standard format shown below:
+91 xxxxx xxxxx
  • The mobile numbers may have +91, 91, or 0 written before the actual 10-digit number. Alternatively, there may not be any prefix at all.
Input Format:
  • The first input line contains an integer N, the number of mobile phone numbers. N lines follow each having a mobile number.

HackerRank Python Solution - Functionals - Reduce Function

Given a list of rational numbers, find their product.

Concept:
  • The reduce() function applies a function of two arguments cumulatively on a list of objects in succession from left to right to reduce it to one value. Say you have a list, say [1,2,3] and you have to find its sum.
>>> reduce(lambda x, y : x + y,[1,2,3])
6

HackerRank Python Solution - Functionals - Validating Email Addresses with a Filter

  • You are given an integer N followed by N email addresses. Your task is to print a list containing only valid email addresses in lexicographical order.
  • Valid email addresses must follow these rules:
    • It must have the username@websitename.extension format type.
    • The username can only contain letters, digits, dashes and underscores [a-z],[A-Z],[0-9],[_-].
    • The website name can only have letters and digits [a-z],[A-Z],[0-9].
    • The extension can only contain letters [a-z],[A-Z].
    • The maximum length of the extension is 3.

HackerRank Python Solution - Functionals - Map and Lambda Function

Let's learn some new Python concepts! You have to generate a list of the first Fibonacci numbers, 0 being the first number. Then, apply the map function and a lambda expression to cube each Fibonacci number and print the list.

Concept:
  • The map() function applies a function to every member of an iterable and returns the result. It takes two parameters: first, the function that is to be applied, and second, the iterables.
  • Let's say you are given a list of names, and you have to print a list that contains the length of each name.
>> print (list(map(len, ['Tina', 'Raj', 'Tom'])))  
[4, 3, 3]  

HackerRank Python Solution - Strings - Merge the Tools!

Consider the following:
  • A string, s, of length n where s=c0c1...cn-1.
  • An integer, k, where k is a factor of n.
We can split s into n/k substrings where each substring, ti, consists of a contiguous block of k characters in s. Then, use each ti to create string ui such that:
  • The characters in ui are a subsequence of the characters in ti.
  • Any repeat occurrence of a character is removed from the string such that each character in ui occurs exactly once. In other words, if the character at some index j in ti occurs at a previous index < j in ti, then do not include the character in string ui.
Given s and k, print n/k lines where each line i denotes string ui.

HackerRank Python Solution - Strings - The Minion Game

  • Kevin and Stuart want to play 'The Minion Game'.
Game Rules:
  • Both players are given the same string, S.
  • Both players have to make substrings using the letters of the string S.
  • Stuart has to make words starting with consonants.
  • Kevin has to make words starting with vowels.
  • The game ends when both players have made all possible substrings.
Scoring:
  • A player gets a +1 point for each occurrence of the substring in the string S.

HackerRank Python Solution - Strings - Alphabet Rangoli

  • You are given an integer, N. Your task is to print an alphabet rangoli of size N. (Rangoli is a form of Indian folk art based on the creation of patterns.)
  • Different sizes of alphabet rangoli are shown below:

#size 3

----c----
--c-b-c--
c-b-a-b-c
--c-b-c--
----c----

HackerRank Python Solution - Strings - String Formatting

Given an integer, n, print the following values for each integer i from 1 to n:
  • Decimal
  • Octal
  • Hexadecimal (capitalized)
  • Binary
Function Description:
  • Complete the print_formatted function in the editor below.
  • print_formatted has the following parameters:
    • int number: the maximum value to print

HackerRank Python Solution - Strings - Capitalize!

  • You are asked to ensure that the first and last names of people begin with a capital letter in their passports. For example, alison heck should be capitalized correctly as Alison Heck.
HackerRank Python Solution - Strings - Capitalize!
  • Given a full name, your task is to capitalize the name appropriately.

HackerRank Python Solution - Strings - Text Wrap

  • You are given a string S and width w.
  • Your task is to wrap the string into a paragraph of width w.
Function Description:
  • Complete the wrap function in the editor below.
  • the wrap has the following parameters:
    • string: a long string
    • int max_width: the width to wrap to
    • Returns string: a single string with newline characters ('\n') where the breaks should be

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...