Posts

Showing posts with the label HackerRank

Python-HackerRank-String Formatting

def   print_formatted ( number ):      results  =  []      for   i   in   range ( 1 ,   n + 1 ):          decimal  =  str ( i )          octal  =  str ( oct ( i )[ 2 :])          hex_  =  str ( hex ( i )[ 2 :]) . upper ()          binary  =  str ( bin ( i )[ 2 :])          results . append ([ decimal ,   octal ,   hex_ ,   binary ])      # print(results)      width  =  len ( results [ -1 ][ 3 ])        # print(width) # largest binary number      for   i   in   results :          print (...

HackerRank Python Problem - String Validators

HackerRank Python Problem: Question: You are given a string S. Your task is to find out if the string S contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters .  Solution: if  __ name__  ==  '__main__' :      s  =  input ()      print ( any ( i . isalnum ()   for   i   in   s ))      print ( any ( i . isalpha ()   for   i   in   s ))      print ( any ( i . isdigit ()   for   i   in   s ))      print ( any ( i . islower ()   for   i   in   s ))      print ( any ( i . isupper ()   for   i   in   s ))

HackerRank Problem - Python

 HackerRank Python Problem: Question: In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left. Solution: def   count_substring ( string ,   sub_string ):      count  =  0      for   i   in   range ( len ( string )):          if   string [ i :] . startswith ( sub_string ):                  count  +=  1      return   count if  __ name__  ==  '__main__' :      string  =  input () . strip ()      sub_string  =  input () . strip ()           count  =  count_subst...

Python Problem- Find the Runner Up Score!!!

Problem Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.   Source Code n =  int ( input ()) list1 =  list ( map ( int , input () .strip () .split ()))[: n ] list2 =  list ( set ( list1 )) list2.sort ( reverse =  True ) x = list2 [ 1 ] print ( x )

Python-HackerRank Problem List Comprehensions

Problem Print a list of all possible coordinates given by (i,j,k) on a 3D grid where the sum of i+j+k is not equal to  n. Here, 0<=i<=x;0<=j<=y;0<=k<=z. Please use list comprehensions rather than multiple loops, as a learning exercise.  Source Code   if  __name__ ==  '__main__' :     x =  int ( input ())     y =  int ( input ())     z =  int ( input ())     n =  int ( input ())      print   ([[ a , b , c ]   for  a  in   range ( 0 , x+1 )   for  b  in   range ( 0 , y+1 )   for  c  in   range ( 0 , z+1 )     if  a + b + c != n  ])