Tuesday, 3 April 2018

hackerrank solution: Project Euler #34: Digit factorials

solution : python2

# Enter your code here. Read input from STDIN. Print output to STDOUT
a = [0]*10
a[0]=1

for i in range(1,10):
    a[i] = int(a[i-1] * i)

n = int(input())
ans = 0

for i in range(10,n+1):
    temp = int(i)
    sum = 0
    while temp > 0 :
        sum += a[int(temp%10)]
        temp//=10

    sum = int(sum)
    if sum % i == 0 :
        ans += i

print (int(ans))

Project Euler #11: Largest product in a grid

HACKERRANK SOLUTION: c++

#include<iostream>
using namespace std;
int main(){
    long long a[20][20],max=0,p;
    for(int i=0;i<20;i++){
        for(int j=0;j<20;j++){
          cin>>a[i][j]; 
        }
    }   
    for(int i=0;i<20;i++){
        for(int j=0;j<=16;j++){
            p = a[i][j]*a[i][j+1]*a[i][j+2]*a[i][j+3];
            if(p> max) max=p;
       
            p=a[j][i]*a[j+1][i]*a[j+2][i]*a[j+3][i];
            if(p> max) max=p;
        }
    }   


    for(int i=0;i<17;i++){
        for(int j=0;j<17;j++){
            p= a[i][j]*a[i+1][j+1]*a[i+2][j+2]*a[i+3][j+3];
            if(p> max) max=p;
        }
    }
    for(int i=3;i<20;i++){
        for(int j=0;j<17;j++){

        p=a[i][j]*a[i-1][j+1]*a[i-2][j+2]*a[i-3][j+3];
        if(p> max) max=p;
        }
    }   

    cout<<max;
    return 0;

}

Saturday, 31 March 2018

Write a function; Leap year problem || Hackerrank

We add a Leap Day on February 29, almost every four years. The leap day is an extra, or intercalary day and we add it to the shortest month of the year, February. 
In the Gregorian calendar three criteria must be taken into account to identify leap years:
  • The year can be evenly divided by 4, is a leap year, unless:
    • The year can be evenly divided by 100, it is NOT a leap year, unless:
      • The year is also evenly divisible by 400. Then it is a leap year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.Source
Task 
You are given the year, and you have to write a function to check if the year is leap or not.
Note that you have to complete the function and remaining code is given as template.
Input Format
Read y, the year that needs to be checked.
Constraints
Output Format
Output is taken care of by the template. Your function must return a boolean value (True/False)
Sample Input 0
1990
Sample Output 0
False
Explanation 0
1990 is not a multiple of 4 hence it's not a leap year.
----------------------------------------------------------------------------
Solution: python2

def is_leap(year):        leap = False    if year % 4 == 0:    if year % 100 != 0:        leap = True    elif year % 400 == 0:        leap = Truereturn leap

Say "Hello, World!" With Python ; HACKERRANK

Here is a sample line of code that can be executed in Python:
print("Hello, World!")
You can just as easily store a string as a variable and then print it to stdout:
my_string = "Hello, World!"
print(my_string)
The above code will print Hello, World! on your screen. Try it yourself in the editor below!
Input Format
You do not need to read any input in this challenge.
Output Format
Print Hello, World! to stdout.
Sample Output 0
Hello, World!
---------------------------------------------------------
solution: python2
if __name__ == '__main__':
    print "Hello, World!"