Saturday 18 June 2016

Hackerrank , 30 Days of Code Challenges ( Day 0-5 Solutions)

Hackerrank , 30 Days of Code Challenges  =>

Day 0: Hello, World (Python) :

inputString = raw_input() # get a line of input from stdin and save it to our variable

# Your first line of output goes here
print 'Hello, World.'

# Write the second line of output
print inputString

Day 1: Data Types (C#) :

// Declare second integer, double, and String variables.
        int i2;
        double d2;
        string s2;
       
        // Read and save an integer, double, and String to your variables.
        i2=int.Parse(Console.ReadLine());
        d2= double.Parse(Console.ReadLine());
        s2 = Console.ReadLine();
       
        // Print the sum of both integer variables on a new line.
        Console.WriteLine("{0:#.#}",i2+i);
       
        // Print the sum of the double variables on a new line.
        Console.WriteLine("{0:0.0}",d+d2);
        // Concatenate and print the String variables on a new line
        // The 's' variable above should be printed first.
        Console.WriteLine(s+s2);

Day 2: Operators (Python) :

a=raw_input()
b=raw_input()
c=raw_input()
print "The total meal cost is "+str(int(round(float(a)+(float(b)*float(a)/100)+(float(c)*float(a)/100))))+" dollars."


Day 3: Intro to Conditional Statements (Python) :

 #!/bin/python

import sys


N = int(raw_input().strip())

if N%2!=0:
    print "Weird"
elif N>=2 and N<=5 :
    print "Not Weird"
elif N>=6 and N<=20 :
    print "Weird"
else:
    print "Not Weird"
    

Day 4: Class vs. Instance (Java) :

public class Person {
    private int age;   
 
    public Person(int initialAge) {
          // Add some more code to run some checks on initialAge
        this.age=initialAge;
    }

    public void amIOld() {
          // Write code determining if this person's age is old and print the correct statement:
        if(this.age<0)
          { System.out.println("Age is not valid, setting age to 0.");
           this.age=0;
           amIOld();
          }
        else if(this.age<13)
            System.out.println("You are young.");
        else if(this.age<18)
            System.out.println("You are a teenager.");
        else if(this.age>=18)
            System.out.println("You are old.");
    }

    public void yearPasses() {
          // Increment this person's age.
        this.age+=1;
    }

Day 5: Loops (Python) :

#!/bin/python

import sys


N = int(raw_input().strip())
for i in xrange(1,11):
    print str(N)+" x "+str(i)+" = "+str(N*i)

No comments:

Post a Comment