Friday, September 8, 2023
HomeSoftware Engineering A Quantity After a Double Reversal in Python

[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 are usually not 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
Rationalization: Reverse num to get 625, then reverse 625 to get 526, which equals num.

Instance 2:

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

Instance 3:

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

Constraints:

`0 <= num <= 106“

The answer

Possibility 1:

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

Possibility 2:

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

        return reverse(reverse(num)) == num 

Possibility 3:

class Resolution:
    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 instances



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments