30 Days of Code: HackerRank

Saurabh
8 min readDec 16, 2020

Day 0: Hello, World.

Objective
In this challenge, we review some basic concepts that will get you started with this series. You will need to use the same (or similar) syntax to read input and write output in challenges throughout HackerRank. Check out the Tutorial tab for learning materials and an instructional video!

Task
To complete this challenge, you must save a line of input from stdin to a variable, print Hello, World. on a single line, and finally print the value of your variable on a second line.

You’ve got this!

Note: The instructions are Java-based, but we support submissions in many popular languages. You can switch languages using the drop-down menu above your editor, and the input string variable may be written differently depending on the best-practice conventions of your submission language.

Input Format

A single line of text denoting input string(the variable whose contents must be printed).

Output Format

Print Hello, World. on the first line, and the contents of input string on the second line.

Sample Input

Welcome to 30 Days of Code!

Sample Output

Hello, World. 
Welcome to 30 Days of Code!

Explanation

On the first line, we print the string literal Hello, World.. On the second line, we print the contents of the input string variable which, for this sample case, happens to be Welcome to 30 Days of Code!. If you do not print the variable's contents to stdout, you will not pass the hidden test case.

Source Code-

# Read a full line of input from stdin and save it to our dynamically typed variable, input_string.

input_string = input()

# Print a string literal saying “Hello, World.” to stdout.

print(‘Hello, World.’)

# TODO: Write a line of code here that prints the contents of input_string to stdout.

print(input_string)

Day 1: Data Types

Objective
Today, we’re discussing data types. Check out the Tutorial tab for learning materials and an instructional video!

Task
Complete the code in the editor below. The variables i, d ,s and are already declared and initialized for you. You must:

  1. Declare 3 variables: one of type int, one of type double, and one of type String.
  2. Read 3 lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your variables.
  3. Use the operator to perform the following operations:
  4. Print the sum of i plus your int variable on a new line.
  5. Print the sum of d plus your double variable to a scale of one decimal place on a new line.
  6. Concatenate s with the string you read as input and print the result on a new line.

Note: If you are using a language that doesn’t support using for string concatenation (e.g.: C), you can just print one variable immediately following the other on the same line. The string provided in your editor must be printed first, immediately followed by the string you read as input.

Input Format

The first line contains an integer that you must sum with i.
The second line contains a double that you must sum with d.
The third line contains a string that you must concatenate with s.

Output Format

Print the sum of both integers on the first line, the sum of both doubles (scaled to decimal place) on the second line, and then the two concatenated strings on the third line.

Sample Input

12
4.0
is the best place to learn and practice coding!

Sample Output

16
8.0
HackerRank is the best place to learn and practice coding!

Explanation

When we sum the integers 4 and 12 , we get the integer 16.
When we sum the floating-point numbers 4.0 and 4.0, we get 8.0.
When we concatenate HackerRank with is the best place to learn and practice coding!, we get HackerRank is the best place to learn and practice coding!.

You will not pass this challenge if you attempt to assign the Sample Case values to your variables instead of following the instructions above and reading input from stdin.

Souce code-

i = 4

d = 4.0

s = ‘HackerRank ‘

# Declare second integer, double, and String variables.

integer=int(input())

double_int=float(input())

string=input()

# Read and save an integer, double, and String to your variables.

# Print the sum of both integer variables on a new line.

print(integer+i)

# Print the sum of the double variables on a new line.

print(d+ double_int)

# Concatenate and print the String variables on a new line

# The ‘s’ variable above should be printed first.

print(s+string)

Day 2: Operators

Objective
In this challenge, you will work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video.

Task
Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal’s total cost. Round the result to the nearest integer.

Example

A tip of 15% * 100 = 15, and the taxes are 8% * 100 = 8. Print the value 123 and return from the function.

Function Description
Complete the solve function in the editor below.

solve has the following parameters:

  • int meal_cost: the cost of food before tip and tax
  • int tip_percent: the tip percentage
  • int tax_percent: the tax percentage

Returns The function returns nothing. Print the calculated value, rounded to the nearest integer.

Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result.

Input Format

There are 3 lines of numeric input:
The first line has a double meal_cost, (the cost of the meal before tax and tip).
The second line has an integer tip_percent, (the percentage of being added as tip).
The third line has an integer tax_percent, (the percentage of being added as tax).

Sample Input

12.00
20
8

Sample Output

15

Source Code-

#!/bin/python3

import math

import os

import random

import re

import sys

# Complete the solve function below.

def solve(meal_cost, tip_percent, tax_percent):

tip= (meal_cost)/100 * tip_percent

tax= (meal_cost)/100 * tax_percent

return (meal_cost + float(tip) + float(tax))

if __name__ == ‘__main__’:

meal_cost = float(input())

tip_percent = int(input())

