Algorithms and Codewars

in Programming & Dev3 years ago (edited)

What are algorithms?

An algorithm is a specific procedure for solving a well-defined computational problem. In simpler terms, an algorithm is a piece of code that has well-defined steps and solves a problem in an optimal way.

Here are some real-life examples of algorithms: the recipe for baking a cake, the process of doing laundry, solving a Rubik’s cube, and the functionality of a search engine are all examples of an algorithm.



Source: flaticon

They are very important!

Algorithms are very important in computer science. The best-chosen algorithm makes sure the computer will do the given task in the best possible manner. In cases where efficiency matters (usually always) a proper algorithm is really vital to be used. An algorithm is important in optimizing a computer program according to the available resources.

Ultimately when anyone decides to solve a problem through better algorithms then searching for the best combination of program speed and least amount of memory consumption is desired.

Algorithmic complexity

When speaking about algorithms we can measure two different resources that really matter that we need to design the algorithm around: time and space.

Algorithmic time complexity is a measure of how long an algorithm would take to complete given an input of size n.

Algorithmic space complexity is a measure of how much memory an algorithm would allocate to complete given an input of size n.

When we want to annotate the complexity of an algorithm mathematically we use the Big O annotation. Big O specifically describes the worst-case scenario and can be used to describe the execution time or space required for an algorithm to run to completion.

If we have an input of size n, meaning the input of the algorithm is an array with n different items, and the algorithm runs in a linear time complexity, we can describe the complexity as O(n).



Source: devopedia

Types of Algorithms

Algorithms are classified based on the concepts that they use to accomplish a task. While there are many types of algorithms, the most basic types of computer science algorithms are:

Divide and conquer algorithms – divide the problem into smaller subproblems of the same type, then solve those smaller problems, and combine those solutions to solve the original problem.

Brute force algorithms – try all possible solutions until a satisfactory solution is found. Algorithms that use this approach are usually worst performing and should be used only when there is not smarter solution to a given problem.

Greedy algorithms – find an optimal solution at the local level with the intent of finding an optimal solution for the whole problem. These algorithms should be used with caution because they might introduce the local optimum problem.

Recursive algorithms – solve the lowest and simplest version of a problem to then solve increasingly larger versions of the problem until the solution to the original problem is found.

Backtracking algorithms – divide the problem into subproblems, each of which can be attempted to be solved; however, if the desired solution is not reached, move backward in the problem until a path is found that moves it forward. These algorithms are most commonly used to solve chess problems, Rubick's cube, Sudoku, etc...

Dynamic programming algorithms – break a complex problem into a collection of simpler subproblems, then solve each of those subproblems only once, storing their solution for future use instead of re-computing their solutions. Usually, dynamic algorithms are custom-made for each problem that they solve. The most famous dynamic algorithm is the knapsack algorithm.

Best language to practice algorithms?

Almost every computer language can be used to create an optimal algorithm, but in my opinion, some languages are better suited for the task. When practicing algorithms, the point is to have well-defined steps that are clear even at first glance and are easy to fix if there is a bug in the solution.

Naturally, strongly typed languages like C#, Java, C++, and many others are most used when learning new algorithms since they force us to write cleaner code than dynamic languages.

Even if that is the case, many developers chose to hone their algorithmic skills using python or ruby because they offer some nice libraries that often solve the most basic types of problems with 1 line of code.

My rule of thumb is to use the language that I'm most familiar with because that way I will not struggle with language syntax and built-in functionalities like sorting, filtering, etc..., and focus on solving the problem at hand.

Where can I practice algorithms?

Codewars is a community-driven platform for creating and solving algorithmic problems. There is a system in place to guide new Codewarriors (users) by introducing them to simpler problems at the beginning and increasing the difficulty as the user gains more experience. Of course, more experienced developers can start with tougher problems from the get-go.

Because the problems are created by the users, the solutions can be posted in a different list of languages for different problems, but most commonly C#, Java, JavaScript, and Python are always implemented. Some more popular problems can be solved in up to 35 different languages even.

The last thing left to do is visit codewars and start coding! 😄


image.png
Source: codewars

Non-affiliation disclaimer!
I'm not endorsed by, directly affiliated with, or sponsored by Codewars.

Codewars glossary

Source: https://www.codewars.com/about

Kata
...is real code challenges focused on improving skill and technique. Some train programming fundamentals, while others focus on complex problem-solving. Each kata is crafted for and by the community.

Kyu/Dan Ranks
Each kata on the site is set to a Kyu/Dan rank, based on its subject area and difficulty. The community collectively determines rank in the Beta Process. Every new Codewarrior on Codewars starts being ranked as 8 Kyu.

What are Kyu and Dan?
The terms are borrowed from a system in Japanese martial arts, which is in turn borrowed from the game of Go. Kyu indicates the number of degrees away from master level (Dan). This is why they count downward. Once you reach master level, we count upward.

Earning Ranks
You can advance through the ranks by completing kata at or above your rank - the harder the kata the faster you advance.

Honor
...represents the level of respect a user has earned from the community, based on their skill and contributions. Honor is earned fastest through creating kata, crafting great solutions, and constructive comments.

Hope this post helps new developers to hone their skills in solving algorithmic problems. 😄

Happy coding,
DotNetGuru

Sort:  

Hello, @dotnetguru ^^. Nice post =D.
I have a doubt: ¿How effective is Lua solving algorithms, comparing with the languages you mentioned in the post?
I'm studying Lua spontaneously when I use CheatEngine to make cheats for videogames.

Hello @aristak,

I think you can learn algorithms in any language, but scripting (dynamically typed) languages have the disadvantage when it comes to clean code, and whenever you learn something new you should strive to code it as cleanly as possible in order to re-read it multiple times and play with it in small variations and see what that small change impacts.

The reason for the need for clean code is two-fold: when learning algorithms you will inevitably learn and create structures that if not well designed can have performance pitfalls, and the second reason is that when developing algorithms you will most likely need to change something in the heart of it after a while, and reading scripting languages is always more difficult.

That being said you can have a clean code structure using any language.

When it comes to algorithm performance, the answer is also two-fold: the libraries that come out of the box from the framework you are using might have slight performance differences, but usually, the algorithms implemented are the same. For example for sorting an array, usually, every framework has quick-sort implemented behind the scenes. The second thing that you need to keep in mind is that scripting languages are interpreted at runtime, while strongly typed languages are compiled into native or intermediate code that is highly optimized and close to the native code. So if you google for language execution speed lists you will always see C and C++ at the top because they are compiled to native code, then probably something like C# and Java (maybe not the java from 15 years ago :D), and after that languages like JavaScript, PHP and Python

Hope this helps and maybe this should have been a post and not a comment :D

Keep in mind that there is a third component, and that is fun. Use whatever language is most comfortable and fun for you.

Thank you, @dotnetguru 😁. Now I know why I didn't see Lua in the top languages respect of the execution speed. ¿Does that happen because Lua hasn't built-in concept of classes, since Lua is an object-oriented language?

Not really, it's more because it's a scripting language and not a compiling one...

Now I understand better. Thank you ^^

Weldone @dotnetguru nice post. Our daily routine is also an algorithm.

Greetings, in programming the algorithm is used well it is true, but when I studied mechanics and electricity we used it a lot to optimize maintenance and repair operations.

Congratulations @dotnetguru! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You distributed more than 100 upvotes.
Your next target is to reach 200 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Hive Power Up Month - Feedback from February day 14
Valentine's day challenge - Give a badge to your beloved!
Support the HiveBuzz project. Vote for our proposal!