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

Palindrome exercise #36

Open
wants to merge 20 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
2 changes: 2 additions & 0 deletions exercises/anagram.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ defmodule Anagram do
true
"""
def anagram?(string1, string2) do
String.to_charlist(string1) |> Enum.sort() == String.to_charlist(string2) |> Enum.sort()
end

@doc """
Expand All @@ -84,6 +85,7 @@ defmodule Anagram do
[]
"""
def filter_anagrams(word_list, anagram) do
Enum.filter(word_list, fn word -> anagram?(word, anagram) end)
end
end
```
Expand Down
6 changes: 6 additions & 0 deletions exercises/animal_generator.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ end
Enter your solution below.

```elixir
names = ["Clifford", "Zoboomafoo", "Leonardo"]
animal_types = ["dog", "lemur", "turtle"]
ages = 1..14

for name <- names, animal_type <- animal_types, age <- ages do
%{name: name, animal_type: animal_type, age: age}
end
```

## Mark As Completed
Expand Down
3 changes: 3 additions & 0 deletions exercises/book_search.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ defmodule Book do
iex> %Book{title: "My Book Title"}
%Book{title: "My Book Title"}
"""
@enforce_keys [:title]
defstruct @enforce_keys

@doc """
Search a list of Book structs. Search should match any book that includes the
Expand Down Expand Up @@ -59,6 +61,7 @@ defmodule Book do
[%Book{title: "ABC"}]
"""
def search(books, query) do
Enum.filter(books, fn book -> Book[:title] == query end)
end
end
```
Expand Down
5 changes: 5 additions & 0 deletions exercises/counting_votes.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ defmodule Votes do
0
"""
def count(votes, vote) do
Enum.count(Enum.filter(votes, fn item -> item == vote end))
end
end
```
Expand Down Expand Up @@ -120,6 +121,10 @@ defmodule VoterTally do
%{dog: 2, cat: 3, bird: 1}
"""
def tally(votes) do
dogs = Enum.count(Enum.filter(votes, fn item -> item == :dog end))
cats = Enum.count(Enum.filter(votes, fn item -> item == :cat end))
birds = Enum.count(Enum.filter(votes, fn item -> item == :bird end))
%{dog: dogs, cat: cats, bird: birds}
end
end
```
Expand Down
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
1 change: 1 addition & 0 deletions exercises/palindrome.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ defmodule Palindrome do
false
"""
def palindrome?(string) do
string == String.reverse(string)
end
end
```
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
14 changes: 7 additions & 7 deletions progress.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"agents_and_ets_reading": false,
"protocols_reading": false,
"games_score_tracker_exercise": false,
"habit_tracker_exercise": false,
"habit_tracker_exercise": true,
"reduce_reading": false,
"code_editors_reading": false,
"exdoc_reading": false,
Expand Down Expand Up @@ -58,13 +58,13 @@
"math_with_guards_exercise": false,
"pokemon_api_exercise": false,
"file_search_exercise": false,
"animal_generator_exercise": false,
"animal_generator_exercise": true,
"group_project_blog_exercise": false,
"treasure_matching_exercise": false,
"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 @@ -102,7 +102,7 @@
"rdbms_reading": false,
"fun_formulas_exercise": false,
"rpg_dialogue_exercise": false,
"palindrome_exercise": false,
"palindrome_exercise": true,
"comprehensions_reading": false,
"comments_reading": false,
"rock_paper_scissors_exercise": false,
Expand All @@ -129,7 +129,7 @@
"naming_numbers_exercise": false,
"deployment_exercise": false,
"typespecs_reading": false,
"card_counting_exercise": false,
"card_counting_exercise": true,
"math_with_protocols_exercise": false,
"stack_server_exercise": false,
"pokemon_battle_exercise": false,
Expand All @@ -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
Loading