资讯专栏INFORMATION COLUMN

Python基础练习100题 ( 21~ 30)

jeffrey_up / 3566人阅读

摘要:刷题继续昨天和大家分享了前道题,今天继续来刷解法一解法一解法二解法三解法一解法二解法一解法一解法二解法一解法二解法一解法二解法一解法二解法一解法二解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击

刷题继续

昨天和大家分享了前10道题,今天继续来刷21~30

Question 21:

</>复制代码

  1. A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:

</>复制代码

  1. UP 5
  2. DOWN 3
  3. LEFT 3
  4. RIGHT 2

</>复制代码

  1. The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer.
    Example:
    If the following tuples are given as input to the program:

</>复制代码

  1. UP 5
  2. DOWN 3
  3. LEFT 3
  4. RIGHT 2

</>复制代码

  1. Then, the output of the program should be:

</>复制代码

  1. 2

解法一

</>复制代码

  1. import math
  2. x,y = 0,0
  3. while True:
  4. s = input().split()
  5. if not s:
  6. break
  7. if s[0]=="UP": # s[0] indicates command
  8. x-=int(s[1]) # s[1] indicates unit of move
  9. if s[0]=="DOWN":
  10. x+=int(s[1])
  11. if s[0]=="LEFT":
  12. y-=int(s[1])
  13. if s[0]=="RIGHT":
  14. y+=int(s[1])
  15. # N**P means N^P
  16. dist = round(math.sqrt(x**2 + y**2)) # euclidean distance = square root of (x^2+y^2) and rounding it to nearest integer
  17. print(dist)
Question 22:

</>复制代码

  1. Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically.

    Suppose the following input is supplied to the program:

</>复制代码

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

</>复制代码

  1. Then, the output should be:

</>复制代码

  1. 2:2
  2. 3.:1
  3. 3?:1
  4. New:1
  5. Python:5
  6. Read:1
  7. and:1
  8. between:1
  9. choosing:1
  10. or:2
  11. to:1

解法一

</>复制代码

  1. ss = input().split()
  2. word = sorted(set(ss)) # split words are stored and sorted as a set
  3. for i in word:
  4. print("{0}:{1}".format(i,ss.count(i)))
解法二

</>复制代码

  1. ss = input().split()
  2. dict = {}
  3. for i in ss:
  4. i = dict.setdefault(i,ss.count(i))
  5. dict = sorted(dict.items())
  6. for i in dict:
  7. print("%s:%d"%(i[0],i[1]))
解法三

</>复制代码

  1. from collections import Counter
  2. ss = input().split()
  3. ss = Counter(ss) # returns key & frequency as a dictionary
  4. ss = sorted(ss.items()) # returns as a tuple list
  5. for i in ss:
  6. print("%s:%d"%(i[0],i[1]))
Question 23:

</>复制代码

  1. Write a method which can calculate square value of number
解法一

</>复制代码

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

</>复制代码

  1. n=int(input())
  2. print(n**2)
Question 24:

</>复制代码

  1. Python has many built-in functions, and if you do not know how to use it, you can read document online or find some books. But Python has a built-in document function for every built-in functions.

    Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input()

  2. And add document for your own function

解法一

</>复制代码

  1. print (abs.__doc__)
  2. print (int.__doc__)
  3. def square(num):
  4. """
  5. Return the square value of the input number.
  6. The input number must be integer.
  7. """
  8. return num ** 2
  9. print (square(2))
  10. print (square.__doc__)
Question 25:

</>复制代码

  1. Define a class, which have a class parameter and have a same instance parameter.
解法一

</>复制代码

  1. class Car:
  2. name = "Car"
  3. def __init__(self,name = None):
  4. self.name = name
  5. honda=Car("Honda")
  6. print("%s name is %s"%(Car.name,honda.name))
  7. toyota=Car()
  8. toyota.name="Toyota"
  9. print("%s name is %s"%(Car.name,toyota.name))
解法二

