We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Introduction
- Print Function
- Discussions
Print Function
Print Function
Sort by
recency
|
3436 Discussions
|
Please Login in order to post a comment
if name == 'main': n = int(input())
for i in range(1, n + 1): print(i, end="")
Did this as recursion (I'm starting to have a love-hate relationship with it)
Perhaps I overthought the solution. Basically instead of use the print(i,end="") solution, I created a math funtion to recalculate in every iteration the entire number. Basically in every iteration it is necesary to multiply the previous result by 10 and then add the current iteration number. The problem is that this only works from 1 to 9. When it comes to i >= 10, the previous result needs to be multiplied by 10^log_10(i). This ensures to move all the numbers previously "concatenated" in the result the necessary spaces to the left, or in other words, multiply the previous result the necessary so when we add the current iteraton number, the previous numbers don't get modified in the addition. It is important to highlight that we only need the integer part of log_10(i), since this integers tell us the number of power that we need to elevate 10 in order to move to the left the result without modifying the nmbers. Here is my code:
The code defines a recursive lambda function to generate a sequence like 1 → 12 → 123 → … up to n. It uses two nested anonymous functions: one builds the number recursively, and the other counts the digits of each number to correctly shift digits using powers of 10. Though more complex than necessary, it showcases functional recursion and number manipulation creatively. https://quickguideofficial.com/