Skip to main content

Posts

Showing posts from April, 2021

Mutations Hackerrank Solution Python

Mutations Hackerrank Solution Python  For Explanation Watch Video:: def  mutate_string(string, position, character):      return string[:position] + character + string[position+1:]  if  __name__ ==  '__main__' :     s =  input ()     i, c =  input ().split()     s_new = mutate_string(s,  int (i), c)      print (s_new)

Capitalize! Hackerrank Solution Python

 Capitalize! Hackerrank Solution Python For Explanation Watch Video:: Code: #!/bin/python3 import  math import  os import  random import  re import  sys # Complete the solve function below. def  solve(s):                                for x in s[:].split():         s = s.replace(x,x.capitalize())      return s if  __name__ ==  '__main__' :     fptr =  open (os.environ[ 'OUTPUT_PATH' ],  'w' )     s =  input ()     result = solve(s)     fptr.write(result +  '\n' )     fptr.close()

Java Date and Time Hackerrank Solution

 Java Date and Sample Input For Explanation Watch Video: Sample Input 08 05 2015 Sample Output WEDNESDAY Code: import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; class  Result {      /*      * Complete the 'findDay' function below.      *      * The function is expected to return a STRING.      * The function accepts following parameters:      *  1. INTEGER month      *  2. INTEGER day      *  3. INTEGER year      */      public static String findDay(int month, int day, int year) {         java.time.LocalDate dt = java.time.LocalDate.of(year, month, day);         return dt.getDayOfWeek().toString();             } } public   class  Solution {      public   static   void  main(String[] args)  throws  IO

Java BitSet Hackerrank Solution

 Java BitSet Hackerrank Solution For Explanation Watch Video: Sample Input 5 4 AND 1 2 SET 1 4 FLIP 2 2 OR 2 1 Sample Output 0 0 1 0 1 1 1 2 Code: import  java.io.*; import  java.util.*; public   class  Solution {      public   static   void  main(String[] args) {         Scanner s =  new  Scanner(System.in);          int  n = s.nextInt();          int  m = s.nextInt();         BitSet b1 =  new  BitSet(n);         BitSet b2 =  new  BitSet(n);          for ( int  i= 0 ;i<m;i++){             String ip = s.next();              switch (ip.charAt( 0 )){                  case   'A' :                  if (s.nextInt()== 1 ){                     b1.and(b2);                 } else {                     b2.and(b1);                 }                  break ;                  case   'O' :                  if (s.nextInt()== 1 ){                     b1.or(b2);                 } else {                     b2.or(b1);                 }                  break ;                  case  

Java Method Overriding 2 (Super Keyword) Hackerrank Solution

 Java Method Overriding 2 (Super Keyword) Hackerrank Solution For Explanation Watch Video: import  java.util.*; import  java.io.*; class  BiCycle{     String define_me(){          return   "a vehicle with pedals." ;     } } class  MotorCycle  extends  BiCycle{     String define_me(){          return   "a cycle with an engine." ;     }          MotorCycle(){         System.out.println( "Hello I am a motorcycle, I am " + define_me());          String temp=super.define_me(); //Fix this line         System.out.println( "My ancestor is a cycle who is " + temp );     }      } class  Solution{      public   static   void  main(String []args){         MotorCycle M= new  MotorCycle();     } }

Java Method Overriding Hackerrank Solution

 Java Method Overriding Hackerrank Solution For Explanation Watch Video: import  java.util.*; class  Sports{     String getName(){          return   "Generic Sports" ;     }         void  getNumberOfTeamMembers(){         System.out.println(  "Each team has n players in "  + getName() );     } } class  Soccer  extends  Sports{      @Override     String getName(){          return   "Soccer Class" ;     }      // Write your overridden getNumberOfTeamMembers method here     @Override     void getNumberOfTeamMembers(){         System.out.println("Each team has 11 players in "+getName());     } } public   class  Solution{           public   static   void  main(String []args){         Sports c1 =  new  Sports();         Soccer c2 =  new  Soccer();         System.out.println(c1.getName());         c1.getNumberOfTeamMembers();         System.out.println(c2.getName());         c2.getNumberOfTeamMembers();     } }

Java Inheritance II Hackerrank Solution

 Java Inheritance II Hackerrank Solution For Explanation Watch Video: Input Format You are not responsible for reading any input from stdin; a locked code stub will test your submission by calling the  add  method on an  Adder  object and passing it   integer parameters. Output Format You are not responsible for printing anything to stdout. Your  add  method must return the sum of its parameters. Sample Output The  main  method in the  Solution  class above should print the following: My superclass is: Arithmetic 42 13 20 Code: import  java.io.*; import  java.util.*; import  java.text.*; import  java.math.*; import  java.util.regex.*; class  Arithmetic{      public   int  add( int  a, int  b){          return  a+b;     } } class  Adder  extends  Arithmetic{      } public   class  Solution{      public   static   void  main(String []args){          // Create a new Adder object         Adder a =  new  Adder();                   // Print the name of the superclass on a new line         Sy

Java Inheritance I Hakerrank Solution

 Java Inheritance I Hakerrank Solution For Explanation Watch Video: import  java.io.*; import  java.util.*; import  java.text.*; import  java.math.*; import  java.util.regex.*; class  Animal{      void  walk(){         System.out.println( "I am walking" );     } } class  Bird  extends  Animal{      void  fly(){         System.out.println( "I am flying" );     }      void sing(){         System.out.println("I am singing");     } } public   class  Solution{     public   static   void  main(String args[]){       Bird bird =  new  Bird();       bird.walk();       bird.fly();       bird.sing();         } }

Java Generics Hackerrank Solution

Java Generics Hackerrank Solution For Explanation Watch Video: import  java.io.IOException; import  java.lang.reflect.Method; class  Printer {    //Write your code here    public <T> void printArray(T[] arr){        for(int i=0;i<arr.length;i++){            System.out.println(arr[i]);        }    }   } public   class  Solution {      public   static   void  main( String args[] ) {         Printer myPrinter =  new  Printer();         Integer[] intArray = {  1 ,  2 ,  3  };         String[] stringArray = { "Hello" ,  "World" };         myPrinter.printArray(intArray);         myPrinter.printArray(stringArray);          int  count =  0 ;          for  (Method method : Printer. class .getDeclaredMethods()) {             String name = method.getName();              if (name.equals( "printArray" ))                 count++;         }          if (count >  1 )System.out.println( "Method overloading is not allowed!" );            } }  

Java Anagrams Hackerrank Solution

 Java Anagrams Hackerrank Solution For Explanation Watch Video: Sample Input 1 anagramm marganaa Sample Output 1 Not Anagrams Sample Input 2 Hello hello Sample Output 2 Anagrams Code: import  java.util.Scanner; public   class  Solution {      static   boolean  isAnagram(String a, String b) {          a = a.toLowerCase();         b = b.toLowerCase();         char[] ch1 = a.toCharArray();         char[] ch2 = b.toCharArray();         java.util.Arrays.sort(ch1);         java.util.Arrays.sort(ch2);         String s1 = new String(ch1);         String s2 = new String(ch2);         if(s1.equals(s2)){             return true;         }else{             return false;         }     }    public   static   void  main(String[] args) {              Scanner scan =  new  Scanner(System.in);         String a = scan.next();         String b = scan.next();         scan.close();          boolean  ret = isAnagram(a, b);         System.out.println( (ret) ?  "Anagrams"  :  "Not Anagrams&qu

Java Substring Comparisons Hackerrank Solution

 Java Substring Comparisons Hackerrank Solution For Explanation Watch Video: Sample Input 0 welcometojava 3 Sample Output 0 ava wel Code: import  java.util.Scanner; public   class  Solution {      public   static  String getSmallestAndLargest(String s,  int  k) {         String smallest =  "" ;         String largest =  "" ;          smallest = s.substring(0,k);         largest = s.substring(0,k);         for(int i=0;i<=s.length()-k;i++){             String sub = s.substring(i,i+k);             if(smallest.compareTo(sub)>0){                 smallest = sub;             }             if(largest.compareTo(sub)<0){                 largest = sub;             }         }         return smallest + "\n" +largest;     }      public   static   void  main(String[] args) {         Scanner scan =  new  Scanner(System.in);         String s = scan.next();          int  k = scan.nextInt();         scan.close();                System.out.println(getSmallestAndL

Java Strings Introduction Hackerrank Solution

 Java Strings Introduction Hackerrank Solution For Explanation Watch Video: Sample Input 0 hello java Sample Output 0 9 No Hello Java Code: import  java.io.*; import  java.util.*; public   class  Solution {      public   static   void  main(String[] args) {                  Scanner sc= new  Scanner(System.in);         String A=sc.next();         String B=sc.next();          /* Enter your code here. Print output to STDOUT. */         System.out.println(A.length()+B.length());          if (A.compareTo(B)> 0 ){             System.out.println( "Yes" );         } else {              System.out.println( "No" );         }         A = A.substring( 0 , 1 ).toUpperCase() + A.substring( 1 );         B = B.substring( 0 , 1 ).toUpperCase() + B.substring( 1 );         System.out.println(A+ " " +B);              } }