Repository navigation
Add the lasagna concept exercise for variables #985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
00135c6
Add the lasagna concept exercise for variables
glennj e6c532b
Bash -> bash
glennj 1d694e7
Use arithmetic expansion in the lasagna exercise
glennj cc3ac3f
Add road map to the exercises/concept directory
glennj 6c3d996
for default values, note there are other expansions
glennj 4d9cd12
road map
glennj ecc7ed3
restore concept "about" content into the introduction(s)
glennj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Hints | ||
|
|
||
| ## General | ||
|
|
||
| - There must be _no spaces_ around the `=` in a variable assignment. | ||
| - Use `$name` or `${name}` to get the value of a variable. | ||
| - Quote parameter expansions: `"$name"`. | ||
| - Use `${name:-default}` to provide a default value when a variable is empty or unset. | ||
| - Use good variable names: lowercase, and descriptive. | ||
|
|
||
| ## 1. Define the expected oven time in minutes | ||
|
|
||
| - Assign the value to a variable, then use [`echo`][echo] with the variable inside a double-quoted string. | ||
| - Braces can separate the variable name from surrounding text: `"${name}text"`. | ||
|
|
||
| ## 2. Calculate the remaining oven time in minutes | ||
|
|
||
| - The first command-line argument is the [positional parameter][positional] `$1`. | ||
| - Use a default value to handle a missing argument: `${1:-0}`. | ||
| - [Arithmetic expansion][arithmetic] expands to the result of a calculation: `result=$(( 7 - 2 ))`. | ||
| - Inside `$(( ))`, you can use variable names without the `$`. | ||
|
|
||
| ## 3. Calculate the preparation time in minutes | ||
|
|
||
| - The second command-line argument is `$2`. | ||
| - Remember to provide a default for a missing argument. | ||
| - Use `*` to multiply: `$(( a * b ))`. | ||
| - Store the minutes per layer in a variable, so the number 2 has a name. | ||
|
|
||
| ## 4. Calculate the total working time in minutes | ||
|
|
||
| - `$(( a + b ))` adds two numbers. | ||
| - You can reuse the variables you have already assigned. | ||
|
|
||
| [echo]: https://www.gnu.org/software/bash/manual/bash.html#Bash-Builtins | ||
| [positional]: https://www.gnu.org/software/bash/manual/bash.html#Positional-Parameters | ||
| [arithmetic]: https://www.gnu.org/software/bash/manual/bash.html#Arithmetic-Expansion |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Instructions | ||
|
|
||
| In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cookbook. | ||
|
|
||
| You have four tasks, all related to the time spent cooking the lasagna. | ||
| Each task adds to the same script, so preserve the output from the previous tasks. | ||
| Do not print anything other than the requested output. | ||
|
|
||
| The script accepts two optional command-line arguments: | ||
|
|
||
| 1. the number of minutes the lasagna has _actually_ been in the oven. | ||
| If it is missing, use `0`. | ||
| 2. the number of layers in the lasagna. | ||
| If it is missing, use `1`. | ||
|
|
||
| Run the script with: | ||
|
|
||
| ```bash | ||
| bash lasagna.sh 18 3 | ||
| ``` | ||
|
|
||
| Use arithmetic expansion to do the math. | ||
| For example, `$(( 7 - 2 ))` expands to `5`. | ||
|
|
||
| ## 1. Define the expected oven time in minutes | ||
|
|
||
| According to the cookbook, the expected oven time is 40 minutes. | ||
| Store this value in a variable, and print it: | ||
|
|
||
| ```text | ||
| Expected minutes in oven: 40 | ||
| ``` | ||
|
|
||
| ## 2. Calculate the remaining oven time in minutes | ||
|
|
||
| Store the actual minutes in the oven (the first argument) in a variable. | ||
| Then calculate how many minutes the lasagna still has to remain in the oven, based on the expected oven time from the previous task. | ||
| Print it on the next line: | ||
|
|
||
| ```bash | ||
| bash lasagna.sh 18 3 | ||
| ``` | ||
|
|
||
| ```text | ||
| Expected minutes in oven: 40 | ||
| Remaining minutes in oven: 22 | ||
| ``` | ||
|
|
||
| ## 3. Calculate the preparation time in minutes | ||
|
|
||
| The preparation time is how long it takes to get the lasagna ready before it is put in the oven. | ||
| It takes 2 minutes to prepare each layer. | ||
| Store the number of layers (the second argument) in a variable, and store the minutes per layer in another variable instead of using a "magic number". | ||
|
glennj marked this conversation as resolved.
Resolved
|
||
| Print the preparation time on the next line: | ||
|
|
||
| ```text | ||
| Expected minutes in oven: 40 | ||
| Remaining minutes in oven: 22 | ||
| Preparation time in minutes: 6 | ||
| ``` | ||
|
|
||
| ## 4. Calculate the total working time in minutes | ||
|
|
||
| Calculate how many minutes in total you've worked on cooking the lasagna. | ||
| This is the sum of the preparation time and the time the lasagna has spent in the oven so far. | ||
| Print it on the last line: | ||
|
|
||
| ```text | ||
| Expected minutes in oven: 40 | ||
| Remaining minutes in oven: 22 | ||
| Preparation time in minutes: 6 | ||
| Total time in minutes: 24 | ||
| ``` | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.