Python​ 重解零基础100题(3)

简介: Python​ 重解零基础100题(3)

第21题


难度:3级


问题:机器人从原点(0,0)开始在平面中移动,机器人可以通过给定的步骤向上,向下,向左和向右移动。


机器人运动的痕迹如下所示:


UP 5;DOWN 3;LETF 3;RIGHT 2;方向之后的数字是步骤。

请编写一个程序,计算一系列运动和原点之后距当前位置的距离。如果距离是浮点数,则只打印最接近的整数。


例:如果程序输入:UP 5;DOWN 3;LETF 3;RIGHT 2

则程序的输出应该是:2

提示:如果输入数据被提供给问题,则应该假定它是控制台输入。


import math
pos = [0,0]
print("请输入:")
while True:
    s = 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))))


我的答案:

>>> s = 'UP 5;DOWN 3;LEFT 3;RIGHT 2'
>>> t = [k.split(' ') for k in s.split(';')]
>>> t
[['UP', '5'], ['DOWN', '3'], ['LEFT', '3'], ['RIGHT', '2']]
>>> T = [(int(t[1])*(-1 if t[0] in 'LEFTDOWN' else 1),'X' if t[0] in 'UPDOWN' else 'Y') for t in t]
>>> T
[(5, 'X'), (-3, 'X'), (-3, 'Y'), (2, 'Y')]
>>> x,y=0,0
>>> x += sum(i[0] for i in T if i[1]=='X')
>>> y += sum(i[0] for i in T if i[1]=='Y')
>>> round((x**2+y**2)**0.5)
2
>>> 
# 输入输出格式 略



第22题


难度:3级


问题:编写一个程序,来计算每个单词出现的频率,按字母顺序对键进行排序后输出。

假设程序输入:

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



freq = {}   # frequency of words in text
print("请输入:")
line = input()
for word in line.split():
    freq[word] = freq.get(word,0)+1
words = sorted(freq.keys())  #按key值对字典排序
for w in words:
    print ("%s:%d" % (w,freq[w]))


我的答案:

>>> s = 'New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.'
>>> print('\n'.join([':'.join(k) for k in sorted([(i,str(s.count(i))) for i in set(s.split(' '))])]))
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1
>>> 



第23题


难度:1级

问题写一个可以计算数字平方值的方法。

提示:使用**运算符


1. def square(num):
2.     return num ** 2
3. print(square(2))
4. print(square(3))


略!




第24题


难度:1级

问题:Python有许多内置函数,如果不知道如何使用它,可以在线阅读文档或查找一些书籍。请编写一个程序来打印一些Python内置函数文档,例如abs(),int(),input(),并为您自己的功能添加文档;

提示:内置文档方法是doc;

print(abs.__doc__)
print(int.__doc__)
print(input.__doc__)
def square(num):
    '''Return the square value of the input number.
    The input number must be integer.
    '''
    return num ** 2
print(square(2))
print(square.__doc__)

略!



第25题


难度:1级

问题:定义一个类,它具有类参数并具有相同的实例参数。

提示:定义一个实例参数,需要在init方法中添加它。您可以使用构造参数初始化对象,也可以稍后设置该值

class Person:
    # Define the class parameter "name"
    name = "Person"
    def __init__(self, name=None):
        # self.name is the instance parameter
        self.name = name
jeffrey = Person("Jeffrey")
print("%s name is %s" % (Person.name, jeffrey.name))
nico = Person()
nico.name = "Nico"
print("%s name is %s" % (Person.name, nico.name))

略!



第26题


问题:定义一个可以计算两个数之和的函数。

提示:定义一个带有两个数字作为参数的函数。可以在函数中计算和并返回值。

1. def SumFunction(number1, number2):
2.  return number1+number2
3. print(SumFunction(1,2))

略!




第27题


问题:定义一个可以将整数转换为字符串并在控制台中打印的函数。

提示:使用str()将数字转换为字符串。

解决方案:

def printValue(n):
    print(str(n))
printValue(3)

略!



第28题


问题:定义一个可以将字符串中的数字进行相加的函数。

提示:使用int()将字符串转换为数字。

def sum_str(str1):
  len1=len(str1)
  sum = n = 0
  for i in range(len1):
    if 49 <= ord(str1[i]) <= 57:  #判断字符ascii码是否在数字ascii值范围内
      n = int(str1[i]) + n
    else:
      sum = n + sum
      n = 0
  sum = n +sum
  print(sum)
str1 = "b532x2x3c4b5"
sum_str(str1)


我的答案:

1. >>> s = 'b532x2x3c4b5'
2. >>> sum([int(i) for i in s if i.isnumeric()])
3. 24
4. >>>



第29题


问题:定义一个函数,它可以接收两个字符串形式的整数并计算它们的和,然后在控制台中输出。

提示:使用int()将字符串转换为整数。

1. def printValue(s1,s2):
2.     print(int(s1)+int(s2))
3. printValue("3","4")


我的答案:

1. >>> def cat(s1,s2):
2.  return int(s1)+int(s2)
3. 
4. >>> cat('3','4')
5. 7
6. >>>




第30题


问题:定义一个函数,它可以接受两个字符串作为输入,并将它们连接起来,然后在控制台中输出。

提示:使用+连接字符串

1. def printValue(s1,s2):
2.     print(s1+s2)
3. printValue("3","4") #34


我的答案:

1. >>> def cat(s1,s2):
2.  return s1+s2
3. 
4. >>> cat('3','4')
5. '34'
6. >>>


目录
相关文章
|
存储 索引 Python
Python​ 重解零基础100题(10-2)
Python​ 重解零基础100题(10-2)
69 0
|
存储 Python
Python​ 重解零基础100题(10)
Python​ 重解零基础100题(10)
68 0
|
索引 Python
Python​ 重解零基础100题(9)
Python​ 重解零基础100题(9)
73 0
|
索引 Python
Python​ 重解零基础100题(8)
Python​ 重解零基础100题(8)
84 0
|
Python
Python​ 重解零基础100题(7)
Python​ 重解零基础100题(7)
117 0
|
Python
Python​ 重解零基础100题(6)
Python​ 重解零基础100题(6)
69 0
|
Python
Python​ 重解零基础100题(5)
Python​ 重解零基础100题(5)
65 0
|
Python
Python​ 重解零基础100题(4)
Python​ 重解零基础100题(4)
82 0
|
JSON 数据安全/隐私保护 数据格式
Python​ 重解零基础100题(2)
Python​ 重解零基础100题(2)
222 0
|
Python 容器
Python​ 重解零基础100题(1)
Python​ 重解零基础100题(1)
136 0