exercism

Exercism - Leap

This post shows you how to get Leap excercise of Exercism.

Stevinator Stevinator
4 min read
exercism dart flutter leap

Preparation

Before we click on our next exercise, let’s see what concepts of DART we need to consider

Leap Exercise

So we need to use the following concepts.

Booleans

void main() {
  bool a = true;
  var b = false;

  print(a);
  print(b);
}

Integers

The variable type where you can store whole numbers, eq 12 or -30

void main() {
  int a = 12;
  var b = 23;

  print(a);
  print(b);
}

Logic aka Logical Operators

Logical operators are used to combining two expressions. There are three types of operators:

&& → logical AND → return true if all expressions are true → a && b
|| → logical OR → return true if any expression is true → a || b
! → logical NOT → return complement of expression → !a
main() {
	bool a = true;
	bool b = false;

	print(a && b);
	print(a || b);
	print(!a);
}

Introduction

Given a year, report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

on every year that is evenly divisible by 4 except every year that is evenly divisible by 100 unless the year is also evenly divisible by 400

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.

What is a leap year?

A leap year (also known as an intercalary year or bissextile year) is a calendar year that contains an additional day (or, in the case of a lunisolar calendar, a month) added to keep the calendar year synchronized with the astronomical year or seasonal year.[1] Because astronomical events and seasons do not repeat in a whole number of days, calendars that have a constant number of days in each year will unavoidably drift over time with respect to the event that the year is supposed to track, such as seasons. By inserting (called intercalating in technical terminology) an additional day or month into some years, the drift between a civilization’s dating system and the physical properties of the solar system can be corrected. A year that is not a leap year is a common year. For example, in the Gregorian calendar, each leap year has 366 days instead of 365, by extending February to 29 days rather than the common 28. These extra days occur in each year which is an integer multiple of 4 (except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400). The leap year of 366 days has 52 weeks and two days, hence the year following a leap year will start later by two days of the week.

— Wikipedia

How can we calculate a leap year?

This chart will help you understand how leap-years calculation work and explain how to implement it to your code.

Leap Year Chart

To check if something is divisible by something else, we use modulo. Thus, we get the remainder of an integer division.

For example:

main() {
	print(10 % 4); //2
	print(10 % 5); //0
}

Solution

class Leap {
  bool leapYear(int year) => (((year % 4) == 0) &&
                             (((year % 100) != 0) || ((year % 400) == 0)));
}

You can watch this tutorial on YouTube. So don’t forget to like and subscribe. 😉

Watch on YouTube
Stevinator

Stevinator

Stevinator is a software engineer passionate about clean code and best practices. Loves sharing knowledge with the developer community.