Showing posts with label implementation. Show all posts
Showing posts with label implementation. Show all posts

Friday, March 27, 2020

UVa - 10662 - The Wedding

Link to pdf on UVa OJ

In the middle of reading the problem I thought I may need to use some kind of backtracking or dynamic programming, but when I saw the constraints it was obvious I need to do nested for loops in order to solve it. Three nested for loops will do the job.
Here we only have travel agency, restaurant and hotels, if this number increases or it is not a static number we have to use recursion to check all combinations.
I solved it using recursion as well. Take a look at my code for the details.

1 - My iterative solution on github
2 - My recursion solution on github



UVa - 12592 - Slogan Learning of Princess

Link to pdf on UVa OJ

This is a use-case for map data structure. At first I wanted to define another class that included a map in it :D haha, then I thought now that all the functionality I need is save and retrieve pairs, I have to use the map itself, getting rid of boilerplate code that doesn't add any advantage to my code.

Link to my code on github.

UVa - 12527 - Different Digits

Link to pdf version on UVa OJ

This seems to be an easy problem. What could be important in solving this problem? The code seems straight forward, right? We check every single number and if it doesn't have repeating digits we increase the result by one.
The important point in solving this problem is to think modular, if you can think in terms of functions for such small codes, then your mind is structured enough to think this way for larger codes and programs.

Let's look at both and compare them.
This is the bad version of coding a solution.



Everything is in a loop, how can you explain this code to someone else? to your teammate. Can you understand this easily if you come back and read it 2 months later? Of course NOT. 

We can easily extract two different functions out of this code. In main the only thing we do is reading input and preparing it in such a way that another function can work on it.

int countNumbersWithNoRepeatingDigits(int a, int b)
This function has the responsibility to receive two numbers and calculate all the numbers in this range (inclusive) that doesn't have repeating digits.

So now the only thing we do in main is this:



We read input and delegate the task to another function (separating concerns).

How does this function itself look like? There must be a loop that goes from a to b, and increases the result if that number doesn't have repeating digits. It seems we can define another function to do that for us, and inside function countNumbersWithNoRepeatingDigits just do the loop.




You see? A simple function that you can explain to anyone in less than 1 minute, and if you come back 2 months later you would be able to read it and understand it easily.




Then remains this last function that is responsible to check whether a number have repeating digits or not? The only concern of this function is this.

At the end we may pay a little cost for the function calls overhead (as you see in my UVa submissions), but that's negligible to the quality and maintainability of the code.




The bottom (first) submission is using functions as you see the running time is 0.010,
the top (second) submission is not using any functions and does everything in main function.


Keep breaking down your code into smaller functions! :)

1 - Link to the modular version on github
2 - Link to the bad and unreadable code on github



USACO - Prime Palindromes

I just skimmed the problem statement and panicked of the high boundary of the input, but something inside told me don't worry everyth...