如何交换 Erlang 矩阵中第一行的第二个元素和最后一行的倒数第二个元素?

huangapple go评论53阅读模式
英文:

How to swap second element of first row and penultimate element of last row from a matrix Erlang?

问题

Description: 给定一个维度为N的方阵。编写两种解决个体任务的实现:

  1. 基于在单独的进程中执行子任务 - 对于矩阵的行进行N次迭代;
  2. 基于顺序编程在一个进程中执行 - 对于矩阵的行进行2N次迭代。在两种实现中,不应使用计数器。

我的个人任务是交换第一行的第二个元素和最后一行的倒数第二个元素。

我已经能够遍历矩阵并替换第一行和最后一行的第一个元素,但如何处理第一行的第二个元素和最后一行的倒数第二个元素 - 对我来说有难度。

任何帮助,甚至只是关于方向的想法,将不胜感激。

-module(mtr).
-export([main1/1, main2/1, swap1/1, swap2/1, mtr_gn1/1]).

%% ver.1
%% (N) -> NewMatrix
main1(N) ->
    timer:tc(mtr, swap1, [mtr_gn1(N)]).

swap1([[A|R1]|Rs]) ->
    Pid = self(),
    spawn(mtr1, ch, [Rs, A, Pid]),
    receive
        {z, Z} -> Z
    end,
    receive
        {list, L1} -> L1
    end,
    [[Z|R1]|L1].

%% ver.2
main2(N) ->
    timer:tc(mtr, swap2, [mtr_gn1(N)]).

swap2([[A|R1]|Rs]) ->
    [Z|_Rz] = lists:last(Rs),
    [[[Z|R1]]|ch2(Rs, A)].

ch2([[_Z|Rz]], A) ->
    [[A|Rz]];
ch2([R|Rs], A) ->
    [R|ch2(Rs, A)].

mtr_gn1(N) ->
    [lists:map(fun(X) -> X/100 end, lists:seq(1, N)) |
     [[R || R <- lists:seq(1, N)] || _K <- lists:seq(1, N-1)]].


-module(mtr1).
-export([ch/3]).

%% ver.1
ch(L, A, Pid) ->
    Pid ! {list, ch1(L, A, Pid)}.

ch1([[Z|Rz]], A, Pid) ->
    Pid ! {z, Z},
    [[A|Rz]];
ch1([X|L], A, Pid) ->
    [X|ch1(L, A, Pid)].

这是您提供的代码的翻译部分。

英文:

Description: a square matrix of dimension N is given. Write two implementations of solving an individual task:

  1. based on the execution of subtasks in separate processes - for N iterations in the rows of the matrix;
  2. based on sequential programming in one process - for 2N iterations along the rows of the matrix. In both implementations, counters should not be used.

My individual task is swap second element of first row and penultimate element of last row.

I was able to go through the matrix and replace the first elements of the first and last row, but how to do this for the second element of the first row and the penultimate element of the last row - difficult for me.

Any help, even just thoughts on direction, would be greatly appreciated.

-module(mtr).
-export([main1/1, main2/1, swap1/1, swap2/1, mtr_gn1/1]).

%% ver.1
%% (N) -&gt; NewMatrix
main1(N) -&gt;
    timer:tc(mtr, swap1, [mtr_gn1(N)]).

swap1([[A|R1]|Rs]) -&gt;
    Pid = self(),
    spawn(mtr1, ch, [Rs, A, Pid]),
    receive
        {z, Z} -&gt; Z
    end,
    receive
        {list, L1} -&gt; L1
    end,
    [[Z|R1]|L1].

%% ver.2
main2(N) -&gt;
    timer:tc(mtr, swap2, [mtr_gn1(N)]).

swap2([[A|R1]|Rs]) -&gt;
    [Z|_Rz] = lists:last(Rs),
    [[[Z|R1]]|ch2(Rs, A)].

ch2([[_Z|Rz]], A) -&gt;
    [[A|Rz]];
ch2([R|Rs], A) -&gt;
    [R|ch2(Rs, A)].

mtr_gn1(N) -&gt;
    [lists:map(fun(X) -&gt; X/100 end, lists:seq(1, N)) |
     [[R || R &lt;- lists:seq(1, N)] || _K &lt;- lists:seq(1, N-1)]].


-module(mtr1).
-export([ch/3]).

%% ver.1
ch(L, A, Pid) -&gt;
    Pid ! {list, ch1(L, A, Pid)}.

ch1([[Z|Rz]], A, Pid) -&gt;
    Pid ! {z, Z},
    [[A|Rz]];
ch1([X|L], A, Pid) -&gt;
    [X|ch1(L, A, Pid)].

