data structure and algorithm in python [Exercises in Chapter 1]

本文提供了Python编程中关于数据结构和算法的实战练习题,包括判断数的倍数、奇偶性检查、求序列最小最大值、求平方和等,涉及列表推导式、随机数操作以及函数实现。

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

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 bool(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):
    while k>0:
        k-=2
    return bool(k+1)

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):
    data.sort()
    return data[0],data[-1]

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 fun(n):
    res=0
    while n>0:
        n-=1
        res+=n**2
    return res

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**2 for i in range(n))

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.

def fun6(n):
    res=0
    k=1
    while k<n:
        res+=k**2
        k+=2
    return res

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**2 for i in range(1,n,2))

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=k+len(s)
s='Shimu Yang'
for k in range(-1,-len(s)-1,-1):
    j=k+len(s)
    print((s[k],s[j]))

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

range(50,90,10)

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?

range(8,-10,-2)

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].

l=[2**i for i in range(0,9)]

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.

import random

def choice(data):
    return data[random.randrange(len(data))]

C-1.13 Write a pseudo-code description of a function that

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值