I think a lot of people know this at this point, but I love writing code. To wake myself up in the morning and get my brain running, I normally solve a code puzzle first thing when I get to work.
I normally find these on Project Euler, CodeWars, or HackerRank -- though I'm totally open to suggestions if anybody has a really good alternate resource! I really love the problem-solving nature of these code challenges, and how I can sometimes can come up with really cool out of the box solutions to them. I do feel like they help me become a stronger programmer, and an added bonus is that if I am interviewing at the time a lot of interview questions are similar to these (for better or for worse).
Since I'm already doing these every day, I thought it would be really cool to share my results on social media to keep me accountable and to see how other people solve the same problem. I normally solve these challenges in Python or JavaScript, but other people have been posting in Ruby, Rust, Ramda, and Clojure so far (which is so cool to see!). Some of them I have solved before at some point, so I post multiple answers in different languages or my refactors!
If you would like to follow along and post your own solutions (or just see other people's), follow me on Twitter or I've been using the hashtag #CodingPuzzle on them, so you could follow there instead! I normally post in between 8 and 9 AM EST since that is normally when I get to work! It's also led to some interesting discussions on efficiency and the strengths of different programming languages for solving these types of problems! These problems are also a variety of difficulties, so if one day's problem is too hard, still follow the next day for a new one!
To get started, today's problem was Project Euler Problem 2: Even Fibonacci Numbers: Each new term in the Fibonacci sequence is generated by adding the previous two terms. Find the sum of the even-valued terms. Please post your answer here!
If you want to catch up on the previous ones, they were:
Hope to see your answers!
Top comments (12)
Okay, just pipe the number of even terms you want into STDIN (an input of 4 will display the sum of the first 4 even terms of fibonacci) and you're good to go.
Befunge 93:
Kind of disappointed as this solution uses get and put and therefore is less... "fungical"? but trying to do this all on a single stack got away from me.
A Funge++ solution might look more elegant...
Here we go, MUCH happier with this Funge++ solution:
Just to show, Day 3 in C, Time: O(n), Space: O(1), the result is processed in place. Of course, this one only works with ASCII…
Quick subquiz for anyone actually reading this: under what circumstances will this program crash or exhibit undefined behaviour?
Awesome, thanks for sharing. 😁 If you have any interest in learning about bioinformatics (genetics, DNA, inheritance, and the data analysis surrounding it) rosalind.info is a really good site with quite a few puzzles!
These are awesome! Definitely adding them to the rotation
Day 1
Time: O(n), Space: O(n)
Day 2
Time: O(n), Space: O(n)
Day 3
Time: O(n), Space: O(n)
Day 4
Time: O(n), Space: O(1)
Nice exercise, this also shows how clumsy JS is when dealing with string data. Everything has to be converted to and from arrays. And these exercises do not work with non-BMP Unicode characters, but that was not in the requirement ;)
Awesome !
Unfortunately I don't have twitter.. but here are my answers:
Day 1
Day 2
Pretty sure it can be waaaay better
Day 3
Day 4
Day 4 (Project Euler #2) has been already proposed by @peter : dev.to/peter/project-euler-2---eve...
I gave my answer there 🙂
Others here have proposed pretty good solutions for the other three, but my answer to the fourth problem is distinctively different IMO.
Thanks for this, Ali. Another source of good programming puzzles is the Daily Programmer subreddit . The tasks are categorized as easy, intermediate and hard and there is a good discussion on how the solutions can be improved.
It's only JS and really hacky, but I've come to like alf.nu.
Oh awesome! Definitely adding those to the rotation -- I like the RegEx series because I don't like writing them so I should get more practice in!
You can try this one:
codingame.com/servlet/urlinvite?u=...
Add me as a friend!!