├── .DS_Store
├── .formatter.exs
├── .gitignore
├── .vscode
└── settings.json
├── LICENSE
├── README.md
├── assets
├── .DS_Store
└── images
│ ├── bs_github_readme_banner.png
│ └── bs_logo.png
├── lib
├── .DS_Store
├── ep10_anonymous_functions
│ └── anonymous.ex
├── ep11_bitstrings
│ └── bitstrings.ex
├── ep12_binaries
│ ├── .DS_Store
│ ├── IMG_9180.HEIC
│ ├── IMG_9254.HEIC
│ ├── IMG_9257.HEIC
│ ├── IMG_9279.HEIC
│ ├── binaries.ex
│ ├── bs_logo.png
│ ├── my_head.jpg
│ └── samplefilehub.heic
├── ep3_numbers
│ ├── basic_math.ex
│ └── calculator.ex
├── ep4_strings
│ └── all_strings.ex
├── ep5_lists
│ └── linked_lists.ex
├── ep6_recursion
│ └── recursion.ex
├── ep7_pattern_matching
│ ├── equipment_details.ex
│ └── saucer_preflight.ex
├── ep8_guards
│ └── guards.ex
└── ep9_control_flow_structures
│ └── control_flow_structures.ex
├── mix.exs
├── mix.lock
└── test
├── ep3_numbers_test
├── basic_math_test.exs
└── calculater_test.exs
├── ep4_strings_test
└── all_strings_test.exs
├── ep5_linked_lists_test
└── linked_lists_test.exs
├── ep6_recursion_test
└── recursion_test.exs
├── ep7_pattern_matching_test
├── equipment_details_test.exs
└── saucer_preflight_test.exs
└── test_helper.exs
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/.DS_Store
--------------------------------------------------------------------------------
/.formatter.exs:
--------------------------------------------------------------------------------
1 | # Used by "mix format"
2 | [
3 | inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4 | ]
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # The directory Mix will write compiled artifacts to.
2 | /_build/
3 |
4 | # If you run "mix test --cover", coverage assets end up here.
5 | /cover/
6 |
7 | # The directory Mix downloads your dependencies sources to.
8 | /deps/
9 |
10 | # Where third-party dependencies like ExDoc output generated docs.
11 | /doc/
12 |
13 | # Ignore .fetch files in case you like to edit your project deps locally.
14 | /.fetch
15 |
16 | # If the VM crashes, it generates a dump, let's ignore it too.
17 | erl_crash.dump
18 |
19 | # Also ignore archive artifacts (built via "mix archive.build").
20 | *.ez
21 |
22 | # Ignore package tarball (built via "mix hex.build").
23 | elixir_basics_solutions-*.tar
24 |
25 | # Temporary files, for example, from tests.
26 | /tmp/
27 |
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "workbench.preferredLightColorTheme": "Default Dark+"
3 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2 | Version 3, May 2010
3 |
4 | Copyright (C) 2023 by Elixir Mentor
5 | Springville, UT USA
6 |
7 | Everyone is permitted to copy and distribute verbatim or modified
8 | copies of this license document, and changing it is allowed as long
9 | as the name is changed.
10 |
11 | This license applies to any copyrightable work with which it is
12 | packaged and/or distributed, except works that are already covered by
13 | another license. Any other license that applies to the same work
14 | shall take precedence over this one.
15 |
16 | To the extent permitted by applicable law, the works covered by this
17 | license are provided "as is" and do not come with any warranty except
18 | where otherwise explicitly stated.
19 |
20 |
21 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
22 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION, AND MODIFICATION
23 |
24 | 0. You just DO WHAT THE FUCK YOU WANT TO.
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # ELIXIR BASICS SOLUTIONS
4 | Subscribe to [Elixir Mentor](https://www.youtube.com/channel/UChbS_z6KHQiIu9et38O37eQ) if you're interested in learning how to build scalable, production ready APIs in Elixir and some DevOps along the way, please subscribe to my channel Backend Stuff, and start your backend development journey.
5 |
6 | You will find the solutions for [Elixir Basics](https://youtube.com/playlist?list=PL2Rv8vpZJz4w7Sm9STyZvoY0JAKUk_JOB) here. Every episode has it's own directory inside the `/lib` directory with documentation and written tests found in the `/test` directory.
7 |
8 | #### INSTALL DEPENDENCIES
9 | ```
10 | mix deps.get
11 | ```
12 |
13 | #### GENERATE DOCUMENTATION
14 | ```
15 | mix docs
16 | ```
17 | You will find generated `html` files and a `.epub` file in `/doc` directory.
18 |
19 | ## SUPPORT ELIXIR MENTOR
20 |
21 | 🌐🌐 My website [Elixir Mentor](https://elixirmentor.com/)
22 |
23 | 🎙🎙 Check out my podcast [Big App Energy](https://www.hiredgunapps.com/podcast)
24 |
25 | 🆘🆘 NEED HELP?? Join the [Discord Server](https://discord.gg/HcnjPsWATg)
26 |
27 | ## FOLLOW ME
28 | Linktree: [@jacob_luetzow](https://linktr.ee/jacob_luetzow)
29 |
30 | Join the [Discord server](https://discord.gg/HcnjPsWATg)
31 |
--------------------------------------------------------------------------------
/assets/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/assets/.DS_Store
--------------------------------------------------------------------------------
/assets/images/bs_github_readme_banner.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/assets/images/bs_github_readme_banner.png
--------------------------------------------------------------------------------
/assets/images/bs_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/assets/images/bs_logo.png
--------------------------------------------------------------------------------
/lib/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/lib/.DS_Store
--------------------------------------------------------------------------------
/lib/ep10_anonymous_functions/anonymous.ex:
--------------------------------------------------------------------------------
1 | defmodule Anonymous do
2 | def get_equipment_list() do
3 | [:space_helmet, :space_suit, :snacks, :grappling_hook, :probe]
4 | end
5 |
6 | def item_details(:space_helmet) do
7 | {3, :kg, 1}
8 | end
9 |
10 | def item_details(:space_suit) do
11 | {16, :kg, 1}
12 | end
13 |
14 | def item_details(:snacks) do
15 | {1, :kg, 16}
16 | end
17 |
18 | def item_details(:grappling_hook) do
19 | {4, :kg, 1}
20 | end
21 |
22 | def item_details(:probe) do
23 | {2, :lb, 1}
24 | end
25 |
26 | #calling an anonymous function uses a period before ()
27 | # pattern matching the arguments works too
28 | def get_first_item(list) do
29 | first = fn [head | _rest] -> head end
30 | first.(list)
31 | end
32 |
33 | #anonymous functions are commonly used as arguments to other functions
34 | def atom_to_string(list) do
35 | convert = fn x -> to_string(x) |> String.upcase() |> String.replace("_", " ") end
36 | Enum.map(list, convert)
37 | end
38 |
39 | def get_weight_lbs(list) do
40 | get_lbs = fn item ->
41 | {wt, type, _qty} = item_details(item)
42 | case type do
43 | :kg -> wt * 2.2
44 | _ -> wt
45 | end
46 | end
47 |
48 | Enum.map(list, get_lbs)
49 | end
50 |
51 | end
52 |
--------------------------------------------------------------------------------
/lib/ep11_bitstrings/bitstrings.ex:
--------------------------------------------------------------------------------
1 | defmodule Bitstrings do
2 |
3 | # fly base FB
4 | # fly earth FE
5 | # hover H
6 | # tractor_beam_on T
7 | # tractor_beam_off O
8 | def encode_instruction(code_point) do
9 | case code_point do
10 | ?H -> 0b0000
11 | ?F -> 0b0001
12 | ?B -> 0b0010
13 | ?E -> 0b0100
14 | ?T -> 0b1000
15 | ?O -> 0b1001
16 | end
17 | end
18 |
19 | def decode_instution(encoded_code) do
20 | case encoded_code do
21 | 0b0000 -> ?H
22 | 0b0001 -> ?F
23 | 0b0010 -> ?B
24 | 0b0100 -> ?E
25 | 0b1000 -> ?T
26 | 0b1001 -> ?O
27 | end
28 | end
29 |
30 | def encode(instructions, encoded_value \\ <<0::size(0)>>)
31 | def encode([], encoded_value), do: encoded_value
32 | def encode([head | tail], encoded_value) do
33 | encode(tail, <>)
34 | end
35 |
36 | def decode(instuctions, decoded_value \\ '')
37 | def decode(<<0::0>>, decoded_value), do: decoded_value
38 | def decode(<>, decoded_value) do
39 | decode(tail, decoded_value ++ [decode_instution(head)])
40 | end
41 |
42 | end
43 |
--------------------------------------------------------------------------------
/lib/ep12_binaries/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/lib/ep12_binaries/.DS_Store
--------------------------------------------------------------------------------
/lib/ep12_binaries/IMG_9180.HEIC:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/lib/ep12_binaries/IMG_9180.HEIC
--------------------------------------------------------------------------------
/lib/ep12_binaries/IMG_9254.HEIC:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/lib/ep12_binaries/IMG_9254.HEIC
--------------------------------------------------------------------------------
/lib/ep12_binaries/IMG_9257.HEIC:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/lib/ep12_binaries/IMG_9257.HEIC
--------------------------------------------------------------------------------
/lib/ep12_binaries/IMG_9279.HEIC:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/lib/ep12_binaries/IMG_9279.HEIC
--------------------------------------------------------------------------------
/lib/ep12_binaries/binaries.ex:
--------------------------------------------------------------------------------
1 | defmodule Binaries do
2 |
3 | def get_png() do
4 | File.read!(Path.join("lib/ep12_binaries", "bs_logo.png"))
5 | end
6 |
7 | def get_jpg() do
8 | File.read!(Path.join("lib/ep12_binaries", "my_head.jpg"))
9 | end
10 |
11 | def get_heic() do
12 | File.read!(Path.join("lib/ep12_binaries", "IMG_9279.HEIC"))
13 | end
14 |
15 | @bmp_type "image/bmp"
16 | @jpg_type "image/jpeg"
17 | @png_type "image/png"
18 | @heic_type "image/heic"
19 | @unsupported "unsupported"
20 |
21 | def type_from_binary(file_binary) when binary_part(file_binary, 0, 8) == <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>>, do: {:ok, "image/png"}
22 | def type_from_binary(file_binary) when binary_part(file_binary, 0, 3) == <<0xFF, 0xD8, 0xFF>>, do: {:ok, "image/jpg"}
23 | def type_from_binary(_file_binary), do: {:error, "unknown"}
24 |
25 | def type_from_binary_pm(<<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, _rest::bitstring>>), do: {:ok, "image/png"}
26 | def type_from_binary_pm(<<_ignore::binary-size(4), 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x69, 0x66,
27 | 0x31, _body::bitstring>>), do: @heic_type
28 | def type_from_binary_pm(_), do: {:error, "unknown"}
29 |
30 |
31 | end
32 |
--------------------------------------------------------------------------------
/lib/ep12_binaries/bs_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/lib/ep12_binaries/bs_logo.png
--------------------------------------------------------------------------------
/lib/ep12_binaries/my_head.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/lib/ep12_binaries/my_head.jpg
--------------------------------------------------------------------------------
/lib/ep12_binaries/samplefilehub.heic:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ElixirMentor/elixir_basics_solutions/961fa8c39306f00df6ff75c12c73c3be3561b5e9/lib/ep12_binaries/samplefilehub.heic
--------------------------------------------------------------------------------
/lib/ep3_numbers/basic_math.ex:
--------------------------------------------------------------------------------
1 | defmodule BasicMath do
2 | @moduledoc """
3 | `BasicMath` was created for episode 3 in Elixir Basics.
4 | """
5 |
6 | @doc """
7 | Returns the sum of two values `addend_1` and `addend_2`.
8 |
9 | Returns `number`
10 |
11 | ## Parameters
12 |
13 | - addend_1: number (integer or float)
14 | - addend_2: number (integer or float)
15 |
16 | ## Examples
17 |
18 | iex> BasicMath.add(4, 5)
19 | 9
20 |
21 | iex> BasicMath.add(4.6, 8.9)
22 | 13.5
23 |
24 | """
25 | @doc since: "0.1.0"
26 | def add(addend_1, addend_2) do
27 | addend_1 + addend_2
28 | end
29 |
30 | @doc """
31 | Returns the difference of two values `minuend` and `subtrahend`.
32 |
33 | Returns `number`
34 |
35 | ## Parameters
36 |
37 | - minuend: number (integer or float)
38 | - subtrahend: number (integer or float)
39 |
40 | ## Examples
41 |
42 | iex> BasicMath.subtract(4, 5)
43 | -1
44 |
45 | iex> BasicMath.subtract(33.3, 8.9)
46 | 24.4
47 |
48 | """
49 | @doc since: "0.1.0"
50 | def subtract(minuend, subtrahend) do
51 | minuend - subtrahend
52 | end
53 |
54 | @doc """
55 | Returns the product of two values `multiplier` and `multplicand`.
56 |
57 | Returns `number`
58 |
59 | ## Parameters
60 |
61 | - multiplier: number (integer or float)
62 | - multplicand: number (integer or float)
63 |
64 | ## Examples
65 |
66 | iex> BasicMath.multiply(4, 5)
67 | 20
68 |
69 | iex> BasicMath.multiply(33.3, 8.9)
70 | 296.37
71 |
72 | """
73 | @doc since: "0.1.0"
74 | def multiply(multiplier, multplicand) do
75 | multiplier * multplicand
76 | end
77 |
78 | @doc """
79 | Returns the quotient of two values `dividend` and `divisor`.
80 |
81 | Returns `number`
82 |
83 | ## Parameters
84 |
85 | - dividend: number (integer or float)
86 | - divisor: number (integer or float)
87 |
88 | ## Examples
89 |
90 | iex> BasicMath.divide(4, 5)
91 | 0.8
92 |
93 | iex> BasicMath.divide(33.3, 8.9)
94 | 3.7415730337078648
95 |
96 | """
97 | @doc since: "0.1.0"
98 | def divide(dividend, divisor) do
99 | dividend / divisor
100 | end
101 |
102 | @doc """
103 | Returns the absolute value of given `value`.
104 |
105 | Returns `number`
106 |
107 | ## Parameters
108 |
109 | - value: number (integer or float)
110 |
111 | ## Examples
112 |
113 | iex> BasicMath.absolute_value(-5.6)
114 | 5.6
115 |
116 | iex> BasicMath.absolute_value(68)
117 | 68
118 |
119 | """
120 | @doc since: "0.1.0"
121 | def absolute_value(value) do
122 | abs(value)
123 | end
124 |
125 | @doc """
126 | Returns a value rounded up to the nearest 100th place of given `value`.
127 |
128 | Returns `number`
129 |
130 | ## Parameters
131 |
132 | - value: number (integer or float)
133 |
134 | ## Examples
135 |
136 | iex> BasicMath.round_up(5.66667)
137 | 5.67
138 |
139 | iex> BasicMath.round_up(-33.333334)
140 | -33.33
141 |
142 | """
143 | @doc since: "0.1.0"
144 | def round_up(value) do
145 | if is_integer(value) == true do
146 | round(value)
147 | else
148 | Float.ceil(value, 2)
149 | end
150 | end
151 |
152 | @doc """
153 | Returns integer part of given `value`.
154 |
155 | Returns `number`
156 |
157 | ## Parameters
158 |
159 | - value: number (integer or float)
160 |
161 | ## Examples
162 |
163 | iex> BasicMath.truncate_value(5.66667)
164 | 5
165 |
166 | iex> BasicMath.truncate_value(-33.333334)
167 | -33
168 |
169 | """
170 | @doc since: "0.1.0"
171 | def truncate_value(value) do
172 | trunc(value)
173 | end
174 | end
175 |
--------------------------------------------------------------------------------
/lib/ep3_numbers/calculator.ex:
--------------------------------------------------------------------------------
1 | defmodule Calculator do
2 | @moduledoc """
3 | `Calculator` was created for episode 3 in Elixir Basics.
4 | """
5 |
6 | @doc """
7 | Returns the `percentage` of given `value` rounded up to the nearest 100th.
8 |
9 | Returns `float`
10 |
11 | ## Parameters
12 |
13 | - percentage: number (integer or float)
14 | - value: number (integer or float)
15 |
16 | ## Examples
17 |
18 | iex> Calculator.find_percentage_of_value(25, 100)
19 | 25.0
20 |
21 | iex> Calculator.find_percentage_of_value(23, 456)
22 | 104.89
23 |
24 | """
25 | def find_percentage_of_value(percentage, value) do
26 | percentage
27 | |> BasicMath.divide(100)
28 | |> BasicMath.multiply(value)
29 | |> BasicMath.round_up()
30 | end
31 |
32 | @doc """
33 | Returns the `distance` traveled when given `speed` and `time`.
34 |
35 | Returns `number`
36 |
37 | ## Parameters
38 |
39 | - speed: number (integer or float)
40 | - time: number (integer or float)
41 |
42 | ## Examples
43 |
44 | iex> Calculator.find_distance_traveled(30, 2)
45 | 60
46 |
47 | iex> Calculator.find_distance_traveled(67, 2.8)
48 | 187.6
49 |
50 | """
51 | def find_distance_traveled(speed, time) do
52 | # Code challenge -
53 | # Make sure to use the pipe operator and exisiting functions we already created to solve this.
54 | # Given the formula `Speed = Distance / Time`
55 | # Calculate the `distance` traveled when given the `speed` and `time`
56 | # Round the value up to the nearest 100th.
57 | speed
58 | |> BasicMath.multiply(time)
59 | |> BasicMath.round_up()
60 | end
61 | end
62 |
--------------------------------------------------------------------------------
/lib/ep4_strings/all_strings.ex:
--------------------------------------------------------------------------------
1 | defmodule AllStrings do
2 | @moduledoc """
3 | `AllStrings` was created for episode 4 in Elixir Basics.
4 | """
5 |
6 | @doc """
7 | Returns the `first_letter` of given `name` with leading and trailing spaces removed.
8 |
9 | Returns `string`
10 |
11 | ## Parameters
12 |
13 | - name: string
14 |
15 | ## Examples
16 |
17 | iex> AllStrings.first_letter("blork")
18 | "b"
19 |
20 | iex> AllStrings.first_letter("Blork ")
21 | "B"
22 |
23 | """
24 | def first_letter(name) do
25 | String.trim(name)
26 | |> String.first()
27 | end
28 |
29 | @doc """
30 | Returns the `first_letter` of given `name` capitalized with a . and space after.
31 |
32 | Returns `string`
33 |
34 | ## Parameters
35 |
36 | - name: string
37 |
38 | ## Examples
39 |
40 | iex> AllStrings.initial("blork")
41 | "B. "
42 |
43 | iex> AllStrings.initial("Blork ")
44 | "B. "
45 |
46 | """
47 | def initial(name) do
48 | "#{String.capitalize(name) |> first_letter()}. "
49 | end
50 |
51 | @doc """
52 | Returns the `intials` of given `full_name` capitalized with a . and space after each letter except last.
53 |
54 | Returns `string`
55 |
56 | ## Parameters
57 |
58 | - full_name: string
59 |
60 | ## Examples
61 |
62 | iex> AllStrings.initials("blork erlang")
63 | "B. E."
64 |
65 | iex> AllStrings.initials("Blork erland jr")
66 | "B. E. J."
67 |
68 | """
69 | def initials(full_name) do
70 | list = String.split(full_name)
71 | Enum.map(list, fn name ->
72 | initial(name)
73 | end)
74 | |> List.to_string()
75 | |> String.trim()
76 | end
77 |
78 | @doc """
79 | Returns a flying saucer string.
80 |
81 | Returns `string`
82 |
83 | ## Examples
84 |
85 | iex> AllStrings.build_saucer()
86 | ~s(\\n %%%%%%%%%%%%%%%%%%%%\\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\\n************************************************************\\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\\n ,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,\\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\\n\\n)
87 |
88 | """
89 | def build_saucer() do
90 | """
91 |
92 | %%%%%%%%%%%%%%%%%%%%
93 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 | ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
96 | ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
97 | ************************************************************
98 | ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
99 | ,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,
100 | ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
101 |
102 | """
103 | end
104 |
105 | @doc """
106 | Returns the `intials`'s SPACECRAFT of given `full_name` capitalized with a . and space after each letter except last.
107 |
108 | Returns `string`
109 |
110 | ## Parameters
111 |
112 | - full_name: string
113 |
114 | ## Examples
115 |
116 | iex> AllStrings.customized_spacecraft("blork erlang")
117 | ~s(\\n %%%%%%%%%%%%%%%%%%%%\\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\\n******************** B. E.'s SPACECRAFT *******************\\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\\n ,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,\\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\\n\\n)
118 |
119 | """
120 | def customized_spacecraft(full_name) do
121 | spacecraft_label = "******************** #{initials(full_name)}'s SPACECRAFT *******************"
122 | "#{build_saucer()}"
123 | |> String.replace("************************************************************", spacecraft_label)
124 | end
125 |
126 | @doc """
127 | Returns a flying saucer output.
128 |
129 | Returns `atom`
130 |
131 | ## Examples
132 |
133 | iex> AllStrings.display_saucer()
134 | :ok
135 |
136 | """
137 | def display_saucer() do
138 | IO.puts(build_saucer())
139 | end
140 |
141 | @doc """
142 | Returns a `intials`'s SPACECRAFT of given `full_name` capitalized with a . output.
143 |
144 | Returns `atom`
145 |
146 | ## Parameters
147 |
148 | - full_name: string
149 |
150 | ## Examples
151 |
152 | iex> AllStrings.display_custom_spacecraft("blork erlang")
153 | :ok
154 |
155 | """
156 | def display_custom_spacecraft(full_name) do
157 | IO.puts(customized_spacecraft(full_name))
158 | end
159 | end
160 |
--------------------------------------------------------------------------------
/lib/ep5_lists/linked_lists.ex:
--------------------------------------------------------------------------------
1 | defmodule LinkedLists do
2 | @moduledoc """
3 | `LinkedLists` was created for episode 5 in Elixir Basics.
4 | """
5 |
6 | @doc """
7 | Returns the `equipment_list`.
8 |
9 | Returns `list` of `strings`
10 |
11 | ## Examples
12 |
13 | iex> LinkedLists.get_equipment_list()
14 | ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]
15 |
16 | """
17 | def get_equipment_list() do
18 | ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]
19 | end
20 |
21 | @doc """
22 | Returns the first element of given `list`.
23 |
24 | Returns `element`
25 |
26 | ## Parameters
27 |
28 | - list: list
29 |
30 | ## Examples
31 |
32 | iex> LinkedLists.get_first_item(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"])
33 | "Space Helmet"
34 |
35 | """
36 | def get_first_item(list) do
37 | hd(list)
38 | end
39 |
40 | @doc """
41 | Returns the tail of a `list`.
42 |
43 | Returns `element`
44 |
45 | ## Parameters
46 |
47 | - list: list
48 |
49 | ## Examples
50 |
51 | iex> LinkedLists.remove_first_item(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"])
52 | ["Space Suit", "Snacks", "Grapling Hook", "Probe"]
53 |
54 | """
55 | def remove_first_item(list) do
56 | tl(list)
57 | end
58 |
59 | @doc """
60 | Returns a `list` with appended new `value` at end.
61 |
62 | Returns `list`
63 |
64 | ## Parameters
65 |
66 | - value: any
67 | - list: list
68 |
69 | ## Examples
70 |
71 | iex> LinkedLists.add_slow("new value", ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"])
72 | ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe", "new value"]
73 |
74 | """
75 | def add_slow(value, list) do
76 | list ++ [value]
77 | end
78 |
79 | @doc """
80 | Returns a `list` with appended new `value` at end.
81 |
82 | Returns `list`
83 |
84 | ## Parameters
85 |
86 | - value: any
87 | - list: list
88 |
89 | ## Examples
90 |
91 | iex> LinkedLists.add_fast("new value", ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"])
92 | ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe", "new value"]
93 |
94 | """
95 | def add_fast(value, list) do
96 | list
97 | |> Enum.reverse()
98 | |> prepend(value)
99 | |> Enum.reverse()
100 | end
101 |
102 | @doc """
103 | Returns a `list` with a `value` removed at specified `index`.
104 |
105 | Returns `list`
106 |
107 | ## Parameters
108 |
109 | - list: list
110 | - index: integer
111 |
112 | ## Examples
113 |
114 | iex> LinkedLists.remove_item(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], 2)
115 | ["Space Helmet", "Space Suit", "Grapling Hook", "Probe"]
116 |
117 | """
118 | def remove_item(list, index) do
119 | List.delete_at(list, index)
120 | end
121 |
122 | @doc """
123 | Returns a `length` of a `list`.
124 |
125 | Returns `integer`
126 |
127 | ## Parameters
128 |
129 | - list: list
130 |
131 | ## Examples
132 |
133 | iex> LinkedLists.equipment_count(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"])
134 | 5
135 |
136 | """
137 | def equipment_count(list) do
138 | length(list)
139 | end
140 |
141 | @doc """
142 | Returns a `boolean` if `item` exists in `list`.
143 |
144 | Returns `boolean`
145 |
146 | ## Parameters
147 |
148 | - list: list
149 | - item: any
150 |
151 | ## Examples
152 |
153 | iex> LinkedLists.check_for_item(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "Space Helmet")
154 | true
155 |
156 | iex> LinkedLists.check_for_item(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "vitamins")
157 | false
158 |
159 | """
160 | def check_for_item(list, item) do
161 | item in list
162 | end
163 |
164 | defp prepend(list, value) do
165 | [value | list]
166 | end
167 | end
168 |
--------------------------------------------------------------------------------
/lib/ep6_recursion/recursion.ex:
--------------------------------------------------------------------------------
1 | defmodule Recursion do
2 | @moduledoc """
3 | `Recursion` was created for episode 6 in Elixir Basics.
4 | """
5 |
6 | @doc """
7 | Returns the `equipment_list`.
8 |
9 | Returns `list` of `strings`
10 |
11 | ## Examples
12 |
13 | iex> Recursion.get_equipment_list()
14 | ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]
15 |
16 | """
17 | def get_equipment_list() do
18 | ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]
19 | end
20 |
21 | def loop([]), do: nil
22 | def loop([head | tail]) do
23 | #Do something with head
24 | IO.puts(head)
25 | loop(tail)
26 | end
27 |
28 | @doc """
29 | Returns a `length` of a `list`.
30 |
31 | Returns `integer`
32 |
33 | ## Parameters
34 |
35 | - list: list
36 |
37 | ## Examples
38 |
39 | iex> Recursion.equipment_count(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"])
40 | 5
41 |
42 | """
43 | def equipment_count([]), do: 0
44 | def equipment_count([_head | tail]), do: 1 + equipment_count(tail)
45 |
46 |
47 | @doc """
48 | Returns a formatted list.
49 |
50 | Returns `list`
51 |
52 | ## Parameters
53 |
54 | - list: list
55 |
56 | ## Examples
57 |
58 | iex> Recursion.format_equipment_list(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"])
59 | ["space_helmet", "space_suit", "snacks", "grapling_hook", "probe"]
60 |
61 | """
62 | def format_equipment_list([]), do: []
63 | def format_equipment_list([head | tail]) do
64 | [head |> format_string() | format_equipment_list(tail)]
65 | end
66 |
67 | @doc """
68 | Returns a formatted list.
69 |
70 | Returns `list`
71 |
72 | ## Parameters
73 |
74 | - list: list
75 |
76 | ## Examples
77 |
78 | iex> Recursion.format_equipment_list_map(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"])
79 | ["space_helmet", "space_suit", "snacks", "grapling_hook", "probe"]
80 |
81 | """
82 | def format_equipment_list_map(list) do
83 | Enum.map(list, fn x ->
84 | x
85 | |> String.downcase()
86 | |> String.replace(" ", "_")
87 | end)
88 | end
89 |
90 | @doc """
91 | Returns a `count` of how many occurrences of a value appear in a `list`.
92 |
93 | Returns `integer`
94 |
95 | ## Parameters
96 |
97 | - list: list
98 | - value: any
99 |
100 | ## Examples
101 |
102 | iex> Recursion.occurrence_count(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "Space Helmet")
103 | 1
104 |
105 | iex> Recursion.occurrence_count(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "Snacks")
106 | 400
107 |
108 | iex> Recursion.occurrence_count(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "Space ")
109 | 0
110 |
111 | """
112 | def occurrence_count([], _value), do: 0
113 | def occurrence_count([_head | _tail], "Snacks"), do: 400
114 | def occurrence_count([value | tail], value), do: 1 + occurrence_count(tail, value)
115 | def occurrence_count([_head | tail], value), do: occurrence_count(tail, value)
116 |
117 | @doc """
118 | Returns a formatted `list` of values that contain part of a `value`.
119 |
120 | Returns `list`
121 |
122 | ## Parameters
123 |
124 | - list: list
125 | - value: any
126 |
127 | ## Examples
128 |
129 | iex> Recursion.partial_occurrence(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "Space Helmet")
130 | ["space_helmet"]
131 |
132 | iex> Recursion.partial_occurrence(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "space")
133 | ["space_helmet", "space_suit"]
134 |
135 | iex> Recursion.partial_occurrence(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "lame")
136 | []
137 |
138 | """
139 | def partial_occurrence([], _value), do: []
140 | def partial_occurrence([head | tail], value) do
141 | if String.contains?(format_string(head), format_string(value)) do
142 | [format_string(head) | partial_occurrence(tail, value)]
143 | else
144 | partial_occurrence(tail, value)
145 | end
146 | end
147 |
148 | defp format_string(value) do
149 | value |> String.downcase() |> String.replace(" ", "_")
150 | end
151 |
152 | end
153 |
--------------------------------------------------------------------------------
/lib/ep7_pattern_matching/equipment_details.ex:
--------------------------------------------------------------------------------
1 | defmodule EquipmentDetails do
2 |
3 | def get_equipment_list() do
4 | [:space_helmet, :space_suit, :snacks, :grappling_hook, :probe]
5 | end
6 |
7 | def item_details(:space_helmet) do
8 | {3, :kg, 1}
9 | end
10 |
11 | def item_details(:space_suit) do
12 | {16, :kg, 1}
13 | end
14 |
15 | def item_details(:snacks) do
16 | {1, :kg, 16}
17 | end
18 |
19 | def item_details(:grappling_hook) do
20 | {4, :kg, 1}
21 | end
22 |
23 | def item_details(:probe) do
24 | {2, :kg, 1}
25 | end
26 |
27 | def item_details(_other) do
28 | raise "unknown item"
29 | end
30 | end
31 |
--------------------------------------------------------------------------------
/lib/ep7_pattern_matching/saucer_preflight.ex:
--------------------------------------------------------------------------------
1 | defmodule SaucerPreflight do
2 |
3 | defp max_flying_load_lbs, do: 55
4 |
5 | defp convert_kg_to_lb(kg_value), do: kg_value * 2.2
6 |
7 | def get_total_load([]), do: 0
8 | def get_total_load([head | tail]) do
9 | (head
10 | |> EquipmentDetails.item_details()
11 | |> elem(0)
12 | |> convert_kg_to_lb()
13 | ) + get_total_load(tail)
14 | end
15 |
16 | def is_under_max_load?(list) do
17 | final_weight = get_total_load(list)
18 | if final_weight < max_flying_load_lbs() do
19 | true
20 | else
21 | false
22 | end
23 | end
24 | end
25 |
--------------------------------------------------------------------------------
/lib/ep8_guards/guards.ex:
--------------------------------------------------------------------------------
1 | defmodule Guards do
2 |
3 | def type(value) when is_integer(value), do: :integer
4 | def type(value) when is_float(value), do: :float
5 | def type(value) when not is_number(value), do: :not_number
6 |
7 | def is_single_digit(value) when is_integer(value) and value < 10, do: true
8 |
9 | defguard is_even(value) when is_integer(value) and rem(value, 2) == 0
10 |
11 | #define default values in headers
12 | def return_even_numbers(value \\ :not_even)
13 | def return_even_numbers(value) when is_even(value), do: value
14 | def return_even_numbers(value) when not is_even(value), do: :not_even
15 |
16 | def is_under_max_load?(load, max_load \\ 55)
17 | def is_under_max_load?(load, max_load) when is_number(load) and load <= max_load, do: true
18 | def is_under_max_load?(load, max_load) when not is_number(load) or load > max_load, do: false
19 |
20 | end
21 |
--------------------------------------------------------------------------------
/lib/ep9_control_flow_structures/control_flow_structures.ex:
--------------------------------------------------------------------------------
1 | defmodule ControlFlowStructures do
2 |
3 | def check_max_flying_load(load) do
4 | if load < 55, do: "Safe to fly!", else: "Too heavy to fly!"
5 | # unless load < 55, do: "Too heavy to fly!", else: "Safe to fly!"
6 | end
7 |
8 | def check_fuel_level(percentage) do
9 | cond do
10 | percentage == 100 -> "full tank"
11 | percentage > 75 -> "3/4 tank"
12 | percentage > 49 -> "1/2 tank"
13 | percentage > 24 -> "1/4 tank"
14 | true -> "empty tank"
15 | end
16 | end
17 |
18 | def error_code_check(value) do
19 | case value do
20 | 200 -> :ok
21 | 201 -> :created
22 | 204 -> :no_content
23 | n when is_integer(n) and n > 299 -> :error
24 | _ -> :unknown
25 | end
26 | end
27 |
28 | defp convert_lb_to_kg(lb_value), do: lb_value / 2.2
29 |
30 | def equipment_check(equipment_tuple) do
31 | case equipment_tuple do
32 | {weight, unit_type, quantity} when weight / quantity < 16 and unit_type == :kg -> :equipment_cleared
33 | {weight, unit_type, quantity} when unit_type == :lb ->
34 | if convert_lb_to_kg(weight) / quantity < 16, do: :equipment_cleared, else: :failed
35 | _ -> :failed
36 | end
37 | end
38 | end
39 |
--------------------------------------------------------------------------------
/mix.exs:
--------------------------------------------------------------------------------
1 | defmodule ElixirBasicsSolutions.MixProject do
2 | use Mix.Project
3 |
4 | def project do
5 | [
6 | app: :elixir_basics_solutions,
7 | version: "0.1.0",
8 | elixir: "~> 1.13",
9 | start_permanent: Mix.env() == :prod,
10 | deps: deps(),
11 | # Docs
12 | name: "Elixir Basics Solutions",
13 | source_url: "https://github.com/jacobluetzow/elixir_basics_solutions",
14 | homepage_url: "https://linktr.ee/jacob_luetzow",
15 | docs: [
16 | main: "Elixir Basics Solutions",
17 | logo: "assets/images/bs_logo.png",
18 | extras: ["README.md"]
19 | ]
20 | ]
21 | end
22 |
23 | # Run "mix help compile.app" to learn about applications.
24 | def application do
25 | [
26 | extra_applications: [:logger]
27 | ]
28 | end
29 |
30 | # Run "mix help deps" to learn about dependencies.
31 | defp deps do
32 | [
33 | {:ex_doc, "~> 0.27", only: :dev, runtime: false}
34 | ]
35 | end
36 | end
37 |
--------------------------------------------------------------------------------
/mix.lock:
--------------------------------------------------------------------------------
1 | %{
2 | "earmark_parser": {:hex, :earmark_parser, "1.4.26", "f4291134583f373c7d8755566122908eb9662df4c4b63caa66a0eabe06569b0a", [:mix], [], "hexpm", "48d460899f8a0c52c5470676611c01f64f3337bad0b26ddab43648428d94aabc"},
3 | "ex_doc": {:hex, :ex_doc, "0.28.5", "3e52a6d2130ce74d096859e477b97080c156d0926701c13870a4e1f752363279", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "d2c4b07133113e9aa3e9ba27efb9088ba900e9e51caa383919676afdf09ab181"},
4 | "makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
5 | "makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
6 | "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
7 | "nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
8 | }
9 |
--------------------------------------------------------------------------------
/test/ep3_numbers_test/basic_math_test.exs:
--------------------------------------------------------------------------------
1 | defmodule BasicMathTest do
2 | use ExUnit.Case
3 | doctest BasicMath
4 |
5 | describe "add/2" do
6 | test "returns correct sum" do
7 | assert BasicMath.add(4, 5) == 9
8 | assert BasicMath.add(4.3, 89.4) == 93.7
9 | end
10 | end
11 |
12 | describe "subtract/2" do
13 | test "returns correct difference" do
14 | assert BasicMath.subtract(4, 5) == -1
15 | assert BasicMath.subtract(33.3, 8.9) == 24.4
16 | end
17 | end
18 |
19 | describe "multiply/2" do
20 | test "returns correct product" do
21 | assert BasicMath.multiply(4, 5) == 20
22 | assert BasicMath.multiply(33.3, 8.9) == 296.37
23 | end
24 | end
25 |
26 | describe "divide/2" do
27 | test "returns correct quotient" do
28 | assert BasicMath.divide(4, 5) == 0.8
29 | assert BasicMath.divide(33.3, 8.9) == 3.7415730337078648
30 | end
31 | end
32 |
33 | describe "absolute_value/1" do
34 | test "returns correct absolute value" do
35 | assert BasicMath.absolute_value(-5.6) == 5.6
36 | assert BasicMath.absolute_value(68) == 68
37 | end
38 | end
39 |
40 | describe "round_up/1" do
41 | test "returns correct rounded value" do
42 | assert BasicMath.round_up(5.66667) == 5.67
43 | assert BasicMath.round_up(-33.333334) == -33.33
44 | end
45 | end
46 |
47 | describe "truncate_value/1" do
48 | test "returns correct integer value" do
49 | assert BasicMath.truncate_value(5.66667) == 5
50 | assert BasicMath.truncate_value(-33.333334) == -33
51 | end
52 | end
53 | end
54 |
--------------------------------------------------------------------------------
/test/ep3_numbers_test/calculater_test.exs:
--------------------------------------------------------------------------------
1 | defmodule CalculaterTest do
2 | use ExUnit.Case
3 | doctest Calculator
4 |
5 | describe "find_percentage_of_value/2" do
6 | test "returns correct percentage of value" do
7 | assert Calculator.find_percentage_of_value(25, 100) == 25.0
8 | assert Calculator.find_percentage_of_value(23, 456) == 104.89
9 | end
10 | end
11 |
12 | describe "find_distance_traveled/2" do
13 | test "returns correct distance traveled" do
14 | assert Calculator.find_distance_traveled(30, 2) == 60
15 | assert Calculator.find_distance_traveled(67, 2.8) == 187.6
16 | end
17 | end
18 | end
19 |
--------------------------------------------------------------------------------
/test/ep4_strings_test/all_strings_test.exs:
--------------------------------------------------------------------------------
1 | defmodule AllStringsTest do
2 | use ExUnit.Case
3 | doctest AllStrings
4 |
5 | describe "initial/1" do
6 | test "returns capitalized first letter with period follwed by a space" do
7 | assert AllStrings.initial("blork") == "B. "
8 | assert AllStrings.initial("Blork erlang") == "B. "
9 | end
10 | end
11 |
12 | describe "initials/1" do
13 | test "returns initials with periods between" do
14 | assert AllStrings.initials("blork erlang") == "B. E."
15 | assert AllStrings.initials("Blork erlang jr") == "B. E. J."
16 | end
17 | end
18 |
19 | describe "build_saucer/0" do
20 | test "returns saucer string" do
21 | assert AllStrings.build_saucer() == "\n %%%%%%%%%%%%%%%%%%%%\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n************************************************************\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n ,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n\n"
22 | end
23 | end
24 |
25 | describe "customized_spacecraft/1" do
26 | test "returns saucer string" do
27 | assert AllStrings.customized_spacecraft("blork erlang") == "\n %%%%%%%%%%%%%%%%%%%%\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n******************** B. E.'s SPACECRAFT *******************\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n ,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n\n"
28 | assert AllStrings.customized_spacecraft("jacob A luetzow") == "\n %%%%%%%%%%%%%%%%%%%%\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n******************** J. A. L.'s SPACECRAFT *******************\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n ,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,,,,&&&,,,\n ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n\n"
29 | end
30 | end
31 |
32 | describe "display_saucer/0" do
33 | test "returns atom" do
34 | assert AllStrings.display_saucer() == :ok
35 | end
36 | end
37 |
38 | describe "display_custom_spacecraft/1" do
39 | test "returns atom" do
40 | assert AllStrings.display_custom_spacecraft("blork erlang") == :ok
41 | end
42 | end
43 | end
44 |
--------------------------------------------------------------------------------
/test/ep5_linked_lists_test/linked_lists_test.exs:
--------------------------------------------------------------------------------
1 | defmodule LinkedListsTest do
2 | use ExUnit.Case
3 | doctest LinkedLists
4 |
5 | describe "get_equipment_list/0" do
6 | test "returns static equipment list" do
7 | assert LinkedLists.get_equipment_list() == ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]
8 | end
9 | end
10 |
11 | describe "get_first_item/1" do
12 | test "returns first element from list" do
13 | assert LinkedLists.get_first_item(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]) == "Space Helmet"
14 | end
15 | end
16 |
17 | describe "remove_first_item/1" do
18 | test "removes first element from list" do
19 | assert LinkedLists.remove_first_item(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]) == ["Space Suit", "Snacks", "Grapling Hook", "Probe"]
20 | end
21 | end
22 |
23 | describe "add_slow/2" do
24 | test "adds element to end of list" do
25 | assert LinkedLists.add_slow("new value", ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]) == ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe", "new value"]
26 | end
27 | end
28 |
29 | describe "add_fast/2" do
30 | test "adds element to end of list" do
31 | assert LinkedLists.add_fast("new value", ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]) == ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe", "new value"]
32 | end
33 | end
34 |
35 | describe "remove_item/2" do
36 | test "removes element from list at index" do
37 | assert LinkedLists.remove_item(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], 2) == ["Space Helmet", "Space Suit", "Grapling Hook", "Probe"]
38 | end
39 | end
40 |
41 | describe "equipment_count/1" do
42 | test "returns length of list" do
43 | assert LinkedLists.equipment_count(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]) == 5
44 | end
45 | end
46 |
47 | describe "check_for_item/2" do
48 | test "checks for value in list" do
49 | assert LinkedLists.check_for_item(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "Probe") == true
50 | end
51 | end
52 | end
53 |
--------------------------------------------------------------------------------
/test/ep6_recursion_test/recursion_test.exs:
--------------------------------------------------------------------------------
1 | defmodule RecursionTest do
2 | use ExUnit.Case
3 | doctest Recursion
4 |
5 | describe "get_equipment_list/0" do
6 | test "returns static equipment list" do
7 | assert Recursion.get_equipment_list() == ["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]
8 | end
9 | end
10 |
11 | describe "equipment_count/1" do
12 | test "returns length of list" do
13 | assert Recursion.equipment_count(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]) == 5
14 | end
15 | end
16 |
17 | describe "format_equipment_list/1" do
18 | test "returns formatted list" do
19 | assert Recursion.format_equipment_list(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]) == ["space_helmet", "space_suit", "snacks", "grapling_hook", "probe"]
20 | end
21 | end
22 |
23 | describe "format_equipment_list_map/1" do
24 | test "returns formatted list" do
25 | assert Recursion.format_equipment_list_map(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"]) == ["space_helmet", "space_suit", "snacks", "grapling_hook", "probe"]
26 | end
27 | end
28 |
29 | describe "occurrence_count/2" do
30 | test "returns occurrences of value from list" do
31 | assert Recursion.occurrence_count(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "Probe") == 1
32 | assert Recursion.occurrence_count(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "vitamins") == 0
33 | end
34 | end
35 |
36 | describe "partial_occurrence/2" do
37 | test "Returns a formatted `list` of values that contain part of a `value`" do
38 | assert Recursion.partial_occurrence(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "Space Helmet") == ["space_helmet"]
39 | assert Recursion.partial_occurrence(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "vitamins") == []
40 | assert Recursion.partial_occurrence(["Space Helmet", "Space Suit", "Snacks", "Grapling Hook", "Probe"], "space") == ["space_helmet", "space_suit"]
41 | end
42 | end
43 |
44 | end
45 |
--------------------------------------------------------------------------------
/test/ep7_pattern_matching_test/equipment_details_test.exs:
--------------------------------------------------------------------------------
1 | defmodule EquipmentDetailsTest do
2 | use ExUnit.Case
3 | doctest EquipmentDetails
4 |
5 | end
6 |
--------------------------------------------------------------------------------
/test/ep7_pattern_matching_test/saucer_preflight_test.exs:
--------------------------------------------------------------------------------
1 | defmodule SaucerPreflightTest do
2 | use ExUnit.Case
3 | doctest SaucerPreflight
4 |
5 | end
6 |
--------------------------------------------------------------------------------
/test/test_helper.exs:
--------------------------------------------------------------------------------
1 | ExUnit.start()
2 |
--------------------------------------------------------------------------------