1
Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.
Suppose the following input is supplied to the program:
7
Then, the output should be:
0
7
class Gen():
def by_seven(self,n):
for i in range(0,int(n/7)+1):
yield i*7
for i in Gen().by_seven(int(input())):
print(i)
yield用法:
理论
yield是 迭代器 下一次调用往下走一步
def gen_example():
print ('before any yield')
yield 'first yield'
print ('between yields')
yield 'second yield'
print ('no yield anymore')
gen= gen_example()
gen.__next__()# 第一次调用显示 before any yield
gen.__next__() #第二次调用 显示 between yields
gen.__next__() #第三次调用 显示 no yield anymore
gen.__next__() # 这个时候函数已经调用完了,就会迭代结束,报错Traceback (most recent call last):
# File "/home/jerry/PY_project/object_detect_factory/yolo/training/copyyyy.py", line 29, in <module>
# gen.__next__() #no yield anymore
# StopIteration
2
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
pos=[0,0]
while True:
s=input()
if not s:
break
mov=s.split(' ')
dir=mov[0]
step=int(mov[1])
if dir=="up":
pos[0]+=step
elif dir=="down":
pos[0]-=step
elif dir=='left':
pos[1]+=step
else:
pos[1]-=step
print(int(round(math.sqrt(pos[0]**2+pos[1]**2))))
from math import sqrt
lst = []
position = [0,0]
while True:
a = input()
if not a:
break
lst.append(a)
for i in lst:
if 'UP' in i:
position[0] -= int(i.strip('UP '))
if 'DOWN' in i:
position[0] += int(i.strip('DOWN '))
if 'LEFT' in i:
position[1] -= int(i.strip('LEFT '))
if 'RIGHT' in i:
position[1] += int(i.strip('RIGHT '))
print(round(sqrt(position[1] ** 2 + position[0] ** 2)))
3
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
s=input().split()
word=sorted(set(s))
for i in word:
print("{0}:{1}".format(i,s.count(i)))
#example:
a = [2, 1, 4, 9, 6]
a.sort()
print a
c = [2, 1, 4, 9, 6]
d = sorted©
print d
print c
输出:
[1, 2, 4, 6, 9]
[1, 2, 4, 6, 9]
[2, 1, 4, 9, 6]
string = 'Hello World ! Hello Python !'
print "string.count(sub) : ", string.count('H')
print "string.count(sub, 1) : ", string.count('H', 1)
print "string.count(sub, 1, 100) : ", string.count('H', 1, 100) # 随便取个 无限大的 end 参数
打印结果:
string.count(sub) : 2
string.count(sub, 1) : 1
string.count(sub, 1, 100) : 1
list = [10, 20, 30, 'Hello', 10, 20]
print "list.count('Hello') : ", list.count('Hello')
print "list.count(10) : ", list.count(10)
打印结果:
list.count('Hello') : 1
list.count(10) : 2
法二:
from pprint import pprint
p=input().split()
pprint({i:p.count(i) for i in p})
pretty print 简化打印 美观
4
Write a method which can calculate square value of number
n=int(input())
print(n**2)
**平方: 2
5
问题:
Python具有许多内置函数,如果您不知道如何使用它,则可以在线阅读文档或查找一些书籍。但是Python对于每个内置函数都有一个内置文档函数。
请编写程序以打印一些Python内置函数文档,例如abs(),int(),raw_input()
并添加您自己的功能的文档
print(str.__doc__)
print(sorted.__doc__)
def pow(n,p):
'''
param n: This is any integer number
param p: This is power over n
return: n to the power p = n^p
'''
return n**p
print(pow(3,4))
print(pow.__doc__)
6
Define a class, which have a class parameter and have a same instance parameter.
class Car:
name="Car"
def __init__(self,name= None):
self.name=name
honda=Car("Honda")
print("%s name is %s"%(Car.name,honda.name))
toyota=Car()
toyota.name="Toyota"
print("%s name is %s"%(Car.name,toyota.name))
输出结果:
Car name is Honda
Car name is Toyota
定义类,调用的时候,如果()里面不带,可用 .name(即所在类下面定义的函数)赋值;或者直接大类的括号里面赋值