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

Ecto changeset reading #84

Open
wants to merge 67 commits into
base: solutions
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
2876b3b
finish start here reading
loteks Jan 25, 2023
6ba1886
added code cell
loteks Jan 25, 2023
14df664
Merge pull request #12 from omnidelic/start-here-reading
loteks Jan 25, 2023
0a2e73c
finish github engineering journal exercise
loteks Feb 1, 2023
8a8ff71
finish github collab exercise
loteks Feb 1, 2023
7542767
finish rock paper scissors exercise
loteks Feb 1, 2023
34a0a16
finish naming numbers exercise
loteks Feb 1, 2023
c981163
finish keyword lists reading
loteks Feb 1, 2023
a55afc6
finish maps reading
loteks Feb 1, 2023
f0a47b9
finish modules reading
loteks Feb 2, 2023
8b3ab22
finish command line reading
loteks Feb 2, 2023
dbd8667
finish rpg dialogue exercise
loteks Feb 2, 2023
73b6e43
finish rock paper scissors lizard spock exercise
loteks Feb 2, 2023
5b672c1
finish ranges reading
loteks Feb 3, 2023
74dbfe7
finish fizzbuzz exercise
loteks Feb 3, 2023
a89e936
finish enum reading
loteks Feb 3, 2023
fa6599b
finish named number lists exercise
loteks Feb 3, 2023
11daeb1
finish counting votes exercise
loteks Feb 6, 2023
f4d0d12
finish animal generator exercise
loteks Feb 6, 2023
fa1f08a
finish palindrome exercise
loteks Feb 6, 2023
5feaba5
finish built-in modules reading
loteks Feb 7, 2023
0123821
finish anagram exercise
loteks Feb 7, 2023
f78fede
finish non enumerables reading
loteks Feb 7, 2023
32173e4
finish filter values by type exercise
loteks Feb 7, 2023
12b63c5
finish tic-tac-toe exercise
loteks Feb 7, 2023
7ba0bb8
finish number finder exercise
loteks Feb 8, 2023
a5d799b
finish weighted voting exercise
loteks Feb 8, 2023
c7a07c9
finish custom enum with reduce exercise
loteks Feb 9, 2023
5df5484
finish time converting exercise
loteks Feb 9, 2023
874ab0a
finish timeline exercise
loteks Feb 9, 2023
3eac094
finish strings and binaries reading
loteks Feb 10, 2023
09cb189
finish email validation exercise
loteks Feb 10, 2023
5ee25d1
finish games setup exercise
loteks Feb 13, 2023
dc4b693
finish games guessing game exercise
loteks Feb 13, 2023
5845d14
finish games rock paper scissors exercise
loteks Feb 13, 2023
d0495ff
finish exunit reading
loteks Feb 14, 2023
8f7c9fe
finish games wordle exercise
loteks Feb 16, 2023
d5b8cf3
finish typespec drills exercise
loteks Feb 16, 2023
aeab815
finish games documentation and static analysis exercise
loteks Feb 16, 2023
a87ae8c
finish typespecs reading
loteks Feb 16, 2023
b703544
finish message validation exercise
loteks Feb 21, 2023
ff5177c
finish with points exercise
loteks Feb 21, 2023
1a23ebb
finish math with guards exercise
loteks Feb 21, 2023
3764b85
finish protocols reading
loteks Feb 22, 2023
172d8c1
finish math with protocols exercise
loteks Feb 22, 2023
8e9b7d9
finish battle map exercise
loteks Feb 22, 2023
3f44dfa
finish fibonacci exercise
loteks Feb 23, 2023
1c2de3b
finish lucas numbers exercise
loteks Feb 23, 2023
4d76bc7
finish file reading
loteks Feb 26, 2023
9471ece
finish save game exercise
loteks Feb 26, 2023
0b4a006
finish file drills exercise
loteks Feb 26, 2023
5a6eb02
finish processes reading
loteks Feb 27, 2023
437d75f
finish stack server exercise
loteks Feb 28, 2023
2fc2261
finish stack exercise
loteks Mar 1, 2023
779c9f4
finish mailbox server exercise
loteks Mar 2, 2023
670241c
finish score tracker exercise
loteks Mar 3, 2023
501b75c
finish timer exercise
loteks Mar 3, 2023
c9ad3d6
finish supervised stack exercise
loteks Mar 6, 2023
c33d4a2
finish dominoes exercise
loteks Mar 6, 2023
38140a1
finish task reading
loteks Mar 7, 2023
3d6cc17
finish capstone project guide reading
loteks Mar 7, 2023
17eb1cf
finish spoonacular recipe api exercise
loteks Mar 9, 2023
69507b2
finish apis reading
loteks Mar 9, 2023
e100a12
finish pokemon api exercise
loteks Mar 10, 2023
fec1c7d
finish web servers reading
loteks Mar 10, 2023
df20e42
finish tailwind reading
loteks Mar 14, 2023
9f1d7cc
finish ecto changeset reading
loteks Mar 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
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
31 changes: 31 additions & 0 deletions exercises/battle_map.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ defprotocol Character do
def can_attack?(character, origin, target)
end

