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

Ranges reading #30

Open
wants to merge 14 commits into
base: solutions
Choose a base branch
from
30 changes: 30 additions & 0 deletions exercises/naming_numbers.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,22 @@ naming_numbers.(1)
Enter your solution below.

```elixir
naming_numbers = fn integer ->
case integer do
0 -> "zero"
1 -> "one"
2 -> "two"
3 -> "three"
4 -> "four"
5 -> "five"
6 -> "six"
7 -> "seven"
8 -> "eight"
9 -> "nine"
end
end

naming_numbers.(1)
```

## Numbering Names
Expand Down Expand Up @@ -169,7 +184,22 @@ flowchart
</details>

```elixir
numbering_names = fn int_str ->
case String.downcase(int_str) do
"zero" -> 0
"one" -> 1
"two" -> 2
"three" -> 3
"four" -> 4
"five" -> 5
"six" -> 6
"seven" -> 7
"eight" -> 8
"nine" -> 9
end
end

numbering_names.("Nine")
```

## Mark As Completed
Expand Down
24 changes: 23 additions & 1 deletion exercises/rock_paper_scissors.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ Then, return the winning choice of either `:rock`, `:paper`, or `:scissors` that
Enter your solution below.

```elixir
player_choice = Enum.random([:rock, :paper, :scissors])

IO.puts(player_choice)

case player_choice do
:rock -> :paper
:paper -> :scissors
:scissors -> :rock
end
```

## Create Two Player Rock Paper Scissors
Expand Down Expand Up @@ -107,7 +115,21 @@ Bind a `player1_choice` and `player2_choice` variable to `:rock`, `:paper`, or `
Enter your solution below.

```elixir

player1 = Enum.random([:rock, :paper, :scissors])
IO.inspect(player1: player1)

player2 = Enum.random([:rock, :paper, :scissors])
IO.inspect(player2: player2)

case {player1, player2} do
{:rock, :scissors} -> "Player 1 Wins!"
{:paper, :rock} -> "Player 1 Wins!"
{:scissors, :paper} -> "Player 1 Wins!"
{:rock, :paper} -> "Player 2 Wins!"
{:paper, :scissors} -> "Player 2 Wins!"
{:scissors, :rock} -> "Player 2 Wins!"
{same, same} -> "Draw"
end
```

## Mark As Completed
Expand Down
22 changes: 22 additions & 0 deletions exercises/rock_paper_scissors_lizard_spock.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ defmodule RockPaperScissorsLizardSpock do
false
"""
def beats?(guess1, guess2) do
{guess1, guess2} in [
{:rock, :scissors},
{:rock, :lizard},
{:paper, :rock},
{:paper, :spock},
{:scissors, :paper},
{:scissors, :lizards},
{:lizard, :paper},
{:lizard, :spock},
{:spock, :scissors}
]
end

@doc """
Expand All @@ -99,9 +110,20 @@ defmodule RockPaperScissorsLizardSpock do
iex> RockPaperScissorsLizardSpock.play(:lizard, :lizard)
"Player 2 Wins!"
"""

def play(player1, player2) do
cond do
beats?(player1, player2) -> "Player 1 Wins!"
not beats?(player1, player2) -> "Player 2 Wins!"
end
end
end

RockPaperScissorsLizardSpock.play(:rock, :paper)
```

```elixir

```

## Mark As Completed
Expand Down
44 changes: 38 additions & 6 deletions exercises/rpg_dialogue.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ defmodule Character do
iex> %Character{name: "Frodo"}
** (ArgumentError) the following keys must also be given when building struct Character: [:name]
"""
defstruct []
@enforced_key [:name]

defstruct @enforced_key ++ [:class, :weapon]

@doc """
Introduce the character by name.
Expand All @@ -95,7 +97,8 @@ defmodule Character do
iex> Character.introduce(%Character{name: "Aragorn"})
"My name is Aragorn."
"""
def introduce(character) do
def introduce(char) do
"My name is #{char.name}."
end

@doc """
Expand All @@ -109,7 +112,8 @@ defmodule Character do
iex> Character.attack(%Character{name: "Aragorn", weapon: "sword"})
"I attack with my sword!"
"""
def attack(character) do
def attack(char) do
"I attack with my #{char.weapon}!"
end

@doc """
Expand All @@ -123,7 +127,8 @@ defmodule Character do
iex> Character.class(%Character{name: "Aragorn", class: "ranger"})
"I am a ranger."
"""
def class(character) do
def class(char) do
"I am a #{char.class}."
end

@doc """
Expand All @@ -137,9 +142,14 @@ defmodule Character do
iex> Character.war_cry(%Character{name: "Aragorn", class: "ranger"})
"My name is Aragorn and I am a ranger!"
"""
def war_cry(character) do
def war_cry(char) do
"My name is #{char.name} and I am a #{char.class}!"
end

# def war_cry2(name, class) do
# "My name is #{name} and I am a #{class}"
# end

@doc """
Declare that one character has defeated another.

