Append numbers at same position in strings
- 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]
ALGOL 68
<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 ) , 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</lang>
- Output:
[ 11019, 21120, 31221, 41322, 51423, 61524, 71625, 81726, 91827 ]
C
<lang 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;
}</lang>
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
Factor
<lang factor>USING: kernel math.parser math.ranges present prettyprint sequences ;
27 [1,b] 9 cut 9 cut [ [ present ] tri@ 3append dec> ] 3map .</lang>
- Output:
{ 11019 21120 31221 41322 51423 61524 71625 81726 91827 }
FreeBASIC
<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 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</lang>
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
Julia
<lang 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"] </lang>
Perl
<lang 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";</lang>
- 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
<lang python>from functools import reduce
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(list(reduce(lambda x, y: x + y, [str(n) for n in z]) for z in zip(list1, list2, list3)))
</lang>
- Output:
['11019', '21120', '31221', '41322', '51423', '61524', '71625', '81726', '91827']
Raku
Not really seeing why we need to show the result twice but that's what the requirement is... ¯\_(ツ)_/¯ (I find myself making that gesture often in response to Calmosofts tasks.)
<lang perl6>say [[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]] xx 2;</lang>
- Output:
([11019 21120 31221 41322 51423 61524 71625 81726 91827] [11019 21120 31221 41322 51423 61524 71625 81726 91827])
Ring
<lang 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
</lang>
- Output:
list = [11019,21120,31221,41322,51423,61524,71625,81726,91827][11019,21120,31221,41322,51423,61524,71625,81726,91827]
Wren
<lang ecmascript>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)</lang>
- Output:
[11019, 21120, 31221, 41322, 51423, 61524, 71625, 81726, 91827]
XPL0
<lang XPL0>intI;forI:=1to9do[IntOut(0,I*10000+(I+9)*100+I+18);ChOut(0,^ )]</lang>
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827