Let you fully understand the classic case of Python programming [examination questions] inverting a value

There are many different ways to learn Python, you can watch videos, read blogs, read public accounts, etc. But it's hard to improve quickly if you just talk and don't practice. It is best to be able to deal with practical problems by hand, so that you can apply the knowledge you have learned more proficiently.
  

This article will explore the classic cases of Python programming with you, so that you can learn Python immersively. Help you get a high score in the final exam and get your favorite offer from a big factory. You can first take the question and think about how many different problem-solving solutions there are, and then compare it with the problem-solving methods in this article. There are different ways to solve problems. Welcome to discuss with me in the official account.

  
insert image description here
  
  

1. Classic case [examination question]

  

1. Basic topic: reverse a three-digit positive integer

  
Input: Any three-digit positive
  
integer Output: The corresponding reversed three-digit positive integer
  
Example:
  
Input: 876
  
Output: 678
  

2. Advanced topic: reverse any character

  
Input: any character
  
Output: Reverse the character
  
Example:
  
Input: 'You are the joy of youth'
  
Output: 'The boy I like is you'

  
  

2. Classic case problem-solving method

  

Method 1: First take out the numbers in the ones, tens, and hundreds, and then reverse the positions

  
Define an inverse function that takes the original number as input. Take out the ones, tens, and hundreds of the original number in turn, and then multiply by different multiples to reverse the position of the number.
  
The specific code is as follows:

def rev_int1(number):
    h1 = int(number/100)
    h2 = int(number%100/10)
    h3 = int(number%10)
    return h3*100+h2*10+h1

rev_int1(876)

got the answer:

678

Among them, number/100: means divide the number by 100.

  

Method 2: first turn the number into a list of characters, and use the range function to splice in reverse order

  
Define an inverse function that takes the original number as input. First convert the numbers into a list of characters, and then use the range function to splicing in reverse order.
  
The specific code is as follows:

def rev_all(x):
    str_x  = list(str(x))
    rev_str_x = ''
    for i in range(len(str_x)-1, -1, -1):
        rev_str_x += str_x[i]
    return rev_str_x
rev_all(876)

got the answer:

678

str(x): Turn x into a string.
  
list(str(x)): Turn a string into a list.
  
range(len(str_x)-1, -1, -1): Arrange the length coordinates of the list in reverse order.
  
str_x += str_x[i]: Merge characters in reverse order.
  
This method can not only reverse three-digit integers, but also can be extended to integers of any digit, and can further reverse any string. For example, inverting a four-digit number

rev_all(4131)

got the answer:

678

For example, reversing a 7-bit string

rev_all('你是年少的欢喜')

got the answer:

'喜欢的少年是你'

So far, the classic case of programming in Python [examination questions] inverting a value has been explained. If you want to know more about functions in Python, you can go to the "Ali Yiyang's Code" official account to read related articles about the "Learning Python" module.

  
You may be interested in:
Draw Pikachu with Python
Draw a word cloud map
with Python Draw 520 eternal heart beats with Python Python face recognition - you are the only one
in my eyes With sound and text) Use the py2neo library in Python to operate neo4j and build a relationship map Python romantic confession source code collection (love, rose, photo wall, confession under the stars)



Long press (scan) to recognize the QR code above to learn more Python and modeling knowledge, making your study and work more brilliant.

Guess you like

Origin blog.csdn.net/qq_32532663/article/details/125456798