</>复制代码

  1. class Person:
  2. # Define the class parameter "name"
  3. name = "Person"
  4. def __init__(self, name = None):
  5. # self.name is the instance parameter
  6. self.name = name
  7. jeffrey = Person("Jeffrey")
  8. print ("{0} name is {1}".format(Person.name, jeffrey.name))
  9. nico = Person()
  10. nico.name = "Nico"
  11. print (f"{Person.name} name is {nico.name}")
Question 26:

</>复制代码

  1. Define a function which can compute the sum of two numbers.
解法一

</>复制代码

  1. sum = lambda n1,n2 : n1 + n2 # here lambda is use to define little function as sum
  2. print(sum(1,2))
解法二

</>复制代码

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

</>复制代码

  1. Define a function that can convert a integer into a string and print it in console.
解法一

</>复制代码

  1. def printValue(n):
  2. print (str(n))
  3. printValue(3)
解法二

</>复制代码

  1. conv = lambda x : str(x)
  2. n = conv(10)
  3. print(n)
  4. print(type(n))
Question 28:

</>复制代码

  1. Define a function that can receive two integer numbers in string form and compute their sum and then print it in console.
解法一

</>复制代码

  1. def printValue(s1,s2):
  2. print int(s1) + int(s2)
  3. printValue("3","4") #7
解法二

</>复制代码

  1. sum = lambda s1,s2 : int(s1) + int(s2)
  2. print(sum("10","45")) # 55
Question 29:

</>复制代码

  1. Define a function that can accept two strings as input and concatenate them and then print it in console.
解法一

</>复制代码

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

</>复制代码

  1. sum = lambda s1,s2 : s1 + s2
  2. print(sum("10","45")) # 1045
Question 30:

</>复制代码

  1. Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print all strings line by line.
解法一

</>复制代码

  1. def printVal(s1,s2):
  2. len1 = len(s1)
  3. len2 = len(s2)
  4. if len1 > len2:
  5. print(s1)
  6. elif len1 < len2:
  7. print(s2)
  8. else:
  9. print(s1)
  10. print(s2)
  11. s1,s2=input().split()
  12. printVal(s1,s2)
源代码下载

这十道题的代码在我的github上,如果大家想看一下每道题的输出结果,可以点击以下链接下载:

Python 21-30题

我的运行环境Python 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:

raw_input()在Python3中是input()

print需要加括号

fstring可以换成.format(),或者%s,%d

谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/43955.html

相关文章

  • Python基础练习100 ( 31~ 40)

    摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题我的运行环境如果你 刷题继续 昨天和大家分享了21-30题,今天继续来刷31~40题 Question 31: Define a function which can pr...

    miracledan 评论0 收藏0
  • Python基础练习100 ( 41~ 50)

    摘要:刷题继续大家好,我又回来了,昨天和大家分享了题,今天继续来看题解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题 刷题继续 大家好,我又回来了,昨天和大家分享了31-40题,今天继续来看41~50题 Question 41: Write a program whi...

    mochixuan 评论0 收藏0
  • Python基础练习100 ( 1~ 10)

    摘要:一套全面的练习,大家智慧的结晶大家好,好久不见,我最近在上发现了一个好东西,是关于夯实基础的道题,原作者是在的时候创建的,闲来无事,非常适合像我一样的小白来练习对于每一道题,解法都不唯一,我在这里仅仅是抛砖引玉,希望可以集合大家的智慧,如果 一套全面的练习,大家智慧的结晶 大家好,好久不见,我最近在Github上发现了一个好东西,是关于夯实Python基础的100道题,原作者是在Pyt...

    Java3y 评论0 收藏0
  • 测试开发必看:《笨办法学Python3》PDF中文高清版,豆瓣高分8.0

    摘要:笨办法学第版结构非常简单,共包括个习题,其中个覆盖了输入输出变量和函数三个主题,另外个覆盖了一些比较高级的话题,如条件判断循环类和对象代码测试及项目的实现等。最后只想说,学习不会辜负任何人,笨办法学 内容简介   《笨办法学Python(第3版)》是一本Python入门书籍,适合对计...

    不知名网友 评论0 收藏0

发表评论

0条评论

jeffrey_up

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<