-1

This is actually a problem on leetcode, I found this part of code in the solution. But I couldn't understand how does this part of code work. The problem was just basically reverse a given integer, for example: x = 123, then the result should return 321. This is the code:

class Solution:
    def reverse(self, x: int) -> int:

    s = str(x)

    def check(m):
        if ans > 4294967295/2 or ans < -4294967295/2:
            return 0
        else: 
            return ans
    
    
    if x < 0:
        ans = int('-' + s[1:][::-1])
        return check(ans)
        
    else:
        ans = int(s[::-1])
        return check(ans)

I'm a beginner in programming so I have never seen anything like that in a string in python.

Le Nguyen
  • 1
  • 2
  • 1
    To make it easier to understand, split it up into `res1 = s[1:]` and then `res2 = res1[::-1]`, because those are really 2 _slicing_ operations. It's just skipping creating a temporary variable. – Gino Mempin Jan 06 '23 at 04:57

2 Answers2

2

These are Python string slices.

There are three parts to a string slice - the starting position, the end position and the step. These take the form string_obj[start_position:end_position:step].

When omitted the start position will default to the start of the string, then end position will default to the end of the string, and the step will default to 1.

The expression s[1:][::-1] is doing two slice operations.

s[1:] uses index 1 as the start position and leaves the end position blank (which defaults to the end of the string). It returns a string that's the same as s only without the first character of the string.

This new string is then fed into the second slicing operation. [::-1] has no start or end position defined so the whole string will be used, and the -1 in the step place means that the returned string will be stepped through backwards one step at a time, reversing the string.

0

I will explain with an example

x = 123 
s = str(x) #index will be 0, 1, 2
print(s[1:])

This will print 23 as the output. ie, The index starts from 0, when you put s[1:] the index starts from 1 and goes till the end gets printed out.

s[::-1] will reverse the string. ":" means starting from the beginning and going till the ending ":" with a step of -1. This means that it will start from the ending and goes to the start as the step is -1.

In this particular problem, we need to reverse the integer. When the number becomes less than zero. -123 when reversed using string ie, s[::-1] gets converted to 321-. In order to reverse the negative integer as -321, s[1:] is used to remove "-" and converting the 123 to 321

ans = int('-' + s[1:][::-1])

When the integer value is less than 0.

The above line of code converts the -123 to -321. "-" gets neglected when reversing the string and gets added up when '-' + s[1:][::-1] this string operation works. Taking int() the string will convert "-321" to -321, ie, string to integer form.

When the integer value is above 0 We only need to reverse the integer using s[::-1]

def check(m):
        if ans > 4294967295/2 or ans < -4294967295/2:
            return 0
        else: 
            return ans

This is a constraint given in the problem that the values of the reversed integer should be between -2^31 and 2^31 otherwise it should be returned 0.