Append numbers at same position in strings
- Task
Given three lists:
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]
Turn the numbers them into strings and concatenate them.
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
An alternative, which of course only works with this particular set:
918+10101×⍳9
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
Arturo
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]
0..dec size list1 | map'i -> join @[list1\[i] list2\[i] list3\[i]]
| print
- 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
BASIC
Applesoft BASIC
Same code as MSX Basic
BASIC256
arraybase 1
dim list1 = {1,2,3,4,5,6,7,8,9}
dim list2 = {10,11,12,13,14,15,16,17,18}
dim list3 = {19,20,21,22,23,24,25,26,27}
dim catlist(9)
for i = 1 to 9
temp$ = string(list1[i]) + string(list2[i]) + string(list3[i])
catlist[i] = FromRadix(temp$,10)
print catlist[i]; " ";
next i
- Output:
Same as FreeBASIC entry.
Chipmunk Basic
100 CLS
110 DIM LIST1(9)
120 DATA 1,2,3,4,5,6,7,8,9
130 FOR I = 1 TO 9
140 READ LIST1(I)
150 NEXT I
160 DIM LIST2(9)
170 DATA 10,11,12,13,14,15,16,17,18
180 FOR I = 1 TO 9
190 READ LIST2(I)
200 NEXT I
210 DIM LIST3(9)
220 DATA 19,20,21,22,23,24,25,26,27
230 FOR I = 1 TO 9
240 READ LIST3(I)
250 NEXT I
260 DIM CATLIST(9)
270 FOR I = 1 TO 9
280 TEMP$ = STR$(LIST1(I))+STR$(LIST2(I))+STR$(LIST3(I))
290 CATLIST(I) = VAL(TEMP$)
300 PRINT CATLIST(I);" ";
310 NEXT I
320 END
Gambas
Public Sub Main()
Dim list1 As Integer[] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Dim list2 As Integer[] = [10, 11, 12, 13, 14, 15, 16, 17, 18]
Dim list3 As Integer[] = [19, 20, 21, 22, 23, 24, 25, 26, 27]
Dim catlist As Integer[] = New Integer[9]
Dim tmp As String
For i As Integer = 0 To 8
tmp = Str(list1[i]) & Str(list2[i]) & Str(list3[i])
catlist[i] = Val(tmp)
Print catlist[i]; " ";
Next
End
- Output:
Same as FreeBASIC entry.
GW-BASIC
Same code as Chipmunk Basic
MSX Basic
110 DIM l1(9)
120 DATA 1,2,3,4,5,6,7,8,9
130 FOR i = 1 TO 9
140 READ l1(i)
150 NEXT i
160 DIM l2(9)
170 DATA 10,11,12,13,14,15,16,17,18
180 FOR i = 1 TO 9
190 READ l2(i)
200 NEXT i
210 DIM l3(9)
220 DATA 19,20,21,22,23,24,25,26,27
230 FOR i = 1 TO 9
240 READ l3(i)
250 NEXT i
260 DIM c(9)
270 FOR i = 1 TO 9
280 t$ = STR$(l1(i))+STR$(l2(i))+STR$(l3(i))
290 c(i) = VAL(t$)
300 PRINT c(i);" ";
310 NEXT i
320 END
PureBasic
Dim list1(9)
Dim list2(9)
Dim list3(9)
Dim catlist(9)
Define temp.s
list1(1) = 1 : list1(2) = 2 : list1(3) = 3 : list1(4) = 4 : list1(5) = 5
list1(6) = 6 : list1(7) = 7 : list1(8) = 8 : list1(9) = 9
list2(1) = 10 : list2(2) = 11 : list2(3) = 12 : list2(4) = 13 : list2(5) = 14
list2(6) = 15 : list2(7) = 16 : list2(8) = 17 : list2(9) = 18
list3(1) = 19 : list3(2) = 20 : list3(3) = 21 : list3(4) = 22 : list3(5) = 23
list3(6) = 24 : list3(7) = 25 : list3(8) = 26 : list3(9) = 27
OpenConsole()
For i = 1 To 9
temp = Str(list1(i)) + Str(list2(i)) + Str(list3(i))
catlist(i) = Val(temp)
Print(Str(catlist(i)) + " ")
Next i
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
QBasic
DIM i AS INTEGER
DIM list1(9) AS INTEGER
DATA 1,2,3,4,5,6,7,8,9
FOR i = 1 TO 9
READ list1(i)
NEXT i
DIM list2(9) AS INTEGER
DATA 10,11,12,13,14,15,16,17,18
FOR i = 1 TO 9
READ list2(i)
NEXT i
DIM list3(9) AS INTEGER
DATA 19,20,21,22,23,24,25,26,27
FOR i = 1 TO 9
READ list3(i)
NEXT i
DIM catlist(9) AS LONG
DIM tmp AS STRING
FOR i = 1 TO 9
tmp = STR$(list1(i)) + STR$(list2(i)) + STR$(list3(i))
catlist(i) = VAL(tmp)
PRINT catlist(i); " ";
NEXT i
QB64
The QBasic solution works without any changes.
Run BASIC
dim list1(9)
data 1,2,3,4,5,6,7,8,9
for i = 1 to 9
read list1(i)
next i
dim list2(9)
data 10,11,12,13,14,15,16,17,18
for i = 1 to 9
read list2(i)
next i
dim list3(9)
data 19,20,21,22,23,24,25,26,27
for i = 1 to 9
read list3(i)
next i
dim catlist(9)
for i = 1 to 9
temp$ = str$(list1(i)) + str$(list2(i)) + str$(list3(i))
catlist(i) = val(temp$)
print catlist(i); " ";
next i
- Output:
Same as FreeBASIC entry.
True BASIC
DIM list1(9)
DATA 1, 2, 3, 4, 5, 6, 7, 8, 9
FOR i = 1 TO 9
READ list1(i)
NEXT i
DIM list2(9)
DATA 10, 11, 12, 13, 14, 15, 16, 17, 18
FOR i = 1 TO 9
READ list2(i)
NEXT i
DIM list3(9)
DATA 19, 20, 21, 22, 23, 24, 25, 26, 27
FOR i = 1 TO 9
READ list3(i)
NEXT i
DIM catlist(9)
FOR i = 1 TO 9
LET tmp$ = STR$(list1(i)) & STR$(list2(i)) & STR$(list3(i))
LET catlist(i) = ROUND(VAL(tmp$))
PRINT catlist(i);
NEXT i
END
XBasic
PROGRAM "Append numbers at same position in strings"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
FUNCTION Entry ()
DIM list1[9]
DIM list2[9]
DIM list3[9]
list1[0] = 1
list1[1] = 2
list1[2] = 3
list1[3] = 4
list1[4] = 5
list1[5] = 6
list1[6] = 7
list1[7] = 8
list1[8] = 9
list2[0] = 10
list2[1] = 11
list2[2] = 12
list2[3] = 13
list2[4] = 14
list2[5] = 15
list2[6] = 16
list2[7] = 17
list2[8] = 18
list3[0] = 19
list3[1] = 20
list3[2] = 21
list3[3] = 22
list3[4] = 23
list3[5] = 24
list3[6] = 25
list3[7] = 26
list3[8] = 27
FOR i = 0 TO 8
tmp$ = TRIM$(STR$(list1[i])) + TRIM$(STR$(list2[i])) + TRIM$(STR$(list3[i]))
PRINT ULONG(tmp$); " ";
NEXT i
END FUNCTION
Yabasic
// Rosetta Code problem: http://rosettacode.org/wiki/Append_numbers_at_same_position_in_strings
// by Jjuanhdez, 09/2022
dim list1(9)
data 1,2,3,4,5,6,7,8,9
for i = 1 to 9
read list1(i)
next i
dim list2(9)
data 10,11,12,13,14,15,16,17,18
for i = 1 to 9
read list2(i)
next i
dim list3(9)
data 19,20,21,22,23,24,25,26,27
for i = 1 to 9
read list3(i)
next i
dim catlist(9)
for i = 1 to 9
temp$ = str$(list1(i)) + str$(list2(i)) + str$(list3(i))
catlist(i) = val(temp$)
print catlist(i), " ";
next i
- Output:
Same as FreeBASIC entry.
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
C++
#include <iostream>
int main() {
int list[3][9], i;
for (i = 0; i < 27; i++) {
list[i / 9][i % 9] = 1 + i;
}
for (i = 0; i < 9; i++) {
std::cout << list[0][i] << list[1][i] << list[2][i] << " ";
}
return 0;
}
C3
import std::io;
import std::collections::list;
fn void main()
{
int[9] list1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[9] list2 = { 10, 11, 12, 13, 14, 15, 16, 17, 18 };
int[9] list3 = { 19, 20, 21, 22, 23, 24, 25, 26, 27 };
List(<String>) list;
list.temp_init();
for (int i = 0; i < 9; i++)
{
list.push(string::tformat("%d%d%d", list1[i], list2[i], list3[i]));
}
io::printn(list);
}
- 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
Dart
void main() {
List<List<int>> list = List.generate(3, (_) => List.filled(9, 0));
for (int i = 0; i < 27; i++) {
list[i ~/ 9][i % 9] = 1 + i;
}
for (int i = 0; i < 9; i++) {
print('${list[0][i]}${list[1][i]}${list[2][i]} ');
}
}
Delphi
const List1: array [0..8] of integer = (1, 2, 3, 4, 5, 6, 7, 8, 9);
const list2: array [0..8] of integer = (10, 11, 12, 13, 14, 15, 16, 17, 18);
const list3: array [0..8] of integer = (19, 20, 21, 22, 23, 24, 25, 26, 27);
function GetAppendNumbers: string;
var I: integer;
begin
Result:='List = [';
for I:=0 to High(List1) do
begin
Result:=Result+IntToStr(List1[I])+IntToStr(List2[I])+IntToStr(List3[I]);
if I=High(List1) then Result:=Result+']'
else Result:=Result+',';
end;
end;
- Output:
List = [11019,21120,31221,41322,51423,61524,71625,81726,91827]
EasyLang
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 ]
#
for i to len list1[]
s$ = list1[i] & list2[i] & list3[i]
r[] &= number s$
.
print r[]
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
FutureBasic
NSUInteger i
CFArrayRef list1, list2, list3
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"]
printf @"[\b"
for i = 0 to 8
CFStringRef format : if i < 8 then format = @"%@%@%@, \b" else format = @"%@%@%@]"
printf format, list1[i], list2[i], list3[i]
next
HandleEvents
- 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]
Haskell
main :: IO ()
main =
mapM_ putStrLn $
zipWith3
(\ x y z -> [x, y, z] >>= show)
[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]
or more flexibly (allowing for an arbitrary number of zipped lists) in terms of ZipList:
import Control.Applicative
main :: IO ()
main =
mapM_ putStrLn $
getZipList $
(\x y z -> [x, y, z] >>= show) <$>
ZipList [1, 2, 3, 4, 5, 6, 7, 8, 9] <*>
ZipList [10, 11, 12, 13, 14, 15, 16, 17, 18] <*>
ZipList [19, 20, 21, 22, 23, 24, 25, 26, 27]
- Output:
11019 21120 31221 41322 51423 61524 71625 81726 91827
J
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
-.&' '@":"1 list1 ,. list2 ,. list3
- 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
Lua
do -- form a list of strings by concatenating numbers from 3 lists
local list1, list2, list3 = { 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 }
local result = {}
for i = 1, #list1 do
result[ i ] = list1[ i ]..list2[ i ]..list3[ i ]
end
print( "[ "..table.concat( result, " " ).." ]" )
end
- Output:
[ 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}
Maxima
list1:makelist(i,i,9)$
list2:makelist(i,i,10,18)$
list3:makelist(i,i,19,27)$
block(makelist(sconcat(list1[i],list2[i],list3[i]),i,1,length(list1)),map(eval_string,%%));
- Output:
[11019,21120,31221,41322,51423,61524,71625,81726,91827]
newLISP
(map string
'(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))
("11019" "21120" "31221" "41322" "51423" "61524" "71625" "81726" "91827")
Nim
import std/strutils
const
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]
var list: array[List1.len, int]
for i in 0..list.high:
list[i] = parseInt($List1[i] & $List2[i] & $List3[i])
echo "list = ", list
- Output:
list = [11019, 21120, 31221, 41322, 51423, 61524, 71625, 81726, 91827]
Nu
const list1 = [1 2 3 4 5 6 7 8 9]
const list2 = [10 11 12 13 14 15 16 17 18]
const list3 = [19 20 21 22 23 24 25 26 27]
[$list1 $list2 $list3] | each { into record } | values | each { str join }
- Output:
╭───┬───────╮ │ 0 │ 11019 │ │ 1 │ 21120 │ │ 2 │ 31221 │ │ 3 │ 41322 │ │ 4 │ 51423 │ │ 5 │ 61524 │ │ 6 │ 71625 │ │ 7 │ 81726 │ │ 8 │ 91827 │ ╰───┴───────╯
OCaml
let 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"]]
let reduce f = function
| h :: t -> List.fold_left f h t
| _ -> invalid_arg "reduce"
let () =
reduce (List.map2 (^)) lists |> String.concat ", " |> print_endline
- 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}
PL/M
... under CP/M (or an emulator)
100H: /* FORM A LIST OF STRINGS BY CONCATENATING NUMBERS FROM 3 LISTS */
/* CP/M BDOS SYSTEM CALLS AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$STRING( .( 0DH, 0AH, '$' ) ); END;
TO$STRING: PROCEDURE( N, S ); /* CONVERTS N TO A STRING AT S */
DECLARE ( N, S ) ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
DECLARE S$POS BYTE, S$STR BASED S ( 6 )BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
S$POS = 0;
DO W = W TO LAST( N$STR );
S$STR( S$POS ) = N$STR( W );
S$POS = S$POS + 1;
END;
END TO$STRING;
STR$LENGTH: PROCEDURE( S )ADDRESS; /* RETURNS THE LENGTH OF S */
DECLARE S ADDRESS;
DECLARE LEN ADDRESS, S$PTR ADDRESS, S$CH BASED S$PTR BYTE;
S$PTR = S;
LEN = 0;
DO WHILE S$CH <> '$';
LEN = LEN + 1;
S$PTR = S$PTR + 1;
END;
RETURN LEN;
END STR$LENGTH;
DECLARE LIST1 DATA( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) /* THREE LISTS */
, LIST2 DATA( 10, 11, 12, 13, 14, 15, 16, 17, 18 ) /* OF BYTE */
, LIST3 DATA( 19, 20, 21, 22, 23, 24, 25, 26, 27 ) /* VALUES */
;
DECLARE RESULT ( 11 )ADDRESS; /* WILL HOLD THE CONCATENATED STRINGS */
DECLARE TEXT$BUFFER ( 256 )BYTE; /* WILL HOLD THE TEXT OF THE STRINGS */
DECLARE TEXT$PTR ADDRESS;
TEXT$PTR = .TEXT$BUFFER;
DECLARE I BYTE;
DO I = 0 TO LAST( LIST1 );
RESULT( I ) = TEXT$PTR;
CALL TO$STRING( LIST1( I ), TEXT$PTR );
TEXT$PTR = TEXT$PTR + STR$LENGTH( TEXT$PTR );
CALL TO$STRING( LIST2( I ), TEXT$PTR );
TEXT$PTR = TEXT$PTR + STR$LENGTH( TEXT$PTR );
CALL TO$STRING( LIST3( I ), TEXT$PTR );
TEXT$PTR = TEXT$PTR + STR$LENGTH( TEXT$PTR ) + 1; /* KEEP THE FINAL $ */
END;
CALL PR$CHAR( '(' );
DO I = 0 TO LAST( LIST1 );
CALL PR$CHAR( ' ' );
CALL PR$STRING( RESULT( I ) );
END;
CALL PR$STRING( .' )$' );
CALL PR$NL;
EOF
- Output:
( 11019 21120 31221 41322 51423 61524 71625 81726 91827 )
Prolog
% Define the initial lists
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]).
% Concatenate the elements of the lists
concat_lists([], [], [], []).
concat_lists([H1|T1], [H2|T2], [H3|T3], [H|T]) :-
atom_concat(H1, H2, Tmp1),
atom_concat(Tmp1, H3, Tmp),
atom_number(Tmp, H),
concat_lists(T1, T2, T3, T).
% Print the resulting list
print_list([]).
print_list([H|T]) :-
write(H), write(' '),
print_list(T).
% Main program
main :-
list1(L1),
list2(L2),
list3(L3),
concat_lists(L1, L2, L3, CatList),
print_list(CatList).
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']
Quackery
[ [] swap witheach
[ number$ nested join ] ] is [number$] ( [ --> [ )
[ swap witheach
[ over i^ peek
join swap
i^ poke ] ] is zip ( [ [ --> [ )
' [ 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 ]
3 times [ rot [number$] ]
zip zip
witheach [ echo$ sp ]
- 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]
RPL
Using the basic instruction set:
≪ 3 →LIST → lists
≪ { } 1 lists 1 GET SIZE FOR j
"" 1 3 FOR k lists k GET j GET →STR + NEXT STR→ + NEXT
≫ ≫ 'TASK' STO
Using the extended instruction set introduced in 2003:
≪ 3 ≪ →STR + + STR→ ≫ DOLIST
≫ 'TASK' STO
Input:
{ 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 } TASK
- Output:
1: { 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]
Scheme
Works with Gauche Scheme.
(map (pa$ format "~a~a~a")
'(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))
("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]
V (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]
X86-64 Assembly
Using FASM in FreeBSD to make the executable without needing gcc or linkers. The solution is simplified because the numbers are known in advance. If it had to work with lists of arbitrary integers, some additional steps (and a longer string buffer) would be needed.
format ELF64 executable 9
segment readable
res1: db "list = ["
segment readable writable
res2: times 100 db 0
db "]", 10, 0
res2_len = $ - res2
segment readable executable
entry start
start:
mov ecx, 9
mov ebx, 10
mov esi, 100
next_num:
mov eax, ecx
imul eax, 10101
add eax, 918
convert:
dec esi
xor edx, edx
div ebx
add dl, '0'
mov [res2 + esi], dl
test eax, eax
jnz convert
dec esi
mov byte [res2 + esi], ","
dec ecx
test ecx, ecx
jnz next_num
inc esi
push rsi
print:
mov eax, 4
mov edi, 1
mov esi, res1
mov edx, 8
syscall
mov eax, 4
mov edi, 1
pop rsi
mov edx, res2_len
sub edx, esi
lea esi, [res2 + esi]
syscall
exit:
mov eax, 1
xor edi, edi
syscall
- Output:
list = [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
- Draft Programming Tasks
- 11l
- Ada
- ALGOL 68
- APL
- Arturo
- AutoHotkey
- AWK
- BASIC
- Applesoft BASIC
- BASIC256
- Chipmunk Basic
- Gambas
- GW-BASIC
- MSX Basic
- PureBasic
- QBasic
- QB64
- Run BASIC
- True BASIC
- XBasic
- Yabasic
- C
- C++
- C3
- CLU
- Dart
- Delphi
- SysUtils
- EasyLang
- F Sharp
- Factor
- FreeBASIC
- FutureBasic
- Go
- Haskell
- J
- Jq
- Julia
- Lambdatalk
- Lua
- Mathematica
- Wolfram Language
- Maxima
- NewLISP
- Nim
- Nu
- OCaml
- Pascal
- Perl
- Phix
- PL/M
- Prolog
- Python
- Quackery
- Raku
- Ring
- RPL
- Ruby
- Scheme
- Sidef
- V (Vlang)
- Wren
- X86-64 Assembly
- XPL0