| Bytes | Lang | Time | Link |
|---|---|---|---|
| 003 | Japt | 250410T142206Z | Shaggy |
| 032 | AWK | 250410T140714Z | xrs |
| 014 | MATL | 161018T213025Z | Suever |
| 034 | Ruby | 161018T160930Z | anna328p |
| 011 | K oK | 171001T175523Z | mkst |
| 004 | J | 161019T221407Z | algorith |
| 033 | JavaScript | 170929T170847Z | DanielIn |
| 036 | Groovy | 161019T195421Z | Magic Oc |
| 014 | Bash | 161018T161806Z | Digital |
| 082 | Java | 161019T025039Z | TNT |
| 055 | PHP | 161018T161703Z | Jör |
| 047 | JavaScript ES6 | 161019T075927Z | edc65 |
| 047 | Haskell | 161019T075229Z | arjanen |
| 047 | R | 161019T070217Z | Billywob |
| 039 | C# | 161019T005355Z | milk |
| 007 | Pyth | 161018T203759Z | Maltysen |
| 025 | Mathematica | 161018T175023Z | JungHwan |
| 026 | Perl | 161018T164613Z | Dada |
| 090 | Kotlin | 161018T165642Z | Psyduck7 |
| 011 | CJam | 161018T163803Z | Martin E |
| 003 | 05AB1E | 161018T163156Z | Adnan |
| 037 | JavaScript ES7 | 161018T163659Z | Hedi |
| 006 | Jelly | 161018T161023Z | Dennis |
| 032 | Python | 161018T161915Z | xnor |
| 049 | JavaScript | 161018T161543Z | xenia |
| 036 | PowerShell v2+ | 161018T160107Z | AdmBorkB |
| 019 | Vim | 161018T161044Z | udioica |
Japt, 3 bytes
ùUÊ
ùUÊ :Implicit input of array U
ù :Left pad each element with spaces to length
UÊ : Length of U
MATL, 14 bytes
'%%%dd'inYDGYD
Try it out at MATL Online
This uses formatted string creation by first constructing the format string: %(NUM)d and then applies string formatting again using this format string and the input.
Ruby, 40 36 34 bytes
->m{m.map{|i|$><<i.rjust(m.size)}}
Can be worked on more.
Call as a lambda.
Explanation:
->m{m.map{|i|$><<i.rjust(m.size)}}
->m{ } # lambda taking array m
m.map{|i| } # map over array using variable i
$><< # output to $> (stdout)
i.rjust(m.size) # right justify i to m's length
K (oK), 11 bytes
Solution:
,/(-#x)$$x:
Explanation:
Interpretted right-to-left. Convert to string, and left-pad with length of list, then flatten:
,/(-#x)$$x: / the solution | example:
x: / save as 'x' |
$ / string | $10 20 30 -> "10","20","30"
$ / pad right by left | 5$"abc" -> "abc "
( ) / do the stuff in brackets together |
#x / count x | #10 20 30 -> 3
- / negate |
,/ / flatten (concatenate-over) | ,/" a"," b"," c" -> " a b c"
J, 4 bytes
":~#
Unary function taking the list of numbers on the right as an array and returning the padded string.
Here it is in use at the REPL. Note that input lines are indented three spaces.
f=: ":~#
f 2 3
2 3
f 2 10
210
f 1111 222 33 4
1111 222 33 4
JavaScript 33 bytes
similar to @Hedi - but the default padding is ' ', so its 4 chars less
a=>a.map(s=>s.padStart(a.length))
f=a=>a.map(s=>s.padStart(a.length))
console.log(f(["0"]))
console.log(f(["1"]))
console.log(f(["2","3"]))
console.log(f(["2","10"]))
console.log(f(["17" ,"19" ,"2"]))
console.log(f(["1000" ,"400" ,"30" ,"7"]))
Groovy, 36 Bytes
{a->a.collect{it.padLeft(a.size())}}
Takes in array of strings only, outputs array of padded strings.
Bash, 14
printf %$#d $@
Input list given at the command line.
Not much to explain here. Simply uses built-in printf facilities to do the necessary padding, based off the number of passed args:
$#is the number of args passed%<n>dis a printf format specifier that prints an integer with up to n leading spaces$@is the list of all args passed- The format specifier is reused for each member of
$@.
Java, 83 82 bytes
a->{String s="";for(int i=a.length,j=i;i-->0;)s+="%"+j+"s";return s.format(s,a);};
Constructs a format string designed to pad the given arguments by a number of spaces equal to the length of the array. The format string is used as an argument for String.format, and the result is then returned. The functional interface can accept either a String[] or an Integer[] or similar.
Full class:
public class Test {
public static void main(String[] args) {
java.util.function.Function<Integer[], String> f = a -> {
String s = "";
for (int i = a.length, j = i; i-- > 0;)
s += "%" + j + "s";
return s.format(s, a);
};
System.out.println(f.apply(new Integer[] {0}));
System.out.println(f.apply(new Integer[] {2, 10}));
System.out.println(f.apply(new Integer[] {7, 8, 9, 10}));
System.out.println(f.apply(new Integer[] {1, 33, 333, 7777}));
System.out.println(f.apply(new Integer[] {0, 0, 0, 0, 0, 0}));
}
}
-1 byte thanks to @KevinCruijssen.
PHP, 55 Bytes
<?foreach($a=$_GET[a]as$i)printf("%".count($a)."s",$i);
Pevious Version 59 Bytes
<?foreach($a=$_GET[a]as$i)echo str_pad($i,count($a)," ",0);
JavaScript (ES6), 47
Anonymous function, input: array of strings, output: array of strings
Using a recursive padding function
a=>a.map(x=>p(x),p=x=>x[a.length-1]?x:p(' '+x))
For an integer/string array as input, 49 bytes:
a=>a.map(x=>p(x),p=x=>(y=' '+x)[a.length]?x:p(y))
Test
f=
a=>a.map(x=>p(x),p=x=>x[a.length-1]?x:p(' '+x))
function update() {
var l=I.value.match(/\d+/g)||[]
O.textContent = f(l)
}
update()
<input id=I oninput='update()' value='1000,400,30,7'>
<pre id=O></pre>
Haskell, 47 bytes
k=length
f l=map(\s->replicate(k l-k s)' '++s)l
That’s a function from a list of strings to a list of strings, like the JavaScript answers. replicate allows one to get a list (Haskell strings are lists of characters) of a given size, so I use it — and the bolded assumption in the problem — to generate the padding (its length is N − <length of element>, for each element of the input list). I would have preferred to use a printf based solution rather than this one with replicate (it would have been shorter, for one thing) but the import statement kills any savings done on the function itself.
R, 47 bytes
cat(sprintf("%*.f",length(n<-scan()),n),sep="")
Reads input from stdin and uses C-style formating with sprintf. There should be some way the cat function is not needed but couldn't find a way to suppress the quotes on each element without it. If we only want start and end quotes we could use the slightly longer option:
paste0(sprintf("%*.f",length(n<-scan()),n),collapse="")
C#, 39 bytes
s=>s.ConvertAll(c=>c.PadLeft(s.Count));
Takes a List<string> and outputs a List<string>.
Explanation:
/*Func<List<string>, List<string>> Lambda =*/ s =>
s.ConvertAll(c => // Create a new List<string> by...
c.PadLeft(s.Count) // ...padding each element by 'N'
)
;
Would've been a few bytes shorter to use LINQ if the import isn't counted and then returning IEnumerable<string> instead of a full blown list:
C#, 35+18 = 53 bytes
using System.Linq;s=>s.Select(c=>c.PadLeft(s.Count));
Mathematica, 25 bytes
#~StringPadLeft~Length@#&
Both input and output are lists of strings.
Explanation
Length@#
Get the length of the input (number of element).
#~StringPadLeft~...
Pad left each element in the input so that their lengths match the length of the input.
Perl, 26 bytes
-4 bytes thanks to @Ton Hospel
25 bytes of code + -a flag.
printf"%*s",~~@F,$_ for@F
Run with :
perl -ae 'printf"%*s",~~@F,$_ for@F' <<< "10 11 12"
(On some older version of Perl, you might need to add -n)
Kotlin, 90 bytes
Golfed:
fun main(a:Array<String>){a.forEach{b->for(i in 1..a.size-b.length){print(" ")};print(b)}}
Ungolfed:
fun main(a: Array<String>) {
a.forEach { b ->
for (i in 1..a.size - b.length) {
print(" ")
}
print(b)
}
}
CJam, 11 bytes
lS%_,f{Se[}
Try it online! (As a test suite.)
Explanation
l e# Read input.
S% e# Split around spaces.
_, e# Copy and get length.
f{ e# Map this block over the list, passing in the length on each iteration.
Se[ e# Left-pad to the given length with spaces.
}
05AB1E, 3 bytes
Code:
Dgj
Explanation:
D # Duplicate the input array
g # Get the length
j # Left-pad with spaces to the length of the array
JavaScript (ES7), 37 bytes
a=>a.map(v=>v.padStart(a.length,' '))
Input: Array of strings
Output: Array of strings
f=
a=>a.map(v=>v.padStart(a.length,' '))
;
console.log(f(['0']))
console.log(f(['1']))
console.log(f(['2','3']))
console.log(f(['2','10']))
console.log(f(['4','5','6']))
console.log(f(['17','19','20']))
console.log(f(['7','8','9','10']))
console.log(f(['100','200','300','0']))
console.log(f(['1000','400','30','7']))
console.log(f(['1','33','333','7777']))
console.log(f(['0','0','0','0','0','0']))
Jelly, 7 6 bytes
L⁶xaUU
Input is an array of strings. Try it online! or verify all test cases.
How it works
L⁶xaUU Main link. Argument: A (array of strings)
L Yield the l, the length of A.
⁶x Repeat ' ' l times.
U Upend; reverse all strings in A.
a Perform vectorizing logical AND, replacing spaces with their corresponding
digits and leaving spaces without corresponding digits untouched.
U Upend; reverse the strings in the result to restore the original order of
its digits, moving the spaces to the left.
Python, 32 bytes
lambda l:'%%%ds'%len(l)*len(l)%l
An anonymous function that takes a tuple as input. Either numbers or strings work.
Example:
l=(1,33,333,7777)
'%%%ds'
## A "second-order" format string
'%%%ds'%len(l) -> '%4s'
## Inserts the length as a number in place of '%d'
## The escaped '%%' becomes '%', ready to take a new format argument
## The result is a format string to pad with that many spaces on the left
'%%%ds'%len(l)*len(l) -> '%4s%4s%4s%4s'
## Concatenates a copy per element
'%%%ds'%len(l)*len(l)%l -> ' 1 33 3337777'
## Inserts all the tuple elements into the format string
## So, each one is padded with spaces
JavaScript, 49 bytes
a=>a.map(n=>" ".repeat(a.length-n.length)+n)
Takes the arguments as a list of strings and also returns a list of strings.
Explanation:
a=> An unnamed function, which takes one argument, a
a.map(n=> ) Do the following to each element n in a:
" ".repeat(a.length-n.length) Generate the spaces needed to justify the number
+n Append the number
PowerShell v2+, 36 bytes
param($a)$a|%{"{0,$($a.count)}"-f$_}
Takes input $a as an array of integers. Loops through them with $a|%{...}. Each iteration, uses the -format operator with the optional Alignment Component (based on $a.count) to left-pad the appropriate number of spaces. That resultant string is left on the pipeline. At end of program execution, the resulting strings are all left on the pipeline as an array.
Examples
Output is newline-separated on each run, as that's the default Write-Output at program completion for an array.
PS C:\Tools\Scripts\golfing> @(0),@(1),@(2,3),@(2,10),@(4,5,6),@(17,19,20),@(7,8,9,10),@(100,200,300,0),@(1000,400,30,7),@(1,33,333,7777),@(0,0,0,0,0,0)|%{""+($_-join',')+" -> ";(.\spaced-out-numbers $_)}
0 ->
0
1 ->
1
2,3 ->
2
3
2,10 ->
2
10
4,5,6 ->
4
5
6
17,19,20 ->
17
19
20
7,8,9,10 ->
7
8
9
10
100,200,300,0 ->
100
200
300
0
1000,400,30,7 ->
1000
400
30
7
1,33,333,7777 ->
1
33
333
7777
0,0,0,0,0,0 ->
0
0
0
0
0
0
Vim, 19 bytes
YPPG!{<C-F>|R%ri<CR>djVGgJ
Takes a list of numbers one-per-line. Relies on :set expandtab, which is popular, but not universal.
You clearly want to use :right for this. The question is how to get the number of lines onto the command line. The traditional way is :%ri<C-R>=line('$'), but all that text is long.
The shorter, more enterprising approach is to form the command line using the normal mode ! command. It involves some weird workarounds, expanding the file by 2 lines then removing them again, but it comes out 2 bytes shorter. And I'm kinda shocked the garbled command line I get (like :%ri+4!) actually works, but it does.