12
\$\begingroup\$

Given a positive integer n, output a number meter formatted like this:

                  0 1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2

The input number is 20 in this example.

More formally, the decimal representations of the numbers are arranged vertically, from most significant digits on the last row to least significant digits at the top, using space-padding.

All numbers should be printed, and any size of number is allowed.

An example solution in Python 3 follows:

def meter(top: int):
    initial = list(str(str(n).ljust(len(str(top + 1)))) for n in range(1, top + 1))
    rotated = list(zip(*initial))[::-1]

    return "\n".join(" ".join(n) for n in rotated)

This is Code Golf! Make the answer with the lowest bytes to win.

\$\endgroup\$
3
  • 5
    \$\begingroup\$ I've added a more formal definition. Feel free to rollback or edit further if necessary. \$\endgroup\$
    – Arnauld
    Aug 18, 2023 at 13:00
  • 2
    \$\begingroup\$ This is almost the same format used in advent of code, though theirs reads top down rather than bottom up. \$\endgroup\$
    – Jonah
    Aug 18, 2023 at 18:28
  • \$\begingroup\$ Thanks for the formalities, I didn't really know how to describe such a weird concept. Yours is more helpful. As for the advent of code, I never realized it was in there, I just wrote this for a decibel meter and thought it would be a cool Code Golf. \$\endgroup\$
    – TKDKid1000
    Aug 18, 2023 at 20:57

18 Answers 18

14
\$\begingroup\$

Canvas, 4 bytes

R↶ *

Try it online.

Explanation:

R     # Push a list in the range [1, (implicit) input-integer],
       # and implicitly print each integer on a separated newline to the Canvas
 ↶    # Rotate the entire Canvas 90 degrees counterclockwise
   *  # Join each character on each line of the Canvas with a space delimiter
\$\endgroup\$
5
\$\begingroup\$

K (ngn/k), 40 30 20 bytes

{" "/'|+(#$x)$$1+!x}

Try it online!

-10 : more straightforward (less fun?)
-10 : Actual golfing courtesy of @coltim and @bstrat

\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 69 bytes

Expects the input number as a string.

f=(n,d=0,i=n)=>i?f(n,d,i-1)+(`${i}`[d]||' ')+' ':n[++d]?f(n,d)+`
`:''

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Vyxal, 40 bitsv2, 5 bytes

ɾ∩Ṙ⁋øɽ

Try it Online!

I honestly don't know how people find these obscure string overloads that I can never seem to remember.

Explained

ɾ∩Ṙ⁋øɽ­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌­
ɾ∩      # ‎⁡Transpose the range [1, input], treating digits as columns.
  Ṙ⁋    # ‎⁢Reverse that, and join each sublist on spaces. Then, join that on newlines.
    øɽ  # ‎⁣Right align that. I honestly don't know why this works. It just does.
💎

Created with the help of Luminespire.

\$\endgroup\$
1
3
\$\begingroup\$

Thunno 2 N, 7 bytes

RðƬrðȷj

Try it online!

Explanation

RðƬrðȷj  # Implicit input
RðƬ      # Transpose [1..input], filling with spaces
   rðȷj  # Reverse and join each on spaces
         # Implicit output, joined on newlines
\$\endgroup\$
3
\$\begingroup\$

Charcoal, 7 bytes

↑IEN⟦⊕ι

Try it online! Link is to verbose version of code. Explanation:

   N    Input as a number
  E     Map over implicit range
      ι Current value
     ⊕  Incremented
    ⟦   Make into sublist (causes the output to be double-spaced)
 I      Cast to string
↑       Print rotated 90°
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 6 bytes

L€SζR»

Try it online.

Explanation:

L       # Push a list in the range [1, (implicit) input-integer]
 €S     # Map each integer to a list of digits
   ζ    # Zip/transpose; swapping rows/columsn,
        # with a space character to fill unequal length rows
    R   # Reverse this list of lists
     »  # Join each inner list by spaces, and then each string by newlines
        # (after which the result is output implicitly)
\$\endgroup\$
3
\$\begingroup\$

sclin, 55 bytes

;1+ I>a10X>b tpose _`"_`\" \"rep +` ; tk _` w>< n>o"map

Try it here! Takes input from the second line.

For testing purposes:

;1+ I>a10X>b tpose _`"_`\" \"rep +` ; tk _` w>< n>o"map
20

Explanation

Prettified code:

; 1+ I>a 10X>b tpose _` ( _` " "rep +` ; tk _` w>< n>o ) map

