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 (52)
Tuesday - Don't give me five! (7 KYU)
In this kata you get the start number and the end number of a region and should return the count of all numbers except numbers with a 5 in it. The start and the end number are both inclusive!
codewars.com/kata/dont-give-me-five
Quick JS solution
This removes more than 5s
True, just realized 😂
Changing the conditional expression should fix that
Perl solution, test included:
Any application that can be written in JavaScript, will eventually be written in JavaScript:
Cool solution! But this will timeout with large numbers!
Another Haskell solution:
Or a little bit shorter in the same language:
Clojure
Common Lisp
Another JS solution:
Here's my Nim solution :)
There are
filter
andkeepIf
functions defined in thesequtils
standard library but that feels like cheating :P$ is Nim's stringify operator, and the find procedure returns -1 if it doesn't find the substring :)
Elixir:
And surprise! I wrote an F# solution in a similar fashion:
Haskell
Ruby
Rust Solution:
Wednesday - Format words into a sentence (6 KYU)
Complete the method so that it formats the words into a single comma separated value. The last word should be separated by the word 'and' instead of a comma.
codewars.com/kata/format-words-int...
This is a fun way of doing it, though it might be a bit confusing at first glance 😂
Rust Solution:
My Js solution:
My Nim solution :)
Nim supports specifying ranges both in absolute terms and in relative terms. ^2 would be the second to last position of a range, etc :) As you can see at the end of the procedure you can also supply just one end of the range and Nim will infer the other end, so filtered[^1] would be just the final element of the sequence.
Common Lisp
Inhumane
format
version:appropriated from Practical Common Lisp
More humane version (kinda like a string builder but using a stream):
Perl solution, tests included:
Ruby
Monday - Transportation on vacation (8 KYU):
You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers.
codewars.com/kata/transportation-o...
Java (!!) solution
Haskell solution:
Perl solution, tests included:
My solution in Nim, my new favourite language :)
It provides an automatic default return variable called "result" that you can just start stuffing things into and it will automatically return it without you doing anything :)
I try to use the
func
keyword when defining methods, that way the compiler can guarantee that I have pure function with no side effects.func
is just sugar forproc {.noSideEffect.}
.I haven't gotten into the habit of doing that yet. I know I should and I think it's a great idea in general, but years of habits are hard to break and the func keyword is still a fairly recent addition to the language :)
Rust Solution:
Ruby
Thursday - Queue from two stacks (Hacker Rank Medium)
Complete the put, pop, and peek methods in the editor below. They must perform the actions as described above.
hackerrank.com/challenges/ctci-que...
Perl solution. Passes all the tests on HackerRank.
Rust Solution:
Friday - Cruise Control (Code Jam):
Annie is a bus driver with a high-stress job. She tried to unwind by going on a Caribbean cruise, but that also turned out to be stressful, so she has recently taken up horseback riding...
codejam.withgoogle.com/codejam/con...
The Perl solution I submitted back in 2017:
Rust Solution:
I had to resort to following along with a youtube video of the challenge, but I understand it now, so...learning?
But seriously, tons of new things learned this week, like handling stdin and stdout data, reading test data from a file and writing answers to a file. Glad to have these challenges to follow!
Python 3
Don't give me 5 answer.
If a number ends in 5 it is divisible by five but not by 10, in java...
void printNumbersNotEndingIn5 (int beginning, int last){
try {
for (int i=beginning;i <= last; i++){
if ((i%5!=0) || ((i%5==0) && (i%10==0))) System.out.println (i);
}
}
catch (Exception e){
}
}
printNumbersNotEndingIn5 (out, 1, 30);
Meta