英文:
How to swap second element of first row and penultimate element of last row from a matrix Erlang?
问题
Description: 给定一个维度为N的方阵。编写两种解决个体任务的实现:
- 基于在单独的进程中执行子任务 - 对于矩阵的行进行N次迭代;
- 基于顺序编程在一个进程中执行 - 对于矩阵的行进行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:
- based on the execution of subtasks in separate processes - for N iterations in the rows of the matrix;
- 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) -> 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)].
答案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) ->
[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]).
module 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]).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论