Daily Coding Problem - My Attempt to Become a Better Problem Solver

in technology •  3 months ago


Recently I started to look for an excellent way to learn more about programming and to be a better problem solver. My first task of problem solving hit me in my first advanced computer class when I was in the 10th grade here in the States. I loved it, the rush of dopamine when you solve a problem (even if it is one that needs no solving) is the greatest feeling in the world. That may come as a surprise to any teenagers out there but trust me, it's my drug of choice now.

Before we continue:

... it’s true that dopamine is associated with things like sex, drugs, and rock and roll, dopamine is NOT your brain’s reward chemical. Or rather, dopamine is not JUST your brain’s reward chemical, nor is it your brain’s ONLY reward chemical. - NeuWriteSD.org

That being said, I'm still going to use "dopamine" as the descriptor here but just keep in mind that what I am referring too is more than just that one chemical. It is the total result and something I have no better way to describe.

Dopamine is the reason I code and work to solve problems in my everyday life. The reward is so good that I keep coming back to the misery of the unknown. The hours or days before solving a problem and getting that hit of dopamine is a slog and often makes me think I'm a complete idiot. This anguish is the exact reason why the brain chemicals hit so good upon conquering a challenge.

As a side note, dopamine is also the reason that I don't pull out my mobile device at every instance I am bored or need to kill time. Today, apps are made to give us small hits of dopamine over and over to keep us there and consuming media. If I am always on a slow drip of these beautiful brain drugs then when I need to solve a problem or tackle some hard coding problem I'll be more likely to quit.

So that brings us to why I love to program and learn new things (especially new coding things). Because I always want to learn more and become better at making effective programs I always look for ways to practice. Practice needs to both reinforce what we already know as well as push us to our boundaries. If we are not forced to the edge of our current understanding, we can never become better. It's also on this edge that I find the most pleasure because for me I tend to get more of those brain drugs upon figuring something out that I have never tried before.

Since I am always looking to learn new ideas, concepts, and be more effective, I was looking around for a way to practice programming. I wanted it not to be myself thinking up problems because then I would be more likely to never step outside my comfort zone. Not because I would purposely pick easy practice problems but because I would not even know the harder ones existed.


More Dope!

Then I found this site called DailyCodingProblem.com from another programmer who has lots of videos YouTube. The channel name is CS Dojos, and he has a lot of great videos even a series for beginners. I like his stuff, probably because he did not go to school to be a programmer and shows that you can learn how to code without a college degree.

After signing up for the Daily Coding Problem I got my first email with a challenge. It was not hard but also not easy. What I love about this mailing list is that if you pay the 9 USD/month, they will send you a detailed explanation of that problem the following day. In the report, they cover different ways to solve the question and what is the best route from the standpoint of time and complexity.

I am not even a week into the mailing list and already have learned more than I would have if I just went through tutorials, books, and self-coding. It's pushing me to my limits and causing me to learn, and when I finally have a solution, the brain drugs come rushing in and give me the motivation for the following day. This motivation is needed because on the tough days of this challenge I get frustrated and want to quit. Push through these dips in confidence and knowledge is what leads us to better understanding.

Here is an example of what the daily email looks like:

cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns
the first and last element of that pair. For example,
car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4.

Given this implementation of cons:

def cons(a, b):
    def pair(f):
        return f(a, b)
    return pair

Implement car and cdr.

This is my solution to the problem above. I by no means have the best answer or the most correct answer. This is just how I was able to solve it during my learning process.

def cons(a, b):
    def pair(f):
        return f(a, b)
    return pair

def first_element(a, b):
    return a

def last_element(a, b):
    return b

car = cons(3, 4)
cdr = cons(3, 4)


Before I get the next coding challenge and see their solution to this one, I will probably try to simplify this more. It is also a good idea to check the time it takes for the answer to run and then scale it by adding more data to see if the complexity is not exponential. Of course, checking the time and complexity is not needed to complete the challenge, but it is a good idea. When checking for time and complexity, you get a sense of how fast your code will execute and how robust it can be with large datasets.

Thanks For Reading!

If you have any topics that you would like me to cover please feel free to comment them below and I'll add them all to my list!

All images came from royalty and attribution free sources unless specified.

Click here to vote with SteemConnect

Or go to https://steemit.com/~witnesses
and type jrswab in the box at the bottom.
Want to know more about my witness? Feel free to message jrswab#3134 on Discord, jrswab on steem.chat, or head over to jrswab.cloud for maximum transparency.

SteepShot | Mastodon | Keybase | Twitter | Gab

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Ahh car and cdr... Reminds me of the text we were using called Structure and Interpretation of Computer Programs and we were learning with the Scheme language.

I'll point out that your solution doesn't quite fit though. car should be a function that takes in something made by cons to output the first arg.

Right now your car is set to the "pair" object itself, whereas it should instead be a function that acts on pairs.

In other words, right now if I typed car(cons(8, 9)) I would get something weird.

Very nice initiative you have going though. Gotta keep sharp :)


car should be a function that takes in something made by cons to output the first arg. Right now your car is set to the "pair" object itself, whereas it should instead be a function that acts on pairs.

Are you saying this because the question uses it as a function in it's asking?

In other words, right now if I typed car(cons(8, 9)) I would get something weird.

Why would you execute that though when the code sets it as an object? car = cons(8, 9) executes as expected. Unless you are referring to the question stating car as a function in which case I get what you are saying.

That being said, I am doing this to learn not get a perfect answer by semantics. I never claim to know "the best" way to achieve the desired result. I am just looking to learn new concepts and ideas.


Unless you are referring to the question stating car as a function in which case I get what you are saying.

Right, perhaps the problem isn't really clear in its goals. It's trying to get us to create an API around dealing with "pairs". Here they chose to represent a pair as a function itself, which itself is weird. They want you define accessors assuming this representation.

car here should be generic. It should be able to take anything generated by cons and generate the correct result.

But there is an important lesson in here. It's about abstraction, the separation between the interface (cons, car, cdr) from the implementation (functions that act on other functions).

On a side note: it's funny to say "car should be a function" because it is a function. But it's not the correct function.


This post was not about my crappy coding but about the love of learning mate


Doh. I know it wasn't the main part of your post, but this jumped out at me. Especially since it's a problem I'm very familiar with. These kinds of toy problems. I'm quite happy that someone else is doing them hahaha!

You did say you wanted to clean up this solution a bit too though, so I wanted to make sure you knew that it's not quite what they were looking for.


Right on man. I'm working on a refactor that runs the functions as the question alludes to. I need to read things more carefully lol.

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!