Python3 hackerRank exercise with solutions (Set 2)

 1. Cab and Walk

Arun is working in an office which is N blocks away from his house. He wants to minimize the time it takes him to the from his house to the office. He can either take the office cab or he can walk to the office.
While walking Arun's velocity is V1 m/s  and V2 m/s  is the velocity of cab. But when he calls the cab it always starts from the office, covers N blocks, collects Arun and goes back to the office.
The cab crosses a total distance of N meters when going from office to Arun's  house and vice versa , whereas Arun covers a distance of √2*N while walking.
Help Arun to decide whether he has to walk or to take the cab to save the time.

Input format :
A single line containing three integers N, V1 and V2 separated by space.

Output format :
Print "Walk" or "Cab" accordingly.

Sample input 1 :
5  10  15

Sample output 1 :
Cab

Sample input 2 :
2  10  14

Sample output 2 :
Walk

Code :


cab and walk in python










Output :








2. End - Sort

Given a list A of N distinct numbers we can sort the list A by moving an element to the end of the list. Find  the minimum number of moves required to sort the list using this method in ascending order

Input format :
The first line of the input contains N distinct numbers of list A separated by space.

Output format :
Print the minimum number of moves required to sort the list.

Sample input :
1 3 2 4 5

Sample output :
3

Code :

end sort in python










Output :






3. Semi primes

A semi primes is a number that can be expressed as a product of two distinct prime. For example 15 = 5*3 is a semi primes whereas 9 = 3*3 is not a semi primes. Given an integers N find whether N can be expressed as a sum of two semi primes  or not (not necessarily distinct).

Input format :
The first line contains the integers N .

Output format :
Print "YES" if it is possible to represent N as a sum of two semi primes. Otherwise print "NO" .

Sample input :
123

Sample output :
YES

Code :


semi prime in python




















Output :







4. Dictionary

Given a positive number N we are supposed to write a program that generates a dictionary d which contains (i,i*i*i) such that i is the key and i*i*i is the corresponding value of the key i .  i ranges from 1 to N (both included) . Then just print the dictionary d.

Input format :
The first line contains the number N.

Output format :
Print the dictionary d.

Sample input :
5

Sample output :
{1:1,2:8,3:27,4:64,5:125}

Code :

dictionary in python









Output :







5. Function

Define a function printDict for an integer N
that prints the dictionary with key i and corresponding value i*i for the key.

Input format :
The first line contains the integer N.

Output format :
Print the dictionary.

Sample input :
4

Sample output :
{1:1,2:4,3:9,4:16}

Code :


function in python










Output :







For python tutorials ,






Comments

Popular posts from this blog

Finding the percentage in python - HackerRank solution

HackerRank challenges with solutions (Rearrangement, Formula, Word sorting, Numbers, Sentence)

ginortS in Python - HackerRank solution