[Python] study notes 8-exercises after class

Convert string to lowercase letters

Implement the function ToLowerCase() , which receives a string parameter str, converts uppercase letters in the string into lowercase letters, and then returns a new string.

def ToLowerCase(str: str):
    #Put you anwser here
    str=str.lower()
    return str

print(ToLowerCase('ABcdefG'))

Reverse words in a string

def reverse(str:str):
    l=str.split(' ')#将string转为列表
    for index,a in enumerate(l):
        result=''
        for index1,value in enumerate(a):
            result+=a[len(a)-1-index1]
        a=result
        l[index]=a
    str=' '.join(l)
    return str

print(reverse("Let's take deep learning contest"))

result:
Insert picture description here

Permutations

There are four numbers: 1, 2, 3, 4. How many different three-digit numbers can be formed without repeated numbers? What is each?

def Permutation():
    list = []
    list1=[1,2,3,4]
    #Code goes here
    for a in list1:
        for b in list1:
            for c in list1:
                num=a*100+b*10+c
                if num not in list:
                    list.append(num)
    return list
print('Num: ',len(Permutation()))
print(Permutation())

result:
Insert picture description here

factorial

Let the function FirstFactorial(num) accept the passed num parameter and return its factorial. For example: if num = 4, the program should return (4 * 3 * 2 * 1) = 24. For the test case, the range will be between 1 and 18, and the input will always be an integer

def FirstFactorial(num):
    result=1
    while num>0:
        result=result*num
        num=num-1
    num=result
    return num

# keep this function call here
print(FirstFactorial(4))

Results:
24

Guess you like

Origin blog.csdn.net/Shadownow/article/details/105847012