Level up

Use of comments, C++ good style coding, basic types, outputs, inputs, calling and creating functions, libraries, conditionals, nesting conditionals, loops, recursion, strings etc. Almost every mastery topics will be covered in WSQ08: yosoy196.

Now things got complicated, it’s time to demonstrate what you’ve learned and more. Ok guys I’ll try to explain everything but I recommend you to check Victoria’s post, she helped me and it’s thanks to her that I wads able to do this WSQ, her post is full of information, she made it in Spanish and her post is full of images that may help you to understand it better.

First of all you’ll have to download ken’s codebase so you may want to check the what do do task before, also pease check the videos in that page, they’ll help a lot.

  • You may want to also check the comments in the code to get a better idea of what to do.
  • Ken gave us 2 functions, we have to make them return what we want to: the first one called is_palindrome; is a bool function, so it has to return true if ‘n’ or whatever your variable is called is a palindrome (if you don’t know what a palindrome is this might help). the second function is called apply 196; and it has to return the sum of your number plus its inverse (Ex: if the number is 10 y has to return 10 + 01 = 11).*In this 2 functions we are working with strings, you may have noticed that there is a weird string thing in both functions, well that allows us to get the inverse of a number and since it’s a sting, we have to use the functions that are also included in the program, the ones that convert a string to BigInteger or vice versa.
    Since we are already talking about BigIntegers you may want to know that we have to use this because it’ll allow us to work with huge numbers, and we can be sure that they won’t run out of space.
  • Now we go to our main, first we have to declare our variables, but I’ll mention them as we start using them.
  • First we have to ask the user for the upper and lower bond of numbers, here we need two variables, I called them uppernum and lowernum. Then we have some text explaining what we’ll do but that’s only esthetics.
  • Here’s were we will do all our operations and decide wether our number is a natural palindrome, an non lychrel or a lychees number. I’ll try to explain everything here step by step and in the best way possible:
  1. We have to do all of our operations with every single number, thats why everything will be inside a for loop, that starts from our BigInteger i that is = to our lowernum, and increments every single loop until it reaches the value of our uppernum.
  2. Inside this for the first thing to check is if it’s a natural palindrome, so with an if condition we check is our function is_palindorme is true: we will add one to our Natpal variable which will tell us how many natural palindromes where in the range of numbers.
  3. If it’s not, then we have to do a simple thing that may be complicate as well, let me explain; if it’s not a natural palindrome we have to make a do while loop, this loop will work with our second function apply196, it will add the number plus its inverse until it becomes a palindrome or it reaches 30 loops (this 30 loops are only because it was an instruction from ken), every loop will be registered in a variable called contador and when we do this it has to check 2 things, if contador was less than 30 loops, then it means it became a palindrome, so it will be added to our Nonly variable. If not (else) then it means that the number is a lychrel number: it will be added to our variable LyNum and also it will print the lychrel number.I know this is easy to visualize or to understand, but I said it could turn very difficult, and thats because we have to have several things in mind: al the beginning or our first else we have to make a copy of our number, this is because we will be applying the function to our copia, the number will change and if it becomes a lychrel, we have to pint the number that originally became lychrel, so we will apply the function to our copia and our n will be there in case we need it (remember that since we have to work with copia: a string as it says in the function, we have to convert it from BigInteger to string). And the second but also important thing to do is that right next to where we did our copia of n, we have to initialize our contador to 0, otherwise it will continue to save the loops that it did with the past numbers and it will accumulate them until it reaches 30 and it will give us a wrong answer.
  4. Finally we print everything and I hope it worked with you as well.

And that’s pretty much it, if you have doubts feel free to ask and as always i’ll include my code on GitHub, it’s the whole carpet, the code that you may want to check is the one called yosoy196.cpp, also  I’ll let my code on pictures and examples of whats you should expect to print:

Captura de pantalla 2017-03-21 a la(s) 20.46.32.pngCaptura de pantalla 2017-03-21 a la(s) 20.46.47.pngCaptura de pantalla 2017-03-21 a la(s) 20.47.03.png

Captura de pantalla 2017-03-21 a la(s) 20.55.38.png

Captura de pantalla 2017-03-21 a la(s) 20.55.57.png

Captura de pantalla 2017-03-21 a la(s) 20.56.25.png

L.out

Deja un comentario