I'm bringing back the coding puzzles after a few month hiatus!
Every day, I post coding puzzles. These are quick coding challenges that increase in difficulty across the span of the week -- with Monday being the most beginner friendly and Friday being super tough. I love seeing other people's solutions as well, and so people post their solution to the problem in any programming language.
I wanted to try posting these here. I'm going to post each question from this week as a comment below, and then we will thread answers under those questions.
I'll be adding in the questions each day, so stay tuned and come back for more 😊
Excited to see your solutions!
Top comments (47)
Tuesday (7 KYU): Find the stray number
codewars.com/kata/57f609022f4d534f...
What is this necromancy?
Haha, bitwise xor. Since it's immutable and commutative it'll reduce down to the stray!
I should add that this only works because it’s an odd number of elements in the array. An even number of matching elements cancel each other out to result in the “stray”.
Oh my, it even works in python!
This is one of those moments when I wish gifs worked better on Dev. But yay!
They should work in normal image markdown!
Whaaa?! How did I not know this! ...game changer
Same idea in Haskell:
Well I can't think of a better answer for this particular problem domain.
XOR is a great idea, thanks!
Math for the win! :D
Thanks for this! I don't really know C++, but I figured I'd give it a shot:
@laurieontech did something really cool in JS with a bitwise XOR (her answer is above), so I figured I'd update this C++ answer with a bitwise XOR since I love it!
in c# using linq:
or:
here my cheap solution:
or shorter and more unreadable:
Looks a bit messy, but I was going for something that might not be too inefficient with a large input array
This is the first I'm finding this, excited to play along! Here is yesterday's (edited to make the colors show up):
I don't think this is better than the bitwise solution but it's a different one lol
Wednesday (6 KYU): Implement Syntax Highlighting
codewars.com/kata/roboscript-numbe...
And now with added re.sub with a callable, which I had no idea was a thing! These coding things are pretty nifty for leaning new tricks I must say!
Oooh, I like this. I was thinking about a dictionary but didn't think about a dictionary with the regex as a key!
Booo regex
Quick edit with a dictionary.
I had no idea you could use \1 in the same expression as the tagged group, never seen that before!
Stackoverflow helped me with that one. I knew it was possible, but wasn't sure how. I actually started with the (.)\1* and iterated to the right regex from there.
You should be able to simplify it a little by changing the + to a * so you don't need the second \D I think?
(\D)\1*|(\d+)
Ooh, you're right. I had it that way to start and then ran into problems with a test case that had 663. It was splitting that grouping because it hit the matching case first. But when I removed the capture group generic and made it non-digit that solved that. So can revert back. Thanks :)
The answers to this kata on codewars are blowing my mind.
Monday (8KYU): How many stairs will Suzuki climb in 20 years?
codewars.com/kata/56fc55cd1f5a93d6...
Javascript (ES6):
Not a one liner like you guys but C#:
Okay, I couldn't help myself:
My Python solution:
Practicing TypeScript for my upcoming internship. And I figured out how to add code highlighting!
Friday (CodeJam): Foregone Solution
codingcompetitions.withgoogle.com/...
brute force for me so far.
Thursday (5 KYU): Perimeter of squares in a rectangle
codewars.com/kata/559a28007caad2ac...
TypeScript. Great practice!