Last active
January 3, 2021 12:33
-
-
Save dem1995/87c5f4ec64dc73f1177c9908c1af016d to your computer and use it in GitHub Desktop.
A very silly program for printing the additive inverse of a provided integer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| A very silly program for printing the additive inverse of a provided integer. | |
| D. Estes McKnight, January 2021 | |
| """ | |
| import math, cmath | |
| from typing import Iterable | |
| """ | |
| Returns true if the number is negative; false, otherwise. | |
| """ | |
| def is_negative(a: int) -> bool: | |
| rotated = a * 0.1j | |
| angle = cmath.phase(rotated) | |
| im_part = math.sin(angle) | |
| return str(im_part)[0] == '-' | |
| """ | |
| Returns the additive inverse of the provided integer. | |
| """ | |
| def additive_inverse(a: int) -> int: | |
| rotated = a * -1j | |
| angle = cmath.phase(rotated) | |
| im_part = math.sin(angle) | |
| return int(abs(rotated) * im_part) | |
| """ | |
| Returns true if the provided int is present in the list of integers; false, otherwise. | |
| """ | |
| def int_is_in_list(i: int, okay_int_list: Iterable[int]) -> bool: | |
| return any(i/okay_i == 1 for okay_i in okay_int_list) | |
| """ | |
| Returns true if the provided string is an integer (composed of 0 through 9 and -); false, otherwise. | |
| """ | |
| def str_is_int(s: str) -> bool: | |
| number_chars_hex = ['0x2d', '0x30', '0x31', '0x32', '0x33', '0x34', '0x35', '0x36', '0x37', '0x38', '0x39'] | |
| number_chars_int = [int(number_char_hex, 16) for number_char_hex in number_chars_hex] | |
| return all(int_is_in_list(ord(c), number_chars_int) for c in s) | |
| if __name__ == '__main__': | |
| print("Please input an integer. We'll output its additive inverse!") | |
| input_int_str = "".join(input().split()) | |
| if str_is_int(input_int_str): | |
| input_int = int(input_int_str) | |
| print(additive_inverse(input_int)) | |
| else: | |
| print("Error. The input you entered was not a recognizable integer. Please use only - and 0 through 9.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment