Posts

Showing posts from July, 2020

Hackerrank | Append and Delete

def   appendAndDelete ( s ,   t ,   k ):      count  =  0      total  =  len ( s )  +  len ( t )      for   i ,   j   in   zip ( s ,   t ):          if   i  ==  j :              count  +=  1          else :              break      if   total  <=  2 * count  +  k   and   total % 2  ==  k % 2   or   total < k :          return   "Yes"      else :          return   "No"

Hackerrank | Extra Long Factorials

def   extraLongFactorials ( n ):      fact  =  1      for   i   in   range ( n ,   1 ,   -1 ):          fact  =  fact  *  i      print ( fact )

Hackerrank | Find Digits

Explanation:     Split the numbers with traditional method and check the remainder. def   findDigits ( n ):      hold  =  n      count  =  0      while   hold  !=  0 :          rem  =  hold  %  10          if   rem  != 0   and   n  %  rem  ==  0   :              count  +=  1          hold  =  int ( hold  /  10 )           return   count

Hackerrank | Jumping cloud revisited

Explanation: Infinite loop     Initialise energy as 100 Energy decreases depending on c[i]                    If c[i] == 0                         e = e -1 - 0                    else:                         e -= 1 - 2(1) def   jumpingOnClouds ( c ,   k ):      e  = 100      i  =  0      while ( True ):          e  =  e  -  1  - ( 2  *  c [ i ])          i  =  ( i  +  k )  %  n          if   i  ==  0 :       ...

Hackerrank | Sequence Equation

Explanation:     Find the index of the element in the array and then take the index value to find it's index. Eg: Array 2 3 1   Index of 1 is 3      Find index of 3 which is 2      Add to new array Index of 2 is 1      Find index of 1 which is 3      Add to new array Repeat Steps def   permutationEquation ( p ):      new  =  []      for   i   in   range ( 1 ,   len ( p ) + 1 ):          new . append ( p . index ( p . index ( i )  + 1 )  + 1 )      return   new      

Hackerank | Bon Appetit

def   bonAppetit ( bill ,   k ,   b ):      print ( b  -  ( sum ( bill )  -  bill [ k ])  //  2   or   'Bon Appetit' )  

Hackerrank | Migratory birds

def   migratoryBirds ( arr ):      count  =  [ 0 ]  *  len ( arr )      for   i   in   arr :          count [ i ]   +=  1      return ( count . index ( max ( count )))

Hackerrank | Circular Array Rotation

def   circularArrayRotation ( a ,   k ,   queries ):      l  =  []                                 for   i   in   range   ( len ( a )):          l . insert (( i + k ) % len ( a ), a [ i ]);                                       for   i   in   range   ( len ( queries )):          #print(queries[i])          queries [ i ]  =  l [ queries [ i ]];                    ...

Hackerrank | Apples and Oranges

Image
Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where   is the start point, and   is the endpoint. The apple tree is to the left of his house, and the orange tree is to its right. You can assume the trees are located on a single point, where the apple tree is at point  , and the orange tree is at point  . When a fruit falls from its tree, it lands   units of distance from its tree of origin along the  -axis. A negative value of   means the fruit fell   units to the tree's left, and a positive value of   means it falls   units to the tree's right. Given the value of   for   apples and   oranges, determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range  )? For example, Sam's house is between   and  . The apple tree is located at...