首頁 >後端開發 >Python教學 >Python Day-String 使用循環、遞迴、任務的函數邏輯

Python Day-String 使用循環、遞迴、任務的函數邏輯

Barbara Streisand
Barbara Streisand原創
2024-12-20 22:15:10158瀏覽

Python Day-String Functions logic using loops, Recursion, Tasks

1) 在字串之間加入空格

txt = "TodayIsFriday"
#Today is Friday
first = True
for letter in txt:
    if letter>='A' and letter<='Z':
        if first==True:
            first = False
        else:
            print(' ',end='')
    print(letter,end='')

輸出:
今天是星期五

2) 刪除字串之間的空格

txt = "    Today Is Friday"
#Today is Friday

for letter in txt:
    if letter==' ':
        pass
    else:
        print(letter,end='')

輸出:
今天是星期五

3) ltrim- 刪除字串左側的空格。

#ltrim
txt = "    Today Is Friday"
#Today is Friday
alphabet = False
for letter in txt:
    if letter==' ' and alphabet == False:
        pass
    else:
        alphabet = True
        print(letter,end='')

4) rtrim- 刪除字串右側的空格。

txt = "Today Is Friday   "
#Today is Friday
alphabet = False
i = len(txt)-1
while i>=0:
    letter = txt[i]
    if letter==' ' and alphabet == False:
        pass
    else:
        alphabet = True
        end = i
        j = 0
        while j<=end:
            print(txt[j],end='')
            j+=1
        break
    i-=1

輸出:

今天是星期五

5) 從給定字串中刪除不需要的空格

txt = "Today          Is             Friday"
#Today is Friday
i = 0 
while i<len(txt):
    if txt[i] != ' ':
        print(txt[i],end='')
    else:
        if txt[i-1]!=' ':
            print(txt[i],end='')
    i+=1

輸出:

今天是星期五

遞迴:
函數呼叫自身。

循環-->迭代方法。
遞歸-->遞歸方法。

範例:1

def display(no):
    print(no, end=' ')
    no+=1
    if no<=5:
        display(no)


display(1)

輸出:

1 2 3 4 5

呼叫階乘的遞歸函數:

5! =5x4x3x2x1(或)5x4!

4! =4x3x2x1(或)4x3!

3! =3x2x1(或)3x2!

2! =2x1(或)2x1!

1! =1

範例:2

def find_fact(no):
    if no==1:
        return 1
    return no * find_fact(no-1)

result = find_fact(5)
print(result)

輸出:
120

說明:
1) find_fact(5)
回傳 5 * find_fact(4) #no-1 = 5-1 -->4

2) find_fact(4)
回傳 4 * find_fact(3) #no-1 = 4-1 -->3

3) find_fact(3)
回傳 3 * find_fact(2) #no-1 = 3-1 -->2

4) find_fact(2)
回傳 2 * find_fact(1) #no-1 = 2-1 -->1

5) find_fact(1)
基本情況:回傳 1

基本情況:遞迴中的基本情況是停止遞迴呼叫的條件。

任務:

strip() - 刪除字串開頭和結尾的所有空白字元(空格、製表符、換行符)。

1) 刪除給定字串前後不需要的空格。

txt = "    Today Is Friday   "

start = 0
end = len(txt) - 1

while start < len(txt) and end >= 0:

    i = start
    while i < len(txt) and txt[i] == ' ':
        i += 1
    start = i

    j = end
    while j >= 0 and txt[j] == ' ':
        j -= 1
    end = j
    break  

result = txt[start:end+1]
print(result)

輸出:

Today Is Friday

2)使用遞歸函數反轉數字:

def reverse_a_no(no,reverse = 0):
    if no==0:
        return reverse    
    rem = no%10
    reverse = (reverse*10) + rem
    no=no//10 
    return reverse_a_no(no,reverse)

no = int(input("Enter no. ")) 
reversed_no = reverse_a_no(no) 
print(reversed_no)

輸出:

Enter no. 15
51

3)是否找到質數:

def find_prime(no,div=2):
    if div<no: 
        if no%div == 0:
            return False
        div+=1
        return find_prime(no,div)
    else:
        return True

no=int(input("Enter the number: "))

if find_prime(no):
    print("EMIRP number")
else:
    print("not EMIRP number")

輸出:

1) Enter the number: 11
   EMIRP number
2) Enter the number: 15
   not EMIRP number

4) 找出斐波那契數:

def find_fibonacci(first_num,sec_num,no):
    if first_num > no:
        return
    print(first_num, end=" ")

    find_fibonacci(sec_num,first_num+sec_num,no)      

no = int(input("Enter the number: ")) 
find_fibonacci(0,1,no)

輸出:

Enter the number: 10
0 1 1 2 3 5 8 

5。是否查找回文:

def palindrome(num,count=0):
    if num == 0:
        return count
    return palindrome(num//10,count*10+num%10)

num=int(input("Enter the number:"))
result=palindrome(num)
if result==num:
    print("Palindrome")
else:
    print("Not palindrome")

輸出:

Enter the number:121
Palindrome

已建立 HackerRank 帳號:https://www.hackerrank.com/dashboard

以上是Python Day-String 使用循環、遞迴、任務的函數邏輯的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn