列出Erlang的复制挑战

列出Erlang的复制挑战,erlang,Erlang,我正在研究Erlang解决黑客问题。有一个问题叫做。我最后给出了这样一个解决方案: -module(solution). -export([main/0]). process_input(Repeats)-> Line = io:get_line(""), case string:len(Line) of 1 -> ok; _ -> output(Line, Repeats), process_input(Repeats)

我正在研究Erlang解决黑客问题。有一个问题叫做。我最后给出了这样一个解决方案:

-module(solution).
-export([main/0]).

process_input(Repeats)->
    Line = io:get_line(""),
    case string:len(Line) of
        1 -> ok;
        _ -> output(Line, Repeats), process_input(Repeats)
    end.

output(_, 0)-> ok;
output(Line, Repeats)->
    io:format(Line),
    output(Line, Repeats - 1).

main()->
  {ok, [Repeats]} = io:fread("", "~d"),
  process_input(Repeats).

但是这个解决方案有一个问题:我希望最后一行是空的(事实上,最后一行是一个没有
\n
的数字)。有什么想法吗

您必须关闭流的回声,并使用


您必须关闭流的回声,并且


您必须关闭流的回声,并且


您必须关闭流的回声,并且


我刚刚更新了解决问题的代码,所以我们的想法是将输入保存在累加器中(在本例中是一个列表),然后在最后处理列表,如果您想避免在任何时候在屏幕上写入内容的输出,您可以禁用回显,如我在另一个答案中所示

-module(solution).
-export([main/0]).

process_input(Data)->
    Number = io:get_line(""),
    case string:len(Number) of
        1 ->
          Data;
        _ -> 
          process_input(Data ++ [Number])
    end.


process_output([], _)->
    ok;
process_output([H|Data], Repeats)->
    print(H, Repeats),
    process_output(Data, Repeats).

print(_, 0) ->
    ok;
print(Element, Times) ->
    io:format(Element),
    print(Element, Times - 1).

main()->
    {ok, [Repeats]} = io:fread("", "~d"),
    Data = process_input([]),
    process_output(Data, Repeats).
以及测试:

rorra:~/erlang > erl
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V6.4  (abort with ^G)
1> c(solution).
{ok,solution}
2> solution:main().
3
1
2
3
4

1
1
1
2
2
2
3
3
3
4
4
4
ok
3> 

我刚刚更新了解决问题的代码,所以我们的想法是将输入保存在累加器中(在本例中是一个列表),然后在最后处理列表,如果您想避免在任何时候在屏幕上写入内容的输出,您可以禁用回显,如我在另一个答案中所示

-module(solution).
-export([main/0]).

process_input(Data)->
    Number = io:get_line(""),
    case string:len(Number) of
        1 ->
          Data;
        _ -> 
          process_input(Data ++ [Number])
    end.


process_output([], _)->
    ok;
process_output([H|Data], Repeats)->
    print(H, Repeats),
    process_output(Data, Repeats).

print(_, 0) ->
    ok;
print(Element, Times) ->
    io:format(Element),
    print(Element, Times - 1).

main()->
    {ok, [Repeats]} = io:fread("", "~d"),
    Data = process_input([]),
    process_output(Data, Repeats).
以及测试:

rorra:~/erlang > erl
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V6.4  (abort with ^G)
1> c(solution).
{ok,solution}
2> solution:main().
3
1
2
3
4

1
1
1
2
2
2
3
3
3
4
4
4
ok
3> 

我刚刚更新了解决问题的代码,所以我们的想法是将输入保存在累加器中(在本例中是一个列表),然后在最后处理列表,如果您想避免在任何时候在屏幕上写入内容的输出,您可以禁用回显,如我在另一个答案中所示

-module(solution).
-export([main/0]).

process_input(Data)->
    Number = io:get_line(""),
    case string:len(Number) of
        1 ->
          Data;
        _ -> 
          process_input(Data ++ [Number])
    end.


process_output([], _)->
    ok;
process_output([H|Data], Repeats)->
    print(H, Repeats),
    process_output(Data, Repeats).

print(_, 0) ->
    ok;
print(Element, Times) ->
    io:format(Element),
    print(Element, Times - 1).

main()->
    {ok, [Repeats]} = io:fread("", "~d"),
    Data = process_input([]),
    process_output(Data, Repeats).
以及测试:

rorra:~/erlang > erl
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V6.4  (abort with ^G)
1> c(solution).
{ok,solution}
2> solution:main().
3
1
2
3
4

1
1
1
2
2
2
3
3
3
4
4
4
ok
3> 

我刚刚更新了解决问题的代码,所以我们的想法是将输入保存在累加器中(在本例中是一个列表),然后在最后处理列表,如果您想避免在任何时候在屏幕上写入内容的输出,您可以禁用回显,如我在另一个答案中所示

-module(solution).
-export([main/0]).

process_input(Data)->
    Number = io:get_line(""),
    case string:len(Number) of
        1 ->
          Data;
        _ -> 
          process_input(Data ++ [Number])
    end.


