Monthly Coding Challenges

Inspired by old code golf challenges and the latest TIS-100 game. Plus in part of the work for the upcoming demoscene courses. I present a monthly coding challenge to members:

September’s challenge:

Create three functions that outputs either a ‘1’ or ‘0’ for that takes a random integer as input within the range of -2 through +2. The function can only use subtraction, addition and invert the sign.

  • Function one outputs ‘1’ or ‘0’ on greater than zero.
  • Function two outputs ‘1’ or ‘0’ on equal to zero.
  • Function three outputs ‘1’ or ‘0’ on

Bonus points for using only arithmetic. One can use any language they choose though assembly, basic, or ansi-c is prefered.

This month’s prize: Gift card to Starbucks

6 Likes

If input is outside range, or non-integer, or non-numeric, how would you like functions to respond?

On what? 1234567890123456798

Since code golf was mentioned is this about brevity or elegance? And, yes please on the missing requirements.

1 Like

Instruction set limitations? What’s the datatype of the input? Output?

Everything is just a number to the computer. :wink:

p.s. This is why strongly-typed languages rock. Try putting a non-numeric value into an unsigned int…

Fixed that for you. If I want to treat my strings as numbers or my doubles as bits, I don’t want the language to get in my way…

1 Like

C never gets in your way. It is more than happy to let you take that unsigned int and explicitly convert it to an array pointer or a float. It gives you the revolver, the bullets, and a bottle of whiskey.

Automatic type-converting languages are never used for safety-critical software. Reason? Run-time errors can occur when the input goes out of range or is of the wrong type for the parameter in question. Perl and Python land in this category, so don’t think I’m trying to slam one or the other.

We should really all be writing Ada.

1 Like

Sounds perfect! It also has the advantage of being more portable then assembler while still keeping all of the power.

Just to save some time, the first Google hit on “your language sucks”:

https://wiki.theory.org/YourLanguageSucks

1 Like

Only 30 lines in the C section, but 76 in the Python section. :slight_smile:

If only they hadn’t decided to go back to the punched-card-era of “columns delineate blocks”, I might have more respect for it.

Now your dissing Holerith coding… The FORTRAN gods will strike you down for your heresy…

1 Like

Hahahahaha, people are now looking at me strangely at my desk.

sorry. less than zero

Its all integers. 0/1 out. random signal value from either -2, -1, 0, 1, 2.

That’s how I feel about c++, c# and obj-c just each one hands a shotgun, malfunctioning plasma gun, or .45 magnum retrospectively.

Either one, I’ll leave that to the author’s choosing. However three areas of scoring will be used.

Lines of code
Function jumps/Heap sizes
Cycle counts

static int[] lookup = new[]{0,0,0,1,1};
int GreaterThanZero(int input)
{
return lookup[input+2];
}

Does that count as only arithmetic?

Ah, the good old truth table.

Which language is that by the way? Looks like either c# or Java?

In all code challeges, input and output can only be an integer with Min/Max for integers is -9999 through 99999. Unless stated otherwise.

For this months challenge we are using a data set that varies between -2 and +2, including 0.