Python 练习100题---No.(21-40)---附其他题目解答链接

本文提供Python编程100题中的第21到40题的详细解答,包括问题描述、博主的Python3解决方案以及题目提供的Python2答案。题目涉及计算距离、单词频率统计、内置函数文档、类与函数定义等编程实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

github展示python100题
链接如下:
https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt
以下为博主翻译后题目及解答,答案代码分为两个,第一条为博主个人解答(Python3),第二条为题目所提供答案(Python2)
………………………………………………………………………………
题目1-20链接:https://blog.csdn.net/weixin_41744624/article/details/103426225
本部分为题目21-40,等级难度1-3不定顺序(建议倒序食用);
题目41-60链接:https://blog.csdn.net/weixin_41744624/article/details/103575741
题目61-80链接:
https://blog.csdn.net/weixin_41744624/article/details/103607992
题目81-98链接:https://blog.csdn.net/weixin_41744624/article/details/103646520

经检测题库去除重复只有98题啦(欢迎评论添加好题目)~
………………………………………………………………………………
21、问题:
机器人从原点(0,0)开始在平面内移动。机器人可以向上、下、左、右移动,只要给定步数。机器人运动轨迹如下:
UP 5
DOWN 3
LEFT 3
RIGHT 2
¡­
方向后面的数字是步数。请写一个程序,计算出一系列运动和原点后距离当前位置的距离。如果距离是浮点数,则只打印最接近的整数。

例子:

如果将下列元组作为程序的输入:
UP 5
DOWN 3
LEFT 3
RIGHT 2
¡­
那么,程序的输出应该是:
2

ls = []
while True:
    n=input("please input:")
    if not n:
        break
    ls.append(tuple(n.split(' ')))
x=0
y=0
for j in ls:
    i = list(j)
    if i[0] == "UP":
        y +=int(i[1]) 
    elif i[0] == "DOWN":
        y +=-int(i[1]) 
    elif i[0] == "LEFT":
        x +=-int(i[1]) 
    elif i[0] == "RIGHT":
        x +=int(i[1])
    else:
        print ("wrong input!")
z=round((x**2+y**2)**1/2,0)
print (z)

import math
pos = [0,0]
while True:
    s = raw_input()
    if not s:
        break
    movement = s.split(" ")
    direction = movement[0]
    steps = int(movement[1])
    if direction=="UP":
        pos[0]+=steps
    elif direction=="DOWN":
        pos[0]-=steps
    elif direction=="LEFT":
        pos[1]-=steps
    elif direction=="RIGHT":
        pos[1]+=steps
    else:
        pass

print int(round(math.sqrt(pos[1]**2+pos[0]**2)))

22、问题:
写一个程序从输入中计算单词的频率。输出应该在按字母数字顺序对键排序后输出。

假设向程序提供了以下输入:

New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.

那么,输出应该是:
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1

ls = input ("please input:").split()

digit = []
digitchar = []
upper = []
lower = []
for i in ls:
    if i.isalpha():            
        if i.islower():           
            lower.append(i)
        else
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值