Leap Years¶

  • Write a function is_leap_year (or isLeapYear, according to your language's case convention) that takes an integer year and returns a boolean value indicating whether the year is a leap year.

  • A year is a leap year if it is divisible by four, except when it is divisible by 100, in which case it is a leap year if divisible by 400.

  • For example, year 2001, 2002, and 2003 are not leaf years, 2004, 2008, 2012 are leap years, 2100, 2200, and 2300 are not leap years, and 2400 is a leap year.

  • Boilerplate source files {go,jl,ml,rs}/leap_year.{go,jl,ml,rs} containing the test code is generated and shown below.

  • Edit the source files either by opening them in a text editor (e.g., vscode), or editing the cells below and executing them.

1. AI tutor¶

1-1. Prepare¶

  • Your personal AI tutor is provided for questions and feedback
  • Execute the following cell before you use it
In [ ]:
import heytutor

1-2. Examples¶

1-2-1. A general question¶

%%hey
How to write a function in Go?

1-2-2. A hint on this specific problem¶

%%hey
Give me a hint on this problem for Rust

1-2-3. NEW: A few builtin variables¶

  • {file:FILENAME} is the content of FILE
  • {bash[-1]} is the output of the last %%bash_ cell, {bash[-2]} that of the second last %%bash_ cell, etc.
  • {problem} is the content of the file you specified by %%hey problem_file=foo.md
  • {answer} is the content of the file you specified by %%hey answer_file=go/foo.go

1-2-4. Help when you struggle¶

%%hey answer_file=go/foo.go
I get this error when I compile it. What's wrong?"

My program:
{answer}

Error message:
{bash[-1]}

1-2-5. Ask feedback¶

  • You are encouraged to ask a feedback once you think you are done with the problem, to know if there is a better answer. You can do so by something like:
%%hey problem_file=foo.md answer_file=go/foo.md
Give me a feedback to my answer.

Problem:
{problem}

My Answer:
{answer}

2. Go¶

2-1. Baseline code¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ go/leap_year.go
package main
import "fmt"

/** begin my answer */

func isLeapYear(year int64) bool {
	if year % 400 == 0 {
		return true
	} else if year % 100 == 0 {
		return false
	} else if year % 4 == 0 {
		return true
	} else {
		return false
	}
}
/** end my answer */
func main() {
	if !isLeapYear(2000) { panic("wrong") }
	if  isLeapYear(1900) { panic("wrong") }
	if !isLeapYear(2024) { panic("wrong") }
	if  isLeapYear(2023) { panic("wrong") }
	fmt.Println("OK")
}

2-2. Compile¶

In [ ]:
%%bash_
export PATH=${PATH}:~/.local/go/bin:~/go/bin
go build -o go/leap_year go/leap_year.go
  • Note: when you run go or other Go commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (export PATH=${PATH}:~/go/bin)
  • You may consider adding that line in your ~/.bash_profile

2-3. Run¶

In [ ]:
%%bash_
go/leap_year

2-4. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=leap_year.md answer_file=go/leap_year.go

Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}

Give me a feedback to my answer.

3. Julia¶

3-1. Baseline code¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ jl/leap_year.jl
### begin my answer

function is_leap_year(year)
    if year % 400 == 0
        true
    elseif year % 100 == 0
        false
    elseif year % 4 == 0
        true
    else
        false
    end
end
### end my answer

function main()
    @assert  is_leap_year(2000)
    @assert !is_leap_year(1900)
    @assert  is_leap_year(2024)
    @assert !is_leap_year(2023)
    println("OK")
end

main()

3-2. Compile¶

  • Julia code is compiled "just in time" (compiled upon executed), so does not need a specific action for compilation before you run

3-3. Run¶

In [ ]:
%%bash_
export PATH=${PATH}:~/.juliaup/bin
julia jl/leap_year.jl
  • Note: when you run julia or other Julia commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (export PATH=${PATH}:~/.juliaup/bin)
  • You may consider adding that line in your ~/.bash_profile

3-4. Interactive execution¶

  • julia command also serves is an interactive command for Julia programs

  • You can run a source code and continue interaction

$ julia -i jl/leap_year.jl
  • For trial and error, you may also consider creating a Julia notebook

3-5. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=leap_year.md answer_file=jl/leap_year.jl

Problem:
{problem}

My Answer (between ### begin my answer and ### end my answer):
{answer}

Give me a feedback to my answer.

4. OCaml¶

4-1. Baseline code¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ ml/leap_year.ml
(** begin my answer *)

let is_leap_year year =
  if year mod 400 = 0 then
    true
  else if year mod 100 = 0 then
    false
  else if year mod 4 = 0 then
    true
  else
    false;;
(** end my answer *)

let main () =
  assert ( is_leap_year 2000 );
  assert (not (is_leap_year 1900));
  assert ( is_leap_year 2024 );
  assert (not (is_leap_year 2023));
  Printf.printf "OK\n"
;;

main()

4-2. Compile¶

In [ ]:
%%bash_
eval $(opam env)
ocamlc ml/leap_year.ml -o ml/leap_year
  • Note: when you run ocamlc or other OCaml commands (see below) in a terminal (SSH or Jupyter terminal), you need to execute the first line (eval $(opam env))
  • You may consider adding that line in your ~/.bash_profile

4-3. Run¶

In [ ]:
%%bash_
ml/leap_year

4-4. Interactive execution¶

  • ocaml command is an interactive command for OCaml programs

  • In terminal (Jupyter or SSH), you can directly run a source code

$ eval $(opam env)   # once in your session or put it in ~/.bash_profile
$ ocaml ml/leap_year.ml
  • You can run a source code and continue interaction
$ eval $(opam env)   # once in your session or put it in ~/.bash_profile
$ ocaml -init ml/leap_year.ml
  • For trial and error, you may also consider creating an OCaml notebook

4-5. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=leap_year.md answer_file=ml/leap_year.ml

Problem:
{problem}

My Answer (between (** begin my answer *) and (** end my answer *)):
{answer}

Give me a feedback to my answer.

5. Rust¶

5-1. Baseline code¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ rs/leap_year.rs
/** begin my answer */

fn is_leap_year(year: i64) -> bool {
    if year % 400 == 0 {
        true
    } else if year % 100 == 0 {
        false
    } else if year % 4 == 0 {
        true
    } else {
        false
    }
}
/** end my answer */

fn main() {
    assert!( is_leap_year(2000) );
    assert!(!is_leap_year(1900));
    assert!( is_leap_year(2024) );
    assert!(!is_leap_year(2023));
    println!("OK")
}

5-2. Compile¶

In [ ]:
%%bash_
. ~/.cargo/env
rustc rs/leap_year.rs -o rs/leap_year
  • Note: when you run rustc or other Rust commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (. ~/.cargo/env)
  • You may consider adding that line in your ~/.bash_profile

5-3. Run¶

In [ ]:
%%bash_
rs/leap_year

5-4. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=leap_year.md answer_file=rs/leap_year.rs

Problem:
{problem}

My Answer (between /** begin my answer */ and /** end my answer */):
{answer}

Give me a feedback to my answer.