erlang练习题(三)

题目一

查询列表A是否为列表B的前缀

解答

isPrefix([], List2) -> io:format("A is prefix of B ~n");

isPrefix([H1 | ListA], [H2 | ListB]) ->

 case H1 == H2 of

  true -> isPrefix(ListA, ListB);

  false -> io:format("A is not prefix of B ~n")

 end.

题目二

接受一个整数列表作为参数,返回一个新列表,其中的元素为原列表中所有出现至少两次的元素

解答

getTwice(List) ->

 getTwice(List, []).

getTwice([], Acc) -> Acc;

getTwice([Item | Rest], Acc) ->

 %% 当前元素在后面有重复出现,且在结果列表中没出现过,就加入

 case lists:member(Item, Rest) and not lists:member(Item, Acc) of

  true -> getTwice(Rest, [Item | Acc]);

  false -> getTwice(Rest, Acc)

 end.

题目三

判断列表A是否在列表B中出现,出现则输出在B列表第几位开始(例如[3,5,7,3]在[8,3,5,3,5,7,3,9,3,5,6,3]的第4位出现

解答

find_sublist_pos(_, []) -> not_found;

find_sublist_pos(Sublist, B) ->

 find_sublist_pos(Sublist, B, 1).

 

find_sublist_pos(Sublist, RestB, Pos) when length(RestB) < length(Sublist) ->

 not_found;

find_sublist_pos(Sublist, RestB, Pos) ->

 case starts_with(Sublist, RestB) of

  true -> {found, Pos};

  false -> find_sublist_pos(Sublist, tl(RestB), Pos + 1) % tl获取列表的尾部集合(除了头元素的其他元素)

 end.

 

%% 第一个列表是否为第二列表的前缀

%% 空列表是任何列表的前缀

starts_with([], _) -> true;

starts_with(_, []) -> false;

starts_with([H1 | T1], [H2 | T2]) when H1 =:= H2 ->

 starts_with(T1, T2);

starts_with(_, _) -> false. %% 元素值不同,为false

题目四

在一个全部元素为{Key, Value}元组结构的元组或列表中,查找列表或元组中第一个

符合指定键的元素

例如:在[{a, 1}, {b, 2}, {c, 3}, {a, 4}] 如果查找第一个a为键的元素即输出 {a, 1}

解答

findfirstkey_a([]) -> false;


findfirstkey_a([Head | Rest]) ->

 {Key, Val} = Head,

 case Key == 'a' of

  true -> Head;

  false -> findfirstkey_a(Rest)

 end.

题目五

在一个全部元素为{Key, Value}元组结构的元组或列表中,用列表输出查找到的列表或元组中全部指定键的元素

例如:在[{a, 1}, {b, 2}, {c, 3}, {a, 4}] 如果查找全部a为键的元素即输出 [{a, 1}, {a, 4}]

解答

findkey_a(List) ->

 findkey_a(List, []).

 

findkey_a([], Acc) -> Acc;

findkey_a([Head | Rest], Acc) ->

 %io:format("Head = ~p~n Acc = ~p~n", [Head, Acc]),

 

 {Key, _} = Head,

 case Key == 'a' of

  true -> findkey_a(Rest, [Head | Acc]);

  false -> findkey_a(Rest, Acc)

 end.

题目六

在一个全部元素为{Key, Value}元组结构的元组或列表中,删除列表或元组中第一个符合指定键的元素且输出时不改变顺序

例如:在[{a, 1}, {b, 2}, {c, 3}, {a, 4}] 如果删除第一个a为键的元素即输出 [{b, 2}, {c, 3}, {a, 4}]

解答

deletefirstkey_a(List) ->

deletefirstkey_a(List, [], false). % 第三个参数表示是否出现过了a

deletefirstkey_a([], Acc,_) -> Acc;

deletefirstkey_a([{Key, _} | Rest], Acc, false) when Key =:= a->

deletefirstkey_a(Rest, Acc, true);

deletefirstkey_a([Head | Rest], Acc, true) ->

deletefirstkey_a(Rest, [Head | Acc], true).

题目七

在一个全部元素为{Key, Value}元组结构的元组或列表中,替换列表或元组中第一个符合指定键的元素且输出时不改变顺序

例如:输入列表[{a, 1}, {b, 2}, {c, 3}, {a, 4}] 查找键为a,值替换为10

即输出[{a, 10}, {b, 2}, {c, 3}, {a, 4}] 46

解答

replacefirstkey_a(List) ->

replacefirstkey_a(List, [], 0). % 第三个参数表示是否出现过了a

replacefirstkey_a([], Acc,_) -> lists:reverse(Acc);

replacefirstkey_a([{Key, _} | Rest], Acc, 0) when Key =:= a->

replacefirstkey_a(Rest, [{Key, 10}|Acc], 1);

%% 第一次插入后,即便 Key为 a ,也不会再走上面的逻辑

replacefirstkey_a([Head | Rest], Acc, _) ->

replacefirstkey_a(Rest, [Head | Acc], 1).

题目八

生成含有指定数量元素的元组,每个元素为随机integer元素, 要求元素不能有重复的(可以使用API)

解答

gen_int(N) ->

gen_int(N, []).

gen_int(0, Acc) -> {tuple, lists:reverse(Acc)};

gen_int(N, Acc) ->

Random = rand:uniform(10000),

case lists:member(Random, Acc) of

true -> gen_int(N, Acc);

false -> gen_int(N - 1, [Random | Acc])

end.

题目九

在一个全部元素为{Key, Value}元组结构的元组或列表中,将列表中相同key的值进行合并

例如:[{a,1},{b,2},{c,3},{b,4},{b,5},{c,6},{d,7},{d,8}] 输出 [{a,1},{b,11},{c,9},{d,15}]

解答

mergekv(List) ->

mergekv(List, maps:new()).

mergekv([], AccMap) -> maps:to_list(AccMap);

mergekv([{Key, Value} | T], AccMap) ->

io:format(“{p,p}~n”, [Key, Value]),

NewValue = case maps:is_key(Key, AccMap) of

​ true -> maps:get(Key, AccMap) + Value;

​ false -> Value

​ end,

NewAccMap = maps:put(Key, NewValue, AccMap), % 注意要用一个新的变量接受结果

mergekv(T, NewAccMap).

题目十

1、对相同类型的数据进行拼接,如:

binary:<<1,2>><<3,4>> 拼接后为 <<1,2,3,4>> 未完成

tuple:{a,b},{c} 拼接后为 {a,b,c}

list:[10],[20] 拼接后为 [10,20]

解答

%% 无法执行!!!

concat_binary(Binary1, Binary2) ->

Size1 = size(Binary1),

Size2 = size(Binary2),

NewSize = Size1 + Size2,

NewBinary = <<0:NewSize/unit:8>>,

NewBinary = binary_concat(Binary1, Binary2, NewBinary, 0, Size1),

NewBinary.

binary_concat(_, _, NewBinary, Size, Size) ->

NewBinary;

binary_concat(Binary1, Binary2, NewBinary, Index, Size1) when Index < size(Binary2) ->

Byte = binary_part(Binary2, Index, 1), % 从Binary2中从Index位置开始提取1长度的数据

UpdatedBinary = setelement(Size1 + Index + 1, NewBinary, Byte),

binary_concat(Binary1, Binary2, UpdatedBinary, Index + 1, Size1).

%% Tuple

concat_tuple(Tuple1, Tuple2) ->

concat_tuple(Tuple1, Tuple2, size(Tuple1), size(Tuple2), {}).

concat_tuple(Tuple1, Tuple2, 0, 0, AccTuple) ->

AccTuple;

%% 将Tuple1拼接到新元组中

concat_tuple(Tuple1, Tuple2, Index1, Index2, AccTuple) when Index1 > 0 ->

NewTuple = erlang:append_element(AccTuple, element(Index1, Tuple1)),

concat_tuple(Tuple1, Tuple2, Index1 - 1, Index2, NewTuple);

%% 将Tuple2拼接到新元组中

concat_tuple(Tuple1, Tuple2, 0, Index2, AccTuple) ->

NewTuple = erlang:append_element(AccTuple, element(Index2, Tuple2)),

concat_tuple(Tuple1, Tuple2, 0, Index2 -1, NewTuple).

%% 列表的拼接

concat_list(List1, List2) ->

concat_list(List1, List2, []).

concat_list([],[], Acc) -> lists:reverse(Acc);

concat_list([H | T], List2, Acc) ->

concat_list(T, List2, [H | Acc]);

concat_list([], [H | T], Acc) ->

concat_list([], T, [H | Acc]).

你可能感兴趣的:(erlang,erlang,开发语言)