lasagna
This commit is contained in:
parent
2cb2de4caf
commit
a33fa3295c
|
@ -0,0 +1,31 @@
|
|||
{
|
||||
"authors": [
|
||||
"tehsphinx"
|
||||
],
|
||||
"contributors": [
|
||||
"ekingery",
|
||||
"andrerfcsantos",
|
||||
"bobtfish"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"lasagna.go"
|
||||
],
|
||||
"test": [
|
||||
"lasagna_test.go"
|
||||
],
|
||||
"exemplar": [
|
||||
".meta/exemplar.go"
|
||||
],
|
||||
"invalidator": [
|
||||
"go.mod"
|
||||
]
|
||||
},
|
||||
"forked_from": [
|
||||
"csharp/lucians-luscious-lasagna"
|
||||
],
|
||||
"blurb": "Learn about packages, functions, and variables by helping Gopher cook lasagna.",
|
||||
"custom": {
|
||||
"taskIdsEnabled": true
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"go","exercise":"lasagna","id":"e00f7b520b484d7490ead31a19844226","url":"https://exercism.org/tracks/go/exercises/lasagna","handle":"negrienko","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,41 @@
|
|||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
To run the tests run the command `go test` from within the exercise directory.
|
||||
|
||||
If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
|
||||
flags:
|
||||
|
||||
go test -v --bench . --benchmem
|
||||
|
||||
Keep in mind that each reviewer will run benchmarks on a different machine, with
|
||||
different specs, so the results from these benchmark tests may vary.
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit lasagna.go` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [Go track's documentation](https://exercism.org/docs/tracks/go)
|
||||
- The [Go track's programming category on the forum](https://forum.exercism.org/c/programming/go)
|
||||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
To get help if you're having trouble, you can use one of the following resources:
|
||||
|
||||
- [How to Write Go Code](https://golang.org/doc/code.html)
|
||||
- [Effective Go](https://golang.org/doc/effective_go.html)
|
||||
- [Go Resources](http://golang.org/help)
|
||||
- [StackOverflow](http://stackoverflow.com/questions/tagged/go)
|
|
@ -0,0 +1,44 @@
|
|||
# Hints
|
||||
|
||||
## General
|
||||
|
||||
- An [integer value][integers] can be defined as one or more consecutive digits.
|
||||
- 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.
|
||||
|
||||
## 1. Define the expected oven time in minutes
|
||||
|
||||
- You need to define a [constant][constants] and assign it the expected oven time in minutes.
|
||||
- If you see an `undefined: OvenTime` error then double check that you have the constant defined.
|
||||
- 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`.
|
||||
- If you see a `syntax error: non-declaration statement outside function body` error then it is likely that you forgot the `const` keyword.
|
||||
- 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 `:=`.
|
||||
|
||||
## 2. Calculate the remaining oven time in minutes
|
||||
|
||||
- You need to define a [function][functions] with a single parameter.
|
||||
- You have to [explicitly return an integer][return] from a function.
|
||||
- The function's parameter is an [integer][integers].
|
||||
- You can [call][calls] one of the other functions you've defined previously.
|
||||
- You can use the [mathematical operator for subtraction][operators] to subtract values.
|
||||
|
||||
## 3. Calculate the preparation time in minutes
|
||||
|
||||
- You need to define a [function][functions] with a single parameter.
|
||||
- You have to [explicitly return an integer][return] from a function.
|
||||
- The function's parameter is an [integer][integers].
|
||||
- You can use the [mathematical operator for multiplication][operators] to multiply values.
|
||||
|
||||
## 4. Calculate the elapsed working time in minutes
|
||||
|
||||
- You need to define a [function][functions] with two parameters.
|
||||
- You have to [explicitly return an integer][return] from a function.
|
||||
- The function's parameter is an [integer][integers].
|
||||
- You can [call][calls] one of the other functions you've defined previously.
|
||||
- You can use the [mathematical operator for addition][operators] to add values.
|
||||
|
||||
[functions]: https://tour.golang.org/basics/4
|
||||
[return]: https://golang.org/ref/spec#Return_statements
|
||||
[operators]: https://golang.org/ref/spec#Operators
|
||||
[integers]: https://golang.org/ref/spec#Integer_literals
|
||||
[calls]: https://golang.org/ref/spec#Calls
|
||||
[constants]: https://tour.golang.org/basics/15
|
|
@ -0,0 +1,151 @@
|
|||
# Gopher's Gorgeous Lasagna
|
||||
|
||||
Welcome to Gopher's Gorgeous Lasagna on Exercism's Go Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||
|
||||
## Introduction
|
||||
|
||||
[Go](https://golang.org) is a statically typed, compiled programming language.
|
||||
This exercise introduces three major language features: Packages, Functions, and Variables.
|
||||
|
||||
## Packages
|
||||
|
||||
Go applications are organized in packages.
|
||||
A package is a collection of source files located in the same directory.
|
||||
All source files in a directory must share the same package name.
|
||||
When a package is imported, only entities (functions, types, variables, constants) whose names start with a capital letter can be used / accessed.
|
||||
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 `PascalCase`.
|
||||
|
||||
```go
|
||||
package lasagna
|
||||
```
|
||||
|
||||
## Variables
|
||||
|
||||
Go is statically-typed, which means all variables [must have a defined type](https://en.wikipedia.org/wiki/Type_system) at compile-time.
|
||||
|
||||
Variables can be defined by explicitly specifying a type:
|
||||
|
||||
```go
|
||||
var explicit int // Explicitly typed
|
||||
```
|
||||
|
||||
You can also use an initializer, and the compiler will assign the variable type to match the type of the initializer.
|
||||
|
||||
```go
|
||||
implicit := 10 // Implicitly typed as an int
|
||||
```
|
||||
|
||||
Once declared, variables can be assigned values using the `=` operator.
|
||||
Once declared, a variable's type can never change.
|
||||
|
||||
```go
|
||||
count := 1 // Assign initial value
|
||||
count = 2 // Update to new value
|
||||
|
||||
count = false // This throws a compiler error due to assigning a non `int` type
|
||||
```
|
||||
|
||||
## Constants
|
||||
|
||||
Constants hold a piece of data just like variables, but their value cannot change during the execution of the program.
|
||||
|
||||
Constants are defined using the `const` keyword and can be numbers, characters, strings or booleans:
|
||||
|
||||
```go
|
||||
const Age = 21 // Defines a numeric constant 'Age' with the value of 21
|
||||
```
|
||||
|
||||
## Functions
|
||||
|
||||
Go functions accept zero or more parameters.
|
||||
Parameters must be explicitly typed, there is no type inference.
|
||||
|
||||
Values are returned from functions using the `return` keyword.
|
||||
|
||||
A function is invoked by specifying the function name and passing arguments for each of the function's parameters.
|
||||
|
||||
Note that Go supports two types of comments.
|
||||
Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`.
|
||||
|
||||
```go
|
||||
package greeting
|
||||
|
||||
// Hello is a public function.
|
||||
func Hello (name string) string {
|
||||
return hi(name)
|
||||
}
|
||||
|
||||
// hi is a private function.
|
||||
func hi (name string) string {
|
||||
return "hi " + name
|
||||
}
|
||||
```
|
||||
|
||||
## Instructions
|
||||
|
||||
In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.
|
||||
|
||||
You have four tasks, all related to the time spent cooking the lasagna.
|
||||
|
||||
## 1. Define the expected oven time in minutes
|
||||
|
||||
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:
|
||||
|
||||
```go
|
||||
OvenTime
|
||||
// => 40
|
||||
```
|
||||
|
||||
## 2. Calculate the remaining oven time in minutes
|
||||
|
||||
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.
|
||||
|
||||
```go
|
||||
func RemainingOvenTime(actual int) int {
|
||||
// TODO
|
||||
}
|
||||
|
||||
RemainingOvenTime(30)
|
||||
// => 10
|
||||
```
|
||||
|
||||
## 3. Calculate the preparation time in minutes
|
||||
|
||||
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.
|
||||
|
||||
```go
|
||||
func PreparationTime(numberOfLayers int) int {
|
||||
// TODO
|
||||
}
|
||||
|
||||
PreparationTime(2)
|
||||
// => 4
|
||||
```
|
||||
|
||||
## 4. Calculate the elapsed working time in minutes
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
```go
|
||||
func ElapsedTime(numberOfLayers, actualMinutesInOven int) int {
|
||||
// TODO
|
||||
}
|
||||
|
||||
ElapsedTime(3, 20)
|
||||
// => 26
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @tehsphinx
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @ekingery
|
||||
- @andrerfcsantos
|
||||
- @bobtfish
|
|
@ -0,0 +1,3 @@
|
|||
module lasagna
|
||||
|
||||
go 1.18
|
|
@ -0,0 +1,19 @@
|
|||
package lasagna
|
||||
|
||||
const OvenTime = 40
|
||||
const LayerPreparationTime = 2
|
||||
|
||||
// RemainingOvenTime returns the remaining minutes based on the `actual` minutes already in the oven.
|
||||
func RemainingOvenTime(actualMinutesInOven int) int {
|
||||
return OvenTime - actualMinutesInOven
|
||||
}
|
||||
|
||||
// PreparationTime calculates the time needed to prepare the lasagna based on the amount of layers.
|
||||
func PreparationTime(numberOfLayers int) int {
|
||||
return numberOfLayers * LayerPreparationTime
|
||||
}
|
||||
|
||||
// ElapsedTime calculates the time elapsed cooking the lasagna. This time includes the preparation time and the time the lasagna is baking in the oven.
|
||||
func ElapsedTime(numberOfLayers, actualMinutesInOven int) int {
|
||||
return PreparationTime(numberOfLayers) + actualMinutesInOven
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
package lasagna
|
||||
|
||||
import "testing"
|
||||
|
||||
type lasagnaTests struct {
|
||||
name string
|
||||
layers, time, expected int
|
||||
}
|
||||
|
||||
func TestOvenTime(t *testing.T) {
|
||||
tests := []lasagnaTests{
|
||||
{
|
||||
name: "Calculates how many minutes the lasagna should be in the oven",
|
||||
layers: 0,
|
||||
time: 40,
|
||||
expected: 40,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := OvenTime; got != tt.expected {
|
||||
t.Errorf("OvenTime(%d) = %d; want %d", tt.expected, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemainingOvenTime(t *testing.T) {
|
||||
tests := []lasagnaTests{
|
||||
{
|
||||
name: "Remaining minutes in oven",
|
||||
layers: 0,
|
||||
time: 15,
|
||||
expected: 25,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := RemainingOvenTime(tt.time); got != tt.expected {
|
||||
t.Errorf("RemainingOvenTime(%d) = %d; want %d", tt.time, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
func TestPreparationTime(t *testing.T) {
|
||||
tests := []lasagnaTests{
|
||||
{
|
||||
name: "Preparation time in minutes for one layer",
|
||||
layers: 1,
|
||||
time: 0,
|
||||
expected: 2,
|
||||
},
|
||||
{
|
||||
name: "Preparation time in minutes for multiple layers",
|
||||
layers: 4,
|
||||
time: 0,
|
||||
expected: 8,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := PreparationTime(tt.layers); got != tt.expected {
|
||||
t.Errorf("PreparationTime(%d) = %d; want %d", tt.layers, got, tt.expected)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestElapsedTime(t *testing.T) {
|
||||
tests := []lasagnaTests{
|
||||
{
|
||||
name: "Total time in minutes for one layer",
|
||||
layers: 1,
|
||||
time: 30,
|
||||
expected: 32,
|
||||
},
|
||||
{
|
||||
name: "Total time in minutes for multiple layers",
|
||||
layers: 4,
|
||||
time: 8,
|
||||
expected: 16,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := ElapsedTime(tt.layers, tt.time); got != tt.expected {
|
||||
t.Errorf("ElapsedTime(%d, %d) = %d; want %d", tt.layers, tt.time, got, tt.expected)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue