python数据结构第四章课后习题答案

这里写目录标题

    • 1
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 26
    • 27

1

def find_max(data, index):
    if index == len(data)-1:
        return True
    max = find_max(data,index+1)
    if max > data[index]:
        return max
    else:
        return data[index]


if __name__ == "__main__":
    data = [1, 2, 3, 4444, 5, 6]
    print(find_max(data, 0))    #时间复杂度为n




6

def harm_num(n):
    if n == 1:
        return 1
    else:
        return 1 / n + harm_num(n - 1)


if __name__ == "__main__":
    print(harm_num(3))  # 时间复杂度为n

7

import math


def change_num(str_num, index):
    if index == 1:
        return int(str_num[-1])
    else:
        return int(str_num[len(str_num) - index]) * int(math.pow(10, index - 1)) + change_num(str_num, index - 1)


if __name__ == "__main__":
    print(change_num("12345", 5))  # 时间复杂度为n

8

def good_sum(data, index=0):
    if 2 * index >= len(data):
        return 0
    elif 2 * index == len(data) - 1:
        return data[index]
    else:
        total = data[2 * index] + data[2 * index + 1]
        return total + good_sum(data, index + 1)


if __name__ == '__main__':
    data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    print(good_sum(data))  # 时间复杂度为log n

9

def ptp(data, index=0, ):
    if index == len(data) - 1:
        return data[-1], data[-1]
    max, min = ptp(data, index + 1)
    if max < data[index] < min:
        return min, max
    elif min > data[index] and max > data[index]:
        return max, data[index]
    elif min < data[index] and data[index] > max:
        return data[index], min
    else:
        return max, min


if __name__ == "__main__":
    data = [1, 2, 66, 8, 9, 100]
    print(ptp(data, 0))  # 时间复杂度为n

10

def log(data):
    if data == 1:
        return 0
    if data <= 0:
        raise ValueError("paramter must greater than 0")
    num = data // 2
    return 1 + log(num)


if __name__ == '__main__':
    print(log(1000))  # 时间复杂度为n

11

def is_unique(sets, index=0):
    if len(sets) - 1 == index:
        return True
    else:
        if is_unique(sets, index + 1):
            for i in range(index + 1, len(sets)):
                if sets[i] == sets[index]:
                    return False
            return True
        else:
            return False


if __name__ == "__main__":
    data = [1, 2, 3, 4, 5, 6,1]
    print(is_unique(data))

12

def recursive_multiply(m, n, index=0):
    if index == n:
        return 0
    else:
        return m + recursive_multiply(m, n, index + 1)


if __name__ == "__main__":
    m = 3
    n = 2
    print(recursive_multiply(3, 2))

14

def hano(n,a,b,c):
    if n == 1:
        print(a,"-->>",c)
    else:
        hano(n-1,a,c,b)
        hano(1,a,b,c)
        hano(n-1,b,a,c)


if __name__ == "__main__":
    hano(3,"a","b","c")

15

from itertools import permutations


def recursive_subset(sets):
    if len(sets) == 0:
        return [[]]
    subsets = []
    first_elt = sets[0]
    rest_list = sets[1:]
    for partial_subset in recursive_subset(rest_list):
        subsets.append(partial_subset)
        next_subset = partial_subset[:] + [first_elt]
        subsets.append(next_subset)
    return subsets


if __name__ == "__main__":
    data = ['a', 'b', 'c','d']
    print(recursive_subset(data))

16

def reverse(string, index=0):
    if len(string) - 1 == index:
        return string[index]
    else:
        return reverse(string, index + 1) + string[index]


if __name__ == "__main__":
    string = "pots&pans"
    print(reverse(string))

17

def reverse(string, index=0):
    if len(string) - 1 == index:
        return string[index]
    else:
        return reverse(string, index + 1) + string[index]


def is_palindrome(string):
    reverse_string = reverse(string)
    if reverse_string == string:
        return True
    else:
        return False


if __name__ == "__main__":
    string = "gohangasalamiimalasagnahog"
    print(is_palindrome(string))

18