defimpl Character, for: Barbarian do
def can_attack?(_character, {x1, y1}, {x2, y2}) do
# can move +/- 2 spaces along the x axis -> horizontal
# can move +/- 2 spaces along the y axis -> vertical
# horizontal # vertical
abs(x2 - x1) <= 2 or abs(y2 - y1) <= 2
end
end

defimpl Character, for: Wizard do
def can_attack?(_character, {x1, y1}, {x2, y2}) do
# vertical/horizontal # diagonal
x1 == x2 || y1 == y2 || abs(x2 - x1) == abs(y2 - y1)
end
end

ExUnit.start(auto_run: false)

defmodule CharacterTests do
Expand Down Expand Up @@ -198,7 +214,22 @@ true
Implement your custom character below.

```elixir
defmodule Archer do
defstruct []
end
```

```elixir
defimpl Character, for: Archer do
def can_attack?(_character, {x1, y1}, {x2, y2}) do
# can move +/- 3 spaces along the x axis -> horizontal
# can move +/- 3 spaces along the y axis -> vertical
# horizontal # vertical
abs(x2 - x1) == 3 or abs(y2 - y1) == 3
end
end

Character.can_attack?(%Archer{}, {4, 4}, {8, 8})
```

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

@doc """
Search a list of Book structs. Search should match any book that includes the
Expand All @@ -38,6 +39,7 @@ defmodule Book do
## Examples

Include books that exactly match the search query.
# Map.get(map, :title) == query

iex> book1 = %Book{title: "A"}
iex> book2 = %Book{title: "B"}
Expand All @@ -46,6 +48,8 @@ defmodule Book do
[%Book{title: "A"}]

Include books that partially match the search query.
# does the query match any letter of the title?
# use String.contains(book, query)

iex> Book.search([%Book{title: "ABC"}], "A")
[%Book{title: "ABC"}]
Expand All @@ -54,15 +58,36 @@ defmodule Book do
[%Book{title: "BAC"}]

Search should be case insensitive.
# lowercase the input and title

iex> Book.search([%Book{title: "ABC"}], "a")
[%Book{title: "ABC"}]
"""

# def fuzzy_match(string, char) do
# mysplit = String.split(string, "", trim: true)
# Enum.filter(mysplit, fn element -> element == char end) && string
# end

def search(books, query) do
# books that match one letter
Enum.filter(books, fn book -> String.contains?(book, query) end)

# books that exactly match
Enum.filter(books, fn book -> Map.get(book, :title) end)
end
end
```

```elixir
book = "ABC"
query = "D"
String.contains?(book, query)

test = %{mybook: "mytitle"}
Map.get(test, )
```

## Mark As Completed

<!-- livebook:{"attrs":{"source":"file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, \"\"), \".livemd\")\n\nsave_name =\n case Path.basename(__DIR__) do\n \"reading\" -> \"book_search_reading\"\n \"exercises\" -> \"book_search_exercise\"\n end\n\nprogress_path = __DIR__ <> \"/../progress.json\"\nexisting_progress = File.read!(progress_path) |> Jason.decode!()\n\ndefault = Map.get(existing_progress, save_name, false)\n\nform =\n Kino.Control.form(\n [\n completed: input = Kino.Input.checkbox(\"Mark As Completed\", default: default)\n ],\n report_changes: true\n )\n\nTask.async(fn ->\n for %{data: %{completed: completed}} <- Kino.Control.stream(form) do\n File.write!(\n progress_path,\n Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)\n )\n end\nend)\n\nform","title":"Track Your Progress"},"chunks":null,"kind":"Elixir.HiddenCell","livebook_object":"smart_cell"} -->
Expand Down
41 changes: 30 additions & 11 deletions exercises/caesar_cypher.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ defmodule CaesarCypher do

## Examples

iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz")
"bcdefghijklmnopqrstuvwxyza"
# iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz")
# "bcdefghijklmnopqrstuvwxyza"

Encoding should work on any string
# Encoding should work on any string

iex> CaesarCypher.encode("hello")
"ifmmp"
# iex> CaesarCypher.encode("hello")
# "ifmmp"
"""
def encode(string) do
end
Expand All @@ -99,14 +99,14 @@ defmodule CaesarCypher do

## Examples

iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz", 1)
"bcdefghijklmnopqrstuvwxyza"
# iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz", 1)
# "bcdefghijklmnopqrstuvwxyza"

iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz", 2)
"cdefghijklmnopqrstuvwxyzab"
# iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz", 2)
# "cdefghijklmnopqrstuvwxyzab"

iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz", 14)
"opqrstuvwxyzabcdefghijklmn"
# iex> CaesarCypher.encode("abcdefghijklmnopqrstuvwxyz", 14)
# "opqrstuvwxyzabcdefghijklmn"

Encoding should work on any string.

Expand All @@ -118,6 +118,25 @@ defmodule CaesarCypher do
end
```

