diff --git a/exercises/itinerary.livemd b/exercises/itinerary.livemd index 58678df08..1200c38e0 100644 --- a/exercises/itinerary.livemd +++ b/exercises/itinerary.livemd @@ -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 +# 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. diff --git a/reading/control_flow.livemd b/reading/control_flow.livemd index 31f96b297..da7ac690c 100644 --- a/reading/control_flow.livemd +++ b/reading/control_flow.livemd @@ -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. @@ -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. @@ -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. @@ -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 @@ -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 @@ -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 diff --git a/reading/functions.livemd b/reading/functions.livemd index 933ad6ba1..a66f41092 100644 --- a/reading/functions.livemd +++ b/reading/functions.livemd @@ -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.