Skip to main content

Posts

Showing posts from November, 2022

Merge two Sorted Linked List hacker rank solution python

  #!/bin/python3 import  math import  os import  random import  re import  sys class  SinglyLinkedListNode:      def  __init__( self , node_data):          self .data = node_data          self . next  =  None class  SinglyLinkedList:      def  __init__( self ):          self .head =  None          self .tail =  None      def  insert_node( self , node_data):         node = SinglyLinkedListNode(node_data)          if   not   self .head:              self .head = node       ...

New year chaos hacker rank python solution

  def  minimumBribes(q):     bribe= 0      for  i  in   range ( len (q) -1 , 0 , -1 ):          #search from last                    if  q[i]!=i+ 1 :              if  q[i -1 ]==i+ 1 :                 bribe+= 1                 q[i -1 ],q[i]=q[i],q[i -1 ]              elif  q[i -2 ]==i+ 1 :                 bribe+= 2                 q[i -2 ],q[i -...

Anagram hackrank solution in python

  #!/bin/python3 import  math import  os import  random import  re import  sys # # Complete the 'anagram' function below. # # The function is expected to return an INTEGER. # The function accepts STRING s as parameter. # from  collections  import  Counter def  anagram(s):     n= len (s)      if  n% 2 == 0 :         sub1=s[:n// 2 ]         sub2=s[n// 2 :]         a=Counter(sub1)         b=Counter(sub2)          return   sum ((a-b).values())      return   -1                     ...

Between Two sets hacker rank solution python

  #!/bin/python3 import  math import  os import  random import  re import  sys # # Complete the 'getTotalX' function below. # # The function is expected to return an INTEGER. # The function accepts following parameters: #  1. INTEGER_ARRAY a #  2. INTEGER_ARRAY b # def  getTotalX(a, b):     count= 0      for  i  in   range ( max (a), min (b)+ 1 ):          if   all  ([i%j== 0   for  j  in  a]) and   all ([j%i== 0   for  j  in  b]):             count+= 1                   return  count      # Write you...

CounterGame hacker rank solution python

  #!/bin/python3 import  math import  os import  random import  re import  sys # # Complete the 'counterGame' function below. # # The function is expected to return a STRING. # The function accepts LONG_INTEGER n as parameter. # import  math def  counterGame(n):     i= 0      while   True :          if  n== 1 :              return   "Richard"   if  i %  2  ==  0   else   'Louise'                  x= 2 ** int (math.log(n, 2 ))          if  n==x:             n=n// 2   ...

textwrap hacker rank solution

  import  textwrap def  wrap(string, max_width):     s= ""      for  i  in   range ( 0 , len (string),max_width):         s=s+string[i:i+max_width]+ "\n"               return  s if  __name__ ==  '__main__' :     string, max_width =  input (),  int ( input ())     result = wrap(string, max_width)      print (result)

Dynamic Array hacker rank solution

  #!/bin/python3 import   math import   os import   random import   re import   sys # # Complete the 'dynamicArray' function below. # # The function is expected to return an INTEGER_ARRAY. # The function accepts following parameters: #  1. INTEGER n #  2. 2D_INTEGER_ARRAY queries # def   dynamicArray ( n ,   queries ):      arr  =  [   [] for   i   in   range ( n )]      answers  =  []      lastAnswer  =  0      for   query   in   queries :          type ,   x ,   y ,  =  query          idx  =  (( x  ^  lastAnswer )  %  n )     ...

MaxMin in unfairness hacker rank solution

  #!/bin/python3 import   math import   os import   random import   re import   sys # # Complete the 'maxMin' function below. # # The function is expected to return an INTEGER. # The function accepts following parameters: #  1. INTEGER k #  2. INTEGER_ARRAY arr # def   maxMin ( k ,   arr ):      arr . sort ()      result = []      for   i   in   range ( len ( arr ) - k + 1 ):          n = arr [ k + i -1 ] - arr [ i ]          result . append ( n )      result . sort ()      return   result [ 0 ]               # Write your code here if  __ n...

Caesar Cipher hacker rank solution

def caesarCipher ( s , k ):     string = "abcdefghijklmnopqrstuvwxyz" * k     newstring = string .upper()     emtstring = ""     for i in s :         if i in string :             index = string .index( i )+ k             emtstring += string [ index ]         elif i in newstring :             index = newstring .index( i )+ k             emtstring += newstring [ index ]         else :             emtstring += i     return emtstring print ( caesarCipher ( "middle-Outz" , 2 )) #okffng-Qwvb

Print prime number upto N using python

prime numbers between 1 to 100 num1 = int ( input ( "num1" )) num2 = int ( input ( "num2" )) for number in range ( num1 , num2 ):     if number > 1 :         for i in range ( 2 , number ):             if number % i == 0 :                 break         else :             print ( number , end = " " ) output :-2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97