99 Erlang Problems: 16 - 25

| Comments

Problem 16: Drop every N’th element from a list.

problems:p16([a,b,c,d,e,f,g,h,i,k], 3).
> [a,b,d,e,g,h,k]

Problem 17: Split a list into two parts; the length of the first part is given. Do not use any Built In Functions (BIF).

problems:p17([1,2,3,4,5], 2).
> {[1,2],[3,4,5]}

Problem 18: Extract a slice from a list.

problems:p18([a,b,c,d,e,f,g,h,i,k], 3, 7).
> [c,d,e,f,g]

Problem 19: Rotate a list N places to the left.

problems:p19([a,b,c,d,e,f,g,h], 3).
> [d,e,f,g,h,a,b,c]

problems:p19([a,b,c,d,e,f,g,h], -2).
> [g,h,a,b,c,d,e,f]

Problem 20: Remove the K’th element from a list

problems:p20([1,2,3], 2).
> [1,3]

Problem21: Insert an element at a given position into a list.

problems:p21([1,2,3], 5, 2).
> [1,2,5,3]

Problem 22: Create a list containing all integers within a given range.

problems:p22(9, 4).
> [9,8,7,6,5,4]
problems:p22(4,9).
> [4,5,6,7,8,9]

Problem 23: Extract a given nubmer of randomly selected elements from a list.

problems:p23([1,2,3,4,5,6], 3).
> [1,2,6]

Problem 24: Lotto: Draw N diference random numbers from the set 1..M. The selected numbers shall be returned in a list.

problems:p24(6, 49).
> [23, 1, 17,33,21,37]

Problem 25: Generate a random permutation of the elements of a list.

problems:p25([a,b,c,d,e,f]).
> [e,d,b,c,a,f]

Answers

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
p16(List, N) ->  p16(List, N, 1).
p16([], _, _) -> [];
p16([H|T], N, A) ->
    if N =:= A ->
        p16(T, N, 1);
    true ->
        [H|p16(T, N, A+1)]
    end.

p17(List, N) ->
    {First, Second} = p17(List, [], N),
    {lists:reverse(Second), First}.

p17([], L, _) ->
    {[], L};
p17([H|T], L, N) ->
    case N =:= 0 of
  true ->
      {[H|T], L};
  false ->
      p17(T, [H|L], N-1)
    end.

p18(List, Start, End) ->
    Rest = con(List, Start),
    rest(Rest, End - Start).

rest([], _) ->
    [];
rest([H|T], End) ->
    if End =:= 0 ->
      [H];
       true ->
      [H|rest(T, End -1)]
    end.

con([], _) ->
    [];
con([H|T], Start) ->
    if Start =:= 1 ->
      [H|T];
       true ->
      con(T, Start-1)
    end.

p19([], _) ->
    [];
p19(List, Places) ->
    if Places > 0 ->
            shift_l(List, Places);
       Places < 0 ->
            shift_r(List, abs(Places));
       true ->
            List
    end.

shift_r(List, 0) ->
    List;
shift_r(List, Places) ->
    Length = length(List),
    {R1, R2} = lists:split(Length -1, List),
    shift_r(lists:append(R2, R1), Places -1).

shift_l(List, 0) -> List;
shift_l([H|T], Places) ->
    shift_l(lists:append(T, [H]), Places -1).

p20([], _) -> [];
p20([H|T], Kth) ->
    if Kth =:= 1 ->
            T;
       true ->
            [H|p20(T, Kth-1)]
    end.

p21([H|T], Ins, Kth) ->
    if Kth =:= 0 ->
            [Ins,H|T];
       true ->
            [H|p21(T, Ins, Kth-1)]
    end.

p22(Start, End) ->
    if Start > End ->
            lists:reverse(p22(End, Start));
       Start =:= End ->
            [Start];
       true ->
            [Start|p22(Start+1, End)]
    end.

p23(List, 0) -> List;
p23(List, Number) ->
    L3 = p20(List, random:uniform(length(List))),
    p23(L3, Number-1).

p24(N, End) ->
    gen(N, p22(1, End)).

gen(0, _) -> [];
gen(N, L) ->
    Idx = random:uniform(length(L)),
    Elem = lists:nth(Idx, L),
    [Elem|gen(N-1, lists:delete(Elem, L))].

p25([]) -> [];
p25(L) ->
    Idx = random:uniform(length(L)),
    Elem = lists:nth(Idx, L),
    [Elem|p25(lists:delete(Elem, L))].