Append numbers at same position in strings: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Raku}}: Updated for changed task requirements. Add some other similar manipulations.) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 11: | Line 11: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
<syntaxhighlight lang=11l>V list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
<syntaxhighlight lang="11l">V list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
||
V list2 = [10, 11, 12, 13, 14, 15, 16, 17, 18] |
V list2 = [10, 11, 12, 13, 14, 15, 16, 17, 18] |
||
V list3 = [19, 20, 21, 22, 23, 24, 25, 26, 27] |
V list3 = [19, 20, 21, 22, 23, 24, 25, 26, 27] |
||
Line 23: | Line 23: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada">with Ada.Text_Io; |
||
with Ada.Strings.Fixed; |
with Ada.Strings.Fixed; |
||
Line 61: | Line 61: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
<syntaxhighlight lang=algol68>BEGIN # form a list of strings by concatenating numbers from 3 lists # |
<syntaxhighlight lang="algol68">BEGIN # form a list of strings by concatenating numbers from 3 lists # |
||
[]INT list1 = ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) |
[]INT list1 = ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) |
||
, list2 = ( 10, 11, 12, 13, 14, 15, 16, 17, 18 ) |
, list2 = ( 10, 11, 12, 13, 14, 15, 16, 17, 18 ) |
||
Line 84: | Line 84: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
<syntaxhighlight lang=apl>list1←1 2 3 4 5 6 7 8 9 |
<syntaxhighlight lang="apl">list1←1 2 3 4 5 6 7 8 9 |
||
list2←10 11 12 13 14 15 16 17 18 |
list2←10 11 12 13 14 15 16 17 18 |
||
list3←19 20 21 22 23 24 25 26 27 |
list3←19 20 21 22 23 24 25 26 27 |
||
Line 93: | Line 93: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="autohotkey">list1 := [1,2,3,4,5,6,7,8,9] |
||
list2 := [10,11,12,13,14,15,16,17,18] |
list2 := [10,11,12,13,14,15,16,17,18] |
||
list3 := [19,20,21,22,23,24,25,26,27] |
list3 := [19,20,21,22,23,24,25,26,27] |
||
Line 107: | Line 107: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="awk"> |
||
# syntax: GAWK -f APPEND_NUMBERS_AT_SAME_POSITION_IN_STRINGS.AWK |
# syntax: GAWK -f APPEND_NUMBERS_AT_SAME_POSITION_IN_STRINGS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 131: | Line 131: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang=c>#include<stdio.h> |
<syntaxhighlight lang="c">#include<stdio.h> |
||
#include<stdlib.h> |
#include<stdlib.h> |
||
Line 143: | Line 143: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
<syntaxhighlight lang=clu>append = proc (lists: sequence[sequence[int]]) returns (sequence[int]) |
<syntaxhighlight lang="clu">append = proc (lists: sequence[sequence[int]]) returns (sequence[int]) |
||
n_lists: int := sequence[sequence[int]]$size(lists) |
n_lists: int := sequence[sequence[int]]$size(lists) |
||
n_items: int := sequence[int]$size(lists[1]) |
n_items: int := sequence[int]$size(lists[1]) |
||
Line 171: | Line 171: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
<syntaxhighlight lang=fsharp> |
<syntaxhighlight lang="fsharp"> |
||
// Append numbers at same position in strings. Nigel Galloway: December 29th., 2021 |
// Append numbers at same position in strings. Nigel Galloway: December 29th., 2021 |
||
let rec fG n g l=seq{match n,g,l with (n::x,g::y,l::z)->yield int((string n)+(string g)+(string l)); yield! fG x y z |_->()} |
let rec fG n g l=seq{match n,g,l with (n::x,g::y,l::z)->yield int((string n)+(string g)+(string l)); yield! fG x y z |_->()} |
||
Line 183: | Line 183: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
<syntaxhighlight lang=factor>USING: kernel math.parser math.ranges present prettyprint |
<syntaxhighlight lang="factor">USING: kernel math.parser math.ranges present prettyprint |
||
sequences ; |
sequences ; |
||
Line 193: | Line 193: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<syntaxhighlight lang=freebasic>dim as integer list1(1 to 9) = {1,2,3,4,5,6,7,8,9} |
<syntaxhighlight lang="freebasic">dim as integer list1(1 to 9) = {1,2,3,4,5,6,7,8,9} |
||
dim as integer list2(1 to 9) = {10,11,12,13,14,15,16,17,18} |
dim as integer list2(1 to 9) = {10,11,12,13,14,15,16,17,18} |
||
dim as integer list3(1 to 9) = {19,20,21,22,23,24,25,26,27} |
dim as integer list3(1 to 9) = {19,20,21,22,23,24,25,26,27} |
||
Line 207: | Line 207: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 230: | Line 230: | ||
{{works with|jq}} |
{{works with|jq}} |
||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
<syntaxhighlight lang=jq>def list1 : [ range(1;10) ]; |
<syntaxhighlight lang="jq">def list1 : [ range(1;10) ]; |
||
def list2 : [ range(10; 19)]; |
def list2 : [ range(10; 19)]; |
||
def list3 : [ range(19; 28) ]; |
def list3 : [ range(19; 28) ]; |
||
Line 243: | Line 243: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang=julia>list1 = [1,2,3,4,5,6,7,8,9] |
<syntaxhighlight lang="julia">list1 = [1,2,3,4,5,6,7,8,9] |
||
list2 = [10,11,12,13,14,15,16,17,18] |
list2 = [10,11,12,13,14,15,16,17,18] |
||
list3 = [19,20,21,22,23,24,25,26,27] |
list3 = [19,20,21,22,23,24,25,26,27] |
||
Line 251: | Line 251: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="scheme"> |
||
Using a function returning an array |
Using a function returning an array |
||
Line 285: | Line 285: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mathematica">list1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
||
list2 = {10, 11, 12, 13, 14, 15, 16, 17, 18}; |
list2 = {10, 11, 12, 13, 14, 15, 16, 17, 18}; |
||
list3 = {19, 20, 21, 22, 23, 24, 25, 26, 27}; |
list3 = {19, 20, 21, 22, 23, 24, 25, 26, 27}; |
||
Line 297: | Line 297: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
The following <tt>program</tt> requires (at least) an ISO 7185-compliant processor supporting features at level 1 (specifically “conformant array parameters” are being used). |
The following <tt>program</tt> requires (at least) an ISO 7185-compliant processor supporting features at level 1 (specifically “conformant array parameters” are being used). |
||
<syntaxhighlight lang=pascal>program appendNumbersAtSamePositionInStrings(output); |
<syntaxhighlight lang="pascal">program appendNumbersAtSamePositionInStrings(output); |
||
type |
type |
||
Line 392: | Line 392: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<syntaxhighlight lang=perl>use strict; |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
Line 415: | Line 415: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%V%V\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">,{{</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">27</span><span style="color: #0000FF;">,</span><span style="color: #000000;">19</span><span style="color: #0000FF;">)},{</span><span style="color: #000000;">1e4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})},</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)}),</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">),</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%V%V\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">,{{</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">27</span><span style="color: #0000FF;">,</span><span style="color: #000000;">19</span><span style="color: #0000FF;">)},{</span><span style="color: #000000;">1e4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})},</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)}),</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">),</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span> |
||
<!--</syntaxhighlight>--> |
<!--</syntaxhighlight>--> |
||
Line 424: | Line 424: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
<syntaxhighlight lang=python>list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
<syntaxhighlight lang="python">list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
||
list2 = [10, 11, 12, 13, 14, 15, 16, 17, 18] |
list2 = [10, 11, 12, 13, 14, 15, 16, 17, 18] |
||
list3 = [19, 20, 21, 22, 23, 24, 25, 26, 27] |
list3 = [19, 20, 21, 22, 23, 24, 25, 26, 27] |
||
Line 435: | Line 435: | ||
which is also expressible as a map: |
which is also expressible as a map: |
||
<syntaxhighlight lang=python>print(list(map( |
<syntaxhighlight lang="python">print(list(map( |
||
lambda x, y, z: f'{x}{y}{z}', |
lambda x, y, z: f'{x}{y}{z}', |
||
[1, 2, 3, 4, 5, 6, 7, 8, 9], |
[1, 2, 3, 4, 5, 6, 7, 8, 9], |
||
Line 446: | Line 446: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Various manipulations. |
Various manipulations. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="raku" line>my @lists = 1..9, 10..18, 19..27; |
||
put [Z~] @lists; # the task |
put [Z~] @lists; # the task |
||
put [Z~] @lists».flip; # each component reversed |
put [Z~] @lists».flip; # each component reversed |
||
Line 458: | Line 458: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight lang=ring> |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
list1 = [1,2,3,4,5,6,7,8,9] |
list1 = [1,2,3,4,5,6,7,8,9] |
||
Line 491: | Line 491: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang=ruby>list1 = (1..9) .to_a |
<syntaxhighlight lang="ruby">list1 = (1..9) .to_a |
||
list2 = (10..18).to_a |
list2 = (10..18).to_a |
||
list3 = (19..27).to_a |
list3 = (19..27).to_a |
||
Line 501: | Line 501: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight lang=ruby>var lists = [ |
<syntaxhighlight lang="ruby">var lists = [ |
||
[1,2,3,4,5,6,7,8,9], |
[1,2,3,4,5,6,7,8,9], |
||
[10,11,12,13,14,15,16,17,18], |
[10,11,12,13,14,15,16,17,18], |
||
Line 516: | Line 516: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
<syntaxhighlight lang=vlang>fn main() { |
<syntaxhighlight lang="vlang">fn main() { |
||
list1 := [1, 2, 3, 4, 5, 6, 7, 8, 9]! |
list1 := [1, 2, 3, 4, 5, 6, 7, 8, 9]! |
||
list2 := [10, 11, 12, 13, 14, 15, 16, 17, 18]! |
list2 := [10, 11, 12, 13, 14, 15, 16, 17, 18]! |
||
Line 530: | Line 530: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
<syntaxhighlight lang=ecmascript>var list1 = (1..9).toList |
<syntaxhighlight lang="ecmascript">var list1 = (1..9).toList |
||
var list2 = (10..18).toList |
var list2 = (10..18).toList |
||
var list3 = (19..27).toList |
var list3 = (19..27).toList |
||
Line 542: | Line 542: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="xpl0">intI;forI:=1to9do[IntOut(0,I*10000+(I+9)*100+I+18);ChOut(0,^ )]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Revision as of 21:28, 29 August 2022
Append numbers at same position in strings is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
Let given three list:
list1 = [1,2,3,4,5,6,7,8,9]
list2 = [10,11,12,13,14,15,16,17,18]
list3 = [19,20,21,22,23,24,25,26,27]
Append numbers at same position in strings.
The result should be:
list = [11019,21120,31221,41322,51423,61524,71625,81726,91827]
11l
V list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
V list2 = [10, 11, 12, 13, 14, 15, 16, 17, 18]
V list3 = [19, 20, 21, 22, 23, 24, 25, 26, 27]
print(zip(list1, list2, list3).map((n1, n2, n3) -> String(n1)‘’n2‘’n3))
- Output:
[11019, 21120, 31221, 41322, 51423, 61524, 71625, 81726, 91827]
Ada
with Ada.Text_Io;
with Ada.Strings.Fixed;
procedure Append_Numbers is
use Ada.Text_Io, Ada.Strings;
type List_Type is array (Positive range <>) of Natural;
procedure Put (List : List_Type) is
begin
Put ("[");
for E of List loop
Put (Natural'Image (E));
end loop;
Put ("]");
end Put;
List_1 : constant List_Type := ( 1, 2, 3, 4, 5, 6, 7, 8, 9);
List_2 : constant List_Type := (10, 11, 12, 13, 14, 15, 16, 17, 18);
List_3 : constant List_Type := (19, 20, 21, 22, 23, 24, 25, 26, 27);
List : List_Type (List_1'Range);
begin
for A in List'Range loop
List (A) := Natural'Value
(Fixed.Trim (Natural'Image (List_1 (A)), Left) &
Fixed.Trim (Natural'Image (List_2 (A)), Left) &
Fixed.Trim (Natural'Image (List_3 (A)), Left));
end loop;
Put (List); New_Line;
end Append_Numbers;
- Output:
[ 11019 21120 31221 41322 51423 61524 71625 81726 91827]
ALGOL 68
BEGIN # form a list of strings by concatenating numbers from 3 lists #
[]INT list1 = ( 1, 2, 3, 4, 5, 6, 7, 8, 9 )
, list2 = ( 10, 11, 12, 13, 14, 15, 16, 17, 18 )
, list3 = ( 19, 20, 21, 22, 23, 24, 25, 26, 27 )
;
[ LWB list1 : UPB list1 ]STRING result;
FOR i FROM LWB list1 TO UPB list1 DO
result[ i ] := whole( list1[ i ], 0 ) + whole( list2[ i ], 0 ) + whole( list3[ i ], 0 )
OD;
STRING prefix := "[ ";
FOR i FROM LWB result TO UPB result DO
print( ( prefix, result[ i ] ) );
prefix := ", "
OD;
print( ( " ]", newline ) )
END
- Output:
[ 11019, 21120, 31221, 41322, 51423, 61524, 71625, 81726, 91827 ]
APL
list1←1 2 3 4 5 6 7 8 9
list2←10 11 12 13 14 15 16 17 18
list3←19 20 21 22 23 24 25 26 27
append←⍎¨∘(,/)∘(⍕¨)∘(⍉↑)
append list1 list2 list3
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
AutoHotkey
list1 := [1,2,3,4,5,6,7,8,9]
list2 := [10,11,12,13,14,15,16,17,18]
list3 := [19,20,21,22,23,24,25,26,27]
list4 := []
for i, v in list1
list4.Push(v . list2[i] . list3[i])
for i, v in list4
result .= v ", "
MsgBox % "[" trim(result, ", ") "]"
- Output:
[11019, 21120, 31221, 41322, 51423, 61524, 71625, 81726, 91827]
AWK
# syntax: GAWK -f APPEND_NUMBERS_AT_SAME_POSITION_IN_STRINGS.AWK
BEGIN {
n1 = split("1,2,3,4,5,6,7,8,9",list1,",")
n2 = split("10,11,12,13,14,15,16,17,18",list2,",")
n3 = split("19,20,21,22,23,24,25,26,27",list3,",")
if (n1 != n2 || n1 != n3) {
print("error: arrays must be same length")
exit(1)
}
for (i=1; i<=n1; i++) {
list[i] = list1[i] list2[i] list3[i]
printf("%s ",list[i])
}
printf("\n")
exit(0)
}
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
C
#include<stdio.h>
#include<stdlib.h>
int main(void) {
int list[3][9], i;
for(i=0;i<27;i++) list[i/9][i%9]=1+i;
for(i=0;i<9;i++) printf( "%d%d%d ", list[0][i], list[1][i], list[2][i] );
return 0;
}
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
CLU
append = proc (lists: sequence[sequence[int]]) returns (sequence[int])
n_lists: int := sequence[sequence[int]]$size(lists)
n_items: int := sequence[int]$size(lists[1])
out_list: array[int] := array[int]$predict(1,n_items)
for i: int in int$from_to(1, n_items) do
item: string := ""
for j: int in int$from_to(1, n_lists) do
item := item || int$unparse(lists[j][i])
end
array[int]$addh(out_list, int$parse(item))
end
return(sequence[int]$a2s(out_list))
end append
start_up = proc ()
list1 = sequence[int]$[1,2,3,4,5,6,7,8,9]
list2 = sequence[int]$[10,11,12,13,14,15,16,17,18]
list3 = sequence[int]$[19,20,21,22,23,24,25,26,27]
lists = sequence[sequence[int]]$[list1,list2,list3]
po: stream := stream$primary_output()
for n: int in sequence[int]$elements(append(lists)) do
stream$puts(po, int$unparse(n) || " ")
end
end start_up
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
F#
// Append numbers at same position in strings. Nigel Galloway: December 29th., 2021
let rec fG n g l=seq{match n,g,l with (n::x,g::y,l::z)->yield int((string n)+(string g)+(string l)); yield! fG x y z |_->()}
fG [1;2;3;4;5;6;7;8;9] [10;11;12;13;14;15;16;17;18] [19;20;21;22;23;24;25;26;27] |> Seq.iter(printf "%d "); printfn ""
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
Factor
USING: kernel math.parser math.ranges present prettyprint
sequences ;
27 [1,b] 9 cut 9 cut [ [ present ] tri@ 3append dec> ] 3map .
- Output:
{ 11019 21120 31221 41322 51423 61524 71625 81726 91827 }
FreeBASIC
dim as integer list1(1 to 9) = {1,2,3,4,5,6,7,8,9}
dim as integer list2(1 to 9) = {10,11,12,13,14,15,16,17,18}
dim as integer list3(1 to 9) = {19,20,21,22,23,24,25,26,27}
dim as integer catlist(1 to 9)
dim as string temp
for i as uinteger = 1 to 9
temp = str(list1(i)) + str(list2(i)) + str(list3(i))
catlist(i) = val(temp)
print catlist(i);" ";
next i
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
Go
package main
import "fmt"
func main() {
list1 := [9]int{1, 2, 3, 4, 5, 6, 7, 8, 9}
list2 := [9]int{10, 11, 12, 13, 14, 15, 16, 17, 18}
list3 := [9]int{19, 20, 21, 22, 23, 24, 25, 26, 27}
var list [9]int
for i := 0; i < 9; i++ {
list[i] = list1[i]*1e4 + list2[i]*1e2 + list3[i]
}
fmt.Println(list)
}
- Output:
[11019 21120 31221 41322 51423 61524 71625 81726 91827]
jq
Works with gojq, the Go implementation of jq
def list1 : [ range(1;10) ];
def list2 : [ range(10; 19)];
def list3 : [ range(19; 28) ];
[list1, list2, list3]
| transpose
| map( map(tostring) | add | tonumber)
- Output:
[11019,21120,31221,41322,51423,61524,71625,81726,91827]
Julia
list1 = [1,2,3,4,5,6,7,8,9]
list2 = [10,11,12,13,14,15,16,17,18]
list3 = [19,20,21,22,23,24,25,26,27]
println([prod(string(n) for n in z) for z in zip(list1, list2, list3)]) # ["11019", "21120", "31221", "41322", "51423", "61524", "71625", "81726", "91827"]
Lambdatalk
Using a function returning an array
{def append_numbers
{def append_numbers.rec
{lambda {:a :b :c :d}
{if {A.empty? :a}
then :d
else {append_numbers.rec {A.rest :a} {A.rest :b} {A.rest :c}
{A.addlast! {A.first :a}{A.first :b}{A.first :c} :d}}
}}}
{lambda {:a :b :c}
{append_numbers.rec :a :b :c {A.new}}}}
-> append_numbers
{append_numbers
{A.new {S.serie 1 9}}
{A.new {S.serie 10 18}}
{A.new {S.serie 19 27}}}
-> [11019,21120,31221,41322,51423,61524,71625,81726,91827]
or a map returning a sequence
{S.map {{lambda {:a :b :c :i}
{A.get :i :a}{A.get :i :b}{A.get :i :c}}
{A.new {S.serie 1 9}}
{A.new {S.serie 10 18}}
{A.new {S.serie 19 27}}
} {S.serie 0 8}}
-> 11019 21120 31221 41322 51423 61524 71625 81726 91827
Mathematica / Wolfram Language
list1 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
list2 = {10, 11, 12, 13, 14, 15, 16, 17, 18};
list3 = {19, 20, 21, 22, 23, 24, 25, 26, 27};
MapThread[
FromDigits@Flatten[IntegerDigits /@ {##}] &, {list1, list2, list3}]
- Output:
{11019,21120,31221,41322,51423,61524,71625,81726,91827}
Pascal
The following program requires (at least) an ISO 7185-compliant processor supporting features at level 1 (specifically “conformant array parameters” are being used).
program appendNumbersAtSamePositionInStrings(output);
type
wholeNumber = 0..maxInt;
var
i: integer;
{ Indices were chosen to ease up initialization in main block. }
list0: array[1..9] of wholeNumber;
list1: array[10..18] of wholeNumber;
list2: array[19..27] of wholeNumber;
{ Returns the number of digits necessary to express as decimal. }
function digitCount(i: wholeNumber): wholeNumber;
begin
{ Instead of an `if` branch you can simply write: }
i := i + ord(i = 0);
{ Remember: Argument to `ln` must be positive. }
digitCount := succ(trunc(ln(i) / ln(10)))
end;
{ Appends two list members in place. }
procedure append(
{ DI: Destination Index; SI: Source Index. }
var destination: array[diMin..diMax: integer] of wholeNumber;
source: array[siMin..siMax: integer] of wholeNumber
);
var
i, n: integer;
begin
{ Determine maximum index range. }
i := diMax - diMin;
if (siMax - siMin) < i then
begin
i := siMax - siMin
end;
{ NB: In Pascal `for`-loop-limits are evaluation exactly once only. }
for i := 0 to i do
begin
{ In Extended Pascal (ISO 10206) you could actually simply write: }
{ … := destination[diMin + i] * 10 pow digitCount(source[siMin + i]) }
for n := 1 to digitCount(source[siMin + i]) do
begin
destination[diMin + i] := destination[diMin + i] * 10
end;
destination[diMin + i] := destination[diMin + i] + source[siMin + i]
end
end;
{ Calls `append` twice. }
procedure appendTwo(
var destination: array[diMin..diMax: integer] of wholeNumber;
source0: array[si0Min..si0Max: integer] of wholeNumber;
source1: array[si1Min..si1Max: integer] of wholeNumber
);
begin
append(destination, source0);
append(destination, source1)
end;
{ === MAIN ============================================================= }
begin
for i := 1 to 9 do
begin
list0[i] := i
end;
for i := 10 to 18 do
begin
list1[i] := i
end;
for i := 19 to 27 do
begin
list2[i] := i
end;
appendTwo(list0, list1, list2);
for i := 1 to 9 do
begin
writeLn(list0[i])
end
end.
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
Perl
use strict;
use warnings;
my @a = < 1 2 3 4 5 6 7 8 9>;
my @b = <10 11 12 13 14 15 16 17 18>;
my @c = <19 20 21 22 23 24 25 26 27>;
my @d = <1 2 2 2 2 2 2 2 2 >;
my @e = < 9 0 1 2 3 4 5 6 7>;
my @f = (\@a, \@b, \@d, \@e);
# for just the three given lists
print $a[$_] . $b[$_] . $c[$_] . ' ' for 0..$#a; print "\n";
# for arbitrary number of lists
for my $i (0 .. $#{$f[0]}) {
map {print $f[$_][$i] } 0 .. $#f and print ' '
}
print "\n";
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827 11019 21120 31221 41322 51423 61524 71625 81726 91827
Phix
printf(1,"%V%V\n",repeat(apply(apply(true,vslice,{{sq_mul({tagset(9),tagset(18,10),tagset(27,19)},{1e4,1e2,1})},tagset(9)}),sum),2))
- Output:
{11019,21120,31221,41322,51423,61524,71625,81726,91827}{11019,21120,31221,41322,51423,61524,71625,81726,91827}
Python
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [10, 11, 12, 13, 14, 15, 16, 17, 18]
list3 = [19, 20, 21, 22, 23, 24, 25, 26, 27]
print([
''.join(str(n) for n in z) for z
in zip(list1, list2, list3)
])
which is also expressible as a map:
print(list(map(
lambda x, y, z: f'{x}{y}{z}',
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24, 25, 26, 27]
)))
- Output:
['11019', '21120', '31221', '41322', '51423', '61524', '71625', '81726', '91827']
Raku
Various manipulations.
my @lists = 1..9, 10..18, 19..27;
put [Z~] @lists; # the task
put [Z~] @lists».flip; # each component reversed
put [RZ~] @lists; # in reversed order
put ([Z~] @lists)».flip; # reversed components in reverse order
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827 10191 21102 32112 43122 54132 65142 76152 87162 98172 19101 20112 21123 22134 23145 24156 25167 26178 27189 91011 02112 12213 22314 32415 42516 52617 62718 72819
Ring
load "stdlib.ring"
list1 = [1,2,3,4,5,6,7,8,9]
list2 = [10,11,12,13,14,15,16,17,18]
list3 = [19,20,21,22,23,24,25,26,27]
list = []
for n = 1 to len(list1)
str1 = string(list1[n])
str2 = string(list2[n])
str3 = string(list3[n])
str = str1 + str2 + str3
add(list,str)
next
showArray(list)
func showArray(array)
txt = ""
see "["
for n = 1 to len(array)
txt = txt + array[n] + ","
next
txt = left(txt,len(txt)-1)
txt = txt + "]"
see txt
- Output:
list = [11019,21120,31221,41322,51423,61524,71625,81726,91827][11019,21120,31221,41322,51423,61524,71625,81726,91827]
Ruby
list1 = (1..9) .to_a
list2 = (10..18).to_a
list3 = (19..27).to_a
p list = [list1, list2, list3].transpose.map{|trio| trio.join.to_i }
- Output:
[11019, 21120, 31221, 41322, 51423, 61524, 71625, 81726, 91827]
Sidef
var lists = [
[1,2,3,4,5,6,7,8,9],
[10,11,12,13,14,15,16,17,18],
[19,20,21,22,23,24,25,26,27],
]
say lists.zip.map_2d {|*a| a.join.to_i }
- Output:
[11019, 21120, 31221, 41322, 51423, 61524, 71625, 81726, 91827]
Vlang
fn main() {
list1 := [1, 2, 3, 4, 5, 6, 7, 8, 9]!
list2 := [10, 11, 12, 13, 14, 15, 16, 17, 18]!
list3 := [19, 20, 21, 22, 23, 24, 25, 26, 27]!
mut list := [9]int{}
for i in 0..9 {
list[i] = list1[i]*int(1e4) + list2[i]*int(1e2) + list3[i]
}
println(list)
}
- Output:
[11019, 21120, 31221, 41322, 51423, 61524, 71625, 81726, 91827]
Wren
var list1 = (1..9).toList
var list2 = (10..18).toList
var list3 = (19..27).toList
var list = (0..8).map { |i| 1e4*list1[i] + 100*list2[i] + list3[i] }.toList
System.print(list)
- Output:
[11019, 21120, 31221, 41322, 51423, 61524, 71625, 81726, 91827]
XPL0
intI;forI:=1to9do[IntOut(0,I*10000+(I+9)*100+I+18);ChOut(0,^ )]
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827