Python 第一次作业

 
           
B=b'China\nhello\tworld!\n'
print(B)
s=B.decode()
print(s)
print(s.encode())
print(s)
b'China\nhello\tworld!\n'
China
hello	world!

b'China\nhello\tworld!\n'
China
hello	world!

A=' hello world  ';
print("A =",A)
print(A.strip())
print(A.lstrip())
print(A.rstrip())
A =  hello world  
hello world
hello world  
 hello world
A='joob centrol south university'
print(A.lower())
print(A.upper())
print(A.capitalize())
joob centrol south university
JOOB CENTROL SOUTH UNIVERSITY
Joob centrol south university
print(A.startswith('joo'))
print(A.endswith('sity'))
B=A.split(' ')
print(B)
C=','.join(B)
print(C)
print(list(reversed(C)))
True
True
['joob', 'centrol', 'south', 'university']
joob,centrol,south,university
['y', 't', 'i', 's', 'r', 'e', 'v', 'i', 'n', 'u', ',', 'h', 't', 'u', 'o', 's', ',', 'l', 'o', 'r', 't', 'n', 'e', 'c', ',', 'b', 'o', 'o', 'j']

单词反转(必须保留空格)

 
          
def reverse(new_str,start,ends):
    while start<ends:
        new_str[start],new_str[ends]=new_str[ends],new_str[start]
        start=start+1
        ends=ends-1
#str=' I love china!   '
str = ' Hello, how are you?   Fine.   '
new_str=list(str)
new_str=list(reversed(str))
print(new_str)
i=0
while i < len(new_str):
    if new_str[i] != ' ':
        start=i
        ends=start+1
        while ends<len(new_str) and new_str[ends] !=' ':
            ends=ends+1
        reverse(new_str,start,ends-1)
        #print("new_str",new_str)
        i=ends
    else:
        i=i+1
    
print(new_str)
print("----------------")
#new_str.reverse()
print(''.join(new_str))
[' ', ' ', ' ', '.', 'e', 'n', 'i', 'F', ' ', ' ', ' ', '?', 'u', 'o', 'y', ' ', 'e', 'r', 'a', ' ', 'w', 'o', 'h', ' ', ',', 'o', 'l', 'l', 'e', 'H', ' ']
[' ', ' ', ' ', 'F', 'i', 'n', 'e', '.', ' ', ' ', ' ', 'y', 'o', 'u', '?', ' ', 'a', 'r', 'e', ' ', 'h', 'o', 'w', ' ', 'H', 'e', 'l', 'l', 'o', ',', ' ']
----------------
   Fine.   you? are how Hello, 

打印素数

import time 
def prim(n):
    num=[]
    i=2
    for i in range(2,n+1):
        for j in range(2,i):
           # print(j)
            if i%j == 0:
                break
        else:
            num.append(i)
            #print(i)
    return num
'''
L=prim(100)
print(L)
'''
t0=time.time()
L=prim(100)
t1=time.time()
print("hh",t1-t0)
print(L)
hh 0.0
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
import math
import time 
def prim(n):
    num=[]
    i=2
    for i in range(2,n+1):
        for j in range(2,int(math.sqrt(i))+1):
           # print(j)
            if i%j == 0:
                break
        else:
            num.append(i)
            #print(i)
    return num
t0=time.time()
L=prim(100)
t1=time.time()
print("hh",t1-t0)
print(L)
hh 0.0
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

支持可变参数

def test_arg(name,*key1,**key2):
    print(name)
    print(key1)
    print(key2)
    
test_arg('zg',12,4,3,'S',china='BJ',UK='LD')
test_arg('xd',*('xz',3,'cdxs'),**{'china':'BJ','UK':'LD'})
zg
(12, 4, 3, 'S')
{'china': 'BJ', 'UK': 'LD'}
xd
('xz', 3, 'cdxs')
{'china': 'BJ', 'UK': 'LD'}

汉诺塔问题

def hanoi(n,A,B,C):
    if(n<1):
        raise ErrorValue
    elif n==1:
        print(A,'-->',B)
    else:
        hanoi(n-1,A,C,B)
        print(A,'-->',B)
        hanoi(n-1,C,B,A)
hanoi(3,'A','B','C')
        
A --> B
A --> C
B --> C
A --> B
C --> A
C --> B
A --> B
print(len([1,3,4,5]))
4

冒泡法排序

import random
def MaopaoSort(nums,des='asc'):
    l=len(nums)
    flag=1
    i=0
    j=l-1
    while i<l and flag==1:
        flag=0
        j=l-1
        while j>i:
            if des =='asc':
                #print(j)
                if nums[j]<nums[j-1]:
                    nums[j],nums[j-1]=nums[j-1],nums[j]
                    #print(nums[j])
                    flag=1
            else:
                if nums[j]>nums[j-1]:
                    nums[j],nums[j-1]=nums[j-1],nums[j]
                    flag=1
            j=j-1
        i=i+1
        #print(nums)
    print("time :",i)
    return nums
            
L=[random.randint(0,100) for _ in range(10)]
LL=[random.randint(0,100) for _ in range(10)]
print(L)
L1=MaopaoSort(L)
print(L1)
print(LL)
L2=MaopaoSort(LL,'desc')
print(L2)
    
[76, 41, 21, 50, 96, 8, 87, 83, 44, 53]
time : 7
[8, 21, 41, 44, 50, 53, 76, 83, 87, 96]
[83, 0, 88, 90, 41, 0, 39, 15, 70, 63]
time : 8
[90, 88, 83, 70, 63, 41, 39, 15, 0, 0]

猜你喜欢

转载自blog.csdn.net/csu_guo_liang/article/details/80441953