def vowel_consonants(string, vowel=0, consonants=0, index=0):
    if index == len(string):
        return 0, 0
    else:
        vowel, consonants = vowel_consonants(string, vowel, consonants, index + 1)
        vowels = ['a', 'e', 'i', 'o', 'u']
        if string[index].lower() in vowels:
            vowel += 1
        else:
            consonants += 1
        return vowel, consonants


def is_judge(string):
    total1,total2 = vowel_consonants(string)
    if total1>total2:
        return True
    else:
        return False


if __name__ == "__main__":
    strings = "abced"
    print(is_judge(strings))

19

def module_1(datas, odd_num=[], even_num=[], index=0):
    if index == len(datas):
        return [],[]
    else:
        odd_num, even_num = module_1(datas, odd_num, even_num, index + 1)
        if datas[index] % 2 == 0:
            even_num.append(index)
        else:
            odd_num.append(index)
        return odd_num, even_num


def module_2(datas):
    odd_num, even_num = module_1(datas)
    new_data = list()
    for index in even_num:
        new_data.append(datas[index])
    for index in odd_num:
        new_data.append(datas[index])
    return new_data


if __name__ == "__main__":
    x = [0,11,23,22,1, 2, 3, 4, 5, 6,28,11,23,]
    print(module_2(x))

20

def re_sort(data, k, index=0):
    if len(data) == index:
        return [k]
    else:
        new_data = re_sort(data, k, index + 1)
        new_index = new_data.index(k)
        first_list = new_data[:new_index]
        second_list = new_data[new_index + 1:]
        if data[index] > k:
            return first_list[:] + [k] + second_list[:] + [data[index]]
        else:
            return first_list[:] + [data[index]] + [k] + second_list[:]


if __name__ == "__main__":
    data = [1, 2, 3, 4, 7, 2, 4, 11]
    data = re_sort(data, 3)
    data.remove(3)
    print(data)  # 算法的时间复杂度为n

21

def FindPair(data,i,j,k):
    if i == j:
        return False
    else:
        if data[i]+data[j] < k:
            return FindPair(data,i+1,j,k)
        else:
            if data[i]+data[j] > k:
                return FindPair(data,i,j-1,k)
            else:
                return  True
            
            
if __name__ == "__main__":
    data = [2,3,1,3,4,22,31,65]
    k = 5
    data.sort()
    print(FindPair(data,0,len(data)-1,90))

22

def recursive_multiply(m, n):
    i = 0
    total = 1
    while i < n:
        total *= m
        i += 1
    return total


if __name__ == "__main__":
    m = 3
    n = 2
    print(recursive_multiply(3, 2))

23

import re
import os.path


def find(paths):
    total = 0
    if os.path.isdir(paths):
        for filePath in os.listdir(paths):
            newPath = os.path.join(paths,filePath)
            if newPath == "F:\\python_code\\venv":
                continue
            total += find(newPath)
    pattern = re.compile(r".*?(\.py)")
    if pattern.findall(paths):
        print(paths,os.path.getsize(paths))
        return 1
    else:
        return total


if __name__ == "__main__":
    path = 'F:\\python_code'
    print(find(path))

26

def hano(k, a, b, c):
    if k == 1:
        print(a, "-->>", c)
    else:
        hano(k - 1, a, c, b)
        hano(1, a, b, c)
        hano(k - 1, b, a, c)


if __name__ == "__main__":
    hano(3, 'a', 'b', 'c')

27

import os


def walk(filename):
    if os.path.isdir(filename):
        for new_file_name in os.listdir(filename):
            path = os.path.join(filename,new_file_name)
            walk(path)
    print(filename)


def find(dirpath, dirnames, filenames=[]):
    if os.path.isdir(dirpath):
        path = os.path.join(dirpath, dirnames)
        for file_name in os.listdir(path):
            if len(filenames) == 0:
                    walk(os.path.join(path, file_name))
            else:
                for target in filenames:
                    if target == file_name:
                        walk(os.path.join(path,file_name))
                    else:
                        find(os.path.join(dirpath,file_name),file_name)


if __name__ == "__main__":
    dirpath = 'F:\\python_code'
    dirnames = "python_senior"
    filenames = ['网络爬虫']
    find(dirpath, dirnames, filenames)

你可能感兴趣的:(数据结构,python,数据结构)