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) My mind still works in OOP - Questions... #8

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
65 changes: 34 additions & 31 deletions exercises/itinerary.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,50 @@ Mix.install([

## Itinerary

You are building a day planning application. Users will provide a list of expected
times for activities and you will determine if they have enough time in the day to fit those activities.
You are building a day planning application. Users will provide the time it takes to complete an activity in minutes and you will determine if there is enough time to fit the activity between two [DateTime](https://hexdocs.pm/elixir/DateTime.html)s.

Activities are provided as a keyword lists of times using only `:minutes`, and `:hours`.

Given a start time and and end time, your `Itinerary` should determine if they have enough time
to complete all of their activities.

<!-- livebook:{"force_markdown":true} -->
<details style="background-color: lightgreen; padding: 1rem; margin: 1rem 0;">
<summary>Example Solution</summary>

```elixir
activities = [hours: 2, minutes: 30, hours: 4.5, minutes: 10] # ~430 minutes of activities
start = DateTime.new!(~D[2022-04-24], ~T[12:00:00])
finish = DateTime.new!(~D[2022-04-24], ~T[20:00:00]) # 8 hours or 480 minutes of time

Itinerary.has_time?(start, finish, activities)
true
defmodule Itinerary do
def has_time?(start, finish, minutes) do
DateTime.diff(finish, start) >= minutes * 60
end
end
```

`has_time?/3` should be inclusive, so one hour of activities should fit into one hour of time.
</details>

<!-- livebook:{"force_markdown":true} -->
Implement the `Itinerary` module as documented.

```elixir
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])
defmodule Itinerary do
@doc """
Given a number of minutes, determine if there is enough time
between two `DateTimes` to fit all activities.

Itinerary.has_time?(start, finish, activities)
true
```
## Examples

Enter your solution below.
iex> Itinerary.has_time?(~U[2020-01-01 12:00:00Z], ~U[2020-01-01 12:01:00Z], 1)
true

iex> Itinerary.has_time?(~U[2020-01-01 12:00:00Z], ~U[2020-01-01 12:10:00Z], 10)
true

```elixir
defmodule Itinerary do
def has_time?(start, finish, activities) do
iex> Itinerary.has_time?(~U[2020-01-01 07:00:00Z], ~U[2020-01-01 08:00:00Z], 60)
true

iex> Itinerary.has_time?(~U[2020-01-01 12:00:00Z], ~U[2020-01-01 12:30:00Z], 31)
false

iex> Itinerary.has_time?(~U[2020-01-01 07:00:00Z], ~U[2020-01-01 08:00:00Z], 61)
false

"""
def has_time?(start, finish, minutes) do
end
end

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

## Commit Your Progress
Expand All @@ -70,6 +73,6 @@ $ git commit -m "finish itinerary exercise"

## Up Next

| Previous | Next |
| -------------------------------------- | ---------------------------------------: |
| [Datetime](../reading/datetime.livemd) | [Timeline](../exercises/timeline.livemd) |
| Previous | Next |
| ------------------------------------------------------ | ---------------------------------------: |
| [Time Converting](../exercises/time_converting.livemd) | [Timeline](../exercises/timeline.livemd) |
116 changes: 116 additions & 0 deletions exercises/time_converting.livemd
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Time Converting

```elixir
Mix.install([
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"},
{:tested_cell, github: "brooklinjazz/tested_cell"},
{:utils, path: "#{__DIR__}/../utils"}
])
```

## Navigation

[Return Home](../start.livemd)<span style="padding: 0 30px"></span>
[Report An Issue](https://github.com/DockYard-Academy/beta_curriculum/issues/new?assignees=&labels=&template=issue.md&title=)

## Time Converting

Often we have to convert seconds, minutes, hours, and days. You're going to create a `TimeConverter` module which can handle all of this for us.

We'll use the atoms `:days`, `:hours`, `:minutes`, or `:seconds` to represent days, hours, minutes, and seconds.

<details style="background-color: lightgreen; padding: 1rem; margin: 1rem 0;">
<summary>Example Solution</summary>

```elixir
defmodule TimeConverter do
def to_seconds(amount, unit) do
case unit do
:seconds -> amount
:minutes -> amount * 60
:hours -> amount * 60 * 60
:days -> amount * 60 * 60 * 24
end
end

def from_seconds(amount, unit) do
case unit do
:seconds -> amount
:minutes -> amount / 60
:hours -> amount / 60 / 60
:days -> amount / 60 / 60 / 24
end
end
end
```

</details>

Implement the `TimeConverter.to_seconds/2` and `TimeConverter.from_seconds/2` functions as documented.

```elixir
defmodule TimeConverter do
@moduledoc """
Documentation for `TimeConverter`
"""

@doc """
Convert a unit of time to a number of seconds.

## Examples

iex> TimeConverter.to_seconds(1, :seconds)
1

iex> TimeConverter.to_seconds(1, :minutes)
60

iex> TimeConverter.to_seconds(1, :hours)
3600

iex> TimeConverter.to_seconds(1, :days)
86400

"""
def to_seconds(amount, unit) do
end

@doc """
Convert a number of seconds to a unit of time.
Return a float, as these values will require division using `/`.

## Examples

iex> TimeConverter.from_seconds(1, :seconds)
1.0

iex> TimeConverter.from_seconds(60, :minutes)
1.0

iex> TimeConverter.from_seconds(3600, :hours)
1.0

iex> TimeConverter.from_seconds(86400, :days)
1.0

"""
def from_seconds(amount, unit) do
end
end
```

## 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.

```
$ git add .
$ git commit -m "finish itinerary exercise"
```

## Up Next

| Previous | Next |
| -------------------------------------- | -----------------------------------------: |
| [Datetime](../reading/datetime.livemd) | [Itinerary](../exercises/itinerary.livemd) |
Loading