Skip to main content
Add emanresu A's -12 cut
Source Link

Python 3, 6464 52 bytes, 3 exit codes (0, 1, 139)

-12 from emanresu A
from ctypes import* 1/(V:=int/~-int(input())-1) ifor V==138:string_at(0) 

Try it out here! (The TIO version uses a semicolon instead of newline, but it functions the same)

This works similarly to this Python 2 answer, but a few things are added:

  • We first import everything from ctypes, since we need string_at later.
  • Since we are in Python 3, we need to cast the input to an int.
  • We also save the result toThen we decrement it, using V~- usingwhich negates it then bitwise NOTs the walrus operatorvalue.
  • If V is 138Then, we try to get the string at address 0. This causes a segmentation fault, which returns exit code 139. (V is off by 1 due to the -1 in thedo floor division by 0 attempt, so we check 138 and not 139)on it with 1:
    • If it is 0, 1//-1 will resolve to -1, so we exit successfully, since -1 is truthy
    • If it is 1, we error out trying to do floor division with 1.
    • If it is 3 or higher, 1//-x will return 0, which is falsy, therefore we run the string_at function from ctypes. Targeted at an address of 0, this causes it to segfault.
      • Since we segfault, this means that for an input of 139, we return the correct exit code.

Non-minified version:

import ctypes 1 / (code_minus_one := int(input()) - 1) if code_minus_one == 138: ctypes.string_at(0) 

Python 3, 64 bytes, 3 exit codes

from ctypes import* 1/(V:=int(input())-1) if V==138:string_at(0) 

This works similarly to this Python 2 answer, but a few things are added:

  • We first import everything from ctypes, since we need string_at later.
  • Since we are in Python 3, we need to cast the input to an int.
  • We also save the result to V using the walrus operator.
  • If V is 138, we try to get the string at address 0. This causes a segmentation fault, which returns exit code 139. (V is off by 1 due to the -1 in the division by 0 attempt, so we check 138 and not 139)

Non-minified version:

import ctypes 1 / (code_minus_one := int(input()) - 1) if code_minus_one == 138: ctypes.string_at(0) 

Python 3, 64 52 bytes, 3 exit codes (0, 1, 139)

-12 from emanresu A
from ctypes import* 1//~-int(input())or string_at(0) 

Try it out here! (The TIO version uses a semicolon instead of newline, but it functions the same)

This works similarly to this Python 2 answer, but a few things are added:

  • We first import everything from ctypes, since we need string_at later.
  • Since we are in Python 3, we need to cast the input to an int.
  • Then we decrement it, using ~- which negates it then bitwise NOTs the value.
  • Then, we do floor division on it with 1:
    • If it is 0, 1//-1 will resolve to -1, so we exit successfully, since -1 is truthy
    • If it is 1, we error out trying to do floor division with 1.
    • If it is 3 or higher, 1//-x will return 0, which is falsy, therefore we run the string_at function from ctypes. Targeted at an address of 0, this causes it to segfault.
      • Since we segfault, this means that for an input of 139, we return the correct exit code.
Source Link

Python 3, 64 bytes, 3 exit codes

from ctypes import* 1/(V:=int(input())-1) if V==138:string_at(0) 

This works similarly to this Python 2 answer, but a few things are added:

  • We first import everything from ctypes, since we need string_at later.
  • Since we are in Python 3, we need to cast the input to an int.
  • We also save the result to V using the walrus operator.
  • If V is 138, we try to get the string at address 0. This causes a segmentation fault, which returns exit code 139. (V is off by 1 due to the -1 in the division by 0 attempt, so we check 138 and not 139)

Non-minified version:

import ctypes 1 / (code_minus_one := int(input()) - 1) if code_minus_one == 138: ctypes.string_at(0)