-
Notifications
You must be signed in to change notification settings - Fork 0
/
problems_1_to_10.exs
145 lines (108 loc) · 4.01 KB
/
problems_1_to_10.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
defmodule Problems1To10 do
def myLast([head | []]), do: head
def myLast([ _head | tail ]) do
myLast(tail)
end
def myButLast( [] ), do: raise "Empty List"
def myButLast( [_head | []]), do: raise "Too few elements"
def myButLast([head | tail]) do
case length(tail) do
1 ->
head
_ ->
myButLast(tail)
end
end
def elementAt([head | _tail], 1), do: head
def elementAt([_head|tail], count) do
elementAt(tail, count - 1)
end
def myLength(list), do: myLength(list, 0)
defp myLength([], count), do: count
defp myLength([_head|tail], count) do
myLength(tail, count + 1)
end
def myReverse([]), do: []
def myReverse([head|tail]), do: myReverse(tail) ++ [head]
def isPalindrome(list), do: list == myReverse(list)
def flatten([]), do: []
def flatten([head | tail]) when is_list(head), do: flatten(head) ++ flatten(tail)
def flatten([head | tail]), do: [ head | flatten(tail)]
def compress(list) do
Enum.reduce( list, %{:el => nil, :list => []}, fn(el, acc) ->
case acc.el == el do
true ->
acc
false ->
%{ :el => el, :list => [el | acc.list] }
end
end) |> Map.get(:list, []) |> myReverse
end
def pack([head|tail]), do: do_pack(tail, head, [], [])
defp do_pack([], prev_element, simple_pack, pack) do
merge_for_pack(prev_element, simple_pack, pack) |> myReverse
end
defp do_pack([head|tail], prev_element, simple_pack, pack) when head == prev_element do
do_pack(tail, head, [head|simple_pack], pack)
end
defp do_pack([head|tail], prev_element, simple_pack, pack) when head != prev_element do
do_pack(tail, head, [], merge_for_pack(prev_element, simple_pack, pack))
end
defp merge_for_pack(el, group, list), do: [to_string([el|group])|list]
def encode(string) do
string
|> String.split("", trim: true)
|> pack
|> Enum.reduce([], fn(el, acc) ->
[ { String.length(el), el |> String.first() |> String.to_charlist() } | acc]
end)
|> myReverse
end
end
case System.argv() do
_ ->
ExUnit.start()
defmodule Problems1To10Test do
use ExUnit.Case
import Problems1To10
test "Find the last element of a list." do
assert myLast([1,2,3,4]) == 4
assert myLast(['x','y','z']) == 'z'
end
test "Find the last but one element of a list." do
assert myButLast([1,2,3,4]) == 3
assert myButLast(Enum.to_list(?a..?z)) == ?y
end
test "Find the K'th element of a list. The first element in the list is number 1." do
assert elementAt([1,2,3], 2) == 2
assert elementAt('haskell', 5) == ?e
end
test "Find the number of elements of a list." do
assert myLength([123, 456, 789]) == 3
assert myLength('Hello, world!') == 13
end
test "Reverse a list." do
assert myReverse('A man, a plan, a canal, panama!') == '!amanap ,lanac a ,nalp a ,nam A'
assert myReverse([1,2,3,4]) == [4,3,2,1]
end
test "Find out whether a list is a palindrome. A palindrome can be read forward or backward; e.g. (x a m a x)." do
assert isPalindrome([1,2,3]) == false
assert isPalindrome('madamimadam') == true
assert isPalindrome([1,2,4,8,16,8,4,2,1]) == true
end
test "Flatten a nested list structure." do
assert flatten([5]) == [5]
assert flatten([1, [2, [3, 4], 5]]) == [1,2,3,4,5]
assert flatten([]) == []
end
test "Eliminate consecutive duplicates of list elements." do
assert compress('aaaabccaadeeee') == 'abcade'
end
test "Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements they should be placed in separate sublists." do
assert pack(['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e']) == ["aaaa","b","cc","aa","d","eeee"]
end
test "Run-length encoding of a list" do
assert encode("aaaabccaadeeee") == [{4,'a'}, {1,'b'}, {2,'c'}, {2,'a'}, {1,'d'}, {4,'e'}]
end
end
end