《Python程序设计》 第六章 函数+编程题解

目录

6-1 使用函数求特殊a串数列和

6-2 使用函数求素数和

6-3 使用函数统计指定数字的个数

6-4 使用函数输出指定范围内Fibonacci数的个数 

6-5 使用函数求余弦函数的近似值

6-6 缩写词

7-1 输入列表,求列表元素和(eval输入应用)

7-2 一帮一

7-3 验证“哥德巴赫猜想”

7-4 列表或元组的数字元素求和

7-5 列表数字元素加权和(1)

7-6 列表元素个数的加权和(2)

7-7 求指定层的元素个数

7-8 输出全排列

此解法为"动态规划",没算法底子可能比较难看懂


6-1 使用函数求特殊a串数列和

def fn(a, n):
    result = a
    key = a
    for index in range(1, n):
        key = key * 10 + a
        result += key
    
    return result

6-2 使用函数求素数和

import math


def prime(num):
    if num < 2:
        return False
    if num < 4:
        return True
    for index in range(2, int(math.sqrt(num)) + 1):
        if num % index == 0:
            return False

    return True


def PrimeSum(m, n):
    result = 0
    for index in range(m, n + 1):
        if prime(index):
            result += index

    return result

6-3 使用函数统计指定数字的个数

def CountDigit(number, digit):
    result = 0
    num = int(abs(number))
    while num > 0:
        if num % 10 == digit:
            result += 1
        num = int(num/10)
    return result


6-4 使用函数输出指定范围内Fibonacci数的个数 

dict1 = {0: 1, 1: 1}  # 记录每一次的斐波那契数
dict2 = [1]           # 记录出现过的斐波那契数
a = b = key = 1
while b < 100001:
    key += 1
    a, b = b, a + b
    dict1[key] = b
    dict2.append(b)


def fib(n):
    return dict1[n]


def PrintFN(m, n):
    result = []

    for index in range(m, n + 1):
        if index in dict2:
            result.append(index)

    return result

6-5 使用函数求余弦函数的近似值

def Jie(num):
    if num == 1:
        return 1
    return num * Jie(num - 1)


def funcos(eps, x):  # 返回cos(x)的值
    result = 1
    flag = 2
    key = pow(x, flag) / 2

    while abs(key) >= eps:
        result += key if flag % 4 == 0 else -key
        flag += 2
        key = pow(x, flag) / Jie(flag)

    return result

6-6 缩写词

def acronym(str1):
    list1 = str1.split()
    result = ""

    for index in range(len(list1)):
        ch = list1[index][0]
        if 'a' <= ch <= 'z':
            ch = chr(ord(ch) - 32)
        result += ch

    return result

7-1 输入列表,求列表元素和(eval输入应用)

box = eval(input())
print(sum(box))

7-2 一帮一

n = int(input())
box = []
for index in range(n):
    sex, name = input().split()
    box.append([sex, name])

while len(box) > 0:
    for index in range(len(box) - 1, 0, -1):
        if box[index][0] != box[0][0]:
            print("%s %s" % (box[0][1], box[index][1]))
            box.pop(index)
            box.pop(0)
            break


7-3 验证“哥德巴赫猜想”

from math import sqrt


def SU(num):
    if num < 2:
        return False
    if num < 4:
        return True
    for index in range(3, int(sqrt(num) + 3), 2):
        if num % index == 0:
            return False
    return True


N = int(input())
key = 2

for z in range(3, N, 2):
    if SU(z) and SU(N - z):
        key = z
        break

print("%d = %d + %d" % (N, key, N - key))

7-4 列表或元组的数字元素求和

def SUM(str1):
    if type(str1) == int:
        return str1
    if type(str1) != str:
        return sum(SUM(x) for x in str1)
    return 0


box = eval(input())
print(SUM(box))

7-5 列表数字元素加权和(1)

def SUM(str1, deep):
    if type(str1) == int:
        # print("%d %d" % (str1, deep))
        return str1 * deep
    if type(str1) != str:
        return sum(SUM(x, deep+1) for x in str1)
    return 0


box = eval(input())
print(SUM(box, 0))

7-6 列表元素个数的加权和(2)

def SUM(str1, deep):
    if type(str1) == int:
        # print("%d %d" % (str1, deep))
        return deep
    if type(str1) != str:
        return sum(SUM(x, deep+1) for x in str1)
    return 0


box = eval(input())
print(SUM(box, 0))

7-7 求指定层的元素个数

result = [0 for index in range(101)] # 记录各层出现元素的次数(数组)


def SUM(str1, deep):
    if type(str1) == int:
        # print("%d %d" % (str1, deep))
        result[deep] += 1
    elif type(str1) != str:
        for index in str1:
            SUM(index, deep + 1)


box = eval(input())
SUM(box, 0)
command = int(input())
print(result[command])

7-8 输出全排列

appear = [0 for cdl in range(520)]
N = int(input())

result = []


def printNum(num, deep):

    if appear[num] == 1:
        return
    else:
        result.append(num)
        appear[num] = 1
        for index in range(1, N + 1):
            printNum(index, deep + 1)

    if deep == N:
        for index in result:
            print(index, end='')
        print()

    appear[num] = 0
    result.pop(deep-1)


for z in range(1, N + 1):
    printNum(z, 1)

你可能感兴趣的:(作业合集,算法,PTA,python)