Skip to main content

Posts

Showing posts from August, 2022

Class and object Task 1| cinema tickets | hacker rank|tcs fresco play

  class   Movie :      def   __init__ ( self , nameofmovie , nooftickets , totalcost ):          self . nameofmovie = nameofmovie          self . nooftickets = nooftickets          self . totalcost = totalcost      def   __str__ ( self ):          return   "Movie : " + str ( self . nameofmovie ) + "\n" + "Number of Tickets : " + str ( self . nooftickets ) + "\n" + "Total Cost : " + str ( self . totalcost )                   # Write your code here if   __name__  ==  '__main__' :

Remove duplicates from sorted linked list

# Definition for singly-linked list. # class ListNode: #     def __init__(self, val=0, next=None): #         self.val = val #         self.next = next class Solution:     def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:                  temp=head         while temp:             while temp.next and temp.next.val==temp.val:                 temp.next=temp.next.next             temp=temp.next         return head                                   

data structures || linked list

  from itertools import count from logging import exception class Node : #ele in  linked list call node,node will have data, and pointer to next     #so create node cls with data and next     def __init__ ( self , data ):         self . data = data         self . next = None         #start of linked list called head class Linkedlist :     def __init__ ( self ):         self . head = None     def addinfront ( self , newdata ):         # to add node in front of linked list, create new node that you wanted to add         # point that to already existing linked list head         #  now make new node as head of linked list         node = Node ( newdata )         node . next = self . head         self . head = node     ...

Functions and OOPs import datetime||Python Hands on datetime \ hacker rank solution

  #!/bin/python3 import   math import   os import   random import   re import   sys import   datetime # # Complete the 'dateandtime' function below. # # The function accepts INTEGER val as parameter. # The return type must be LIST. # def   dateandtime ( val , tup ):      emtlist = []      if   val == 1 :          d1 = datetime . date ( tup [ 0 ], tup [ 1 ], tup [ 2 ])          emtlist . append ( d1 )          dd1 = d1 . strftime ( "%d/%m/%Y" )          emtlist . append ( dd1 )      if   val == 2 :          d  =  datetime . datetime . fromtimestamp ( int ( tup [ 0 ]))   ...

Handling Exceptions 4 | Library| hacker rank solution

#!/bin/python3 import   math import   os import   random import   re import   sys # # Complete the 'Library' function below. #   def   Library ( memberfee , installment , book ):      if   installment > 3 :          raise   ValueError ( "Maximum Permitted Number of Installments is 3" )      #amount=memberfee/installment      elif   installment == 0 :          raise   ZeroDivisionError ( "Number of Installments cannot be Zero." )      else :          amount = memberfee / installment          print ( "Amount per Installment is  " + str ( amount ))      available = [ "philos...

Handling exceptions 2 for loop hacker rank solution

  #!/bin/python3 import   math import   os import   random import   re import   sys # # Complete the 'FORLoop' function below. # def   FORLoop ():      n = int ( input ())      l1 = []      iter1 = []      for   i   in   range ( n ):          ele = int ( input ())          l1 . append ( ele )      print ( l1 )      iter1 = iter ( l1 )      for   a   in   iter1 :          print   ( a )      return   iter1                    # Write your code here if   __name__  ==  '__main__' :     ...

Handling Exceptions 1 | hacker rank solution

  #!/bin/python3 import   math import   os import   random import   re import   sys # # Complete the 'Handle_Exc1' function below. # # def   Handle_Exc1 ():      a = int ( input ())      b = int ( input ())      if   a > 150   or   b < 100 :          raise   ValueError   ( "Input integers value out of range." )        if   a + b > 400 :          raise   ValueError ( "Their sum is out of range" )      print ( "All in range" )      # Write your code here if   __name__  ==  '__main__' :      try :          Handle_Exc1 ()   ...

Classes and objects 2|task1 & 2| inheritance and polymorphism

  #!/bin/python3 task 1 Inheritance: parent and children share import   math import   os import   random import   re import   sys class   parent :    def   __init__ ( self , total_asset ):      self . total_asset  =  total_asset    def   display ( self ):      print ( "Total Asset Worth is " + str ( self . total_asset ) + " Million." )      print ( "Share of Parents is " + str ( round ( self . total_asset / 2 , 2 )) + " Million." ) class   son ( parent ):      def   __init__ ( self ,   total_asset , Percentage_for_son ):          self . Percentage_for_son = Percentage_for_son          parent . __init__ ( self , total_asset )             ...

python program to print prime numbers from 1 to n

  def prime ( num ):     i = 2     while i < num :         prime = True         for a in range ( 2 , i ):             if i % a == 0 :                 prime = False                 break         if prime == True :             print ( i , end = " " )         i = i + 1   prime ( 21 )

Magic constant generator -python3/hacker rank solution / tcs fresco play

  def   generator_Magic ( n1 ):      # the value starts from 3 and m is formula for constant,      #for generator  yield should use      for   a   in   range ( 3 , n1 + 1 ):          m = a * ( a ** 2 + 1 ) / 2          yield   m               # Write your code here if   __name__  ==  '__main__' :