Posts

Showing posts from August, 2020

10: Binary Numbers ( python)

                            Binary Numbers ( python)                            Task Given a base-  integer,  , convert it to binary (base- ). Then find and print the base-  integer denoting the maximum number of consecutive  's in  's binary representation. Input Format A single integer,  . Constraints Output Format Print a single base-  integer denoting the maximum number of consecutive  's in the binary representation of  . solution : #!/bin/python3 import  math import  os import  random import  re import  sys if  __name__ ==  '__main__' :   n =  int ( input ()) max_one_count =  0 one_count =  0 while  n !=  0 :     factor = n //  2     remainder...

9: Recursion in python

                                9: Recursion in python Recursive Method for Calculating Factorial Task Write a  factorial  function that takes a positive integer,   as a parameter and prints the result of   (  factorial). Note:  If you fail to use recursion or fail to name your recursive function  factorial  or  Factorial , you will get a score of  . Input Format A single integer,   (the argument to pass to  factorial ). Constraints Your submission must contain a recursive function named  factorial . Output Format Print a single integer denoting  . Solution: #!/bin/python3 import math import os import random import re import sys def factorial(n):     if n == 0:         return 1     else:         return n * factorial(n - 1) n = int(input()) print(factorial(n))

8: Dictionaries and Maps IN PYTHON

                      Dictionaries and Maps IN PYTHON Task Given   names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each   queried, print the associated entry from your phone book on a new line in the form  name=phoneNumber ; if an entry for   is not found, print  Not found  instead. Note:  Your phone book should be a Dictionary/Map/HashMap data structure. Input Format The first line contains an integer,  , denoting the number of entries in the phone book. Each of the   subsequent lines describes an entry in the form of   space-separated values on a single line. The first value is a friend's name, and the second value is an  -digit phone number. After the   lines of phone book entries, there are  an ...