答案1

得分: 0

我认为在N次迭代中是不可能的,所以我在单独的进程中进行了N+1次迭代(这是主要任务):

模块1:

-module(mtr).
-export([main1/1, main2/1, swap1/1, swap2/1, mtr_gn1/1]).

mtr_gn1(N) ->
    [lists:map(fun(X) -> X/100 end, lists:seq(1, N)) |
    [ [R||R <- lists:seq(1, N)] || _K<- lists:seq(1,N-1)]].

main1(N) -> 
    timer:tc(mtr, swap1, [mtr_gn1(N)]).

swap1([[A, B|R1]|Rs]) ->
    Pid = self(),
    spawn(mtr1, ch, [Rs,B,Pid]),
    receive
    {z, Z} -> Z
    end, receive
    {list, L1} -> L1 end,
    [[A,Z|R1]|L1].

main2(N) -> 
    timer:tc(mtr, swap2, [mtr_gn1(N)]).

swap2([[A, B |R1]|Rs]) ->
    [_Z, P|_Rz] = lists:reverse(lists:last(Rs)), 
    [[A, P|R1]|ch2(Rs,B)].

ch2([[Z|Rz]],A) ->
    [swap_penultimate([Z|Rz], A, [])];

ch2([R|Rs],A) ->
    [R|ch2(Rs,A)].

swap_penultimate([_Z,Y], A, Acc) -> 
    Acc ++ [A, Y];

swap_penultimate([H|T], A, Acc) ->
    swap_penultimate(T, A, Acc ++ [H]).

模块2:

-module(mtr1). 
-export([ch/3]).

ch(L,A,Pid) ->
    Pid ! {list, ch1(L,A,Pid)}. 

ch1([[Z | Rz]], A, Pid) -> 
    [swap_penultimate([Z | Rz], A, Pid, [])];

ch1([X|L],A,Pid) ->
    [X|ch1(L,A,Pid)].

swap_penultimate([Z,Y], A, Pid, Acc) -> 
    Pid ! {z, Z},
    Acc ++ [A, Y];

swap_penultimate([H|T], A, Pid, Acc) ->
    swap_penultimate(T, A, Pid, Acc ++ [H]).
英文:

I think it`s impossible for N iterations, so I did it like that for N+1 iterations in separate processes (it was main task):

module 1:

-module(mtr).
-export([main1/1, main2/1, swap1/1, swap2/1, mtr_gn1/1]).

mtr_gn1(N) -&gt;
    [lists:map(fun(X) -&gt; X/100 end, lists:seq(1, N)) |
    [ [R||R &lt;- lists:seq(1, N)] || _K&lt;- lists:seq(1,N-1)]].

main1(N) -&gt; 
    timer:tc(mtr, swap1, [mtr_gn1(N)]).

swap1([[A, B|R1]|Rs]) -&gt; 
    Pid = self(),
    spawn(mtr1, ch, [Rs,B,Pid]),
    receive
    {z, Z} -&gt; Z
    end, receive
    {list, L1} -&gt; L1 end,
    [[A,Z|R1]|L1].


main2(N) -&gt; 
    timer:tc(mtr, swap2, [mtr_gn1(N)]).

swap2([[A, B |R1]|Rs]) -&gt; 
    [_Z, P|_Rz] = lists:reverse(lists:last(Rs)), 
    [[A, P|R1]|ch2(Rs,B)].

ch2([[Z|Rz]],A) -&gt;
    [swap_penultimate([Z|Rz], A, [])];

ch2([R|Rs],A) -&gt;
    [R|ch2(Rs,A)].

swap_penultimate([_Z,Y], A, Acc) -&gt; 
    Acc ++ [A, Y];
    
swap_penultimate([H|T], A, Acc) -&gt;
    swap_penultimate(T, A, Acc ++ [H]).

module 2:

-module(mtr1). 
-export([ch/3]).

ch(L,A,Pid) -&gt;
    Pid ! {list, ch1(L,A,Pid)}. 

ch1([[Z | Rz]], A, Pid) -&gt; 
    [swap_penultimate([Z | Rz], A, Pid, [])];

ch1([X|L],A,Pid) -&gt;
    [X|ch1(L,A,Pid)].

swap_penultimate([Z,Y], A, Pid, Acc) -&gt; 
    Pid ! {z, Z},
    Acc ++ [A, Y];

swap_penultimate([H|T], A, Pid, Acc) -&gt;
    swap_penultimate(T, A, Pid, Acc ++ [H]).

huangapple
  • 本文由 发表于 2023年6月1日 01:52:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定