python编程100+题21—30(第三天)

python编程100题21—30(第三天)

自学python提高编程能力(第三天)

以下为python编程100题,来自github,原文链接如下:
https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt
文章的最后是一些知识点总结和提示
完成这些题目的基础上,进行了一些题目要求的翻译理解;添加了自己理解的注释和出过的错误,请同样学习的朋友们指正!

QUESTION21

'''
Question 21
Level 3

Question£º
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:
UP 5
DOWN 3
LEFT 3
RIGHT 2

{定义一个机器人的位置坐标,通过输入控制行动,输出末尾距离原点的距离}

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:
UP 5
DOWN 3
LEFT 3
RIGHT 2
Then, the output of the program should be:
2
'''

import math
position=[0,0]
while True:
    s=input()
    if not s:
        break
    movement=s.split(" ")
    direction=movement[0]
    steps=int(movement[1])
    if direction=="UP":
        position[1]=position[1]+steps
    elif direction=="DOWN":
        position[1]=position[1]-steps
    elif direction=="LEFT":
        position[0]=position[0]-steps
    elif direction=="RIGHT":
        position[0]=position[0]+steps
    else:
        pass
print(round(math.sqrt(position[0]**2+position[1]**2))) #这里需要注意的是math库中函数以int型输入(可以是float输入),
                                                       #以float型输出。round函数的返回值是int类型。

Question 22

'''
Question 22
Level 3

Question:
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:

{输入一句话,以空格打断为字符串,输出字符串出现频率,并排序}
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
Then, the output should be:
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1
{这个顺序就是一般ASCII码的顺序,数字,大写,小写}
Hints
In case of input data being supplied to the question, it should be assumed to be a console input.
'''

array=input().split(" ")
frequency={}   #创建了一个字典,将字符与数字做对应
for each in array:
    frequency[each]=frequency.get(each,0)+1  #.get函数是得到字典中对应key的值,0是如果不存在时的返回值
    
words=[str(x) for x in frequency.keys()]  #.keys返回字典所有的关键字,或者叫键
words_sort=sorted(words)              #这里的sorted是将排序后的列表作为返回值,而words没有变化,所以一定要再用一个列表来接受排序后的列表

for w in words_sort:
    print(w,":",frequency[w])

QUESTION23

'''
Question 23
level 1

Question:
    Write a method which can calculate square value of number
{写一个产生平方数字的生成器}
Hints:
    Using the ** operator
'''
def square(num):
    return num**2

print(square(2))
print(square(3))

QUESTION24

'''
Question 24
Level 1

Question:
    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()
    And add document for your own function
    {打印python中的函数文档,并且建立一个自己函数文档}
Hints:
    The built-in document method is __doc__
'''
print(abs.__doc__)
print(int.__doc__)
print(input.__doc__)

def square(num):
    '''
    Return the square value of the input number.
    The input & output number should be integer.
    '''
    return num**2

print (square(2))
print (square.__doc__)

QUESTION25

'''
Question 25
Level 1

Question:
    Define a class, which have a class parameter and have a same instance parameter.
{完成一个类的定义}
Hints:
    Define a instance parameter, need add it in __init__ method
    You can init an object with construct parameter or set the value later
'''
class Person:
    name="Person"

    def __init__(self,name=None):
        self.name=name

jeffrey=Person("Jeffrey")
print(Person.name,jeffrey.name)
#关于类的定义的知识在文章最后

QUESTION26

'''
Question:
Define a function which can compute the sum of two numbers.
{定义一个函数,可以计算两个数的和}
Hints:
Define a function with two numbers as arguments. You can compute the sum in the function and return the value.
'''
def sumoftwo(number1,number2):
    return number1+number2

print(sumoftwo(1,2))

QUESTION27

'''
Question:
Define a function that can convert an integer into a string and print it in console.
{定义一个函数,把int类型数转为str类型并输出}
Hints:

Use str() to convert a number to string.
'''
def printValue(n):
    print(str(n))
    
printValue(3)

QUESTION28

'''
Question:
Define a function that can receive two integral numbers in
string form and compute their sum and then print it in console.
{定义一个函数接受两个整型数,求他们的和并输出}
Hints:

Use int() to convert a string to integer.
'''
def printValue(a,b):
    print(int(a)+int(b))

a,b=input("逗号分隔输入两个数字:").split(",")
printValue(a,b)

QUESTION29

'''
Question:
Define a function that can accept two strings as input and concatenate them and then print it in console.
{定义一个函数,接受两个str,拼接后输出}
Hints:

Use + to concatenate the strings
'''
a,b=input().split(" ")
print(a+b)

QUESTION30

'''
Question:
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.
{定义一个函数,输入两个字符串,输出长度最长的字符串,如果长度相同,则都输出}
Hints:

Use len() function to get the length of a string
'''
def printmax(a,b):
    len1=len(a)
    len2=len(b)
    if len1>len2:
        print(a)
    elif len2>len1:
        print(b)
    else:
        print(a)
        print(b)

printmax("one","THERR")

python的面向对象的编程:
(什么是面向对象的编程?)面向对象是相对于面向过程来说的,面向对象可以把相关数据、方法组织为一个整体;因此面向对象的编程可以使编程更加贴近实际应用。常用的面向对象的编程有JAVA,C++,python等
(什么是类,什么是对象?)类就是对事物的抽象,通过一种有规则的模型,可以对身边的任何事物进行分类;而对象就是这个类中的一个具体物件,是一种实例化的过程,具备所属类的特性。
(为什么在python种要用类?)程序员需要用的数据类型不能简单地用基本数据类型来表示时,就可以用类来表示一个复杂的数据。——这里类似C语言中的自定义结构体变量——但类可以把数据和函数封装在一起。

2.python的类:

类的定义:

定义一个类:
'''格式
>>>class <类名>:
>		成员变量
>		成员函数
>#定义类的三要素:句柄(名字)、属性(变量)、方法(函数)
'''举例:
>>>class Breakfast:  #类的首字母应该大写
>>>    egg=1
>>>    cake=10
>>>    def checkout(self):  #类的函数里面一定要有一个形参,一般写为self
>>>        return  "over"

类的实例化和调用:(将定义的类中的参数认为是这个类的属性,函数认为是这个类的方法)

举例一
>>>jeff=Breakfast() #进行了实例化,将jeff实例化为Breakfast类的一个元素
>>>jeff.egg
1
>>>jeff.cake
10
>>>jeff.checkout()
'over'
举例二
>>>l=[1,2,3,4]#这里就相当于把l声明为了列表这个类的一个对象
>>>l[1]
2
>>>l.append(1)#这里相当于是用了列表中的一个成员函数(或者叫做方法)append
>>>l
[1, 2, 3, 4, 1]

另外,通过print(list)可以查看到list列表这个类的定义,可以发现其中的定义和在上面定义Breakfast类的格式是一样的,由此可知,list、dictionary等等都是通过类来定义的
类的应用相较于之前是比较难的,周末将继续类的学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值