process_output([], _)->
    ok;
process_output([H|Data], Repeats)->
    print(H, Repeats),
    process_output(Data, Repeats).

print(_, 0) ->
    ok;
print(Element, Times) ->
    io:format(Element),
    print(Element, Times - 1).

main()->
    {ok, [Repeats]} = io:fread("", "~d"),
    Data = process_input([]),
    process_output(Data, Repeats).
以及测试:

rorra:~/erlang > erl
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V6.4  (abort with ^G)
1> c(solution).
{ok,solution}
2> solution:main().
3
1
2
3
4

1
1
1
2
2
2
3
3
3
4
4
4
ok
3> 

我认为在缺少最后换行符的情况下,从
io:get_line/1
处理
eof
更容易,同时处理一个空白行以指示输入结束:

-module(solution).
-export([start/0]).

process_input(Repeat, Acc)->
    case io:get_line("") of
        Done when Done == eof; Done == "\n" ->
            output(lists:reverse(Acc));
        Line ->
            Val = string:strip(Line, right, $\n),
            Str = lists:duplicate(Repeat, Val),
            process_input(Repeat, [Str|Acc])
    end.

output(Lines)->
    Out = [string:join(L, "\n")++"\n" || L <- Lines],
    io:format("~s", [Out]).

start()->
    {ok, [Repeat]} = io:fread("", "~d"),
    process_input(Repeat, []).
然后像这样运行编译的
解决方案
模块,得到本例中预期的三倍输出:

$ cat in | erl -noshell -s solution -s init stop
1
1
1
2
2
2
3
3
3
4
4
4
将最后一个换行符添加到文件中的
,会得到相同的结果(请尝试)。我们还可以创建每行超过一个字符的输入:

$ printf '2\nhey\nhey\nwhat\ncan\nI\ndo' > in2
$ cat in2 | erl -noshell -s solution -s init stop
hey
hey
hey
hey
what
what
can
can
I
I
do
do

对于2
文件中的
,我们也得到了预期的加倍输出。

我认为在缺少最后换行符的情况下,从
io:get_line/1
处理
eof
以及处理空白行以指示输入结束更容易:

-module(solution).
-export([start/0]).

process_input(Repeat, Acc)->
    case io:get_line("") of
        Done when Done == eof; Done == "\n" ->
            output(lists:reverse(Acc));
        Line ->
            Val = string:strip(Line, right, $\n),
            Str = lists:duplicate(Repeat, Val),
            process_input(Repeat, [Str|Acc])
    end.

output(Lines)->
    Out = [string:join(L, "\n")++"\n" || L <- Lines],
    io:format("~s", [Out]).

start()->
    {ok, [Repeat]} = io:fread("", "~d"),
    process_input(Repeat, []).
然后像这样运行编译的
解决方案
模块,得到本例中预期的三倍输出:

$ cat in | erl -noshell -s solution -s init stop
1
1
1
2
2
2
3
3
3
4
4
4
将最后一个换行符添加到
文件中的
,会得到相同的结果(请尝试)。我们还可以创建每行超过一个字符的输入:

$ printf '2\nhey\nhey\nwhat\ncan\nI\ndo' > in2
$ cat in2 | erl -noshell -s solution -s init stop
hey
hey
hey
hey
what
what
can
can
I
I
do
do

对于2
文件中的
,我们也得到了预期的加倍输出。

我认为在缺少最后换行符的情况下,从
io:get_line/1
处理
eof
以及处理空白行以指示输入结束更容易:

-module(solution).
-export([start/0]).

process_input(Repeat, Acc)->
    case io:get_line("") of
        Done when Done == eof; Done == "\n" ->
            output(lists:reverse(Acc));
        Line ->
            Val = string:strip(Line, right, $\n),
            Str = lists:duplicate(Repeat, Val),
            process_input(Repeat, [Str|Acc])
    end.

output(Lines)->
    Out = [string:join(L, "\n")++"\n" || L <- Lines],
    io:format("~s", [Out]).

start()->
    {ok, [Repeat]} = io:fread("", "~d"),
    process_input(Repeat, []).
然后像这样运行编译的
解决方案
模块,得到本例中预期的三倍输出:

$ cat in | erl -noshell -s solution -s init stop
1
1
1
2
2
2
3
3
3
4
4
4
将最后一个换行符添加到
文件中的
,会得到相同的结果(请尝试)。我们还可以创建每行超过一个字符的输入:

$ printf '2\nhey\nhey\nwhat\ncan\nI\ndo' > in2
$ cat in2 | erl -noshell -s solution -s init stop
hey
hey
hey
hey
what
what
can
can
I
I
do
do

对于2
文件中的
,我们也得到了预期的加倍输出。

我认为在缺少最后换行符的情况下,从
io:get_line/1
处理
eof
以及处理空白行以指示输入结束更容易:

-module(solution).
-export([start/0]).

