Mamy wiele poziomych osi dla liczb, ale szczerze uważam, że są one trochę nudne. Twoim zadaniem dzisiaj jest zbudowanie części osi ukośnej między dwiema różnymi liczbami całkowitymi nieujemnymi podanymi jako dane wejściowe.
Jak zbudować oś przekątną?
Weźmy przykład z danymi wejściowymi
0, 5
. Nasza oś powinna wyglądać następująco:0 1 2) 3) 4 5
Nasza oś powinna jednak dobrze wyglądać w przypadku liczb, które mają więcej cyfr! Jeśli na przykład wejściem jest,
0, 14
nowa oś powinna być:0 1 2) 3) 4 5 6 7 8 9 10 11 12 13 14
Chodzi o to, że pierwsza cyfra następnego numeru na osi zawsze musi być umieszczona dokładnie za ostatnią cyfrą poprzedniego numeru. Aby jeszcze lepiej zrozumieć ten pomysł, oto kolejny przykład
997, 1004
:997 998 999 1000 1001 1002 1003 1004
Zasady
Możesz założyć, że dane wejściowe są w porządku rosnącym lub malejącym (możesz wybierać między
5,3
i3,5
).Możesz również założyć, że różnica między dwiema liczbami całkowitymi jest mniejsza niż 100.
Możesz mieć wiodącą nową linię lub spójną spację wiodącą (w każdej linii). Spacje końcowe / znaki nowej linii również są w porządku.
Możesz przyjmować dane wejściowe i dostarczać dane wyjściowe dowolnym standardowym środkiem .
To jest golf golfowy , więc wygrywa najkrótszy kod w bajtach w każdym języku!
Inne przypadki testowe
1, 10
:1 2) 3) 4 5 6 7 8 9 10
95, 103
:95 96 97 98 99 100 101 102 103
999999, 1000009
:999999 1000000 1000001 1000002 1000003 1000004 1000005 1000006 1000007 1000008 1000009
Odpowiedzi:
05AB1E,
876 bytesThanks to Magic Octopus Urn for saving a byte!
It somehow works, but honestly I have no idea why.
Code
Uses the 05AB1E encoding. Try it online!
Explanation
źródło
.O = pop a,b push connected_overlap(b) (deprecated)
- Oh, I guess?.O
is extremely buggy and deprecated for over a year so I have no idea what works and what doesn't. I could swear that I neededÎ
, but that suddenly doesn't seem to be the case anymore (?). Thanks! :)Î
was needed to reduce the maximum number of leading spaces to 1.Python 2, 43 bytes
Makes use of vertical tab to make the ladder effect. The way thet
\v
is rendered is console dependent, so it may not work everywhere (like TIO).źródło
\x0b
in your code to save a byte?\\v
and replaced with\x0B
which shows up aVT
character in its place for scoring you can either post a reversible hexdump (xxd
or something) or just state that: "\v
is a literal vertical tab", I think that would be fair. Hope that helps!Charcoal,
98 bytesTry it online!
Link is to the verbose version of the code. Input in ascending order.
źródło
R,
706961 bytesFunction that takes the start and end variable as arguments. Loops over the sequence, and prints each element, prepended with enough spaces.
F
starts asFALSE=0
, and during each iteration, the amount of characters for that value is added to it.F
decides the amount of spaces printed.Try it online!
-8 bytes thanks to @Giuseppe
źródło
scan()
twice it can be reduced to 67 bytesfor(i in scan():scan()){cat(rep(' ',F),i,'\n',sep='');F=F+nchar(i)}
.F=0;for(i in scan():scan()){cat(rep(' ',F),i,'\n',sep='');F=F+nchar(i)}
(71 byte)nchar
.\n
for an actual newline works too, and that doesn't cost two bytes apparently.cat
, but I couldn't think straight and figure it out for some reason.C#,
908985 bytesSaved 1 byte thanks to @LiefdeWen.
Saved 4 bytes thanks to @auhmaan.
Try it online!
Full/Formatted version:
źródło
i<=e
toe>i
i
and reusing thes
insteadPython 2,
5854 bytesTry it online!
źródło
Mathematica, 59, bytes
input
-3 bytes @JungHwanMin
problem with 0 fixed (see comments for details)
thanx to @ngenisis
źródło
Spacings -> 0
if you want this to be character-exact.Grid[(DiagonalMatrix@Range[1+##]/. 0->""+1)-1,Spacings->0]&
is the shortest way I could find to fix those problemsJelly, 9 bytes
Try it online!
źródło
Mathematica, 48 bytes
since there are so many answers, I thought this one should be included
input
output
źródło
C,
1661349582 BytesNew Answer
Just as a function not as a whole program.
Thanks to Falken for helping knock off 13 Bytes (and fix a glitch)!
Thanks to Steph Hen for helping knock off 12 Bytes!
Thanks to Zacharý for help knock off 1 Byte!
Old Answers
Got rid of the int before main and changed const char*v[] to char**v and got rid of return 0;
This is my first time golfing and I wanted to try something in C. Not sure if I formatted this correctly, but I had fun making it!
Explanation
Usage
źródło
argc
andargv
to one letter variables.int s=0
to the for loop, as infor(int s=0;a<=b;a++)
.int i=s;while(i--)
instead offor(int i=0;i<s;i++)
for the inner loop will save two bytes.C++,
167165 bytes-2 bytes thanks to Zacharý
źródło
int m=0,i=l,j
to the firstfor
loop to save a byte? 2. Can you changer+=t;r+=10
tor+=t+10
? 3. I beat someone, yay.r+=t+=10
but notr+=t+10
, it gave me an errorr+=t+=10
does work? Wouldn't that affectt.size()
?++i
to thestd::to_string(i)
asstd::to_string(i++)
to save one more byte?APL (Dyalog),
2524 bytes-1 thanks to Zacharý.
Assumes
⎕IO←0
for zero based counting. Takes the lower bound as left argument and the upper bound as right argument.Try it online!
(
…)
apply the following tacit function between the arguments:-
subtract the upper lower from the upper bound1-
subtract that from one (i.e. 1 + ∆)⊣+∘⍳
left lower bound plus the integers 0 through that⍕¨
format (stringify) each{
…}
apply the following anonymous on that (represented by ⍵):≢¨
length of each (number)+\
cumulative sum-
negate⍵↑⍨¨
for each stringified number, take that many characters from the end (pads with spaces)↑
mix list of strings into character matrixźródło
+-⍨
be--
?Retina,
8178 bytesTry it online! Takes input as a newline-separated list of two integers. Edit: Saved 3 bytes by stealing the range-expansion code from my answer to Do we share the prime cluster? Explanation:
Convert both inputs to unary.
While the last two elements (a, b) of the list differ by more than 1, replace them with (a, a+1, b). This expands the list from a tuple into a range.
Convert back to decimal in duplicate.
Convert the duplicate copy to spaces.
Cumulatively sum the spaces from each line to the next.
źródło
Pyth,
1413 bytesTry it online!
źródło
LOGO, 53 bytes
There is no "Try it online!" link because all online LOGO interpreter does not support template-list.
That is a template-list (equivalent of lambda function in other languages).
Usage:
(
apply
calls the function)will print
Note:
This uses turtle's
ycor
(Y-coordinate) to store the number of spaces needed to type, therefore:window
should be executed ifycor
gets too large that the turtle moves off the screen. Description ofwindow
command:if the turtle is asked to move past the boundary of the graphics window, it will move off screen.
, unlike the default settingwrap
, whichif the turtle is asked to move past the boundary of the FMSLogo screen window, it will "wrap around" and reappear at the opposite edge of the window.
Explanation:
źródło
05AB1E, 8 bytes
Try it online!
-2 thanks to Adnan.
źródło
vy
byʒ
andgF
byv
to save 2 bytes.ʒ
trick to still be used...JavaScript (ES8),
696762 bytesTakes input as integers, in ascending order, using currying syntax. Returns an array of strings.
Try it
źródło
Japt, 12 bytes
Takes input in either order and always returns the numbers in ascending order, as an array of lines.
Try it online! with the
-R
flag to join the array with newlines.Explanation
Implicit input of
U
andV
.Create inclusive range
[U, V]
and map each value to...The values before the current (
¯Y
), joined to a string (¬
) and filled with spaces (ç
).Plus the current number. Resulting array is implicitly output.
źródło
Python 2,
65 63 6261 bytes-2 bytes Thanks to @Mr. Xcoder:
exec
doesn't need braces-1 bye thanks to @Zacharý:
print s*' '
asprint' '*s
Try it online!
źródło
m,n=input();s=0;exec(n-m+1)*"print s*' '+`m`;s+=len(`m`);m+=1;"
suffices.print s*' '
toprint' '*s
to save one byte.JavaScript, 57 bytes
źródło
y=>g=(x,s='')=>y<x?s:s+'\n'+g(x+1,s.replace(/./g,' ')+x)
Call with currying with the integers reversed:f(103)(95)
.x=>y=>g=(s='')=>y<x?s:s+'\n'+g(s.replace(/./g,' ')+x++)
Call asf(x)(y)()
.Python 2,
6059 bytes-1 byte thanks to Mr.Xcoder for defining my s=0 as an optional variable in my function.
Try it online!
I think it is possible to transfer this into a lambda version, but I do not know how. I also think that there is some sort of mapping between the spaces and the length of the current number, but this I also did not figure out yet. So I think there still is room for improvement.
What i did was creating a range from the lowerbound
l
to the upper boundu
printing each line with a space multiplied with a numbers
. I am increasing the multiplier with the length of the current number.źródło
Python 2,
787779 bytesTry it online!
f(A, B)
will print the portion of the axis between A and B inclusive.First time I answer a challenge!
Uses and abuses Python 2's backticks to count the number of spaces it has to add before the number.
-1 byte thanks to Mr.Xcoder
+2 because I forgot a
+1
źródło
sum(len(`j`)) for
can becomesum(len(`j`)for
, -1 bytesrange(a,b)
withrange(a,b+1)
, because Python has semi inclusive ranges.C (gcc),
4138 bytes-3 bytes Thanks to ASCII-only
Works on RedHat6, accessed via PuTTY
Try it online!
źródło
more
on that fileV, 16 bytes
Try it online!
This would be way easier if I could take
start
end - start
but I think that's changing the challenge a bit too much.This takes the start number as input in the buffer and the end number as an argument. It actually creates the ladder from
start
tostart + end
and then deletes everything after the end number.źródło
MATL, 11 bytes
Try it online!
Explanation
This works by generating a string for each number and concatenating it with a logically-negated copy of the previous string. Thus char 0 is prepended 0 as many times as the length of the previous string. Char 0 is displayed as a space, and each string is displayed on a different line
źródło
Swift 4, 115 bytes
I think nobody would have posted a Swift solution anyway...
Try it online!
źródło
Perl, 19 bytes
Note:
\x0b
is counted as one byte.Along with others, I thought using cursor movements would be the shortest route, this does mean it doesn't work on TIO:
Usage
źródło
1..
does there, since you are given two integers.1..
was me not fully reading the spec, that's fixed now! As for testing it online, because the output contains the vertical tab, it doesn't render as expected. Trying to see if I can find a renderer that does support control chars... If not, that might be my new project!Japt,
109 bytesTest it online! Returns an array of lines;
-R
flag included to join on newlines for easier viewing.Explanation
Old version, 10 bytes:
Test it online!
źródło
D,
133127126125121119 bytesJelly and APL were taken.
Try it online!
If you're fine with console-dependent results (goes off the same principle as Giacomos's C answer) here's one for
7271 bytes:How? (Only D specific tricks)
f(T)(T a,T b,T s=0)
D's template system can infer typesfor(T j;j++<s;)
Integers default to0
.' '.write;a.writeln
D lets you callfun(arg)
likearg.fun
(one of the few golfy things D has)a.text.length
Same as above, and D also allows you to call a method with no parameters as if it was a property (text
is conversion to string)źródło
Java 8, 79 bytes
Try it online!
źródło
(a,b)->
tob->a->
. (And you could save three more bytes by going to Java 10 and changingString
tovar
.) Try it online.