How to variable Swap Mastery and Reverse Digit using python

Veröffentlicht am: 14 März 2024
auf dem Kanal: I need-Tech
15
0

The XOR swapping technique uses the bitwise XOR operator (^) to swap the values of two variables. The XOR operator returns a 1 for each bit where the two compared bits are different, and a 0 where they are the same.

The swapping technique works by first XORing the values of a and b and storing the result in a. Then, it XORs the original value of b with the current value of a (which is the XOR of the original a and b values) and stores the result in b. Finally, it XORs the current value of a with the result of the second XOR operation and stores the result back in a.

This technique works because XOR has the following properties:

a ^ a = 0
a ^ 0 = a
(a ^ b) ^ b = a
Using these properties, the XOR swapping technique effectively swaps the values of a and b without using a third variable.
The extract_digits function takes an integer num as input and returns a list of its digits in reverse order.
Here's how it works:

The first line of the function converts the integer num to a string using the str function.

This is because it's easier to manipulate strings than integers when dealing with individual digits.

The second line uses slicing to reverse the string.

The [::-1] slice means "start at the end of the string and end at position 0, move with the step -1",
effectively reversing the string.
The third line uses a list comprehension to convert each character in the reversed string back to an integer
and store it in a list. The list comprehension [int(digit) for digit in reversed_str_num] creates a new list
where each element is the integer value of the corresponding character in the reversed string.
Finally, the function returns the list of reversed digits


Auf dieser Seite können Sie das Online-Video How to variable Swap Mastery and Reverse Digit using python mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer I need-Tech 14 März 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 15 Mal angesehen und es wurde von 0 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!