0
\$\begingroup\$

It's simple, simply write the shortest code to raise a TypeError.

In Python you can do:

raise TypeError

However it should be shorter with a code that gives a TypeError without raising it.

\$\endgroup\$
7
  • 2
    \$\begingroup\$ this isn't really defined for other languages. You've also immediately answered the shortest possible way for Python... \$\endgroup\$
    – Jo King
    Commented Jul 31, 2019 at 2:29
  • \$\begingroup\$ @JoKing I answer one out of 3 or more, feel free to post the other 3 byte ones \$\endgroup\$ Commented Jul 31, 2019 at 2:30
  • 1
    \$\begingroup\$ This shouldn't be closed, but it's only well-defined for one language (Python) \$\endgroup\$
    – MilkyWay90
    Commented Jul 31, 2019 at 2:31
  • 8
    \$\begingroup\$ I'm retracting my close vote, since this is pretty clear. But I'm downvoting this because it is an extremely simple challenge with an obvious optimal answer with no possibility of improvement. \$\endgroup\$
    – Jo King
    Commented Jul 31, 2019 at 2:37
  • 3
    \$\begingroup\$ Oh right, you might want to post your challenges to the Sandbox before you post this here. \$\endgroup\$
    – user85052
    Commented Jul 31, 2019 at 3:27

7 Answers 7

16
\$\begingroup\$
for chr1 in range(32, 128):
  for chr2 in range(32, 128):
    for chr3 in range(32, 128):
      code = chr(chr1) + chr(chr2) + chr(chr3)
      try:
        output = exec(code, {})
      except TypeError:
        print(code)
      except:
        pass

Try it online!

A naive search may result that you cannot trigger a TypeError within 2 characters. You may get a TypeError with 3 characters. All solutions including:

1. Math operator, non-numeric types

  • Math operator including +, -, ~
  • Non-numeric types including "", (), [], id, {}
+[]
-""
~id

2. Invoke number as function

0()

3. Matrix multiplication between numbers

@ is __matmul__ in python, read more here

0@0

4. Binary operator with float / complex number

~0.
~.0
~0j

5. Iterate over number

Thanks to @Dennis to point out this.

You may iterate some variable by star operator. And () for a tuple may be omitted.

*0,
\$\endgroup\$
0
3
\$\begingroup\$

JavaScript (V8), 4 bytes

1.()

Try it online!

[Output]

.code.tio:1: TypeError: 1 is not a function 1.() ^ TypeError: 1 is not a function at .code.tio:1:3

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

Python 3, 3 bytes

+''

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Uh oh. I can not make my program less than 3 bytes. Anyway, nice trick! \$\endgroup\$
    – user85052
    Commented Jul 31, 2019 at 2:31
  • \$\begingroup\$ @A__ Yeah, anyway you can post the other 3 byte ones \$\endgroup\$ Commented Jul 31, 2019 at 2:31
2
\$\begingroup\$

Python, 3 bytes

This is self-explanatory. The unary - operator does not take a string argument. This was covered in @tsh's solution above.

-""

Try it online!

Another one:

~id

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Haha yeah nice idea i still have more than 2 different styles in mind tho \$\endgroup\$ Commented Jul 31, 2019 at 2:35
  • \$\begingroup\$ yeah, double quotes and that's about it \$\endgroup\$
    – Jo King
    Commented Jul 31, 2019 at 2:36
  • \$\begingroup\$ There is also -id and +id \$\endgroup\$
    – Jo King
    Commented Jul 31, 2019 at 2:38
2
\$\begingroup\$

Japt, 1 byte

Throws TypeError: U.í is not a function

í

Test it (Errors are displayed below the output field)


Or, a bit less trivial:

Japt, 2 bytes

Throws TypeError: (U++) is not a function

°(

Test it

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

Python 3, 3 bytes

+[]

It gives an TypeError: bad operand type for unary +: 'list

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

Python 3, 3 bytes

0()                                                              

raises a TypeError

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I just made a program that finds all the possible combinations for one and two bytes, execs that, and no two bytes raised a TypeError. Therefore, raising an TypeError with less than three bytes is impossible. \$\endgroup\$
    – forever
    Commented Oct 27, 2020 at 17:22

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.