Posts

Showing posts with the label python

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