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...
For further actions, you may consider blocking this person and/or reporting abuse
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
Python 3 solution update. Posted to the wrong part of the discussion :P
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!
Refactored to the code-golfiest thing ever with help of @joshcheek !
F#
This returns a tuple instead of an array, which I believe is an improvement.
Haskell
TypeScript
Q/kdb+ solution (let l be 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
Q/kdb+ solution (let l be the input list):
with list multiplication:
with adverb over:
F#
Edit: shortened
Here is a C# version
Note:
Aggregate
is equivalent toreduce
in JavaScript or other languagesand
seed
value is the first argument unlike in JavaScript, in which it's the last argument.My Python solution:
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... 😮
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 Fiddle
Oh that's a really cool approach!
F#
while
loop on compile)i % 10
gets the right-most digit ofi
rev * 10
shifts the numbers left, with right-most zeroi / 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.
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).
Thanks for the suggestion there, @asparallel.
Here is the updated C# code (with the same logic as JavaScript one) using
.Aggregate
.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
JavaScript
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 which is thirty-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 factOh 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
I will one day be able to look at APL without my brain leaking out of my ears.
Today is not that day.
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?
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!
Ah ha. Tuple being a value type, it is better optimized.
Thanks for the tips there 👊
Common Lisp FTW!
the positive integer shouldn't have a leading zero I don't think. It's at least up to interpretation.
987654321
is how I expect it to work though without the 0 in the beginning as you should return apositive integer
.Haha there are worse problems out there
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.
true -- I'm on Python 3 though and the test cases are all small