In [6]:
#1、写函数,给定符号和行数,如’*’,5,可打印相应行数的如下菱形。主程序输入符号和行数调用该函数进行验证。(20分)

def a():
    m=input('请输入一个符号:')
    n=int(input('请输入行数:'))
    i=1
    while i<=n:
        print(' '*(n-i)+(m+' ')*i)
        i=i+1
    i=i-2
    while i>0:
        print(' '*(n-i)+(m+' ')*i)
        i=i-1
a()


请输入一个符号:*
请输入行数:5
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

In [9]:
#2.用递归和非递归分别实现函数求1!+2!+3!+...+n!,主程序以n=10分别调用。(20分)

#递归
def recusive_factorial(n):
    if n == 0:
        return 1
    else:
        return n*recusive_factorial(n-1)
def a(n):
    sum=0
    for i in range(1,n+1):
        sum=sum+(recusive_factorial(i-1))
    return sum

print(a(10))


409114

In [10]:
#非递归
def a(n):
    total=1
    if n==0:
        return 1
    else:
        for i in range(1,n):
            total=total*i
        return total
def b(n):
    sum=0
    for i in range(1,n+1):
        sum=sum+a(i)
    return sum

print(b(10))


409114

In [27]:
#3、北京车牌号的一般形式为:“京X-YYYYY”,其中X为字母,Y为字母或者数字,字母不能为I或者O,数字只能0-9之间.
#   请编写程序模拟选号过程:一次可以随机生成10个车牌号(不能有重复),依次将其编号为0-9,显示给用户。(20分)

import random
numbers=[]
for i in range(10):
    numbers[i]=(random.choice('ABCDEFGHJKLMNPQRST'))
    for a in range(1,6):
        numbers[i].append(random.randint(0,10))
for i in range(10):
    print(i,'京',numbers[i])


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-27-e0f2090f80c2> in <module>()
      5 numbers=[]
      6 for i in range(10):
----> 7     numbers[i]=(random.choice('ABCDEFGHJKLMNPQRST'))
      8     for a in range(1,6):
      9         numbers[i].append(random.randint(0,10))

IndexError: list assignment index out of range

In [18]:
#4.两个向量间的距离可定义为两个向量间的夹角余弦值,给定三个向量,求向量间距离的最小值。三个向量为:[1,2,3,4],[4,5,6,7],[7,8,9,10](20分)

import math

line1=[1,2,3,4]
line2=[4,5,6,7]
line3=[7,8,9,10]
a=0
b=0
c=0
for i in range(4):
    a=a+line1[i]*line2[i]
A=a/(math.sqrt(156))
for i in range(4):
    b=b+line2[i]*line3[i]
B=b/(math.sqrt(420))
for i in range(4):
    c=c+line1[i]*line3[i]
C=c/(math.sqrt(324))
if A<B:
    d=A
else:
    d=B
if C<d:
    d=C
else:
    d=d
print(d)


4.803844614152614

In [28]:
#随机生成100000个整数(1-10000之间)作为集合A,随机生成100000个整数(1-15000之间)作为集合B。
#(a)得到A,B所有出现的数及出现次数,分别输出到文件a.txt,b.txt中(新建文件),请输出多行,每行为两个数,两个数之间用逗号分隔(5分);
#(b)列出A中所有回文数(例:12321)以及出现的次数(5分);
#(c)列出B中分别由1-5个数字组成的数各自的总次数与总和,并按照总次数排序输出(如223就是3个数字组成的数,2834就是4个数字组成的数)(5分);
#(d)从a.txt,b.txt读入得到A与B中出现的数(注意不要次数),计算既出现在A又出现在B中的数,追加输出到文件a.txt(5分)。

import random

A=[]
B=[]
for i in range(100):
    A.append(random.randint(1,10000))
    B.append(random.randint(1,15000))
fh = open(r'd:\temp\a.txt', 'w')
fh = open(r'd:\temp\b.txt', 'w')
for i in a:
    a.append(A[i]+' ')
    fh.writelines(A)
for j in b:
    b.append(B[i]+' ')
    fh.writelines(B)
count_words_freq(a,numbers)


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-28-27f95a66c5e6> in <module>()
     12     A.append(random.randint(1,10000))
     13     B.append(random.randint(1,15000))
---> 14 fh = open(r'd:\temp\a.txt', 'w')
     15 fh = open(r'd:\temp\b.txt', 'w')
     16 for i in a:

FileNotFoundError: [Errno 2] No such file or directory: 'd:\\temp\\a.txt'

In [ ]: