`
junfeng_feng
  • 浏览: 18491 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Erlang 练习题

 
阅读更多
-module(exam).
-compile(export_all).

%1杨辉三角
start(N) when is_integer(N) ->
        print(N,1,1).

%C表示行数
%LC表示C对应的list
print(N,1,_) ->
        io:format("~p~n",[[1]]),
        print(N,2,1);
print(N,2,_) ->
        io:format("~p~n",[[1,1]]),
        print(N,3,[1,1]);
%Lc_minus_1 表示C-1行的list
print(N,C,Lc_minus_1) ->
    if
        C =:= (N + 1) ->
            ok;
        true    ->
            LC = get_increment(C,Lc_minus_1,[1]),
            io:format("~p~n",[LC]),
            print(N,C+1,LC)
    end.

%L 是 N-1行 的列表
%Result结果是 N行 的列表
get_increment(N,L,Result) ->
    Len = length(Result), 
    if
        Len =:= N-1 -> [1|Result];
        true ->
            get_increment(N,L,[lists:nth(Len,L) + lists:nth(Len+1,L) | Result])
    end.


%2 选择排序
select_sort(L) ->
    select_sort_impl(L,[]).

select_sort_impl([],_) ->
    io:format("~n");
select_sort_impl(L,Result) ->
    {Pos,Value} = get_min(L),
    L_new = lists:sublist(L,Pos-1) ++ lists:sublist(L,Pos+1,length(L)-Pos+1),
    Result_new = Result ++ [Value],
    io:format("~p~n",[Result_new ++ L_new]),
    select_sort_impl(L_new,Result_new).

get_min([]) ->
    error;
get_min(L) ->
    %第一次取第一个元素为初始最小值
    get_min_impl(L,1,1,lists:nth(1,L)).

%获得list最小元素的{位置,值}
%Idx 表示遍历到当前list的第几个
get_min_impl([],_,Pos,Value) ->
    {Pos,Value};
get_min_impl(L,Idx,Pos,Value) ->
    [H|T] = L,
    if
        H < Value ->
            get_min_impl(T,Idx+1,Idx,H);
        true      ->
            get_min_impl(T,Idx+1,Pos,Value)
    end.

%3
%交集
insect(L1,L2) ->
    lists:sort(insect_impl(lists:sort(L1),lists:sort(L2),[])).

insect_impl(L1,L2,Result) ->
    if
        L1 =:= [];
        L2 =:= [] ->
            Result;
        true ->
            [H1|T1] = L1,
            [H2|T2] = L2,
            if
                H1 =:= H2 ->
                    insect_impl(T1,T2,[H1|Result]);
                H1 <   H2 ->
                    %pass H1
                    insect_impl(T1,L2,Result);
                true      ->
                    %pass H2
                    insect_impl(L1,T2,Result)
            end
    end.

%并集
union(L1,L2) ->
    lists:sort(union_impl(lists:sort(L1),lists:sort(L2),[])).
union_impl(L1,L2,Result) ->
    if
        L1 =:= [] ->
            [L2|Result];
        L2 =:= [] ->
            [L1|Result];
        true ->
            [H1|T1] = L1,
            [H2|T2] = L2,
            if
                H1 =:= H2 ->
                    insect_impl(T1,T2,[H1|Result]);
                true      ->
                    insect_impl(T1,T2,[H1|[H2|Result]])
            end
    end.

%4
start(Alg, CacheSize) ->
    case Alg of 
        lru ->
            lru(CacheSize);
        lfu ->
            lfu(CacheSize)
    end.

lru(CacheSize) ->
    {ok,S} = file:open("production-build00-2-4K.req",[read]),
    lru_impl(S,CacheSize,[]),
    file:close(S),
    ok.
    
lru_impl(S,CacheSize,Cache) ->
    R = file:read_line(S),
    if
        R == eof ->
            ok;
        true    ->
            {ok,Line_} = R,
            %删除\n
            Line = string:sub_string(Line_,1,length(Line_)-1),
            [_,_,Id] = string:tokens(Line," "),

            case lists:member(Id,Cache) of 
                true ->
                    Cache_new = Cache;
                false ->
                    Len = length(Cache),
                    if
                        Len < CacheSize ->
                            Cache_new = [Id|Cache];
                        true            ->
                            %lru
                            Cache_new = [Id|lists:sublist(Cache,CacheSize-1)]
                    end
            end,
            io:format("LRU Cache:~p~n",[Cache]),
            lru_impl(S,CacheSize,Cache_new)
   end.

lfu(CacheSize) ->
    {ok,S} = file:open("production-build00-2-4K.req",[read]),
    lfu_impl(S,CacheSize,[]),
    file:close(S),
    ok.

lfu_impl(S,CacheSize,Cache) ->
    R = file:read_line(S),
    if
        R == eof ->
            ok;
        true    ->
            {ok,Line_} = R,
            %删除\n
            Line = string:sub_string(Line_,1,length(Line_)-1),
            [_,_,Id] = string:tokens(Line," "),

            %tuple格式{id,count}
            Tuple = lists:keyfind(Id,1,Cache),
            if
                %不存在Id
                Tuple =:= false ->
                    Len = length(Cache),
                    if
                        Len < CacheSize ->
                            Cache_new = [{Id,1}|Cache];
                        true            ->
                            %lfu
                            %按count升序排列
                            [_|Cache_sorted_tail] = lists:keysort(2,Cache),
                            Cache_new = [{Id,1}|Cache_sorted_tail]
                    end;
                %存在id,count+1
                true ->
                     {_,Count} = Tuple,
                     Cache_new = lists:keystore(Id,1,Cache,{Id,Count+1})
            end,
            io:format("LFU Cache:~p~n",[Cache_new]),
            lfu_impl(S,CacheSize,Cache_new)
   end.

分享到:
评论

相关推荐

    ERLANG测试题

    可以参考一下ERLANG测试题

    xiandiao_erlang_Erlang课后习题_

    erlang程序设计第二版课后习题源码,源码实现

    erlang程序设计第二版课后习题答案(精简版).zip

    erlang程序设计第二版习题答案 ,是我自己写的习题解答,也对照过网上的一些答案,相对来说是更加简洁和符合题意的解答,不过后面几章因为时间问题没有写完,有问题或者意见可以私信找我。

    Erlang程序设计(第二版)及源码

    本书由Erlang之父Joe Armstrong编写,是毋庸置疑的经典著作。书中兼顾了顺序编程、并发编程和...第2版全新改写,反应了自第1版面世以来Erlang历经的所有变化,添加了大量针对初学者的内容,并在每章后都附上了练习题。

    Erlang编程指南

    “即便我已经使用Erlang多年,在编程的时候仍然需要参考《Erlang编程指南》。不同层次的Erlang程序员都会发现本书是有价值的学习和参考资料..., 《Erlang编程指南》每章末尾都提供了练习题,并且由简单的示例贯穿全书。

    erlings:小练习让您习惯于阅读和编写Erlang代码

    练习题 练习分为5个部分。 阅读要完成它们。一定要 ,但是您可以根据自己的意愿随意做。 首先单击下面的练习之一或运行: $ make A.顺序编程 过滤值 你好,世界 你好模式 清单 撤销 删除连续 偶数斐波那契数 减少...

    erlang-exercises:一系列 Erlang 练习,展示了该语言的一些基础知识

    二郎练习 01 - pi.erl - 计算 pi 的值到小数点后 5 位。 02 - list.erl - 删除重复项并计算列表的长度。 还有另一个函数可以读取文件并删除重复项并计算列表的长度。 03 - charcount.erl - 在不使用多个进程的情况...

    erlang_etudes

    erlang_etude 我的《 Erlang练习曲》编程解决方案-http: (代码示例,作者版权所有)。 在尝试解决练习题时,我修改了一些代码示例,而某些代码示例则取自上述网站。

    移动通信原理习题.doc

    移动通信原理习题 Chapter 3 1、证明对六边形系统,同频复用因子为,其中. 2、一个FDD 蜂窝电话系统分配有24MHz总带宽,并使用两个30kHz信道来提供全双工语音和控制信道 。设每个小区电话用户的业务量为0.1 Erlang。...

    functional-programming:这是KTH提供的函数式和并发编程课程

    练习题强烈建议您进行一些练习。 一旦掌握了这些诀窍,它们将非常容易且可行,除了学习Elixir之外,还有望为您提供有关功能和并发编程的更深入的知识。研讨班我们每年将使用的研讨会只是练习的一部分。 在研讨会期间...

    Y分钟学习X种语言

    在每节后面有问题和习题的编程书籍是很好的学习工具,可以练习你学到的知识,但这样的 书很少。最近出版的《Functional Programming with F#》是一个很优秀的例子,它的每 章后面都有问题习题。 也许几个小时你就能...

Global site tag (gtag.js) - Google Analytics