HackerRank Python Solution - Strings - Split and Join

In Python, a string can be split on a delimiter.

Example:

>>> a = "this is a string"
>>> a = a.split(" ") # a is converted to a list of strings. 
>>> print a
['this', 'is', 'a', 'string']
Joining a string is simple:
 
>>> a = "-".join(a)
>>> print a
this-is-a-string 

HackerRank Python Solution - Strings - sWAP cASE

You are given a string and your task is to swap cases. In other words, convert all lowercase letters to uppercase letters and vice versa.

For Example:

Www.HackerRank.com → wWW.hACKERrANK.COM
Pythonist 2 → pYTHONIST 2  

HackerRank Python Solution - Strings - Designer Door Mat

Mr. Vincent works in a doormat manufacturing company. One day, he designed a new doormat with the following specifications:
  • Mat size must be N x M. (N is an odd natural number, and M is 3 times N.)
  • The design should have 'WELCOME' written in the center.
  • The design pattern should only use |,. and - characters.

HackerRank Python Solution - Built-Ins - ginortS

  • You are given a string S. S contains alphanumeric characters only.
  • Your task is to sort the string S in the following manner:
    • All sorted lowercase letters are ahead of uppercase letters.
    • All sorted uppercase letters are ahead of digits.
    • All sorted odd digits are ahead of sorted even digits.

HackerRank Python Solution - Built-Ins - Any or All

any():
  • This expression returns True if any element of the iterable is true. If the iterable is empty, it will return False.
Code:

>>> any([1>0,1==0,1<0])
True
>>> any([1<0,2<1,3<2])
False

HackerRank Python Solution - Built-Ins - Athlete Sort

  • You are given a spreadsheet that contains a list of N athletes and their details (such as age, height, weight, and so on). You are required to sort the data based on the Kth attribute and print the final resulting table. Follow the example given below for a better understanding.
  • Note that K is indexed from 0 to M-1, where M is the number of attributes.
  • Note: If two attributes are the same for different rows, for example, if two athletes are of the same age, print the row that appeared first in the input.

HackerRank Python Solution - Built-Ins - eval()

  • The eval() expression is a very powerful built-in function of Python. It helps in evaluating an expression. The expression can be a Python statement or a code object.
  • For example:
>>> eval("9 + 5")
14
>>> x = 2
>>> eval("x + 3")
5
  • Here, eval() can also be used to work with Python keywords or defined functions and variables. These would normally be stored as strings.

HackerRank Python Solution - Built-Ins - Input()

input():
  • In Python, the expression input() is equivalent to eval(raw _input(prompt)).
Code:
 
>>> input()  
1+2
3
>>> company = 'HackerRank'
>>> website = 'www.hackerrank.com'
>>> input()
'The company name: '+company+' and website: '+website
'The company name: HackerRank and website: www.hackerrank.com'

HackerRank Python Solution - Built-Ins - Zipped!

Zip([iterable, ...]):
  • This function returns a list of tuples. The ith tuple contains the ith element from each of the argument sequences or iterables.
  • If the argument sequences are of unequal lengths, then the returned list is truncated to the length of the shortest argument sequence.
Sample Code:

>>> print zip([1,2,3,4,5,6],'Hacker')
[(1, 'H'), (2, 'a'), (3, 'c'), (4, 'k'), (5, 'e'), (6, 'r')]
>>> 
>>> print zip([1,2,3,4,5,6],[0,9,8,7,6,5,4,3,2,1])
[(1, 0), (2, 9), (3, 8), (4, 7), (5, 6), (6, 5)]
>>> 
>>> A = [1,2,3]
>>> B = [6,5,4]
>>> C = [7,8,9]
>>> X = [A] + [B] + [C]
>>> 
>>> print zip(*X)
[(1, 6, 7), (2, 5, 8), (3, 4, 9)]

Microsoft Azure Fundamentals AZ-900 Exam Practice Test

Microsoft Azure Fundamental AZ-900 Topic:

  • Describe Cloud Concepts (20-25%)
  • Describe Core Azure Services (15-20%)
  • Describe core solutions and management tools on Azure (10-15%)
  • Describe general security and network security features (10-15%)
  • Describe identity, governance, privacy, and compliance features (15-20%)
  • Describe Azure cost management and Service Level Agreements (10-15%)
48 questions | 70% correct required to PASS

HackerRank Python Solution - Sets - No Idea!

There is an array of n integers. There are also 2 disjoint sets, A and B, each containing m integers. You like all the integers in set A and dislike all the integers in set B. Your initial happiness is 0. For each i integer in the array, if i ∊ A, you add 1 to your happiness. If i ∊ B, you add -1 to your happiness. Otherwise, your happiness does not change. Output your final happiness at the end.

Note: Since A and B are set, they have no repeated elements. However, the array might contain duplicate elements.

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...