# 留学生作业之7进制与序列产生

### Python海龟宝典含200多个原创的用turtle模块制作的创意程序，原名《Python趣味编程200例》。准备参加全国创意编程与智能设计大赛的同学们可以用来做参考。

1. (25 Points) Fill in the definition of the recursive function base7digits(𝑛) below that prints out the base 7
digits of the positive integer 𝑛. All output will be on a single line with no spaces between the digits. Your
function need not print anything when 𝑛 ≤ 0. (Hint: this was discussed as an exercise in lecture on 4-1-
21, and in the examples on the class webpage.) 主要意思是把n转换成7进制数.

def base7digits(n):
# begin solution

2. (25 Points) Fill in the definition of the Python function sequence_gen(𝑎, 𝑏) below that returns a generator
object which produces the (infinite) sequence of positive multiples of 𝑎 that have remainder 1 when divided
by 𝑏. Thus sequence_gen(3, 5) would produce the sequence: 6, 21, 36, 51, 66, 81,… . You may assume
that 𝑎 and 𝑏 are positive integers. 主要意思是定义一个生成器,产生6,21,36,51,66……,81这个序列.
def sequence_gen(a, b):

# begin solution

```def base7digits(n):

n,r = divmod(n,7)
if n !=0:
return base7digits(n) + str(r)
else:
return str(r)

print(base7digits(19))
```

```def sequence_gen(a, b):

n=6
while True:
yield n
n = n + a*b

x = sequence_gen(3,5)
for _ in range(10):
print(next(x),end=',')

```

#### 学本领，探索更大的世界！

error: Content is protected !!