Python Day-String 使用循环、递归、任务的函数逻辑
来源:dev.to
时间:2024-12-16 16:40:05 161浏览 收藏
本篇文章给大家分享《Python Day-String 使用循环、递归、任务的函数逻辑》,覆盖了文章的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

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
今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
142 收藏
-
259 收藏
-
113 收藏
-
327 收藏
-
358 收藏
-
340 收藏
-
365 收藏
-
391 收藏
-
392 收藏
-
105 收藏
-
442 收藏
-
291 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习