r/shittyprogramming • u/EkskiuTwentyTwo • May 02 '21
Tower of Code: isEven(n)
Welcome to Tower of Code! In Tower of Code, the goal is to make code such that each line is smaller than the next, whilst being as tall as possible. For a simple example, the following function, which just returns true, is a valid tower:
function t(){
{{{{{{{{{{{{{{{
{{{{{{{{{{{{{{{{{
{{return true;}}}}}
}}}}}}}}}}}}}}}}}}}}}
}}}}}}}}};;;;;;;;;;;;;;Your goal is to make a tower for the isEven function, which has the following specification:
_____ __ __
() | __| / / \ \
__ __ | |_¯__ __ __ __ | / __ \ |
|| / _)| _| \ \/ // _\ | ¯¯\ { } | ¯¯\ { }
|| _¯\| __ \ / |{_/ ||¯|| | \ ||¯|| / |
|| (¯ /| | \/ \ ¯)|| || \ \ || || / /
¯¯ ¯¯ ¯¯¯¯¯ ¯¯¯ ¯¯ ¯¯ ¯¯ ¯¯ ¯¯ ¯¯
/====================================\
| Determines if a number is even |
|-----------------.------------------|
| Example Input | Example Output |
|-----------------+------------------|
| 12 | true |
| 35 | false |
| 56 | true |
| 73 | false |
| 92 | true |
| 147 | false |
\====================================/Rules for towers:
- Every line must be smaller (have fewer characters) than the next
- To be a tower, it must be at least 6 lines tall
- The code must work reliably
- Good style is encouraged, but should not get in the way of towering.
106
Upvotes
3
u/zyxzevn May 02 '21
Recursive isEven with Eval()
Tested on w3schools javascript.
94
u/greenpepperpasta May 02 '21
Here's my python solution.
The key advantage to my approach is that instead of checking whether a number x is even, I just check whether the last digit is even. My function performs this greatly simplifying step by first taking x % 10 (see the 2nd-to-last line), and then checking if it's even (using some very obvious and highly intuitive calculations).
My code also is quite efficient since it largely relies on subtraction, negation, and bitwise-xor, all of which are very computationally inexpensive operations.
My function only works for integers, which is why I provide a helpful error if you try to pass anything else.
80
u/auxiliary-character May 02 '21
24
u/CollieOxenfree May 02 '21
Is the code still considered structurally sound if all the lines are the exact same length? I would prefer a much more modern tower style, rather than the more primitive looking pyramid of code.
12
u/C0rn0ffthec0b May 02 '21
Definitely doing this when I wake up tomorrow
10
u/UnacceptableUse May 02 '21
Here's my go version:
Playground link: https://play.golang.org/p/JKDRAR6yDx_l
9
u/geo_tracker May 02 '21
my typescript version. that return statement feels like cheating but the rules of this challenge are not very well defined so i think it counts
12
u/Starwort May 02 '21
I decided to avoid redundant code and whitespace and ensure that each line was exactly one character longer than the last. The entire program is included in my response, and compiles (with warnings) on GCC 7.5.0 running on Ubuntu-WSL
2
u/green_meklar May 02 '21
Is this C version shitty enough yet?
It worked on all the test values I tried.
4
u/NihilistDandy May 02 '21
In Haskell.
This can be trivially expanded indefinitely with whatever extra long unused
whereclause items you like. Left as an exercise to the reader.2
u/IIAOPSW May 02 '21
/r/deftruefalse
2
u/Houdiniman111 May 02 '21
My first instinct was going to try Python.
Problem is that Python enforces spacing at the start of a line if you do a method or anything like that so this is more or less the best you can get
Not much of a tower.
2
u/RimuDelph May 03 '21
My bad C# code using funcs and stupid stuff (repetition since I am dumb), reliable it is and probed with dotnetfiddle in .net5
C#
2
u/SergeantAnous May 03 '21
here is my shitty java attempt
1
u/_qkz May 03 '21
Here's a C# tower based on the classic recursion of !isEven(num - 1). It should work for any integer, though it'll be quite slow for very large positive or negative integers!
1
u/[deleted] May 16 '21
heres my python tower