```elixir
string = "abcdefghijklmnopqrstuvwxyz"
offset = 2

chars = String.to_charlist(string)

Enum.map(chars, fn char -> char + 2 end)

?z - offset

# Enum.map(chars, fn char ->
# if char < 'z' - offset do
# 'z'
# else
# char + 2
# end
# end)
```

## Mark As Completed

<!-- livebook:{"attrs":{"source":"file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, \"\"), \".livemd\")\n\nsave_name =\n case Path.basename(__DIR__) do\n \"reading\" -> \"caesar_cypher_reading\"\n \"exercises\" -> \"caesar_cypher_exercise\"\n end\n\nprogress_path = __DIR__ <> \"/../progress.json\"\nexisting_progress = File.read!(progress_path) |> Jason.decode!()\n\ndefault = Map.get(existing_progress, save_name, false)\n\nform =\n Kino.Control.form(\n [\n completed: input = Kino.Input.checkbox(\"Mark As Completed\", default: default)\n ],\n report_changes: true\n )\n\nTask.async(fn ->\n for %{data: %{completed: completed}} <- Kino.Control.stream(form) do\n File.write!(\n progress_path,\n Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)\n )\n end\nend)\n\nform","title":"Track Your Progress"},"chunks":null,"kind":"Elixir.HiddenCell","livebook_object":"smart_cell"} -->
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
27 changes: 25 additions & 2 deletions exercises/custom_enum_with_reduce.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ defmodule CustomEnum do
[7, 6, 5, 4]
"""
def reverse(list) do
# put first element on head of new list
Enum.reduce(list, [], fn element, acc ->
# IO.inspect(binding())
[element | acc]
end)
end

@doc """
Expand All @@ -92,6 +97,11 @@ defmodule CustomEnum do
[true, true, true]
"""
def map(list, callback_function) do
# while list not empty apply fn to element
Enum.reduce(list, [], fn element, acc ->
[callback_function.(element) | acc]
end)
|> reverse()
end

@doc """
Expand All @@ -107,6 +117,15 @@ defmodule CustomEnum do
["2", "3"]
"""
def filter(list, callback_function) do
# add element to new list if callback_function(element) returns true
Enum.reduce(list, [], fn element, acc ->
if callback_function.(element) do
[element | acc]
else
acc
end
end)
|> reverse()
end

@doc """
Expand All @@ -117,10 +136,12 @@ defmodule CustomEnum do
iex> CustomEnum.sum([1, 2, 3])
6

iex> CustomEnum.sum([1, 1, 1])
3
# iex> CustomEnum.sum([1, 1, 1])
# 3
"""
def sum(list_of_integers) do
# add each int to accumulator and return accumulator
Enum.reduce(list_of_integers, 0, fn int, acc -> int + acc end)
end

@doc """
Expand All @@ -135,6 +156,8 @@ defmodule CustomEnum do
"Hello, World!"
"""
def join(list_of_strings) do
# concat each string to acc string
Enum.reduce(list_of_strings, "", fn string, acc -> acc <> string end)
end
end
```
Expand Down
17 changes: 16 additions & 1 deletion exercises/dominoes.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,22 @@ Supervisor.start_link(children, strategy: :rest_for_one)
Keep in mind, if you have already started a named process, the supervisor might crash when you attempt to start it again. Re-evaluate the cell after the livebook crashes to resolve this issue.

```elixir
children = [
%{
id: D1,
start: {Domino, :start_link, [[name: D1]]}
},
%{
id: D2,
start: {Domino, :start_link, [[name: D2]]}
},
%{
id: D3,
start: {Domino, :start_link, [[name: D3]]}
}
]

{:ok, pid} = Supervisor.start_link(children, strategy: :rest_for_one)
```

Send your dominos messages to ensure they are crashing in the correct order. They will log a message that demonstrates the `Domino.start_link/1` function was called again.
Expand All @@ -116,7 +131,7 @@ Process.send(:domino_name, :fall, [])
Test sending each `Domino` process a message individually.

```elixir

Process.send(D3, :fall, [])
```

## Mark As Completed
Expand Down
32 changes: 31 additions & 1 deletion exercises/drill-patternmatching-replace-nils.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,39 @@ defmodule ReplaceNils do

@doc """
replace nil values in the first list with values from the second list in the same position.

iex> input1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
iex> input2 = [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
iex> ReplaceNils.replace(input1, input2)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

iex> input1 = [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
iex> input2 = [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]
iex> ReplaceNils.replace(input1, input2)
[:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]

iex> input1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
iex> input2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
iex> ReplaceNils.replace(input1, input2)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

iex> input1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
iex> input2 = [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]
iex> ReplaceNils.replace(input1, input2)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

iex> input1 = [1, 2, 3, nil, nil, 6, 7, nil, 9, 10]
iex> input2 = [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]
iex> ReplaceNils.replace(input1, input2)
[1, 2, 3, :d, :e, 6, 7, :h, 9, 10]


"""
def replace(input1, input2) do
nil
Enum.zip_with(input1, input2, fn
nil, y -> y
x, _ -> x
end)
end
end
```
Expand Down
Loading