R, 1040 bytes
-2 bytes by using as.name
.
The credit for this answer should go to digEmAll, who did 499(!) bytes better than my version (left below). Most characters are printed by using the octal representation, e.g. cat('\041')
prints character number 41 in octal, or 33 in decimal, i.e. !
. There are a couple of minor additional tricks, explained in more detail in my original answer below. For the characters ct
, use as.name
instead of cat
; for the characters ()
redefine the function ?
which has a special syntax and doesn't need brackets.
The solution for a
is
`c\141t`('\141')
since c\141t
in backticks is interpreted as cat
.
The other notable exception is the character \
, for which we have to use the more standard cat(intToUtf8(92))
.
cat('','')
cat('\041')
cat('\042')
cat('\043')
cat('\044')
cat('\045')
cat('\046')
cat("\047")
`?`=cat;?'\050'
`?`=cat;?'\051'
cat('\052')
cat('\053')
cat('\054')
cat('\055')
cat('\056')
cat('\057')
cat(+F)
cat(+T)
cat(1+1)
cat(2+1)
cat(3+1)
cat(4+1)
cat(5+1)
cat(6+1)
cat(7+1)
cat(8+1)
cat('\072')
cat('\073')
cat('\074')
cat('\075')
cat('\076')
cat('\077')
cat('\100')
cat('\101')
cat('\102')
cat('\103')
cat('\104')
cat('\105')
cat('\106')
cat('\107')
cat('\110')
cat('\111')
cat('\112')
cat('\113')
cat('\114')
cat('\115')
cat('\116')
cat('\117')
cat('\120')
cat('\121')
cat('\122')
cat('\123')
cat('\124')
cat('\125')
cat('\126')
cat('\127')
cat('\130')
cat('\131')
cat('\132')
cat('\133')
cat(intToUtf8(92))
cat('\135')
cat('\136')
cat('\137')
cat('\140')
`c\141t`('\141')
cat('\142')
as.name('\143')
cat('\144')
cat('\145')
cat('\146')
cat('\147')
cat('\150')
cat('\151')
cat('\152')
cat('\153')
cat('\154')
cat('\155')
cat('\156')
cat('\157')
cat('\160')
cat('\161')
cat('\162')
cat('\163')
as.name('\164')
cat('\165')
cat('\166')
cat('\167')
cat('\170')
cat('\171')
cat('\172')
cat('\173')
cat('\174')
cat('\175')
cat('\176')
Try it online!
Original version:
R, 1541 bytes
Most non-alphanumeric characters are of the form cat(intToUtf8(33))
. The intToUtf8
part can be made shorter for most alphanumeric characters. The main issue is the characters cat()
, which I will handle last.
For digits, simple operations like cat(1+1)
work.
For letters, the shortest seems to be to use the constants letters
and LETTERS
(the alphabet) whenever possible, e.g. cat(LETTERS[1])
for A
. This doesn't work for the characters LETRSletrs
, for which we move to the slightly longer cat(toupper("l"))
or cat(tolower("S"))
. For elr
we have to revert to cat(intToUtf8(101))
and the like.
The code for the space can be made shorter with cat('','')
, because cat
will by default add spaces between strings (here two empty strings).
The brackets ()
are annoying: not using them seems to disallow calling functions. Here I used a standard R golfing technique, redefining some unary functions which have special syntax: cat(intToUtf8(40))
becomes
`?`=cat;`!`=intToUtf8;?!40
Finally, the letters cat
. We need the cat()
function, otherwise R will add some fluff; for example letters[1]
or print(letters[1])
outputs [1] "a"
instead of a
. We'll need two distinct workarounds.
The function write
is usually used to write to a file, but can be made to write to STDOUT: write(letters[1],'')
works for a
and c
. Unfortunately, that still doesn't work for t
. I almost gave up here...
Let's take a step back. The reason R adds [1]
by default when printing is that most objects are of a vector type (here, a vector of length 1). The main exceptions are functions; in particular, calling for the body()
or args()
of a function doesn't add anything, but I couldn't find a way to use this. There are however internal non-vector types, such as promises, expressions and symbols (the latter are also called names), so a solution is to use as.name
: as.name("t")
converts the string to an object name, and outputs it at just t
. I also used this for c
, since as.name(letters[3])
is shorter than write(letters[3],'')
(but we still need write
for a
).
We now need to find a way of creating the string "t"
, but cannot use letters
, tolower
or intToUtf8
. The best I could come up with is as.name(rawToChar(as.raw(116)))
.
Full list:
cat('','')
cat(intToUtf8(33))
cat(intToUtf8(34))
cat(intToUtf8(35))
cat(intToUtf8(36))
cat(intToUtf8(37))
cat(intToUtf8(38))
cat(intToUtf8(39))
`?`=cat;`!`=intToUtf8;?!40
`?`=cat;`!`=intToUtf8;?!41
cat(intToUtf8(42))
cat(intToUtf8(43))
cat(intToUtf8(44))
cat(intToUtf8(45))
cat(intToUtf8(46))
cat(intToUtf8(47))
cat(1-1)
cat(3-2)
cat(1+1)
cat(2+1)
cat(3+1)
cat(4+1)
cat(5+1)
cat(6+1)
cat(7+1)
cat(8+1)
cat(intToUtf8(58))
cat(intToUtf8(59))
cat(intToUtf8(60))
cat(intToUtf8(61))
cat(intToUtf8(62))
cat(intToUtf8(63))
cat(intToUtf8(64))
cat(LETTERS[1])
cat(LETTERS[2])
cat(LETTERS[3])
cat(LETTERS[4])
cat(toupper("e"))
cat(LETTERS[6])
cat(LETTERS[7])
cat(LETTERS[8])
cat(LETTERS[9])
cat(LETTERS[10])
cat(LETTERS[11])
cat(toupper("l"))
cat(LETTERS[13])
cat(LETTERS[14])
cat(LETTERS[15])
cat(LETTERS[16])
cat(LETTERS[17])
cat(toupper("r"))
cat(toupper("s"))
cat(toupper("t"))
cat(LETTERS[21])
cat(LETTERS[22])
cat(LETTERS[23])
cat(LETTERS[24])
cat(LETTERS[25])
cat(LETTERS[26])
cat(intToUtf8(91))
cat(intToUtf8(92))
cat(intToUtf8(93))
cat(intToUtf8(94))
cat(intToUtf8(95))
cat(intToUtf8(96))
write(letters[1],'')
cat(letters[2])
as.name(letters[3])
cat(letters[4])
cat(intToUtf8(101))
cat(letters[6])
cat(letters[7])
cat(letters[8])
cat(letters[9])
cat(letters[10])
cat(letters[11])
cat(intToUtf8(108))
cat(letters[13])
cat(letters[14])
cat(letters[15])
cat(letters[16])
cat(letters[17])
cat(intToUtf8(114))
cat(tolower("S"))
as.name(rawToChar(as.raw(116)))
cat(letters[21])
cat(letters[22])
cat(letters[23])
cat(letters[24])
cat(letters[25])
cat(letters[26])
cat(intToUtf8(123))
cat(intToUtf8(124))
cat(intToUtf8(125))
cat(intToUtf8(126))
Try it online!