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

如本文章标有价格,需议价或其它事情商议请加微信pythonxia

作业如下所示:

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

以下是7进制的参考答案:

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=',')

晚是10:41了,还没洗脸和脚和刷牙,但要睡觉了!

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » 留学生作业之7进制与序列产生
scratch

学本领,探索更大的世界!

李兴球博客 风火轮编程主页