Created
March 31, 2021 06:50
-
-
Save victor-iyi/82081a254a6d1797b34c62417113ab16 to your computer and use it in GitHub Desktop.
A function that takes 4 or 7 as input and returns 7 or 4 [f(4) = 7 OR f(7) = 4] WITHOUT USING if-statement
This file contains 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
"""Implement a function which takes an integer which does the following: | |
- if input is 4: it returns 7 | |
- if input is 7: it returns 4 | |
Otherwise (unspecified). | |
However: | |
CANNOT USE `if-statement`. | |
To Run: | |
- Make sure you `pip install pytest`. | |
Then: | |
```$ pytest -v swap_without_if.py``` | |
""" | |
import pytest | |
from typing import Dict | |
def f_ternary(x: int) -> int: | |
"""Uses an `if-expression` not if statement. | |
Args: | |
x (int) - Given either 4 or 7. | |
Return: | |
int -- Flips the input. | |
f(4) = 7 | |
f(7) = 4 | |
""" | |
assert x == 4 or x == 7, "input must be 4 or 7" | |
return 7 if x == 4 else 4 | |
def f_old_ternary(x: int) -> int: | |
"""Uses ternary operator. | |
Args: | |
x (int) - Given either 4 or 7. | |
Return: | |
int -- Flips the input. | |
f(4) = 7 | |
f(7) = 4 | |
""" | |
assert x == 4 or x == 7, "input must be 4 or 7" | |
return x == 4 and 7 or 4 | |
def f_dict(x: int) -> int: | |
"""Uses dictionary indexing. | |
Args: | |
x (int) - Given either 4 or 7. | |
Return: | |
int -- Flips the input. | |
f(4) = 7 | |
f(7) = 4 | |
""" | |
assert x == 4 or x == 7, "input must be 4 or 7" | |
return {4: 7, 7: 4}[x] | |
# Uses one-liner dictionary indexing. | |
f_dict2: Dict[int, int] = {4: 7, 7: 4}.__getitem__ | |
def f_list(x: int) -> int: | |
"""Uses a list indexing. | |
Args: | |
x (int) - Given either 4 or 7. | |
Return: | |
int -- Flips the input. | |
f(4) = 7 | |
f(7) = 4 | |
""" | |
assert x == 4 or x == 7, "input must be 4 or 7" | |
return [0, 0, 0, 0, 7, 0, 0, 4][x] | |
def f_addition(x: int) -> int: | |
"""Uses integer addtion/subtraction. | |
Args: | |
x (int) - Given either 4 or 7. | |
Return: | |
int -- Flips the input. | |
f(4) = 7 | |
f(7) = 4 | |
""" | |
assert x == 4 or x == 7, "input must be 4 or 7" | |
return 11 - x | |
def f_bitflip(x: int) -> int: | |
"""Using a clever bit flip operation. | |
Note: | |
```python | |
>>> bin(4) | |
'0b100' | |
>>> bin(7) | |
'0b111' | |
>>> bin(4 ^ 0b11) # == 7 | |
'0b111' | |
>>> bin(7 ^ 0b11) # == 4 | |
'0b100' | |
``` | |
Args: | |
x (int) - Given either 4 or 7. | |
Return: | |
int -- Flips the input. | |
f(4) = 7 | |
f(7) = 4 | |
""" | |
assert x == 4 or x == 7, "input must be 4 or 7" | |
return x ^ 0b11 | |
# `pytest` mark decorator for creating parameters. | |
params = pytest.mark.parametrize( | |
'f', | |
( | |
f_ternary, | |
f_old_ternary, | |
f_dict, | |
f_dict2, | |
f_list, | |
f_addition, | |
f_bitflip, | |
), | |
) | |
@params | |
def test_4_returns_7(f): | |
assert f(4) == 7 | |
with pytest.raises((AssertionError, KeyError)): | |
f(2) | |
@params | |
def test_7_returns_4(f): | |
assert f(7) == 4 | |
with pytest.raises((AssertionError, KeyError)): | |
f(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment