### Daily Coding Puzzles - Oct 29th - Nov 2nd

#### Ali Spittel on November 02, 2018

Every day on Twitter, I post coding puzzles. These are quick coding challenges that increase in difficulty across the span of the week -- with Mond...
[Read Full]

## Monday

Count of positives / sum of negatives (8 KYU):

Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers.

link

Solved it awhile ago (forgot about it).

Here is a C# answer.

And just re-solved it using JavaScript

The C# version is likely better solved with the linq aggregate monad. Less ram, no side effects.

Thanks for the suggestion there, @asparallel .

Here is the updated C# code (with the same logic as JavaScript one) using

`.Aggregate`

.Further optimization I'd probably press for, use a tuple to move the entire aggregation onto the stack:

Ah ha. Tuple being a value type, it is better optimized.

Thanks for the tips there 👊

Usage:

Common Lisp FTW!

Python 3 solution update. Posted to the wrong part of the discussion :P

TypeScript

Refactored to the code-golfiest thing ever with help of @joshcheek !

The golfiest version would probably be in Dyalog APL - even with newlines:

Output:

I will one day be able to look at APL without my brain leaking out of my ears.

Today is not that day.

This was my first time trying to actually

dosomething with it. It was fun and it still sucked my brains out. (Both of them.)Oh holy shit! I was looking at it for several minutes and it started making sense! Maybe because I thought through problem with @aspittel , or maybe this APL is more sensible (the last one I saw, someone had spilled a bag of unicode across its source).

I'm using Ruby's comment syntax because IDK how to do it in APL

That's the thing with first tries: You mostly write code which is

relativelyeasy to understand. The APL symbol for comments is`⍝`

, by the way. And I have no idea how to write it without a Unicode table. ;-)Thank you for your explanation. I finally understand wtf I was doing. (I know that I could probably have obfuscated it even more, "not negative" is longer than "positive", but I think I still won.)

F#

This returns a tuple instead of an array, which I believe is an improvement.Haskell

Idk that this is an actual good Go solution - I'm not a Go expert so it can be hard for me to tell sometimes. It's so idiomatically verbose!

Q/kdb+ solution (let

lbe the input list):## Tuesday

Ones and Zeros (7 KYU):

Given an array of one's and zero's convert the equivalent binary value to an integer.

link

Here is a C# version

Note:`Aggregate`

is equivalent to`reduce`

in JavaScript or other languagesand

`seed`

value is the first argument unlike in JavaScript, in which it's the last argument.Q/kdb+ solution (let

lbe the input list):with list multiplication:

with adverb over:

F#

Edit: shortenedCommon Lisp again:

Output:

This is entirely type-agnostic and will not overflow for large input arrays.

Elixir.

My Python solution:

This will cause problems on Python 2 where

`int`

has a maximum value which is easily exceeded.true -- I'm on Python 3 though and the test cases are all small

"Nobody will notice" is a developer's worst enemy. ;-)

Plain JS:

## Wednesday

Write a program to reverse the digits of a positive integer but without converting it to a string.

My Python solution:

And now this is... 😮

... broken.

You did not pass.

`987654321`

is how I expect it to work though without the 0 in the beginning as you should return a`positive integer`

.That was not a part of the question, so I would say that the result should still start with a 0.

the positive integer shouldn't have a leading zero I don't think. It's at least up to interpretation.

F#

(F# will TCO to a`while`

loop on compile)`i % 10`

gets the right-most digit of`i`

`rev * 10`

shifts the numbers left, with right-most zero`i / 10`

shifts the numbers right, dropping right-most digitI happened to remember these little number tricks from a previous challenge. This is basically using integers as digit stacks.Thank you, Ali.

This has been one of the most fun challenges.

Took me awhile but here is the C# version.

The gist is that, remainder is calculated for each digit, stored in a stack (LIFO - last in first out) to reverse the remainder.

Lastly the

`total`

is reconstructed from the stack.Runnable code on .NET FiddleOh that's a really cool approach!

probably not the best solution but it works. (unless you want leading zeros...but I'm assuming that's not the case since it wants a number).

As nothing in the question says that the result must still be an integer, why not make it an array instead? That would also elegantly solve the problem that an input that ends with a "0" would be chopped... or, even better, don't use

anyreturn value. After all, you did not ask for one.Output:

## Thursday

Calculating with Functions:

This time we want to write calculations using functions and get the results. For example:

seven(times(five())); // must return 35

link

thanks, it's fun :)

Ugly but worked :p

JavaScriptRuby cheat.

Python solution:

Though I prefer this one from the CodeWars solutions!

Apparently I've done this kata before...but I don't know if that even helped me this time. It's so hard to wrap my brain around.

Why not "must return

`thirtyFive`

- a function whichisthirty-five"?Related: Church Numerals.

A groovy version that will do it out to billions. For example:

The only unfortunate thing is in order to support methodMissing it has to be done on instance methods so the

`words`

closure hides that fact## Meta

Discussion on how I should format this post!

I think you should add the problems to the post itself. Otherwise, the later problems will get lost in the comment thread. It will be even better to create a separate post per problem and embed them in the weekly post. That way, discussion will be more focused.

yeah -- the only thing that makes me nervous about that is it seeming spammy to post one every day?

Meta-meta: Discussion on how you ignore all the replies!

Haha there are worse problems out there

## Friday

Bathroom Stalls (Code Jam):

A certain bathroom has N + 2 stalls in a single row; the stalls on the left and right ends are permanently occupied by the bathroom guards. The other N stalls are for users.

link

Python solution!

So I was curious since this solution didn't seem correct, and tried to run this through the codejam grader, and it seems like it isn't correct.

I had some fun with this problem, though (it entertained some part of a plane ride), and came up with a nice concise solution:

A few insights here:

At the end you get a nice concise O(log(K)) runtime and O(1) space solution.

Here's an example of how this works by manually solving N=1000 and K=100:

You start with a group of size 1000 for free stalls, so your group size -> number of groups maps looks like this:

You split 1000 into two groups, one of 499, and one of size 500, and this represents the choice of 1 person, since there is 1 group of size 1000. So now our state looks like:

Now you split 500 into 249 and 250:

Now I'll just draw the next steps, without explaining:

Now things get special, because there are 41 groups of 15 free stalls, but only 36 people left to choose a stall. Therefore, the last person will choose the middle stall of a group of 15 free stalls, and thus Ls = 7 and Rs = 7!

Ah -- I must have introduced an issue while refactoring -- it passed the tests during the competition. But then I refactored, should have checked again!

You've got me on a go kick, and now I'm going back and doing previous exercises you've posted! (Sorry for the large volume of comments on your posts today.