Skip to main content

Posts

Showing posts from July, 2022

python-Decorator

 Decorator: the decorators are used to modify the behavior of function or class. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function. Example: def shout(text):      return text.upper()   def greet(func):      # storing the function in a variable      greeting = func( "Hi, I am created by a function passed as an argument." )      print (greeting)   greet(shout) Output: HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT. In the above example, the greet function takes another function as a parameter (shout ). The function passed as an argument is then called inside the function greet

Lambda, Map ,filter in python

Lambda:- lambda is used to define an anonymous function which is used only once. it takes an argument and expression it can have any number of arguments but only one expression Syntex: lambda a,b:a+b Map:-  Map takes two parameters and it is used to update values  map(function or none, iterable) filter:  filter takes two parameters  map(function or none, iterable) when the function given is true  it stores the value in variable ex:- numbs =[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] y = list ( filter ( lambda a : a % 2 == 0 , numbs )) x = list ( map ( lambda a : a * a , y )) print ( x )

TCS DCA wings 1 Python most asked question and Solution

 TCS DCA wings 1 Python most asked question and Solution Solution list1=input() list1=list1.split(",") st=list1[0] for i in list1:     for j in i:         if j not in st:             st+=j print(st)

Python 3 functions and Oops -string method / hacker rank

  def   stringmethod ( para ,   special1 ,   special2 ,   list1 ,   strfind ):           word1 = ""      for   letter   in   para :          if   letter   not   in   special1 :              word1 += letter      wordn = word1 [ 0 : 70 ]        rword2 = wordn [:: -1 ]        print ( rword2 )      rword2 = rword2 . replace ( " " , "" )      print ( special2 . join ( rword2 ))      list3 = []      if   all ( SearchStr   in   para   for   SearchStr   in   list1 ):          print ( "Every string in %s were present"  ...

Swap Case hacker rank solution

  def  swap_case ( s ):     result =  ""      for  letter  in  s :          if  letter == letter.upper ():             result += letter.lower ()          else :             result += letter.upper ()      return  result or we can use built in swapcase library def Swap_case(s): return s.swapcase()

Lonely Integer hacker rank solution

  #!/bin/python3 import  math import  os import  random import  re import  sys # # Complete the 'lonelyinteger' function below. # # The function is expected to return an INTEGER. # The function accepts INTEGER_ARRAY a as parameter. # def  lonelyinteger ( a ):      for  i  in   range ( 0 , len ( a )):          if  a.count ( a [ i ]) ==1 :              return  a [ i ]      # Write your code here if  __name__ ==  '__main__' :     fptr =  open ( os.environ [ 'OUTPUT_PATH' ],   'w' )     n =  int ( input () .strip ())     a =  list ( map ( int ,   input () .rstrip ...

Sparse Arrays hacker rank

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.   #!/bin/python3 import  math import  os import  random import  re import  sys # # Complete the 'matchingStrings' function below. # # The function is expected to return an INTEGER_ARRAY. # The function accepts following parameters: #  1. STRING_ARRAY strings #  2. STRING_ARRAY queries # def  matchingStrings ( strings ,  queries ):      # Write your code here          result= []     count=0           for  index , element  in   enumerate ( queries ):     ...

Time Conversion

Given a time in  -hour AM/PM format , convert it to military (24-hour) time. Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock. - 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock. #!/bin/python3 import  math import  os import  random import  re import  sys # # Complete the 'timeConversion' function below. # # The function is expected to return a STRING. # The function accepts STRING s as parameter. # def  timeConversion ( s ):      if  s [ -2 ]  ==  "A"   and  s [ 0 : 2 ]  ==  "12" :          return ( "00"  + s [ 2 : -2 ])      elif  s [ -2 ]  ==  "P"   and  s [ 0 : 2 ]  ==  "12"   or  s [ -2 ]  ==  "A" :        ...

Mini -Max sum Hacker rank

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.   #!/bin/python3 import  math import  os import  random import  re import  sys # # Complete the 'miniMaxSum' function below. # # The function accepts INTEGER_ARRAY arr as parameter. # def  miniMaxSum ( arr ):     arr.sort ()     maxsum=0     minsum=0      for  i  in   range ( 1 , len ( arr )):         maxsum+=arr [ i ]      for  n  in   range ( 0 , len ( arr ) -1 ):         minsum+=arr [ n ]           print ( ...

plus minus hacker rank

  #!/bin/python3 import  math import  os import  random import  re import  sys # # Complete the 'plusMinus' function below. # # The function accepts INTEGER_ARRAY arr as parameter. # def  plusMinus ( arr ):     sumpositive=0     sumneg=0     sumzero=0      for  i  in   range ( 0 , len ( arr )):          if  arr [ i ] >0 :             sumpositive+=1          elif  arr [ i ] <0 :             sumneg+=1          else :             sumzero+=1     dec1=sumpositive/ len ( arr )...

Final Project - Word Cloud

  Final Project - Word Cloud # Here are all the installs and imports you will need for your word cloud script and uploader widget !pip install wordcloud !pip install fileupload !pip install ipywidgets !jupyter nbextension install --py --user fileupload !jupyter nbextension enable --py fileupload import wordcloud import numpy as np from matplotlib import pyplot as plt from IPython.display import display import fileupload import io import sys # This is the uploader widget def _upload():     _upload_widget = fileupload.FileUploadWidget()     def _cb(change):         global file_contents         decoded = io.StringIO(change['owner'].data.decode('utf-8'))         filename = change['owner'].filename         print('Uploaded `{}` ({:.2f} kB)'.format(             filename, len(decoded.read()) / 2 **10))         file_contents = decoded.getvalue...