程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Let you thoroughly understand the inversion value of Python Programming classic case [examination question]

編輯:Python

Study Python There are many different ways , You can watch the video 、 The blog 、 Look at official account, etc . But just talking, not practicing , It's hard to improve quickly . It's best to deal with practical problems with your hands , Only in this way can we apply the learned knowledge more skillfully .

This article explores with you Python A classic case of programming , Let you immerse yourself in learning Python. Help you get high marks in the final exam , Get the favorite of Dachang offer. You can take the problem and think about how many different solutions there are , Then compare with the problem-solving methods in this paper . There are different ways to solve problems. Welcome to official account to discuss with me .




One 、 Classic case 【 Examination questions 】

1. Basic topics : Invert a three digit positive integer


Input : Any three digit positive integer

Output : Corresponding inverted three digit positive integer

example :

Input : 876

Output : 678

2. Advanced topic : Invert any character


Input : Any character

Output : Reverse the character

example :

Input :‘ You are the joy of youth ’

Output :‘ The boy I like is you ’


Two 、 Classic case solving methods

Method 1 : First take out a bit 、 ten 、 The number in the hundreds , Then reverse the position


Define an inversion function , Take the original number as the input value . Take out the bits of the original number in turn 、 ten 、 Hundred bit , 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)

Get the results :

678

among number/100: Express the number Divide 100.

Method 2 : First turn the number into a character list , utilize range Function reverse splicing


Define an inversion function , Take the original number as the input value . First turn numbers into character lists , recycling range Functions 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)

Get the results :

678

str(x): hold x It becomes a string .

list(str(x)): Turn the 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 the characters arranged in reverse order .

This method can not only reverse three digit integers , And it can be extended to any bit integer , Further, you can reverse any string . For example, reverse a four digit number

rev_all(4131)

Get the results :

678

For example, reverse one 7 A string

rev_all(' You are the joy of youth ')

Get the results :

' The boy I like is you '

thus ,Python Classic programming cases in 【 Examination questions 】 The reversal of a value has been explained . If you want to know more Python The function in , You can go to “ The code of Ali Yiyang ” Look through the official account “ Study Python” Module related articles .


You may be interested in :
use Python Drawing Picasso
use Python Draw a cloud of words
use Python draw 520 Eternal heart
Python Face recognition — You are the only one in my eyes
Python Draw a nice picture of the starry sky ( Aesthetic background )
【Python】 Valentine's Day confession fireworks ( With sound and text )
use Python Medium py2neo Library operation neo4j, Building the association map
Python Romantic confession source collection ( love 、 The roses 、 Photo wall 、 Advertising under the stars )

Long press ( scan ) Identify the QR code above and learn more Python And modeling knowledge , Make your study and work more brilliant .

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved