[Solved] A Quantity After a Double Reversal in Python


The issue

Reversing an integer means to reverse all its digits.

For instance, reversing 2021 offers 1202. Reversing 12300 offers 321 because the main zeros aren’t retained.
Given an integer num, reverse num to get reversed1, then reverse reversed1 to get reversed2. Return true if reversed2 equals num. In any other case return false.

Instance 1:

Enter: num = 526
Output: true
Clarification: Reverse num to get 625, then reverse 625 to get 526, which equals num.

Instance 2:

Enter: num = 1800
Output: false
Clarification: Reverse num to get 81, then reverse 81 to get 18, which doesn't equal num.

Instance 3:

Enter: num = 0
Output: true
Clarification: Reverse num to get 0, then reverse 0 to get 0, which equals num.

Constraints:

`0 <= num <= 106“

The answer

Choice 1:

class Answer:
    def isSameAfterReversals(self, num: int) -> bool:
        return num == 0 or num % 10 != 0

Choice 2:

class Answer:
    def isSameAfterReversals(self, num: int) -> bool:
        def reverse(quantity):
            end result = 0
            whereas quantity:
                end result = end result * 10 + quantity % 10
                quantity //= 10
            return end result

        return reverse(reverse(num)) == num 

Choice 3:

class Answer:
    def isSameAfterReversals(self, num: int) -> bool:
        s = str(num)
        s1 = int(s[::-1])
        s2 = str(s1)
        print(s2[::-1])
        return int(s2[::-1])==int(s)

Check circumstances

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles