+ 1

why the answer of this code is 321123? this is the explain attatched with the answer. but i could not understand it

Explanation First, the last digit is printed (by applying the modulus method % with 10), ie. 3, then the result of applying generate to 12, and finally again the last digit. So, the first and the last digits printed are 3: 3....3. After we print the first 3, we print the result of generate(12). This is 2 (last digit), the result of generate(1), 2. The result of generate(1) is 11. Putting all together, we get 321123. So, we see that the method always generates palindromes with an even number of digits. The first half is the second half reversed. https://code.sololearn.com/c9z2Krk6AIBZ/?ref=app

1st Jan 2019, 12:18 AM
Ahmad Ali
Ahmad Ali - avatar
5 odpowiedzi
+ 10
generate(123) : ● 3 printed (printed from 1st print statement in generate funcn) ● if condition (12!=0) : generate(12) //123/10 = 12 ● 2 printed (printed from 1st print statement in generate funcn) ● if condition (1!=0) : generate(1) //12/10 = 1 ● 1 printed (printed from 1st print statement in generate funcn) ● if condition in generate(1) not works : ● 1%10=1 printed [part of last printed statement of generate(1) funcn] (printed from from 2nd print statement in generate funcn) ● move to not-completed generate(12) funcn now (if condition already done, so move to last print statement in it , ie print (12%10) that is 2 (printed from 2nd print statement in generate funcn) ● move to not-completed generate(123) now(if condition already done, so move to last print statement in it , ie print(123%10) that is 3 (printed from 2nd print statement in generate funcn) ■ this method will generate palindrome with any number of digits (not necessarily even) //hope it makes things clear.
1st Jan 2019, 9:58 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
Sham XR Oh I'm really sorry! I must've misunderstood what you were saying (I thought that you were just answering your own question, which would be a violation, but looking back now this thread should be okay). I'll try to talk to the moderators to remove the mfd from this question, but otherwise this seems to be a fair question. (However I would recommend trying to fix the code you attached as currently it only raises an exception, and having the full and functioning code would make it easier to explain).
1st Jan 2019, 8:37 AM
Faisal
Faisal - avatar
+ 2
Please try to only use this section if you have a question related to programming or just need help with something (coding-related). I would recommend checking out the post below to help give you a better understanding of how to use this section. https://www.sololearn.com/discuss/1316935/?ref=app
1st Jan 2019, 12:38 AM
Faisal
Faisal - avatar
+ 2
i found this code with its solution in one book and i could not understand it
1st Jan 2019, 8:28 AM
Ahmad Ali
Ahmad Ali - avatar
+ 1
Faisal sorry what is the problem
1st Jan 2019, 8:27 AM
Ahmad Ali
Ahmad Ali - avatar