Every day on Twitter, 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. Please feel free to post your solutions to the ones you are interested in below! Then, you can comment with insights into people's solutions below that! I will also add a meta thread if you have advice on how to format this in the future!
Excited to see your solutions!
Top comments (92)
Wednesday
Array.diff (6 KYU):
Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
CodeWars
Ruby
Ah, cannot
unsee
this answer... πSimilar effort when done in APL! :)
That's amazing!
On the other hand, Ruby probably allocated about 10,000 object references under the hood and used 1gb of memory to make that happen. π
Honestly, I just came out here to have a good time and I'm feeling so attacked right now π π π
github.com/thepracticaldev/dev.to/... π
Haha nice. Honestly, I'm just so used to copy/pasting emojis from slack and discord. Fun fact, when you do that, it copies the colon emoji syntax, not the unicode glyph π π
@bendhalpern some numbers (using /usr/bin/time -l on OSX):
Disclaimer: I'm inexperienced with Go; using the example from below π
Oh, I know almost nothing about Go. I'm specifically using these exercises to learn the syntax. No clue if and how what I'm doing can be optimized. Go is famously, weirdly restrictive and intentionally verbose.
Same in Kotlin :)
kkkkk it's very simple dude :D
Tried in Javascript
Nice!!
this is beautiful. thanks
Wow, great! So clean!
Python!
List comprehensions are truly a gift.
YES -- they are so elegant.
Or with sets!
Rust
Go
Common Lisp
I'll use lists because... well, it's Lisp, right?
Can't beat a good standard library ;)
F#
also sequence expression syntax
Or (although it will also remove duplicates from a):
Yes, this was my first thought, but I was trying to retain dupes.
Just define the subtraction operator and done.
It wasn't as easy as doing a set difference as the
a
needed to keep the duplicate values.Below is the answer in C#.
Where
is the same asfilter
in JavaScript.APL
Replied to the post instead of the comment! My bad!
PHP
Haskell
Here's one in Dart:
PowerShell
Expanding the ? alias:
Another Haskell solution:
Hey all, Why I don't see any java code here?
Thursday
Scramblies (5 KYU):
Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false
CodeWars
F#
Edit: I had previously posted a version that had ever-so-slightly more performance but was more code. I'm also including that version below since it solves the problem differently.
Here are the tests (console).
Nice! yeah -- I only sometimes think about the efficiencies of these. They're contrived and we're doing them for fun. Part of me cares, part of me doesn't haha
I agree. I would have posted a much more expressive version, but you beat me to it! I did find an alternative way of solving the problem that was a little more expressive and nearly the same perf. I updated my post to include it.
Go
Rust
Solved it awhile ago in C#.
β Warning: Really ugly...
Python!
Python style
def scramble(s1, s2):
return all([s1.count(l) >= s2.count(l) for l in s2])
Not a fancy solution, but it seems to work π
Common Lisp
Tuesday
Product of Array Items (7 KYU):
Calculate the product of all elements in an array.
CodeWars
F#
I probably wouldn't define a separate function for this in actual code.
Does F# require you to name the
arr
parameter explicitly or can you simply do the following:I've been curious about F# for some time but haven't really done a deep dive.
Yes, you can do that in F#. (You probably know this, but for the sake of onlookers) it is called point-free notation. It can be handy for small functions like this. But I noticed when I use it too much, my code can become hard to understand.
Especially for code examples, I rarely use it because it can confuse readers.
Common Lisp
as
*
in Common Lisp can take as many arguments as you like...Javascript
Rust
A simple aggregation of data (using
reduce
) worked like a charm.C# answer.
I decided to not use reduce so that I could return early if I hit a zero.
Python!
Go
JavaScript
Haskell:
Friday
The Last Word (CodeJam):
You are the next contestant on this show, and the host has just showed you the string S. What's the winning last word that you should produce?
CodeJam
F#
Testing it (console)
Awesome -- this one (for me) was a lot easier than they made it sound!
Same here. The hard part was understanding the problem. (It felt very much like "A train leaving SF at 50kph ..." word problems.) But after that the code wasn't so bad.
I think the possible "gotcha" here is that they do not want a reverse alphabetically sorted string, which, if you're not careful about the requirements, could be what you try to build and have it trip you up.
Rust
usage:
last_word(.exe) < small.in > small.txt
Common Lisp
Quick little test...
Python!
Go
TypeScript
JS Solution: Takes two arrays, returns a single array with all of the items from array A, which do not exist in array B.
Just realized @thesoreon posted almost the same solution already. Oh well, I guess great minds think alike π
That's nice! When i made this solution i was curious and searched for another approches and found the exact same solution on stack overflow π
Meta!
Ooh. I tried to do Advent of Code in Go last year, but AoC was probably too steep a challenge for a language I didn't know at all. This is a much more manageable entrypoint. Thanks for this! π€
For sure! I may try and incorporate those next month! We'll see! Go is so much fun, I should do more with it!
Possibly dumb question - what does KYU stand for?
That's a great question -- I'm not totally sure, but it's the ranking system CodeWars uses, so I include it -- 8 KYU are the most beginner friendly, 1 KYU takes a really long time.
KYU is used for grading the difficulty levels, or degree of proficiency or experience.
Wiki
Monday
Number Drills: Blue and red marbles (8 KYU):
You've decided to write a function, guess_blue() to help automatically calculate whether you should guess "blue" or "red". The function should take four arguments.
CodeWars
Go
Common Lisp
the fun of a language with rationals:
Rust
Python!
For thos who are looking for solution in C#
using System;
using System.Collections.Generic;
public class Kata
{
public static int[] ArrayDiff(int[] a, int[] b) {
}
}