Python Day-String 使用循环、递归、任务的函数逻辑

花韻仙語
发布: 2024-12-16 12:54:27
转载
681人浏览过

python day-string 使用循环、递归、任务的函数逻辑

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
登录后复制

输出:

今天是星期五

立即学习Python免费学习笔记(深入)”;

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
登录后复制

输出:

今天是星期五

立即学习Python免费学习笔记(深入)”;

递归:
函数调用自身。

循环-->迭代方法。
递归-->递归方法。

示例: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!

AI图像编辑器
AI图像编辑器

使用文本提示编辑、变换和增强照片

AI图像编辑器46
查看详情 AI图像编辑器

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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号