Expand All @@ -151,9 +161,31 @@ defmodule Character do
iex> Character.defeat(%Character{name: "Aragorn"}, %Character{name: "Gimli", class: "warrior"})
"My name is Aragorn and I have defeated the warrior Gimli!"
"""
def defeat(character1, character2) do
def defeat(char1, char2) do
"My name is #{char1.name} and I have defeated the #{char2.class} #{char2.name}!"
end
end

# defmodule Test do
# import Character

# def test do
# aragorn = %Character{name: "Aragorn", class: "ranger", weapon: "sword"}
# gandalf = %Character{name: "Gandalf", class: "wizard", weapon: "staff"}
# Character.introduce(aragorn)
# Character.attack(aragorn)
# Character.class(aragorn)
# Character.war_cry(aragorn)
# Character.war_cry2(aragorn.name, aragorn.class)
# Character.defeat(aragorn, gandalf)
# end
# end

# Test.test
```

```elixir

```

### Bonus: Character Instances
Expand Down
10 changes: 5 additions & 5 deletions progress.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"games_score_tracker_exercise": false,
"habit_tracker_exercise": false,
"reduce_reading": false,
"code_editors_reading": false,
"code_editors_reading": true,
"exdoc_reading": false,
"process_drills_exercise": false,
"saferange_exercise": false,
Expand Down Expand Up @@ -64,7 +64,7 @@
"github_engineering_journal_exercise": false,
"mailbox_server_exercise": false,
"blog_posts_exercise": false,
"git_reading": false,
"git_reading": true,
"named_number_lists_exercise": false,
"big_o_notation_reading": false,
"battle_map_exercise": false,
Expand Down Expand Up @@ -96,7 +96,7 @@
"process_mailbox_exercise": false,
"io_reading": false,
"tuples_reading": false,
"ranges_reading": false,
"ranges_reading": true,
"task_reading": false,
"executables_reading": false,
"rdbms_reading": false,
Expand Down Expand Up @@ -145,7 +145,7 @@
"task_drills_exercise": false,
"monster_spawner_exercise": false,
"metaprogramming_reading": false,
"start_here_reading": false,
"start_here_reading": true,
"games_supervisor_setup_exercise": false,
"pic_chat_pub_sub_reading": false,
"lists_reading": false,
Expand All @@ -158,7 +158,7 @@
"save_game_exercise": false,
"document_tools_exercise": false,
"factorial_exercise": false,
"livebook_reading": false,
"livebook_reading": true,
"filter_values_by_type_exercise": false,
"book_search_exercise": false,
"guards_reading": false,
Expand Down
10 changes: 6 additions & 4 deletions reading/keyword_lists.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ In the Elixir cell below, create a keyword list of your favourite super hero. In
Enter your solution below.

```elixir

[hero1: "superman", hero2: "batman", hero3: "robin"]
```

## Accessing A Key
Expand All @@ -184,6 +184,7 @@ Access the `:hello` key in the following keyword list.

```elixir
keyword_list = [hello: "world"]
keyword_list[:hello]
```

## Keyword List Operators
Expand All @@ -206,13 +207,13 @@ evaluate as a tuple again. Remember that keyword lists are simply lists of tuple
In the Elixir cell below, use `++` to add `[one: 1]` to `[two: 2]` to make `[one: 1, two: 2]`.

```elixir

[one: 1] ++ [two: 2]
```

Remove `[two: 2]` from `[one: 1, two: 2]` to make `[one: 1]`.

```elixir

[one: 1, two: 2] -- [two: 2]
```

## Pattern Matching
Expand Down Expand Up @@ -274,7 +275,8 @@ Bind `1` in the following keyword list to a variable `one`.
Enter your solution below.

```elixir
[one: 1, two: 2, three: 3, four: 4]
[{key, one} | tail] = [one: 1, two: 2, three: 3, four: 4]
one
```

## Further Reading
Expand Down
9 changes: 7 additions & 2 deletions reading/maps.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ map[:hello]

```elixir
map = %{hello: "world"}
map.hello
map[:hello]
```

### Updating Maps
Expand Down Expand Up @@ -253,7 +255,9 @@ todo = %{title: "finish maps exercise", completed: false}
</details>

```elixir

todo = %{title: "finish maps exercises", completed: false}
updated = %{todo | completed: true}
updated.completed
```

## Pattern Matching
Expand Down Expand Up @@ -332,7 +336,8 @@ Bind `2` and `4` in the following map to variables `two` and `four`.
Enter your solution below.

```elixir
%{one: 1, two: 2, three: 3, four: 4}
%{two: two, four: four} = %{one: 1, two: 2, three: 3, four: 4}
four
```

## Further Reading
Expand Down
Loading