Skip to content

Instantly share code, notes, and snippets.

@xbns
Last active December 15, 2020 14:55
Show Gist options
  • Save xbns/71e92218c65463e4b674cc8a429cd716 to your computer and use it in GitHub Desktop.
Save xbns/71e92218c65463e4b674cc8a429cd716 to your computer and use it in GitHub Desktop.
Problem: Leap Year #hackerrank

Problem: Leap Year

An extra day is added to the calendar almost every four years as February 29, and the day is a called a leap year.It corrects the calendar for the fact that our planet takes approximately 365.25 days to orbit the sun.A leap year contains a leap day.

In the Gregorian calendar,three conditions are used to identify leap year:

  • 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.
  • 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

Task Given a year, determine whether it is a leap year.If it is a leap year,return the Boolean True,otherwise False.

Note that the code stub provided reads from the STDIN and passes arguments to the is_leap function.

Input Format

Read year,the year to test

Constraints

1900 ≤ year ≤10**5

Output Formats

False

Sample Input 0

2000

Sample Output 0

True

Explanation 0

1990 is not a multiple of 4 hence it is not a leap year 1990 mod 4 is 2 but 2000 mod 4 is 0

Code Stub

import os
import sys
# Complete the is_leap function below.
def is_leap(year):
# your code here

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())
    result = is_leap(n)

    fptr.write(str(result) + '\n')

    fptr.close()
# Python program to check if year is a leap year or not
n = 0
year = False
def is_leap(year):
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
return True
else:
return False
else:
return True
else:
return False
# n = 2000
n = int(input())
result = is_leap(n)
print(result)

Debugging your script using the online hackerrank editor

In the hackerrank editor,you are given the code stub as:

snippet 1

...

if __name__ == '__main__':

  fptr = open(os.environ['OUTPUT_PATH'], 'w')     

  n = int(input())
  result = is_leap(n)

  fptr.write(str(result) + '\n')

  fptr.close()

Since you can't set the Environment Variable OUTPUT_PATH there(and probably you don't need that line of code) unless you are working with files. Something you can normally do in your workstation as;

$ pwd -> get the current working directory first

$ echo OUTPUT_PATH='<pwd>/result.txt'

It is better you comment out that line and add this one in place of it to write to STDOUT

fptr = sys.stdout

Otherwise you can as well use you inbuilt one preferrably,check the checkbox Test against custom input and enter your input and forget about the file pointer one denoted by variable fptr as

if __name__ == '__main__':
  n = int(input())
  result = is_leap(n)  
  print(result)

Incase you are required to enter more than one input like for instance as with the case with Simple Array Sum Then you should enter your inputs one line at a time;meaning if your first input comprises an integer and your second input comprises an array of integer. You should enter the inputs as

6
1 2 3 4 10 11

It is important to pay attention to the input format section of the question especially if you are using your own IDE as opposed to the online editor. It determines if the test cases will pass,if you hit the Submit Code button.Hitting the Run Code button and seeing you code running as expected is no big deal.The most important part is ensuring it run successfully against all the test cases set.This is the only way to determine if you passed that problem. Since most test cases are hidden

Actually you don't even need this part in your code + the file pointer parts denoted by variable fptr.

if __name__ == '__main__':

You can write the code as follows and it will still pass all test cases.

ar = []
n = 0
def simpleArraySum(ar):

	sum = 0;
    n = len(ar)
    
    for i in range(n):
    	sum += ar[i]
    return (sum)
    
n = int(input().strip())
ar = list(map(int,input().rstrip().split()))
# driver code to test
# n = 3
# ar = [10,20,31]  # expected "61"
result = simpleArraySum(ar)

print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment