Skip to content
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

(COMPLETE) Itinerary - Review, please :) #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions exercises/itinerary.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,71 @@ Enter your solution below.
```elixir
defmodule Itinerary do
def has_time?(start, finish, activities) do
available_mins = DateTime.diff(finish, start, :minute)

activities_mins =
Enum.reduce(activities, 0, fn {unit, amount}, acc ->
case unit do
:hours -> amount * 60 + acc
:minutes -> amount + acc
end
end)

available_mins >= activities_mins
end
end

activities = [hours: 1]
start = DateTime.new!(~D[2022-04-24], ~T[12:00:00])
finish = DateTime.new!(~D[2022-04-24], ~T[13:00:00])

Itinerary.has_time?(start, finish, activities)

Utils.feedback(:itinerary, Itinerary)
```

```elixir
# INPUTS = Keyword list, Start Time, End Time
# OUTPUT = True or False

# Problem = Is the time between Start and End enough to fit all of the activities?
# I need to know:
# 1 Amount of minutes between Start and End (inclusive)
# 2 Amount of minutes of activities
# 3 Difference between the two

# 1 INPUT: Two times
# 1 OUTPUT: Minutes
# Subtract the end time to the start time (diff/3?)

start = DateTime.new!(~D[2022-04-24], ~T[12:00:00])
finish = DateTime.new!(~D[2022-04-24], ~T[13:00:00])
available_mins = DateTime.diff(finish, start, :minute)

# 2 INPUT: Keyword List

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great problem solving, starting with input and output!

# 2 OUTPUT: Minutes
# Problem / Solution
# Go through the keyword list, convert all activities to minutes, add them all up.

activities = [hours: 2, minutes: 30, hours: 4.5, minutes: 10]

activities_mins =
Enum.reduce(activities, 0, fn {unit, amount}, acc ->
case unit do
:hours -> amount * 60 + acc
:minutes -> amount + acc
end
end)

# 3 INPUT minutes available & minutes of activities
# 3 OUTPUT true or false
# Problem / Solution
# If minutes available > minutes of activities, then true. Else false.
IO.inspect(available_mins)
IO.inspect(activities_mins)
available_mins > activities_mins
```

## Commit Your Progress

Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.
Expand Down
14 changes: 7 additions & 7 deletions reading/control_flow.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ We've already seen `if` statements, which are a useful when we have one or two b

For example, we might build a weather app that provides recommendations of what to wear.

We'll have some condition like **if it is cold** then provide a recommendation to **wear a coat**. If it is not cold, then we'll provide a recommendation to wear a coat.
We'll have some condition like **if it is cold** then provide a recommendation to **wear a coat**. If it is not cold, then we'll provide a recommendation to wear a t-shirt.

<!-- livebook:{"break_markdown":true} -->

Expand Down Expand Up @@ -147,9 +147,9 @@ We can simplify our programs by reducing the number of branching decisions and l

## Case

Case is a control flow structure that allows you to define a series of cases, and handle them separately.
`case` is a control flow structure that allows you to define a series of cases, and handle them separately.

Case is best used when you have many branching conditions, that all work well with pattern matching.
`case` is best used when you have many branching conditions, that all work well with pattern matching.

We'll use this to improve our weather application that handles many different conditions.

Expand Down Expand Up @@ -188,7 +188,7 @@ end

### Case Breakdown

Let's break down the case statement above. To use a case statement start with the `case` keyword.
Let's break down the `case` statement above. To use a `case` statement, start with the `case` keyword.

<!-- livebook:{"force_markdown":true} -->

Expand Down Expand Up @@ -294,7 +294,7 @@ If the values would throw a [MatchError](https://hexdocs.pm/elixir/MatchError.ht
{:not_exactly, :equal} = {:exactly, :equal}
```

That's why the following case statement does not trigger the `"non-matching case"`.
That's why the following `case` statement does not trigger the `"non-matching case"`.

```elixir
case {:exactly, :equal} do
Expand Down Expand Up @@ -434,7 +434,7 @@ cond do
end
```

Like `case` statements, conditions will raise an error if we don't trigger any condition.
Like `case` statements, `cond` will raise an error if we don't trigger any condition.

```elixir
cond do
Expand Down Expand Up @@ -547,7 +547,7 @@ end

In the Elixir cell below

* Create an variable named `tired` which is `true` or `false`.
* Create a variable named `tired` which is `true` or `false`.
* Create an `unless` statement. which returns `"awake"` unless `tired`.

```elixir
Expand Down
2 changes: 1 addition & 1 deletion reading/functions.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ Enum.map([1, 2, 3], fn element -> element * 2 end)

### Your Turn

Create a `call_with_10_and_20/1` function. the `call_with_10_and_20/1` function should accept a call back function, and call the function with `10` as the first argument, and `20` as the second argument.
Create a `call_with_10_and_20/1` function. The `call_with_10_and_20/1` function should accept a call back function, and call the function with `10` as the first argument, and `20` as the second argument.

<!-- livebook:{"force_markdown":true} -->

Expand Down