32
\$\begingroup\$

The task is to display n characters of the ASCII table.

You may write a function (or a program that takes the argument as a parameter, STDIN is allowed as well) that takes a parameter n, which will be the index of the last character to print.

The task is quite simple, so as an example here's a possible implementation in Python 2.7:

(lambda n:map(chr, range(n)))(256)

As I said it's a simple task. So this is code-golf and the shortest codes wins!

EDIT

As some of you pointed out this code doesn't print the result. It's just an example since I might struggle explaining the problem in english ;-).

EDIT2

Feel free to post the answer in any programming language, even if it's not the shortest code. Maybe there are some interesting implementations out there!

EDIT3

Fixed the example so it prints the result.

\$\endgroup\$
14
  • \$\begingroup\$ 1. Does it have to be a function? 2. According to your reference code, n would be the first character that is not printed. \$\endgroup\$
    – Dennis
    Oct 28, 2014 at 12:49
  • 2
    \$\begingroup\$ Actually the reference code prints nothing. It just returns a list of the characters and lets the REPL do whatever it wants with the result. \$\endgroup\$
    – manatwork
    Oct 28, 2014 at 12:50
  • 1
    \$\begingroup\$ Can somebody please explain the downvote? I am sorry if my english isn't that good. If there's something unclear within the question please tell me. \$\endgroup\$
    – oopbase
    Oct 28, 2014 at 12:57
  • 1
    \$\begingroup\$ for x in range(input()):print chr(x) Would actually print the characters, if you want to edit your example. \$\endgroup\$ Oct 28, 2014 at 12:59
  • 2
    \$\begingroup\$ nota [i for i in range(n)] is quite similar to range(n) \$\endgroup\$
    – njzk2
    Oct 28, 2014 at 19:22

101 Answers 101

1 2 3
4
0
\$\begingroup\$

QC 18 bytes

'00901)01016010003

'00 Read from stdin and store in memory at address 00
901 Print memory from address 01 until 0 is reached
)0101 Increment memory at 01 by 1
6010003 Jump to 03 if value at 01 and 00 are not equal

Edited because of invalid amount of bytes

\$\endgroup\$
0
\$\begingroup\$

REXX, 28 bytes

say xrange('0'x,d2c(arg(1)))
\$\endgroup\$
0
\$\begingroup\$

Sinclair ZX81 171 bytes for the listing (method 1):

 1 LET N$=""
 2 GOTO 2+(INKEY$<>"")
 3 LET A$=INKEY$
 4 IF A$=CHR$ 118 THEN GOTO 9
 5 IF CODE A$<28 OR CODE A$>37 THEN GOTO 2
 6 PRINT A$;
 7 LET N$=N$+A$
 8 GOTO 2
 9 LET N=VAL N$
10 IF N>255 THEN LET N=255
11 PRINT AT 0,0;
12 FOR I=0 TO N
13 PRINT CHR$ N;
14 NEXT I

This is using the INKEY$ command to build a string of numbers, which is checked by comparing each input to the character range from 0 to 9 inclusing in line 5. Each character is stored in the N$ variable and outputted to the screen. CHR$ 118 is the ZX81 equivalent of a new line "\r\n" in PHP for instance.

There is a check to see if you have entered a valid range once you have pressed NEW LINE (0 to 255 inclusive) - as you can only enter numbers, the range is not going to be below zero.

Then there is a simple FOR...NEXT loop from line 12 to output the non-ASCII compatible character set. So I lose points because the ZX81 does not handle ASCII without some conversion tables.

Also, the ZX81 character set is incomplete to upper-case and inversed upper-case only; the remaining characters are either graphical symbols or they are ZX81 BASIC keywords, such as PI, INKEY$, PRINT etc... There is a much simpler method that I will post later, but is more prone to human error.

Method 2, 34 bytes for the listing, no error checks and sanitizing inputs:

 1 INPUT N
 2 FOR I=0 TO N
 3 PRINT CHR$ I;
 4 NEXT I

Of course I'm sure that someone has already pointed out that true ASCII is 7 bit and the first 8 bit compatible ASCII set was PETSCII.

\$\endgroup\$
0
\$\begingroup\$

Commodore 64/128

This solution uses PETSCII which is an 8-bit wide ASCII-compatible character set.

Solution 1 - 42 BASIC bytes used (35 bytes for the listing)

0 printchr$(14);:fori=0to255:poke1024+i,i:next

Solution 2 - 10 bytes assembled, ~72 bytes source code

* = 49152
lda #$00
tax
.loop
    sta $0400,x
    dex
    txa
bne loop
rts

The assembly is doing what the BASIC listing is doing (without switching the character set to business mode first), but is obviously many times quicker. As the 6502 (6510/85xx) processor has an 8-bit accumulator and [8-bit] registers, we start at zero and count down, so 0 - 1 is 0xff. Each character is stored in the screen RAM located at 0x0400 (1024) and sta $0400,x says store the content of X to location $0400 + x. The bne loop will branch back based on the zero flag, once that is set (i.e., x=0x00) it will drop out of the loop and return to BASIC with rts.

Once assembled, run with sys 49152 - this solution should work on the C64 and 128. It will work on other Commodore machines as long as you move the routine to a free area of RAM and relocate the starting point of the screen RAM from 1024.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 3 bytes

L<ç

Try it online!

L<ç  # full program
  ç  # convert...
     # (implicit) each element of...
L    # [1, 2, 3, ...,
     # ..., implicit input...
L    # ]...
     # (implicit) with each element...
 <   # decremented...
  ç  # to chars
     # implicit output
\$\endgroup\$
0
\$\begingroup\$

C

I still get better, hopefully.

#include <stdio.h>
int main() {
    for(int i='a';i<='z';printf("%d\n",i),i++);
  return 0;
}
\$\endgroup\$
0
\$\begingroup\$

Pxem, Filename: 19 bytes + Content: 0 bytes = 19 bytes.

Now the debut of lazypxem.min.posixism.

  • Filename (escaped some): ._.c.w\001.-.c.c.a.s.p
  • Content is empty.

Try it online! The footer has |od -vtu1 to be commented; uncomment it to inspect the exact output.

\$\endgroup\$
0
\$\begingroup\$

Factor, 16 bytes

[ <iota> print ]

In modern versions of Factor, you can output sequences directly as strings. Here's a pic:

enter image description here

To get it to run on build 1525 (the one TIO uses), you'd have to convert the range to a string like this:

Factor, 21 bytes

[ iota ""like print ]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Ly, 7 bytes

n[:o,]o

Try it online!

This one, like at least one previous entry, prints the numbers from largest codepoint to 0, which wasn't disallowed in the rules as far as I can tell.

 n        - read the number of characters to print
  [   ]   - loop until the top of stack is 0
   :      - duplicate the top of stack
    o     - print it
     ,    - decrement the top of stack
       o  - print the x'00' entry
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 27 bytes

f(c){c&&f(c-1),putchar(c);}

Try it online!

C (MSVC), 25 bytes

f(c){c&&f(c-1),putch(c);}
\$\endgroup\$
0
\$\begingroup\$

Vyxal s, 2 bytes

ʁC

Try it Online!

\$\endgroup\$
1 2 3
4

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.