Learn coding by simple questions

Hi folks,

You must be very patient to read my previous blog about momentum strategies, so I really appreciate that. In this blog, I just want to share with you some interesting problems in coding that I have collected from books and my friends. The problems are not neccesarily difficult, they are just ones with something interesting while I’m trying to solve them.

My favorite approach for solving every problem (and everything in life) is simplifying the problem first by divide-and-conquer or solving it in special cases. However, in this blog, I do a reverse way: first I raise a simple problem, then I add more constraints/assumptions to make the problem harder. Now, here we go!

See? It’s very simple, even you are beginning to learn coding, I’m quite sure that you could give the solution for this problem.

I want to show you some ways to calculate the mode of a list. By the way, the first thing is a test function for my solution:

Mode is the element that appears most in a series. There maybe some built-in functions to calculate mode that you may know as below:

It’s surprising that the function fails test 2, the reason is this built-in function only give out one mode. By the way the function run quite fast.

Yay it passes test 2, the code runs slower than the previous.

Again, but it’s worse: fail the test 2 and run extremely slow.

Maybe it is the best solution for this problem. And this is my code:

It gives the right answer, but it runs slower:

A little bit sadness! By the way I hope that you have seen different ways to find mode of a series. You can see that not all built-in functions give the answers that you expected, so check it carefully before using!

Thanks again, see you in next blog!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
K for What?

Quant Researcher, Data Scientist, Food Hater (so I eat them a lot).