二郎山。删除列表的第一个四分之一,并按照

时间:2017-05-31 20:28:45

标签: erlang

长度为4的倍数的集合列表。删除列表的第一个四分之一,并按给定的相反顺序排列第三个元素。这是我的变体 - >

-import(lists, [filter/2, member/2, split/2, reverse/1]).

foo(L) when (length(L) rem 4) =/= 0 -> "Your list in non-multiple to 4!!!";
foo(L) ->   {Sub1, Sub2} = split(length(L) div 2, L),
            { _ ,Second} = split(length(Sub1) div 2, Sub1),
            {Third,Fourth} = split(length(Sub2) div 2, Sub2),
             Second ++ reverse(Third) ++ Fourth.

可能有人有更好的想法如何做到这一点。谢谢!

1 个答案:

答案 0 :(得分:1)

我有非常有效的解决方案:

2> timer:tc(fun(L) -> fun() -> F(L) end end(lists:seq(1, 1000000))).
{22770,
 [250001,250002,250003,250004,250005,250006,250007,250008,
  250009,250010,250011,250012,250013,250014,250015,250016,
  250017,250018,250019,250020,250021,250022,250023,250024,
  250025,250026,250027|...]}
3> timer:tc(fun(L) -> fun() -> F(L) end end(lists:seq(1, 32))).                                                                                                                                                                                                       
{79,                                                                                                                                                                                                                                                                   
 [9,10,11,12,13,14,15,16,24,23,22,21,20,19,18,17,25,26,27,28,                                                                                                                                                                                                          
  29,30,31,32]}

您为F/1定义向我支付多少费用?