数据结构与算法 Python语言实现 课后习题解答Chapter 1

本文提供了Python实现数据结构与算法的课后习题解答,包括判断数字是否为倍数、判断奇偶性、寻找序列最小最大值、计算平方和、使用列表解析式以及实现随机选择等功能。同时,探讨了Python中的负索引、范围生成、列表翻转、奇数平方和等概念,并提出了一些创意性的编程挑战,如判断序列中是否存在特定配对、列表元素唯一性检查等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.12 Exercises
Reinforcement
R-1.1 Write a short Python function, is_multiple(n, m), that takes two integer
values and returns True if n is a multiple of m, that is, n = mi for some
integer i, and False otherwise.

def is_multiple(n,m):
    return not n%m 

R-1.2 Write a short Python function, is_even(k), that takes an integer value and
returns True if k is_even, and False otherwise. However, your function
cannot use the multiplication, modulo, or division operators.

def is_even(k):
    k=abs(k)
    while True:
       if k==0:
          return True
       elif k==1:
          return False
       k=k-2
    
R-1.3 Write a short Python function, minmax(data), that takes a sequence of
one or more numbers, and returns the smallest and largest numbers, in the
form of a tuple of length two. Do not use the built-in functions min or
max in implementing your solution.

def minmax(data):
    min=data[0]
    max=data[0]
    for i in data:
       if i<min:
          min=i
       if i>max:
          max=i
    return (min,max)

R-1.4 Write a short Python function that takes a positive integer n and returns
the sum of the squares of all the positive integers smaller than n.

def sumOfSquares(n):
    sum=0
    for i in range(1,n+1):
        sum=sum+i*i
    return sum
    

R-1.5 Give a single command that computes the sum from Exercise R-1.4, relying
on Python’s comprehension syntax and the built-in sum function.

sum([i*i for i in range(1,n+1)])

R-1.6 Write a short Python function that takes a positive integer n and returns
the sum of the squares of all the odd positive integers smaller than n.


In [20]: def sumOfSquaresOfOdd(n):
    ...:     return sum([i*i for i in range(1,n+1) if i%2>0])
    ...:
    ...:

In [21]: sumOfSquaresOfOdd(3)
Out[21]: 10

R-1.7 Give a single command that computes the sum from Exercise R-1.6, relying
on Python’s comprehension syntax and the built-in sum function.

sum([i*i for i in range(1,n+1) if i%2>0])

R-1.8 Python allows negative integers to be used as indices into a sequence,
such as a string. If string s has length n, and expression s[k] is used for index
−n≤k<0, what is the equivalent index j ≥0 such that s[j] references
the same element?

j=n-k

R-1.9 What parameters should be sent to the range constructor, to produce a
range with values 50, 60, 70, 80?


In [26]: list(range(50,90,10))
Out[26]: [50, 60, 70, 80]
R-1.10 What parameters should be sent to the range constructor, to produce a
range with values 8, 6, 4, 2, 0, −2, −4, −6, −8?

In [27]: list(range(8,-10,-2))
Out[27]: [8, 6, 4, 2, 0, -2, -4, -6, -8]

R-1.11 Demonstrate how to use Python’s list comprehension syntax to produce
the list [1, 2, 4, 8, 16, 32, 64, 128, 256].


In [29]: [pow(2,i) for i in range(9)]
Out[29]: [1, 2, 4, 8, 16, 32, 64, 128, 256]
R-1.12 Python’s random module includes a function choice(data) that returns a
random element from a non-empty sequence. The random module includes
a more basic function randrange, with parameterization similar to
the built-in range function, that return a random choice from the given
range. Using only the randrange function, implement your own version
of the choice function.

In [31]: lst
Out[31]: [1, 2, 3]

In [32]: random.choice(lst)
Out[32]: 3

In [33]: random.randrange(10)
Out[33]: 2

In [34]: def myChoice(lst):
    ...:     import random
    ...:     minN=min(lst)
    ...:     maxN=max(lst)
    ...:     while True:
    ...:
    ...:         i=random.randrange(minN,maxN+1)
    ...:     &nb

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值