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

Named number lists exercise #33

Open
wants to merge 17 commits into
base: solutions
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
10 changes: 10 additions & 0 deletions exercises/fizzbuzz.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,18 @@ defmodule FizzBuzz do
["buzz", 11, "fizz", 13, 14, "fizzbuzz"]
"""
def run(range) do
Enum.map(range, fn int ->
cond do
rem(int, 15) == 0 -> "fizzbuzz"
rem(int, 3) == 0 -> "fizz"
rem(int, 5) == 0 -> "buzz"
true -> int
end
end)
end
end

FizzBuzz.run(1..15)
```

## Mark As Completed
Expand Down
17 changes: 16 additions & 1 deletion exercises/named_number_lists.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,22 @@ flowchart
Enter your solution below.

```elixir

rando = Enum.map(1..10, fn _ -> Enum.random(0..9) end)

Enum.map(rando, fn int ->
case int do
0 -> "zero"
1 -> "one"
2 -> "two"
3 -> "three"
4 -> "four"
5 -> "five"
6 -> "six"
7 -> "seven"
8 -> "eight"
9 -> "nine"
end
end)
```

## Mark As Completed
Expand Down
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
12 changes: 6 additions & 6 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,8 +64,8 @@
"github_engineering_journal_exercise": false,
"mailbox_server_exercise": false,
"blog_posts_exercise": false,
"git_reading": false,
"named_number_lists_exercise": false,
"git_reading": true,
"named_number_lists_exercise": true,
"big_o_notation_reading": false,
"battle_map_exercise": false,
"group_project_blog_presentation_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
25 changes: 19 additions & 6 deletions reading/enum.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ Use [Enum.map/2](https://hexdocs.pm/elixir/1.12/Enum.html#map/2) to convert a li
Enter your solution below.

```elixir

Enum.map(1..10, fn int -> to_string(int) end)
```

## Enum.reduce/2 and Enum.reduce/3
Expand Down Expand Up @@ -492,7 +492,13 @@ Use [Enum.reduce/3](https://hexdocs.pm/elixir/Enum.html#reduce/3) or [Enum.reduc
Enter your solution below.

```elixir

Enum.reduce(1..10, 0, fn int, acc ->
if rem(int, 2) == 0 do
acc + int
else
acc
end
end)
```

## Enum.filter
Expand Down Expand Up @@ -530,7 +536,8 @@ odd_numbers = Enum.filter(1..10, fn integer -> rem(integer, 2) != 0 end)
Enter your solution below.

```elixir

even = Enum.filter(1..10, fn int -> rem(int, 2) == 0 end)
odd = Enum.filter(1..10, fn int -> rem(int, 2) != 0 end)
```

## Enum.all?/2
Expand Down Expand Up @@ -585,6 +592,7 @@ Use [Enum.all/2](https://hexdocs.pm/elixir/Enum.html#all/2) to determine if all

```elixir
colors = [:green, :green, :red]
Enum.all?(colors, fn color -> color == :green end)
```

## Enum.any/2
Expand Down Expand Up @@ -628,7 +636,8 @@ Enum.any?(1..10_000_000, fn integer -> is_bitstring(integer) end)
Use [Enum.any/2](https://hexdocs.pm/elixir/Enum.html#any/2) to determine if any of the animals in the `animals` list are `:dogs`. You may change the `animals` list to experiment with [Enum.any/2](https://hexdocs.pm/elixir/Enum.html#any/2).

```elixir
[:cats, :dogs, :bears, :lions, :penguins]
animals = [:cats, :dogs, :bears, :lions, :penguins]
Enum.any?(animals, fn animal -> animal == :dogs end)
```

## Enum.count/1
Expand Down Expand Up @@ -656,6 +665,8 @@ In the Elixir cell below, count the number of elements in the `collection`. It s

```elixir
collection = [1, 2, 3, 4, 5]

Enum.count(collection)
```

## Enum.find/3
Expand Down Expand Up @@ -683,7 +694,9 @@ Enum.find(["h", "e", "l", "l", "o"], 10, fn each -> is_integer(each) end)
Use [Enum.find/2](https://hexdocs.pm/elixir/Enum.html#find/2) to find the first even integer in this list.

```elixir
[1, "2", "three", 4, "five", 6]
mylist = [1, "2", "three", 4, "five", 6]

Enum.find(mylist, fn item -> is_integer(item) && rem(item, 2) == 0 end)
```

## Enum.random/1
Expand All @@ -710,7 +723,7 @@ Enum.map(1..10, fn _ -> Enum.random(0..9) end)
Enter your solution below.

```elixir

Enum.random(0..9)
```

## Capture Operator and Module Functions
Expand Down
Loading