HackerRank 50 Python Practice

1. Write Function

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.

Task:
You are given the year, and you have to write a function to check if the year is leap or not.

1
2
3
4
5
6
7
8
def is_leap(year):   
if year%400==0:
return True
elif year%100==0:
return False
elif year%4==0:
return True
return False

Note: Pay attention to the order among the if condition statement.

2. List Comprehensions

You are given three integers X,Y and Z representing the dimensions of a cuboid along with an integer N. You have to print a list of all possible coordinates given by (i,j,k) on a 3D grid where the sum of i+k+j is not equal to N. Here, 0 $\leq$ i $\leq$ X; 0 $\leq$ j $\leq$ Y; 0 $\leq$ k $\leq$ Z

Sample Input:

1
2
3
4
1
1
1
2

Sample Output:

1
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]

Solution:

1
2
3
4
5
6
7
8
9
10
if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())
print([[i,j,k]
for i in range(x+1)
for j in range(y+1)
for k in range(z+1)
if ( ( i + j+k )!=n)])
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2020-2021 By Yang Yue
  • Visitors: | Views:

请我喝杯奶茶吧~