Assuming input n:

  • ; 1+ I>a range [1, n]
  • 10X>b digitize (literally "convert each to base-10 digits")
  • tpose _` transpose, reverse resulting lines
  • (...) map map over each line...
    • _` " "rep +` ; tk _` left-pad with spaces to length n
      • _` reverse
      • " "rep +` concatenate to infinite list of spaces
      • ; tk take first n elements
      • _` reverse again
    • w>< n>o join with space and output

The (current) lack of string manipulation commands present in sclin makes it a tad unwieldy for string challenges...

\$\endgroup\$
3
\$\begingroup\$

Python, 72 bytes

n=input()
*map(print,*(f'{j+1:<{len(n)}}'[::-1]for j in range(int(n)))),

Attempt This Online!

-1 thanks to @Arnauld (by switching to Python 3.8)
-5 thanks to @c-- (by using f-strings)
-12 thanks to @loopy walt (by using map rather than a for loop)

\$\endgroup\$
2
  • \$\begingroup\$ 84 bytes using f-strings \$\endgroup\$
    – c--
    Aug 18, 2023 at 14:51
  • 2
    \$\begingroup\$ 72 \$\endgroup\$
    – loopy walt
    Aug 19, 2023 at 6:10
2
\$\begingroup\$

Ruby, 79 bytes

->n{(1..n).map{d=_1.digits
[" "]*(n.to_s.size-d.size)+d}.transpose.map{_1*" "}}

Attempt This Online!

\$\endgroup\$
3
  • \$\begingroup\$ Alternate 79, maybe you can improve it? ->n{(1..n).map{_1.to_s.ljust(n.to_s.size).chars}.transpose.reverse.map{_1*" "}} \$\endgroup\$
    – Jonah
    Aug 19, 2023 at 7:31
  • 1
    \$\begingroup\$ 68 bytes \$\endgroup\$
    – Jonah
    Aug 19, 2023 at 21:06
  • 1
    \$\begingroup\$ @Jonah Very nice. That’s a very different approach from mine; you should post it as your own answer. \$\endgroup\$
    – Jordan
    Aug 20, 2023 at 2:21
2
\$\begingroup\$

Japt -R, 8 bytes

õs ÕÔˬ¸

Try it

\$\endgroup\$
2
\$\begingroup\$

R, 82 bytes

\(n)write(t(Reduce(cbind,strsplit(format(paste(1:n),j="l"),""))[nchar(n):1,]),1,n)

Attempt This Online!

\$\endgroup\$
2
\$\begingroup\$

J, 25 23 20 bytes

1j1#"#.0|.@|:1":@+i.

Try it online!

-3 thanks to doug!

  • 1 ... +i. 1...n
  • ":@ Each of them formatted as a string
  • 0|.@|: Transposed and reversed
  • 1j1#"#. Add the space padding
\$\endgroup\$
6
  • \$\begingroup\$ Shouldn’t the most significant digit be on the bottom row? \$\endgroup\$
    – doug
    Aug 18, 2023 at 21:57
  • 1
    \$\begingroup\$ Yep, fixed now. Thanks! \$\endgroup\$
    – Jonah
    Aug 18, 2023 at 22:48
  • \$\begingroup\$ FWIW, I just learned of # with complex count. I think think could be useful here. \$\endgroup\$
    – doug
    Aug 19, 2023 at 21:17
  • \$\begingroup\$ It's a good thought but eg [:;"{1j1<@#"+0|.@|:1":@+i. is 26, and I don't see a way to beat 23 with that method. \$\endgroup\$
    – Jonah
    Aug 19, 2023 at 21:29
  • 1
    \$\begingroup\$ I think the idea would be to do the take rank 1 at the end. 1j1#"#.0|.@|:1":@+i. \$\endgroup\$
    – doug
    Aug 19, 2023 at 21:40
2
\$\begingroup\$

Ruby, 68 bytes

->n{(0...s=n.to_s.size).map{|i|(0..n).map{_1.digits[i-s]||" "}*" "}}

Attempt This Online!

Inspired by Jordan's ruby answer, but different enough to merit a new post.

\$\endgroup\$
1
\$\begingroup\$

Retina, 57 bytes

.+
*¶
¶
$^$.>`#¶#¶
P^`.+
N$`.
$.%`
¶

~L$`#+
L`.{$.&}
A`#

Try it online! Explanation: Based on my golf to @TwiNight's answer to Enklactify these strings to transpose the text.

.+
*¶
¶
$^$.>`#¶#¶

Create a range from 1 to the input, but with the digits reversed, and each line padded with a #, and the lines separated by a line with just a #.

P^`.+

Left pad everything with spaces so that it lines up.

N$`.
$.%`

Sort all characters by their column index.

¶

~L$`#+
L`.{$.&}

Complete the transposition by joining everything together and then splitting by the number of #s.

A`#

Remove the final line, which will always be the #s.

\$\endgroup\$
1
\$\begingroup\$

Excel, 49 bytes

=MID(SEQUENCE(,A1),1+LEN(A1)-SEQUENCE(LEN(A1)),1)

Input in cell A1.

\$\endgroup\$
1
\$\begingroup\$

Jelly, 6 bytes

Ṿ€z⁶ṚG

Try it online!

-2 thanks to @UnrelatedString

Explanation

Ṿ€z⁶ṚG  # Main link - argument n
 €      # To each number in the range [1..n]:
Ṿ       #  Convert it to a string
  z⁶    # Transpose using filler space
    Ṛ   # Reverse this list of lists
     G  # Format as a grid
        # (join each on spaces, then newlines)
\$\endgroup\$
2
  • \$\begingroup\$ For starters, there's G :P \$\endgroup\$ Aug 20, 2023 at 23:49
  • \$\begingroup\$ @UnrelatedString nice! Love how it says "Attempt to format z as a grid" in the docs lol \$\endgroup\$
    – The Thonnu
    Aug 21, 2023 at 8:36
1
\$\begingroup\$

jq, 68 66 bytes

-2 bytes with a better use of range

[range(.)+1|@sh/""]|transpose|map(map(.//" ")|join(" "))|reverse[]

Attempt This Online! Attempt This Online!

[
  range(.) + 1   # `range` generates 0 to n - 1; add 1
  | @sh / ""     # Since numbers do not have spaces or special characters,
                     # `@sh` behaves the same as `@text`/`tostring`.
                     # `/ ""` splits into an array of characters
]                # `[ ... ]` collects values into an array
| transpose      # transpose, filling missing values with null.
| map(
    map(.//" ")  # replace null with space
    | join(" ")  # join on spaces
  )
| reverse[]      # most-significant digits last.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.