Sort by

recency

|

3436 Discussions

|

  • + 0 comments
        results = [i for i in range(1,n+1)]
        print(*results,sep='')
    
  • + 0 comments

    if name == 'main': n = int(input())

    for i in range(1, n + 1): print(i, end="")

  • + 0 comments

    Did this as recursion (I'm starting to have a love-hate relationship with it)

    def reverse_string (n):
            if n<=0: return ""
            else:
                return reverse_string(n-1)+str(n)
        print(reverse_string(n))
    
  • + 0 comments

    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:

    import math
    if __name__ == '__main__':
        n = int(input())
        output = 0
        for i in range(1, n+1):
            output = (output * (10**int(math.log10(i) + 1))) + i
        print(output)
    
  • + 0 comments

    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/