Python的三大数据转换实例
一、反转三位数
class Solution:
def funtcion(self,number):
h=int(number/100)
t=int(number%100/10)
z=int(number%10)
return 100*z+10*t+h
if __name__=='__main__':
solution = Solution()
num=123
new_num = solution.funtcion(num)
print("输入:{}".format(num))
print("输出:{}".format(new_num))
二、合并两个数列
class Solution:
def function(self,A,B):
i,j=0,0
lists=[]
while i < len(A) and j < len(B):
if A[i]<B[j]:
lists.append(A[i])
i+=1
else:
lists.append(B[j])
j+=1
while i < len(A):
lists.append(A[i])
i+=1
while j < len(B):
lists.append(B[j])
j+=1
return lists
def built(self,A,B):
A.extend(B)
A.sort()
return A
if __name__ == '__main__':
A=[1,2,3,4]
B=[2,4,6,8]
solution = Solution()
print("输入:{}{}".format(A,B))
print("输出:{}".format(solution.function(A,B)))
print("使用内置函数:{}".format(solution.built(A,B)))
三、旋转字符串
class Solution:
def function(self,S,offset):
new_s=S
if len(new_s) > 0:
offset = offset % len(new_s)
temp = (new_s+new_s)[len(new_s)-offset:2*len(new_s)-offset]
for i in range(len(temp)):
s[i]= temp[i]
return new_s
if __name__ == '__main__':
s=["a","b","c","d","e","f","g"]
offset = 3
solution = Solution()
print("输入:s={},off={}".format(s, offset))
print("输入:s={}".format(solution.function(s,offset)))
注意事项
- 可以从Python的网页编辑器开始学起来:https://lightly.teamcode.com/python?bd_vid=7545637281465357038
- 注意各样之间的缩进,比如class和if一般要对齐
- 反复检查