Area of Triangle¶
Write a function
triangle_area(ortriangleArea, according to your language's case convention) that takes six 64-bit floating point numbers $x_1$, $y_1$, $x_2$, $y_2$, $x_3$, $y_3$ --- the coordinates of three points in the plane --- and returns the area of the triangle formed by these points.In a mathematical expression,
$$ triangle\_area(x_1, y_1, x_2, y_2, x_3, y_3) = \frac{1}{2} \left| (x_2 - x_1)(y_3 - y_1) - (x_3 - x_1)(y_2 - y_1) \right| $$
Use 64-bit floating point numbers for both inputs and the output.
Consider introducing necessary local variables to make the computation easier to understand.
Boilerplate source files
{go,jl,ml,rs}/triangle_area.{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.
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¶
import heytutor
%%writefile_ go/triangle_area.go
package main
import "fmt"
import "math"
/** begin my answer */
func triangleArea(x1, y1, x2, y2, x3, y3 float64) float64 {
dx21 := x2 - x1
dy21 := y2 - y1
dx31 := x3 - x1
dy31 := y3 - y1
return 0.5 * math.Abs(dx21*dy31 - dx31*dy21)
}
/** end my answer */
func main() {
if !(math.Abs(triangleArea(0,0,1,0,0,1) - 0.5) < 1.0e-5) { panic("wrong") }
if !(math.Abs(triangleArea(0,0,2,0,0,2) - 2.0) < 1.0e-5) { panic("wrong") }
if !(math.Abs(triangleArea(1,1,4,1,1,5) - 6.0) < 1.0e-5) { panic("wrong") }
fmt.Println("OK")
}
2-2. Compile¶
%%bash_
export PATH=${PATH}:~/.local/go/bin:~/go/bin
go build -o go/triangle_area go/triangle_area.go
- Note: when you run
goor 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¶
%%bash_
go/triangle_area
2-4. Ask Questions or Get Feedback¶
%%hey problem_file=triangle_area.md answer_file=go/triangle_area.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¶
import heytutor
%%writefile_ jl/triangle_area.jl
### begin my answer
function triangle_area(x1, y1, x2, y2, x3, y3)
dx21 = x2 - x1
dy21 = y2 - y1
dx31 = x3 - x1
dy31 = y3 - y1
0.5 * abs(dx21 * dy31 - dx31 * dy21)
end
### end my answer
function main()
@assert abs(triangle_area(0,0,1,0,0,1) - 0.5) < 1.0e-5
@assert abs(triangle_area(0,0,2,0,0,2) - 2.0) < 1.0e-5
@assert abs(triangle_area(1,1,4,1,1,5) - 6.0) < 1.0e-5
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¶
%%bash_
export PATH=${PATH}:~/.juliaup/bin
julia jl/triangle_area.jl
- Note: when you run
juliaor 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¶
juliacommand also serves is an interactive command for Julia programsYou can run a source code and continue interaction
$ julia -i jl/triangle_area.jl
- For trial and error, you may also consider creating a Julia notebook
3-5. Ask Questions or Get Feedback¶
%%hey problem_file=triangle_area.md answer_file=jl/triangle_area.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¶
import heytutor
%%writefile_ ml/triangle_area.ml
(** begin my answer *)
let triangle_area x1 y1 x2 y2 x3 y3 =
let dx21 = x2 -. x1 in
let dy21 = y2 -. y1 in
let dx31 = x3 -. x1 in
let dy31 = y3 -. y1 in
0.5 *. abs_float (dx21 *. dy31 -. dx31 *. dy21);;
(** end my answer *)
let main () =
assert (abs_float (triangle_area 0.0 0.0 1.0 0.0 0.0 1.0 -. 0.5) < 1.0e-5);
assert (abs_float (triangle_area 0.0 0.0 2.0 0.0 0.0 2.0 -. 2.0) < 1.0e-5);
assert (abs_float (triangle_area 1.0 1.0 4.0 1.0 1.0 5.0 -. 6.0) < 1.0e-5);
Printf.printf "OK\n"
;;
main()
4-2. Compile¶
%%bash_
eval $(opam env)
ocamlc ml/triangle_area.ml -o ml/triangle_area
- Note: when you run
ocamlcor 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¶
%%bash_
ml/triangle_area
4-4. Interactive execution¶
ocamlcommand is an interactive command for OCaml programsIn 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/triangle_area.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/triangle_area.ml
- For trial and error, you may also consider creating an OCaml notebook
4-5. Ask Questions or Get Feedback¶
%%hey problem_file=triangle_area.md answer_file=ml/triangle_area.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¶
import heytutor
%%writefile_ rs/triangle_area.rs
/** begin my answer */
fn triangle_area(x1: f64, y1: f64,
x2: f64, y2: f64,
x3: f64, y3: f64) -> f64 {
let dx21 = x2 - x1;
let dy21 = y2 - y1;
let dx31 = x3 - x1;
let dy31 = y3 - y1;
0.5 * (dx21 * dy31 - dx31 * dy21).abs()
}
/** end my answer */
fn main() {
assert!((triangle_area(0.0,0.0,1.0,0.0,0.0,1.0) - 0.5).abs() < 1.0e-5);
assert!((triangle_area(0.0,0.0,2.0,0.0,0.0,2.0) - 2.0).abs() < 1.0e-5);
assert!((triangle_area(1.0,1.0,4.0,1.0,1.0,5.0) - 6.0).abs() < 1.0e-5);
println!("OK")
}
5-2. Compile¶
%%bash_
. ~/.cargo/env
rustc rs/triangle_area.rs -o rs/triangle_area
- Note: when you run
rustcor 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¶
%%bash_
rs/triangle_area
5-4. Ask Questions or Get Feedback¶
%%hey problem_file=triangle_area.md answer_file=rs/triangle_area.rs
Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}
Give me a feedback to my answer.