# Print the ASCII table

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.

• 1. Does it have to be a function? 2. According to your reference code, n would be the first character that is not printed. – Dennis Oct 28 '14 at 12:49
• 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. – manatwork Oct 28 '14 at 12:50
• 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. – oopbase Oct 28 '14 at 12:57
• for x in range(input()):print chr(x) Would actually print the characters, if you want to edit your example. – FryAmTheEggman Oct 28 '14 at 12:59
• nota [i for i in range(n)] is quite similar to range(n) – njzk2 Oct 28 '14 at 19:22

Input""n
For i=1To n

{10h$til x} # Gema, 43 characters *=@set{i;0}@repeat{*;@int-char{$i}@incr{i}}

Sample run:

bash-4.3$gema '*=@set{i;0}@repeat{*;@int-char{$i}@incr{i}}' <<< 96 | xxd
0000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f  ................
0000010: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f  ................

Takes a number form the command line and loops from 1 to that number, feeding each iteration into CHR$. Non-competing, QBIC was thought of a year after this challenge was posted. # Pushy, 2 bytes Non-competing as the language postdates the challenge. Input is given on the command line,$ pushy asciitable.pshy 90. Note that this will not work for numbers larger than 127.

X"

Explanation:

\ Implicit: Input number on stack
X   \ Get range [0, n)
"  \ Interpret as charcodes and print

# Java 8, 45 bytes

A lambda expression which takes an integer and prints the table to STDOUT. Golfing suggestions welcome!

x->{for(char i=0;i<x;System.out.print(i++));}

It's not clear whether OP wants x to be the last character printed, or the first character not printed. If it's the second, just change i<x to i<=x :)

# Python, 30 bytes

An unnamed lambda function which returns the result as a string.

lambda n:('%c'*n)%(*range(n),)

Try it online!

This uses the printf-style string formatting: when '%c' is formatted with an integer, it converts the integer to a character and displays that. So, we multiply the string by n, and format it with all values in range(n).

# 29 bytes

If we want to be cheeky and return a list of characters instead of a string, we can get 1 byte shorter:

lambda n:[*map(chr,range(n))]

Try it online!

# QC 18 bytes

'00901)01016010003

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

## REXX, 28 bytes

say xrange('0'x,d2c(arg(1)))

# 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.

1 INPUT N
2 FOR I=0 TO N

## 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.