Skip to content

Commit 0a682dd

Browse files
author
Aadit Kamat
authored
Add solution for lasagna in go
1 parent cbaea08 commit 0a682dd

File tree

8 files changed

+378
-0
lines changed

8 files changed

+378
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"blurb": "Learn about packages, functions, and variables by helping Gopher cook lasagna.",
3+
"authors": [
4+
"tehsphinx"
5+
],
6+
"contributors": [
7+
"ekingery",
8+
"andrerfcsantos",
9+
"bobtfish"
10+
],
11+
"forked_from": [
12+
"csharp/lucians-luscious-lasagna"
13+
],
14+
"files": {
15+
"solution": [
16+
"lasagna.go"
17+
],
18+
"test": [
19+
"lasagna_test.go"
20+
],
21+
"exemplar": [
22+
".meta/exemplar.go"
23+
]
24+
}
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"go","exercise":"lasagna","id":"57d255fad4944b13a7c4ee53233b6e0c","url":"https://exercism.org/tracks/go/exercises/lasagna","handle":"aaditkamat","is_requester":true,"auto_approve":false}

Exercism/go/lasagna/HELP.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Help
2+
3+
## Running the tests
4+
5+
To run the tests run the command `go test` from within the exercise directory.
6+
7+
If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
8+
flags:
9+
10+
go test -v --bench . --benchmem
11+
12+
Keep in mind that each reviewer will run benchmarks on a different machine, with
13+
different specs, so the results from these benchmark tests may vary.
14+
15+
## Submitting your solution
16+
17+
You can submit your solution using the `exercism submit lasagna.go` command.
18+
This command will upload your solution to the Exercism website and print the solution page's URL.
19+
20+
It's possible to submit an incomplete solution which allows you to:
21+
22+
- See how others have completed the exercise
23+
- Request help from a mentor
24+
25+
## Need to get help?
26+
27+
If you'd like help solving the exercise, check the following pages:
28+
29+
- The [Go track's documentation](https://exercism.org/docs/tracks/go)
30+
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
31+
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
32+
33+
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
34+
35+
To get help if you're having trouble, you can use one of the following resources:
36+
37+
- [How to Write Go Code](https://golang.org/doc/code.html)
38+
- [Effective Go](https://golang.org/doc/effective_go.html)
39+
- [Go Resources](http://golang.org/help)
40+
- [StackOverflow](http://stackoverflow.com/questions/tagged/go)

Exercism/go/lasagna/HINTS.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Hints
2+
3+
## General
4+
5+
- An [integer value][integers] can be defined as one or more consecutive digits.
6+
- If you see a `panic:` error when running the tests, this is because you have not implemented one of the functions (it should say which one) or you have left the boilerplate in place. You need to remove the `panic(...)` line from the supplied code and replace it with a real implementation.
7+
8+
## 1. Define the expected oven time in minutes
9+
10+
- You need to define a [constant][constants] and assign it the expected oven time in minutes.
11+
- If you see an `undefined: OvenTime` error then double check that you have the constant defined.
12+
- If you see an `invalid operation: got != tt.expected (mismatched types float64 and int)` error then you have likely put a decimal point into the `OvenTime` causing Go to infer the type as a floating point number. Remove the decimal and the type will be inferred as an `int`.
13+
- If you see a `syntax error: non-declaration statement outside function body` error then it is likely that you forgot the `const` keyword.
14+
- If you see a `syntax error: unexpected :=, expecting =` error then you are likely trying to assign the constant using `:=` like a variable; constants are assigned using `=` not `:=`.
15+
16+
## 2. Calculate the remaining oven time in minutes
17+
18+
- You need to define a [function][functions] with a single parameter.
19+
- You have to [explicitly return an integer][return] from a function.
20+
- The function's parameter is an [integer][integers].
21+
- You can [call][calls] one of the other functions you've defined previously.
22+
- You can use the [mathematical operator for subtraction][operators] to subtract values.
23+
24+
## 3. Calculate the preparation time in minutes
25+
26+
- You need to define a [function][functions] with a single parameter.
27+
- You have to [explicitly return an integer][return] from a function.
28+
- The function's parameter is an [integer][integers].
29+
- You can use the [mathematical operator for multiplication][operators] to multiply values.
30+
31+
## 4. Calculate the elapsed working time in minutes
32+
33+
- You need to define a [function][functions] with two parameters.
34+
- You have to [explicitly return an integer][return] from a function.
35+
- The function's parameter is an [integer][integers].
36+
- You can [call][calls] one of the other functions you've defined previously.
37+
- You can use the [mathematical operator for addition][operators] to add values.
38+
39+
[functions]: https://tour.golang.org/basics/4
40+
[return]: https://golang.org/ref/spec#Return_statements
41+
[operators]: https://golang.org/ref/spec#Operators
42+
[integers]: https://golang.org/ref/spec#Integer_literals
43+
[calls]: https://golang.org/ref/spec#Calls
44+
[constants]: https://tour.golang.org/basics/15

Exercism/go/lasagna/README.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Gopher's Gorgeous Lasagna
2+
3+
Welcome to Gopher's Gorgeous Lasagna on Exercism's Go Track.
4+
If you need help running the tests or submitting your code, check out `HELP.md`.
5+
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
6+
7+
## Introduction
8+
9+
[Go](https://golang.org) is a statically typed, compiled programming language.
10+
This exercise introduces three major language features: Packages, Functions, and Variables.
11+
12+
## Packages
13+
14+
Go applications are organized in packages.
15+
A package is a collection of source files located in the same directory.
16+
All source files in a directory must share the same package name.
17+
When a package is imported, only entities (functions, types, variables, constants) whose names start with a capital letter can be used / accessed.
18+
The recommended style of naming in Go is that identifiers will be named using `camelCase`, except for those meant to be accessible across packages which should be `CamelCase`.
19+
20+
```go
21+
package lasagna
22+
```
23+
24+
## Variables
25+
26+
Go is statically-typed, which means all variables [must have a defined type](https://en.wikipedia.org/wiki/Type_system) at compile-time.
27+
28+
Variables can be defined by explicitly specifying a type:
29+
30+
```go
31+
var explicit int // Explicitly typed
32+
```
33+
34+
You can also use an initializer, and the compiler will assign the variable type to match the type of the initializer.
35+
36+
```go
37+
implicit := 10 // Implicitly typed as an int
38+
```
39+
40+
Once declared, variables can be assigned values using the `=` operator.
41+
Once declared, a variable's type can never change.
42+
43+
```go
44+
count := 1 // Assign initial value
45+
count = 2 // Update to new value
46+
47+
count = false // This throws a compiler error due to assigning a non `int` type
48+
```
49+
50+
## Constants
51+
52+
Constants hold a piece of data just like variables, but their value cannot change during the execution of the program.
53+
54+
Constants are defined using the `const` keyword and can be numbers, characters, strings or booleans:
55+
56+
```go
57+
const Age = 21 // Defines a numeric constant 'Age' with the value of 21
58+
```
59+
60+
## Functions
61+
62+
Go functions accept zero or more parameters.
63+
Parameters must be explicitly typed, there is no type inference.
64+
65+
Values are returned from functions using the `return` keyword.
66+
67+
A function is invoked by specifying the function name and passing arguments for each of the function's parameters.
68+
69+
Note that Go supports two types of comments.
70+
Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`.
71+
72+
```go
73+
package greeting
74+
75+
// Hello is a public function
76+
func Hello (name string) string {
77+
return hi(name)
78+
}
79+
80+
// hi is a private function
81+
func hi (name string) string {
82+
return "hi " + name
83+
}
84+
```
85+
86+
## Instructions
87+
88+
In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.
89+
90+
You have four tasks, all related to the time spent cooking the lasagna.
91+
92+
## 1. Define the expected oven time in minutes
93+
94+
Define the `OvenTime` constant with how many minutes the lasagna should be in the oven. According to the cooking book, the expected oven time in minutes is 40:
95+
96+
```go
97+
OvenTime
98+
// Output: 40
99+
```
100+
101+
## 2. Calculate the remaining oven time in minutes
102+
103+
Define the `RemainingOvenTime()` function that takes the actual minutes the lasagna has been in the oven as a parameter and returns how many minutes the lasagna still has to remain in the oven, based on the expected oven time in minutes from the previous task.
104+
105+
```go
106+
func RemainingOvenTime(actual int) int {
107+
// TODO
108+
}
109+
110+
RemainingOvenTime(30)
111+
// Output: 10
112+
```
113+
114+
## 3. Calculate the preparation time in minutes
115+
116+
Define the `PreparationTime` function that takes the number of layers you added to the lasagna as a parameter and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.
117+
118+
```go
119+
func PreparationTime(numberOfLayers int) int {
120+
// TODO
121+
}
122+
123+
PreparationTime(2)
124+
// Output: 4
125+
```
126+
127+
## 4. Calculate the elapsed working time in minutes
128+
129+
Define the `ElapsedTime` function that takes two parameters: the first parameter is the number of layers you added to the lasagna, and the second parameter is the number of minutes the lasagna has been in the oven.
130+
The function should return how many minutes in total you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.
131+
132+
```go
133+
func ElapsedTime(numberOfLayers, actualMinutesInOven int) int {
134+
// TODO
135+
}
136+
137+
ElapsedTime(3, 20)
138+
// Output: 26
139+
```
140+
141+
## Source
142+
143+
### Created by
144+
145+
- @tehsphinx
146+
147+
### Contributed to by
148+
149+
- @ekingery
150+
- @andrerfcsantos
151+
- @bobtfish

Exercism/go/lasagna/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module lasagna
2+
3+
go 1.16

Exercism/go/lasagna/lasagna.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package lasagna
2+
3+
// TODO: define the 'OvenTime' constant
4+
const OvenTime = 40;
5+
6+
// RemainingOvenTime returns the remaining minutes based on the `actual` minutes already in the oven.
7+
func RemainingOvenTime(actualMinutesInOven int) int {
8+
return OvenTime - actualMinutesInOven;
9+
}
10+
11+
// PreparationTime calculates the time needed to prepare the lasagna based on the amount of layers.
12+
func PreparationTime(numberOfLayers int) int {
13+
const timePerLayer = 2;
14+
return timePerLayer * numberOfLayers;
15+
}
16+
17+
// ElapsedTime calculates the total time needed to create and bake a lasagna.
18+
func ElapsedTime(numberOfLayers, actualMinutesInOven int) int {
19+
return actualMinutesInOven + PreparationTime(numberOfLayers);
20+
}

Exercism/go/lasagna/lasagna_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package lasagna
2+
3+
import "testing"
4+
5+
type lasagnaTests struct {
6+
name string
7+
layers, time, expected int
8+
}
9+
10+
func TestOvenTime(t *testing.T) {
11+
tests := []lasagnaTests{
12+
{
13+
name: "Calculates how many minutes the lasagna should be in the oven",
14+
layers: 0,
15+
time: 40,
16+
expected: 40,
17+
},
18+
}
19+
for _, tt := range tests {
20+
t.Run(tt.name, func(t *testing.T) {
21+
if got := OvenTime; got != tt.expected {
22+
t.Errorf("OvenTime(%d) = %d; want %d", tt.expected, got, tt.expected)
23+
}
24+
})
25+
}
26+
}
27+
28+
func TestRemainingOvenTime(t *testing.T) {
29+
tests := []lasagnaTests{
30+
{
31+
name: "Remaining minutes in oven",
32+
layers: 0,
33+
time: 15,
34+
expected: 15,
35+
},
36+
}
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
if got := RemainingOvenTime(25); got != tt.time {
40+
t.Errorf("RemainingOvenTime(%d) = %d; want %d", tt.expected, got, tt.expected)
41+
}
42+
})
43+
}
44+
45+
}
46+
func TestPreparationTime(t *testing.T) {
47+
tests := []lasagnaTests{
48+
{
49+
name: "Preparation time in minutes for one layer",
50+
layers: 1,
51+
time: 0,
52+
expected: 2,
53+
},
54+
{
55+
name: "Preparation time in minutes for multiple layer",
56+
layers: 4,
57+
time: 0,
58+
expected: 8,
59+
},
60+
}
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
if got := PreparationTime(tt.layers); got != tt.expected {
64+
t.Errorf("PreparationTime(%d) = %d; want %d", tt.layers, got, tt.expected)
65+
}
66+
})
67+
68+
}
69+
}
70+
71+
func TestElapsedTime(t *testing.T) {
72+
tests := []lasagnaTests{
73+
{
74+
name: "Total time in minutes for one layer",
75+
layers: 1,
76+
time: 30,
77+
expected: 32,
78+
},
79+
{
80+
name: "Total time in minutes for multiple layer",
81+
layers: 4,
82+
time: 8,
83+
expected: 16,
84+
},
85+
}
86+
for _, tt := range tests {
87+
t.Run(tt.name, func(t *testing.T) {
88+
if got := ElapsedTime(tt.layers, tt.time); got != tt.expected {
89+
t.Errorf("ElapsedTime(%d, %d) = %d; want %d", tt.layers, tt.time, got, tt.expected)
90+
}
91+
})
92+
93+
}
94+
}

0 commit comments

Comments
 (0)