tax_percent = int(input())

x=solve(meal_cost, tip_percent, tax_percent)

print(round(x))

Day 3: Intro to Conditional Statements

Objective
In this challenge, we learn about conditional statements. Check out the Tutorial tab for learning materials and an instructional video.

Task
Given an integer, , perform the following conditional actions:

  • If is odd, print Weird
  • If is even and in the inclusive range of 2 to 5, print Not Weird
  • If is even and in the inclusive range of 6 to 20, print Weird
  • If is even and greater than 20, print Not Weird

Complete the stub code provided in your editor to print whether or not is weird.

Input Format

A single line containing a positive integer, .

Constraints

Output Format

Print Weird if the number is weird; otherwise, print Not Weird.

Sample Input 0

3

Sample Output 0

Weird

Sample Input 1

24

Sample Output 1

Not Weird

Source Code-

#!/bin/python3

import math

import os

import random

import re

import sys

if __name__ == ‘__main__’:

n = int(input())

if n%2==0:

if n>=2 and n<=5:

print(‘Not Weird’)

if n>=5 and n<=20:

print(‘Weird’)

if n>20:

print(‘Not Weird’)

else:

print(‘Weird’)

Day 4: Class vs. Instance

Objective
In this challenge, we’re going to learn about the difference between a class and an instance; because this is an Object Oriented concept, it’s only enabled in certain languages. Check out the Tutorial tab for learning materials and an instructional video!

Task
Write a Person class with an instance variable, age, and a constructor that takes an integer,initialAge , as a parameter. The constructor must assign initialAge to age after confirming the argument passed as initialAge is not negative; if a negative argument is passed as initialAge, the constructor should set age to 0 and print Age is not valid, setting age to 0.. In addition, you must write the following instance methods:

  1. yearPasses() should increase the instance variable by .
  2. amIOld() should perform the following conditional actions:
  • If age<13, print You are young..
  • If age≥13 and age<18, print You are a teenager..
  • Otherwise, print You are old..

To help you learn by example and complete this challenge, much of the code is provided for you, but you’ll be writing everything in the future. The code that creates each instance of your Person class is in the main method. Don’t worry if you don’t understand it all quite yet!

Note: Do not remove or alter the stub code in the editor.

Input Format

Input is handled for you by the stub code in the editor.

The first line contains an integer, T(the number of test cases), and the T subsequent lines each contain an integer denoting the of a Person instance.

Constraints

Output Format

Complete the method definitions provided in the editor so they meet the specifications outlined above; the code to test your work is already in the editor. If your methods are implemented correctly, each test case will print or lines (depending on whether or not a valid was passed to the constructor).

Sample Input

4
-1
10
16
18

Sample Output

Age is not valid, setting age to 0.
You are young.
You are young.
You are young.
You are a teenager.
You are a teenager.
You are old.
You are old.
You are old.

Source Code-

class Person:

def __init__(self, initialAge):

if initialAge > 0:

self.age = initialAge

else:

print(“Age is not valid, setting age to 0.”)

self.age = 0

def amIOld(self):

if self.age < 13:

print(“You are young.”)

elif self.age < 18:

print(“You are a teenager.”)

else:

print(“You are old.”)

def yearPasses(self):

self.age += 1

t = int(input())

Day 5: Loops

Objective
In this challenge, we will use loops to do some math. Check out the Tutorial tab to learn more.

Task
Given an integer , n , print its first 10 multiples. Each multiple n x i (where 1≤i≤10 ) should be printed on a new line in the form: n x i = result.

Example

n=3

The printout should look like this:

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

Input Format

A single integer, .

Constraints

2≤n≤20

Output Format

Print lines 10 of output; each line (where 1≤i≤10) contains the result of n x i in the form:
n x i = result.

Sample Input

2

Sample Output

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

Source Code-

#!/bin/python3

import math

import os

import random

import re

import sys

if __name__ == ‘__main__’:

n = int(input())

for i in range(1,11):

print(str(n) + “ x “ + str(i) + “ = “ + str(n*i))

Day 6: Let’s Review

Objective
Today we will expand our knowledge of strings, combining it with what we have already learned about loops. Check out the Tutorial tab for learning materials and an instructional video.

Task
Given a string, S , of length N that is indexed from 0 to N-1 , print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail).

Note: 0 is considered to be an even index.

Example

Print abc def

Input Format

The first line contains an integer, T(the number of test cases).
Each line i of the T subsequent lines contain a string, S.

Output Format

For each String (where ), print ‘s even-indexed characters, followed by a space, followed by ‘s odd-indexed characters.

Sample Input

2
Hacker
Rank

Sample Output

Hce akr
Rn ak

Source Code-

n=int(input())

for i in range(n):

string=input()

print(string[::2], string[1::2])

--

--