Skip to main content

Posts

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