Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
varnie committed Jan 24, 2025
1 parent 6ec4e3a commit 3200d96
Show file tree
Hide file tree
Showing 22 changed files with 72 additions and 143 deletions.
15 changes: 3 additions & 12 deletions lib/challenge1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@ defmodule Challenge1 do
@moduledoc """
(*) Find the last element of a list.
"""
def p01_my_last(lst) do
List.last(lst)
end
def p01_my_last(lst), do: List.last(lst)

@doc """
(*) Find the last element of a list.
"""
def p01_my_last_02([head]) do
head
end
def p01_my_last_02([head]), do: head

def p01_my_last_02([_head | tail]) do
p01_my_last_02(tail)
end

def p01_my_last_02(_) do
nil
end
def p01_my_last_02(_), do: nil
end
4 changes: 1 addition & 3 deletions lib/challenge11.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ defmodule Challenge11 do
def encode_modified(lst) do
lst
|> Challenge10.encode()
|> Enum.map(fn item ->
{len, letter} = item

|> Enum.map(fn {len, letter} = item ->
if len > 1 do
item
else
Expand Down
25 changes: 12 additions & 13 deletions lib/challenge13.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@ defmodule Challenge13 do
dst,
countLetter = %CountLetter{count: count, letter: letter}
) do
if is_nil(letter) do
encode_direct_helper(tail, dst, %{countLetter | count: 1, letter: head})
else
case head == letter do
true ->
encode_direct_helper(tail, dst, %{countLetter | count: count + 1, letter: head})
cond do
is_nil(letter) ->
encode_direct_helper(tail, dst, %{countLetter | count: 1, letter: head})

head == letter ->
encode_direct_helper(tail, dst, %{countLetter | count: count + 1, letter: head})

_ ->
encode_direct_helper(tail, [generated_item(countLetter) | dst], %{
countLetter
| count: 1,
letter: head
})
end
true ->
encode_direct_helper(tail, [generated_item(countLetter) | dst], %{
countLetter
| count: 1,
letter: head
})
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/challenge18.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule Challenge18 do
@moduledoc """
(**) Extract a slice from a list.
Given two indices, i and k, the slice is the list containing the elements between the i'th and k'th element of the original list (both limits included).
Given two indices, i and k, the slice is the list containing the elements
between the i'th and k'th element of the original list (both limits included).
Start counting the elements with 1.
"""
def slice(lst, i, k) do
Expand Down
16 changes: 4 additions & 12 deletions lib/challenge2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,16 @@ defmodule Challenge2 do
p02_but_last_1_helper(hd(lst), tl(lst))
end

defp p02_but_last_1_helper(acc, [_head]) do
acc
end
defp p02_but_last_1_helper(acc, [_head]), do: acc

defp p02_but_last_1_helper(_acc, [head | tail]) do
p02_but_last_1_helper(head, tail)
end
defp p02_but_last_1_helper(_acc, [head | tail]), do: p02_but_last_1_helper(head, tail)

@doc """
(*) Find the last but one element of a list.
"""
def p02_but_last_2([a, _b]) do
a
end
def p02_but_last_2([a, _b]), do: a

def p02_but_last_2([_a, b | tail]) do
p02_but_last_2([b | tail])
end
def p02_but_last_2([_a, b | tail]), do: p02_but_last_2([b | tail])

def p02_but_last_2(_) do
raise("Length must be greater than one")
Expand Down
4 changes: 1 addition & 3 deletions lib/challenge22.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ defmodule Challenge22 do
"""
def range(x, y) when x >= y, do: []

def range(x, y) do
range_helper(x, y, []) |> Enum.reverse()
end
def range(x, y), do: range_helper(x, y, []) |> Enum.reverse()

defp range_helper(x, y, dst) when x > y, do: dst
defp range_helper(x, y, dst), do: range_helper(x + 1, y, [x | dst])
Expand Down
7 changes: 4 additions & 3 deletions lib/challenge3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ defmodule Challenge3 do
end

defp helper([head | tail], cur_index, k) do
case cur_index == k do
true -> head
_ -> helper(tail, cur_index + 1, k)
if cur_index == k do
head
else
helper(tail, cur_index + 1, k)
end
end
end
4 changes: 1 addition & 3 deletions lib/challenge31.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ defmodule Challenge31 do
"""
def is_prime(num) when num == 1, do: true

def is_prime(num) do
is_prime_helper(num, 2)
end
def is_prime(num), do: is_prime_helper(num, 2)

defp is_prime_helper(num, index) when index == num, do: true

Expand Down
4 changes: 1 addition & 3 deletions lib/challenge33.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ defmodule Challenge33 do
(*) Determine whether two positive integer numbers are coprime.
Two numbers are coprime if their greatest common divisor equals 1.
"""
def coprime(a, b) do
Challenge32.gcd(a, b) == 1
end
def coprime(a, b), do: Challenge32.gcd(a, b) == 1
end
7 changes: 4 additions & 3 deletions lib/challenge39.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ defmodule Challenge39 do
Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.
"""
def prime_numbers_list(from, to) do
Enum.filter(from..to, fn elem ->
Challenge31.is_prime(elem)
end)
Enum.filter(
from..to,
&Challenge31.is_prime/1
)
end
end
16 changes: 4 additions & 12 deletions lib/challenge4.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@ defmodule Challenge4 do
@moduledoc """
(*) Find the number of elements of a list.
"""
def p04_find_len(lst) do
length(lst)
end
def p04_find_len(lst), do: length(lst)

def p04_find_len_my(lst) do
helper(lst, 0)
end
def p04_find_len_my(lst), do: helper(lst, 0)

defp helper([_h | t], acc) do
helper(t, acc + 1)
end
defp helper([_h | t], acc), do: helper(t, acc + 1)

defp helper([], acc) do
acc
end
defp helper([], acc), do: acc
end
16 changes: 4 additions & 12 deletions lib/challenge5.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@ defmodule Challenge5 do
@moduledoc """
(*) Reverse a list.
"""
def reverse(lst) do
Enum.reverse(lst)
end
def reverse(lst), do: Enum.reverse(lst)

def reverse_my(lst) do
helper(lst, [])
end
def reverse_my(lst), do: helper(lst, [])

defp helper([], dst) do
dst
end
defp helper([], dst), do: dst

defp helper([h | tail], dst) do
helper(tail, [h | dst])
end
defp helper([h | tail], dst), do: helper(tail, [h | dst])
end
13 changes: 5 additions & 8 deletions lib/challenge54.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ defmodule Challenge54 do
(*) Check whether a given expression represents a binary tree
Write a function istree which returns true if and only if its argument is a list representing a binary tree.
"""
def is_tree(src) do
is_tree_helper(src)
end

defp is_tree_helper(nil), do: true
def is_tree(nil), do: true

defp is_tree_helper([root, l, r]) when root != nil and is_atom(root),
do: is_tree_helper(l) && is_tree_helper(r)
def is_tree([root, l, r]) when root != nil and is_atom(root) do
is_tree(l) && is_tree(r)
end

defp is_tree_helper(_), do: false
def is_tree(_), do: false
end
4 changes: 1 addition & 3 deletions lib/challenge56.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ defmodule Challenge56 do
is the mirror image of the left subtree. Write a function symmetric to check whether a given binary tree is symmetric.
We are only interested in the structure, not in the contents of the nodes.
"""
def is_symmetric(node) do
is_symmetric_helper(node, node)
end
def is_symmetric(node), do: is_symmetric_helper(node, node)

defp is_symmetric_helper(a, b) do
cond do
Expand Down
31 changes: 13 additions & 18 deletions lib/challenge57.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ defmodule Challenge57 do
* (symmetric '(3 2 5 7))
NIL
"""
def construct([]) do
nil
end
def construct([]), do: nil

def construct([x]) do
%TreeNode{symbol: x, left: nil, right: nil}
Expand All @@ -28,9 +26,7 @@ defmodule Challenge57 do
construct_leafs(root_node, tail)
end

defp construct_leafs(result_node, []) do
result_node
end
defp construct_leafs(result_node, []), do: result_node

defp construct_leafs(result_node, [cur_val | tail]) do
new_node = %TreeNode{symbol: cur_val, left: nil, right: nil}
Expand All @@ -39,18 +35,17 @@ defmodule Challenge57 do
end

defp gen_node(parent_node, new_node) do
if is_nil(parent_node) do
new_node
else
cond do
new_node.symbol <= parent_node.symbol ->
new_left_node = gen_node(parent_node.left, new_node)
TreeNode.set_left(parent_node, new_left_node)

true ->
new_right_node = gen_node(parent_node.right, new_node)
TreeNode.set_right(parent_node, new_right_node)
end
cond do
is_nil(parent_node) ->
new_node

new_node.symbol <= parent_node.symbol ->
new_left_node = gen_node(parent_node.left, new_node)
TreeNode.set_left(parent_node, new_left_node)

true ->
new_right_node = gen_node(parent_node.right, new_node)
TreeNode.set_right(parent_node, new_right_node)
end
end
end
4 changes: 1 addition & 3 deletions lib/challenge6.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ defmodule Challenge6 do
@moduledoc """
(*) Find out whether a list is a palindrome.
"""
def is_palindrome(lst) do
lst == Challenge5.reverse(lst)
end
def is_palindrome(lst), do: lst == Challenge5.reverse(lst)
end
16 changes: 4 additions & 12 deletions lib/challenge62B.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@ defmodule Challenge62B do
(atlevel tree l) returns the list of nodes of the binary tree tree at level L
"""
def atlevel(_tree, l) when l <= 0 do
[]
end
def atlevel(_tree, l) when l <= 0, do: []

def atlevel(tree, _l) when is_nil(tree) do
[]
end
def atlevel(tree, _l) when is_nil(tree), do: []

def atlevel(tree, l) do
atlevel_helper(tree, l, 1)
end
def atlevel(tree, l), do: atlevel_helper(tree, l, 1)

defp atlevel_helper(tree, l, current_level) when current_level == l do
[tree]
end
defp atlevel_helper(tree, l, current_level) when current_level == l, do: [tree]

defp atlevel_helper(tree, l, current_level) do
cond do
Expand Down
4 changes: 1 addition & 3 deletions lib/challenge63.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ defmodule Challenge63 do
(complete-binary-tree n) returns a complete binary tree with n nodes
"""

def complete_binary_tree(n) when n <= 0 do
nil
end
def complete_binary_tree(n) when n <= 0, do: nil

def complete_binary_tree(n) do
%TreeNode{symbol: 1, left: create_successor(n, 2 * 1), right: create_successor(n, 2 * 1 + 1)}
Expand Down
15 changes: 6 additions & 9 deletions lib/challenge9.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ defmodule Challenge9 do
(**) Pack consecutive duplicates of list elements into sublists.
If a list contains repeated elements they should be placed in separate sublists.
"""
def pack(lst) do
pack_helper(lst, [])
end
def pack(lst), do: pack_helper(lst, [])

defp pack_helper(_src = [], dst) do
Enum.reverse(dst)
end
defp pack_helper(_src = [], dst), do: Enum.reverse(dst)

defp pack_helper(_src = [head | tail], dst) do
first_item_dst = get_first_item(dst)
Expand All @@ -31,9 +27,10 @@ defmodule Challenge9 do
end

def get_first_item(lst_or_obj) do
case is_list(lst_or_obj) do
true -> get_first_item(List.first(lst_or_obj))
_ -> lst_or_obj
if is_list(lst_or_obj) do
get_first_item(List.first(lst_or_obj))
else
lst_or_obj
end
end
end
1 change: 0 additions & 1 deletion lib/challenge90.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ defmodule Challenge90 do
Use the generate-and-test paradigm.
"""

# @letters %{1 => :a, 2 => :b, 3 => :c, 4 => :d, 5 => :e, 6 => :f, 7 => :g, 8 => :h}
@letters Map.new(Enum.zip(1..8, [:a, :b, :c, :d, :e, :f, :g, :h]))
@board_axis_indexes 1..8

Expand Down
1 change: 0 additions & 1 deletion lib/challenge90Another.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ defmodule Challenge90Another do
@top 57..64
@bottom 1..8

# @letters %{1 => :a, 2 => :b, 3 => :c, 4 => :d, 5 => :e, 6 => :f, 7 => :g, 8 => :h}
@letters Map.new(Enum.zip(1..8, [:a, :b, :c, :d, :e, :f, :g, :h]))

def solve() do
Expand Down
Loading

0 comments on commit 3200d96

Please sign in to comment.