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

HackerRank Python Solution - Strings - Text Alignment

  • In Python, a string of text can be aligned left, right and center.
.ljust(width):
  • This method returns a left-aligned string of length width.
>>> width = 20
>>> print 'HackerRank'.ljust(width,'-')
HackerRank----------

HackerRank Python Solution - Strings - String Validators

  • Python has built-in string validation methods for basic data. It can check if a string is composed of alphabetical characters, alphanumeric characters, digits, etc.
str.isalnum():
  • This method checks if all the characters of a string are alphanumeric (a-z, A-Z and 0-9).
>>> print 'ab123'.isalnum()
True
>>> print 'ab123#'.isalnum()
False

HackerRank Python Solution - Strings - Find a String

  • In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.
  • NOTE: String letters are case-sensitive.
Input Format:
  • The first line of input contains the original string. The next line contains the substring.

HackerRank Python Solution - Strings - Mutations

  • We have seen that lists are mutable (they can be changed), and tuples are immutable (they cannot be changed).
  • Let's try to understand this with an example.
  • You are given an immutable string, and you want to make changes to it.
Example:

>>> string = "abracadabra"

HackerRank Python Solution - Strings - What's Your Name?

You are given the firstname and lastname of a person on two different lines. Your task is to read them and print the following:
 
Hello firstname lastname! You just delved into python.
Function Description:
  • Complete the print_full_name function in the editor below.
  • print_full_name has the following parameters:
    • string first: the first name
    • string last: the last name
    • Prints string: 'Hello firstname lastname! You just delved into python' where and are replaced with first and last.

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