| Bytes | Lang | Time | Link |
|---|---|---|---|
| 048 | AWK | 250806T201049Z | xrs |
| 066 | Cubically | 170808T031437Z | Kamil Dr |
| 060 | ><> | 180111T141216Z | Jo King |
| 010 | Japt v2.0a0 | 170808T105859Z | Shaggy |
| 083 | PHP | 170809T024717Z | Petah |
| 082 | Cubically | 170809T161628Z | TehPers |
| 128 | R | 170808T145247Z | Giuseppe |
| 093 | Java 8 | 170808T104936Z | Akash Th |
| 010 | 05AB1E | 170808T001708Z | Oliver N |
| 013 | Husk | 170808T004607Z | H.PWiz |
| 022 | Ruby | 170808T002607Z | Value In |
| 021 | Gema | 170808T153841Z | manatwor |
| 037 | JavaScript ES8 | 170808T000932Z | user7234 |
| 009 | V | 170808T125703Z | nmjcman1 |
| 065 | Python 2 | 170808T010027Z | totallyh |
| 068 | Python 2 | 170808T062510Z | Mr. Xcod |
| 063 | Haskell | 170808T073546Z | Laikoni |
| 015 | Pyth | 170808T054215Z | Mr. Xcod |
| 019 | Jelly | 170808T003136Z | hyperneu |
| nan | Perl 5 | 170808T010806Z | Xcali |
| 019 | Jelly | 170808T003940Z | Jonathan |
| 009 | Retina | 170808T001743Z | Neil |
| 095 | Python 3 | 170808T000731Z | hyperneu |
Cubically, 69 66 bytes
RBR3B~(+50<7?6{+54>7?6{-002+7~?6{(@5*1-1/1)6}}}(-6>7?6&@7+70-4~)6)
Explanation:
First we do this initialization:
RBR3B
To set up this cube:
533
004
000
411223455441
311222331440
311222331440
555
555
200
The most important thing about this cube is that face 5 sums to 32, which is the value required to print spaces. Coincidentally, it also happens to be fairly short for all other computation. After that is done:
~( . . . ) Takes the first input, then loops indefinitely
+50<7?6{+54>7?6{-002+7~?6{(@5*1-1/1)6}}} Handle leading digit:
+50<7?6{ } If input is greater than 47 ('0' is 48)
+54>7?6{ } And input is less than 58 ('9' is 57)
Then input is a digit
-002+7 Set notepad equal to value of input digit
~ Take next input (only convenient place for it)
?6{ } If the notepad isn't 0
( )6 While the notepad isn't 0:
@5 Print a space
*1-1/1 Decrement the notepad by one
Leading digit handled
(-6>7?6&@7+70-4~)6 Handle rest of line:
( )6 While the notepad isn't 0:
-6>7?6& Exit if End of Input
@7 Print the next character
+70-4 Set notepad to 0 if it was a newline
~ Take the next character
><>, 60 bytes
!^i:0(?;::"/")$":"(*0$.
v"0"-
>:?!v1-" "o
;>:o>a=&10&?.i:0(?
How It Works:
..i:0(?;... Gets input and ends if it is EOF
...
...
...
.^......::"/")$":"(*0$. If the inputted character is a digit go to the second line
... Else go to the fourth
...
...
.... If it was a digit
v"0"- Subtract the character "0" from it to turn it into the corresponding integer
>:?!v1-" "o And print that many spaces before rejoining the fourth line
...
.^.. On the fourth line,
.... Copy and print the input (skip this if it was a digit)
....v If the input is a newline, go back to the first line.
;>:o>a=&10&?.i:0(? Else get the input, ending on EOF
Japt (v2.0a0), 11 10 bytes
Japt beating Jelly and 05AB1E? That doesn't seem right!
r/^\d/m_°ç
Explanation
Implicit input of string U
r/^\d/m
Use Regex replace (r) all occurrences of a digit at the beginning of a line (m is the multiline flag - the g flag is enabled by default in Japt).
_
Pass each match through a function, where Z is the current element.
°
The postfix increment operator (++). This converts Z to an integer without increasing it for the following operation.
ç
Repeat a space character Z times.
Implicitly output the resulting string.
PHP, 83 chars
preg_replace_callback('/^\d/m',function($m){return str_repeat(' ',$m[0]);},$argv);
Cubically, 82 bytes
R3D1R1D1+0(?6{?7@7~:1+2<7?6{+35>7?6{:7-120?6{(B3@5B1-0)6}:0}}}?6!@7~-60=7&6+4-3=7)
Note: This will not work on TIO. To test this, use the Lua interpreter with the experimental flag set to true (to enable conditionals). There's currently a bug with conditional blocks on the TIO interpreter. When using the TIO interpreter, you should replace ?6! with !6 and &6 with ?6&, which keeps the byte count the same.
R3D1R1D1 Set the cube so that face 0 has value 1 and the rest of the values are easy to calculate
+0 Set the notepad to 1 so that it enters the conditional below
( Do
?6{ If the notepad is 1 (last character was \n or start of input)
?7@7 Output the current character if it's \n
~ Get the next character
:1+2<7?6{ If the input is >= '0'
+35>7?6{ If the input is <= '9'
:7-120 Set the notepad to the input - '0'
?6{ If the notepad isn't 0
( Do
B3@5 Output a space
B1-0 Subtract 1 from notepad
)6 While notepad > 0
} End if
:0 Set notepad to 1
} End if
} End if
} End if
?6!@7 If the notepad is 0 (did not attempt to print spaces), print current character
~ Get next character
-60=7&6 If there is no more input, exit the program
+4-3=7 Check if current character is \n, setting notepad to result
) Repeat forever
This isn't as short as the other Cubically answer, but I thought I'd give this a try anyway :D
R, 138 128 bytes
-9 bytes thanks to CriminallyVulgar
n=readLines();for(d in grep("^[0-9]",n))n[d]=gsub('^.?',paste0(rep(' ',eval(substr(n[d],1,1))),collapse=''),n[d]);cat(n,sep='
')
This is pretty bad, but it's a bit better now... R is, once again, terrible at strings.
Java 8, 105 99 97 93 bytes
Saved few more bytes thanks to Nevay's suggestion,
s->{int i=s.charAt(0);if(i>47&i<58)s=s.substring(1);while(i-->48)s=" "+s;System.out.print(s);}
Husk, 15 13 bytes
-2 bytes thanks to @Zgarb
mΓo+?oR' i;±¶
Uses the same technique as @Jonathan Allan
Explanation
¶ -- split input into a list of lines
m -- apply the following function to each line
Γ -- deconstruct the string into a head and a tail
o+ -- prepend to the tail of the string ...
? ± -- if the head is a digit (n)
oR' i -- the string of n spaces
-- else
; -- the head of the string
-- implicitly print list of strings line-by-line
Gema, 21 characters
\N<D1>=@repeat{$1;\ }
Sample run:
bash-4.4$ gema '\N<D1>=@repeat{$1;\ }' <<< 'foo bar foo bar
> 1foo bar foo bar foo bar
> 2foo bar foo bar foo bar foo bar
>
> --------v
> 8|
> 8|
> 80
> 8,
> 7&'
foo bar foo bar
foo bar foo bar foo bar
foo bar foo bar foo bar foo bar
--------v
|
|
0
,
&
JavaScript (ES8), 38 37 bytes
a=>a.replace(/^\d/gm,a=>''.padEnd(a))
I don't think it can be improved much more.
Saved 1 byte thanks to Shaggy - Use ES8 features.
V, 9 bytes
ç^ä/x@"é
Explanation
ç / ' On lines matching
^ä ' (Start)(digit)
x ' Delete the first character
@" ' (Copy Register) number of times
é ' Insert a space
Python 2, 98 74 67 65 bytes
-24 bytes thanks to Jonathan Allan. -7 bytes thanks to Mr. Xcoder.
for i in open('f'):print' '*int(i[0])+i[1:]if'/'<i[:1]<':'else i,
Takes input in the file named f.
Python 2, 76 72 68 bytes
-4 bytes thanks to @ovs!
@DeadPossum suggested switching to Python 2, which saved 4 bytes too.
Just thought it's nice to have a competitive full program in Python 2 that does not explicitly check if the first character is a digit. This reads the input from a file, f.
for i in open('f'):
try:r=int(i[0])*" "+i[1:]
except:r=i
print r,
Try it online! (courtesy of @ovs)
Haskell, 63 bytes
unlines.map g.lines
g(x:r)|x<';',x>'/'=(' '<$['1'..x])++r
g s=s
Try it online! The first line is an anonymous function which splits a given string into lines, applies the function g to each line and joins the resulting lines with newlines. In g it is checked whether the first character x of a line is a digit. If this is the case, then ['1'..x] yields a string with length equal to the value of the digit x and ' '<$ converts the string into as many spaces. Finally the rest of the line r is appended. If x is not a digit we are in the second equation g s=s and return the line unmodified.
Pyth, 16 15 bytes
jm.x+*;shdtdd.z
Explanation
jm.x+*;shdtdd.z - Full program that works by reading everything from STDIN.
.z - Read all STDIN and split it by linefeeds.
m - Map with a variable d.
.x - Try:
*;shd - To convert the first character to an Integer and multiply it by a space.
+ td - And add everything except for the first character
d - If the above fails, just add the whole String.
j - Join by newlines.
Let's take an example that should be easier to process. Say our input is:
foo bar foo bar
1foo bar foo bar foo bar
2foo bar foo bar foo bar foo bar
The program above will do the following:
.z- Reads it all and splits it by newlines, so we get['foo bar foo bar', '1foo bar foo bar foo bar', '2foo bar foo bar foo bar foo bar'].We get the first character of each:
['f', '1', '2'].If it is convertible to an integer, we repeat a space that integer times and add the rest of the String. Else, we just place the whole String. Hence, we have
['foo bar foo bar', ' foo bar foo bar foo bar', ' foo bar foo bar foo bar foo bar'].Finally, we join by newlines, so our result is:
foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar
Jelly, 19 bytes
Ḣ⁶ẋ;µ¹µḣ1ẇØDµ?
ỴÇ€Y
-5 bytes total thanks to Jonathan Allan's comments and by looking at his post
Explanation
Ḣ⁶ẋ;µ¹µḣ1ẇØDµ? Main link
? Ternary if
if:
ḣ1 the first 1 element(s) (`Head` would modify the list which is not wanted)
ẇ is a sublist of (essentially "is an element of")
ØD "0123456789"
then:
ẋ repeat
⁶ ' '
Ḣ n times where n is the first character of the line (head)
; concatenate the "beheaded" string (wording choice credited to Jonathan Allan)
else:
¹ Identity (do nothing)
µ µ µ Link separators
ỴÇ€Y Executed Link
Ỵ Split by newlines
€ For each element,
Ç call the last link on it
Y Join by newlines
Jelly, 19 bytes
V⁶ẋ
Ḣǹe?ØD;
ỴÇ€Yḟ0
A monadic link taking and returning lists of characters, or a full program printing the result.
How?
V⁶ẋ - Link 1, make spaces: character (a digit)
V - evaluate as Jelly code (get the number the character represents)
⁶ - a space character
ẋ - repeat
Ḣǹe?ØD; - Link 2, process a line: list of characters
Ḣ - head (get the first character and modify the line)
- Note: yields zero for empty lines
ØD - digit characters = "0123456789"
? - if:
e - ...condition: exists in? (is the head a digit?)
Ç - ...then: call the last link as a monad (with the head as an argument)
¹ - ...else: identity (do nothing; yields the head)
; - concatenate with the beheaded line
ỴÇ€Yḟ0 - Main link: list of characters
Ỵ - split at newlines
Ç€ - call the last link (1) as a monad for €ach
Y - join with newlines
ḟ0 - filter out any zeros (the results of empty lines)
Python 3, 95 bytes
lambda y:'\n'.join(re.sub('^\d',lambda x:' '*int(x.group()),z)for z in y.split('\n'))
import re
-4 bytes by stealing the regex idea from ThePirateBay