Posts

Find a String | Hacker Rank Solution in Python

Problem: 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. Solution: def  count_substring(s,b):      return   len ([i  for  i  in   range ( len (s))  if  s[i:i+ len (b)] == b]) if  __name__ ==  '__main__' :     string =  raw_input ().strip()     sub_string =  raw_input ().strip()          count = count_substring(string, sub_string)      print  count

Mutations| Hacker Rank Solution in Python

Problem: 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" You can access an index by: >>> print string[5] a Solution: def  mutate_string(string, position, character):     l1= list (string)     l1[position]=character     stri =  '' .join(l1)      return  stri if  __name__ ==  '__main__' :     s =  raw_input ()     i, c =  raw_input ().split()     s_new = mutate_string(s,  int (i), c)      print  s_new

What's Your Name? | Hacker Rank Solution in Python

Problem: 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. Solution: def  print_full_name(a, b):      print  ( "Hello "  + a +  " " + b + "! You just delved into python." ) if  __name__ ==  '__main__' : first_name =  raw_input ()     last_name =  raw_input ()     print_full_name(first_name, last_name)

Print Function | Hacker Rank Solution in Python

Problem: The included code stub will read an integer,  , from STDIN. Without using any string methods, try to print the following: Note that " " represents the consecutive values in between. Example Print the string  . Solution: from  __future__  import  print_function if  __name__ ==  '__main__' :     n =  int ( raw_input ())     i= 1      print (* range ( 1 ,n+ 1 ),sep= "" )         

Write a Function | Hacker Rank Solution in Python

Problem: An extra day is added to the calendar almost every four years as February 29, and the day is called a  leap day . It corrects the calendar for the fact that our planet takes approximately 365.25 days to orbit the sun. A leap year contains a leap day. In the Gregorian calendar, three conditions are used to identify leap years: The year can be evenly divided by 4, is a leap year, unless: The year can be evenly divided by 100, it is NOT a leap year, unless: The year is also evenly divisible by 400. Then it is a leap year. This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.  Source Solution: def is_leap(year):     leap = False     if year%4==0 and (year%400==0 or year%100!=0):             leap=True     else:         leap=False     return leap year =  int ( raw_input ()) print ...

Python If-Else | Hacker Rank

Problem: Given an integer,  , perform the following conditional actions: If   is odd, print  Weird If   is even and in the inclusive range of   to  , print  Not Weird If   is even and in the inclusive range of   to  , print  Weird If   is even and greater than  , print  Not Weird Solution: #!/bin/python import math import os import random import re import sys #print(help(os)) #print(help(re)) #print(help(sys)) if __name__ == '__main__':     n = int(raw_input().strip())     if(n%2!=0):         print('Weird')     if(n%2==0):         if((n>6 and n<21)):             print('Weird')         else:             print('Not Weird')          

Counter game | Hacker Rank in C

Problem: Louise and Richard have developed a numbers game. They pick a number and check to see if it is a power of  . If it is, they divide it by  . If not, they reduce it by the next lower number which is a power of  . Whoever reduces the number to   wins the game. Louise always starts. Given an initial value, determine who wins the game. As an example, let the initial value  . It's Louise's turn so she first determines that   is not a power of  . The next lower power of   is  , so she subtracts that from   and passes   to Richard.   is a power of  , so Richard divides it by   and passes   to Louise. Likewise,   is a power so she divides it by   and reaches  . She wins the game. Update  If they initially set counter to  , Richard wins. Louise cannot make a move so she loses. Solution: #include <assert.h> #include <limits.h> #i...