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

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 reverses a list of n integers, so that the numbers are listed in the opposite order than they were before, and compare this method to an equivalent Python function for doing the sam

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: data structure and algorithm analysis in c是一本C语言数据结构和算法分析的经典教材,由Mark Allen Weiss编写。该书的主要特点是对数据结构和算法的讲解非常详细且透彻,采用了很多实例进行讲解,使得读者可以很快地掌握这些内容。此外,该书还特别强调了算法分析和设计的重要性,帮助读者理解复杂算法的实现方式,提高算法的优化能力。 而data structure and algorithm analysis in c的下载方式也非常简单,可以在网上找到相关的资源下载,并且也可以通过购买实体书的方式获得。对于想要深入学习C语言数据结构和算法分析的人来说,这本书是非常值得推荐的一本入门教材。 ### 回答2: data structure and algorithm analysis in c是一本关于C语言数据结构和算法分析的经典教科书。该书涵盖了广泛的数据结构和算法,包括数组、链表、树、图、排序、查找等。本书不仅涵盖了基本概念和技术,而且提供了深入的分析和高级应用。书中有丰富的例子和习题,方便读者深入理解和应用。该书是学习数据结构和算法的好材料,对于提高程序员的编程能力和解决问题的能力有很大的帮助。 ### 回答3: Data Structure and Algorithm Analysis in C是一本面向C++程序设计开发人员的算法和数据结构分析书籍。它涵盖了许多重要的算法和数据结构,如排序和搜索算法,二叉树,平衡树和图论等方面。本书旨在帮助读者深入了解算法和数据结构的基本知识,并提供了许多实用的示例和演练题来帮助读者巩固自己的知识。 本书包含了许多实际的示例和演练题,这些题目涵盖了从简单到复杂的各种情况,有助于读者更好地理解和应用所学知识。与此同时,这本书也提供了大量的编码和调试技巧,帮助读者编写出高效和可维护的代码。 总之,Data Structure and Algorithm Analysis in C是一本优秀的参考书,无论你是刚接触算法和数据结构或是已经具备一定的基础,它都可以提供丰富的知识和实践经验。如果你正在寻找一本深入了解算法和数据结构的书籍,那么它一定是首选之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值