process_input(Repeat, Acc)->
    case io:get_line("") of
        Done when Done == eof; Done == "\n" ->
            output(lists:reverse(Acc));
        Line ->
            Val = string:strip(Line, right, $\n),
            Str = lists:duplicate(Repeat, Val),
            process_input(Repeat, [Str|Acc])
    end.

output(Lines)->
    Out = [string:join(L, "\n")++"\n" || L <- Lines],
    io:format("~s", [Out]).

start()->
    {ok, [Repeat]} = io:fread("", "~d"),
    process_input(Repeat, []).
然后像这样运行编译的
解决方案
模块,得到本例中预期的三倍输出:

$ cat in | erl -noshell -s solution -s init stop
1
1
1
2
2
2
3
3
3
4
4
4
将最后一个换行符添加到
文件中的
,会得到相同的结果(请尝试)。我们还可以创建每行超过一个字符的输入:

$ printf '2\nhey\nhey\nwhat\ncan\nI\ndo' > in2
$ cat in2 | erl -noshell -s solution -s init stop
hey
hey
hey
hey
what
what
can
can
I
I
do
do
对于2
文件中的
,我们也得到了预期的加倍输出。

我自己的答案:

-module(solution).
-export([main/0]).

process_input(Repeats)->
    Line = io:get_line(""),
    case lists:reverse(Line) of
        "\n" ++ _ -> output(Line, Repeats), process_input(Repeats);
        _ -> output(Line ++ "~n" , Repeats)
    end.

output(_, 0)-> ok;
output(Line, Repeats)->
    io:format(Line),
    output(Line, Repeats - 1).

main()->
  {ok, [Repeats]} = io:fread("", "~d"),
  process_input(Repeats).
我自己的回答是:

-module(solution).
-export([main/0]).

process_input(Repeats)->
    Line = io:get_line(""),
    case lists:reverse(Line) of
        "\n" ++ _ -> output(Line, Repeats), process_input(Repeats);
        _ -> output(Line ++ "~n" , Repeats)
    end.

output(_, 0)-> ok;
output(Line, Repeats)->
    io:format(Line),
    output(Line, Repeats - 1).

main()->
  {ok, [Repeats]} = io:fread("", "~d"),
  process_input(Repeats).
我自己的回答是:

-module(solution).
-export([main/0]).

process_input(Repeats)->
    Line = io:get_line(""),
    case lists:reverse(Line) of
        "\n" ++ _ -> output(Line, Repeats), process_input(Repeats);
        _ -> output(Line ++ "~n" , Repeats)
    end.

output(_, 0)-> ok;
output(Line, Repeats)->
    io:format(Line),
    output(Line, Repeats - 1).

main()->
  {ok, [Repeats]} = io:fread("", "~d"),
  process_input(Repeats).
我自己的回答是:

-module(solution).
-export([main/0]).

process_input(Repeats)->
    Line = io:get_line(""),
    case lists:reverse(Line) of
        "\n" ++ _ -> output(Line, Repeats), process_input(Repeats);
        _ -> output(Line ++ "~n" , Repeats)
    end.

output(_, 0)-> ok;
output(Line, Repeats)->
    io:format(Line),
    output(Line, Repeats - 1).

main()->
  {ok, [Repeats]} = io:fread("", "~d"),
  process_input(Repeats).

下面是我的想法(在上面答案的帮助下)。我之所以添加我的解决方案,是因为它还处理问题中所述的约束:

-模块(解决方案)。
-导出([main/0])。
main()->get_输入([])。
打印列表(u,[124;[])->确定;
打印列表(NumTimes,[\u124; list])->
编号=hd(列表),
打印列表编号(NumTimes,number),
打印列表(NumTimes,list)。
打印列表编号(NumTimes,number)
什么时候
(NumTimes>0)
和
(数字>=1)
和
(数字=<100)
->
io:fwrite(“~B~n,[Number]),
打印列表编号(NumTimes-1,编号);
打印列表编号(u,u)->确定。
获取输入(Acc)->
案例io:get_行(“”)of
什么时候做的
完成==eof;
完成==“\n”
->
NumTimes=hd(Acc),
%%确保我们仅在列表长度不大于10时运行此操作
如果
(长度(Acc)-1=<10)
和
(NumTimes=<100)
->
打印列表(NumTimes,Acc);
正确->确定
结束;
行->
{NumInt,}=string:to_整数(行),
列表=Acc++[NumInt],
获取输入(列表)
结束。

我对Erlang很陌生,所以请原谅我,如果这是混乱/低效的

以下是我的想法(在上面答案的帮助下)。我之所以添加我的解决方案,是因为它还处理问题中所述的约束:

-模块(解决方案)。
-导出([main/0])。
main()->get_输入([])。
打印列表(u,[124;[])->确定;
打印列表(NumTimes,[\u124; list])->
编号=hd(列表),
打印列表编号(NumTimes,number),
打印列表(NumTimes,list)。
打印列表编号(NumTimes,number)
什么时候
(NumTimes>0)
和
(数字>=1)
和
(数字=<100)
->
io:fwrite(“~B~n,[Number]),
打印列表编号(NumTimes-1,编号);