Experiment 1

Experimental Objective:

Proficient in Python operators; Proficient in Python built-in functions; Understand the simplification of combinatorial number definition; Understand the errors and problems associated with floating point operations.

Experimental Requirements:

Before the experiment, we should be familiar with the relevant knowledge points, draw up the corresponding experimental operation steps, and make clear the experimental purpose and requirements. During the experiment, obey the arrangement of the experiment instructor, observe the rules and regulations of the laboratory, and take good care of the experimental equipment; After the completion of the experiment, write the experiment report carefully (all the source code is given in the experiment report).

Experimental:

Verification-Driven.

Experimental Content:

  1.  Write a program, input any large natural number, output the sum of each number.
  2.  Write a program, input two sets seta and setb, respectively output their intersection, union and difference set.
  3.  Write a program, input a natural number, output its binary, octal, hexadecimal representation.
  4.  Read and debug the following code, analyze the code function, find and solve the errors in the code:
def cni(n,i):
    minNI=min(i,n-i)
    result=1
    for j in range(0,minNI):
        result=result*(n-j)//(minNI-j)
    return result

  5.   Procedures in Chapter 2 of the book.

Experimental steps:

Code for question 1:

x=input('Please input:')
num=list(map(int,str(x)))
result=sum(num)
print(result)

Code for question 2:

# 输入两个集合setA和setB,分别输出他们的交集,并集和差集
x=input('Please input:')
setA=set(x);
y=input('Please input:')
setB=set(y);
B=setA | setB
J=setA & setB
C=setA - setB
print('A与B的并集:',B)
print('A与B的交集:',J)
print('A与B的差集:',C)

Code for question 3:

# 输入1个自然数,输出他的二、八、十六进制
x=input('Please input:')
y=int(x)
er=bin(y)
ba=oct(y)
sl=hex(y)
print('二进制串:',er)
print('八进制串:',ba)
print('十六进制串:',sl)

Code for question 4:

def cni(n,i):
    minNI=min(i,n-i)
    x=1
    y=1
    for j in range(0,minNI):
        x=x*(n-j)
        y=y*(minNI-j)
    return int(x/y)

  The second solution:

from fractions import Fraction
def cni(n,i):
    minNI=min(i,n-i)
    result=Fraction(1,1)
    for j in range(0,minNI):
        x=Fraction((n-j),(minNI-j))
        result=result*x
    return result

knowledge Point:

Enter Help (?) in the Python shell window. ? Is the usage of the keyword you are looking for.

  1. int: ([x])→integer; int(x,base=10)→integer.(int variable=0)

  2. list: List is the most frequently used data type in Python.(variable=list['a',1,2.1,'bc'])

  3. set: The function creates an unordered set of non-repeating elements, tests relationships, deletes duplicates, and computes intersection, difference, union, and so on.(x=set('runoob'))

  4. oct: Function converts an integer to an octal string.(oct(10));

  5. hex: The hexadecimal () function is used to convert decimal integers to hexadecimal and is represented as a string.(hex(12))

  6. For additional keywords or operators, refer to the Python documentation or other resources: map, input, print, |,&,-,/,//,bin,min,from...import...,fractions,Fraction,def.

Problem Analysis:

Since questions 1 through 3 are relatively simple, this paper only makes some simple analysis of questions 4.

First observe the function expression of the circulant body, and get the formula of combinatorial number easily after mathematization.

Then analyze the variables defined in the question, get the properties of the combinatorial number easily, and then call the function test program.

 It is found that the calculation result of this problem is wrong. It's easy to see where the problem is in the formula for the circulant body. Because the formula has the floating point error of the division computer.

Latex code for the above formula:

\begin{equation}
C_n^i=\dfrac{A_n^i}{i!}=\dfrac{(n-i)!}{i!}
\label{eq:energy}
\end{equation}

\begin{equation}
C_n^i=C_n^{n-i}
\label{eq:energy}
\end{equation}
formula

 

 

 

 

-------------------------To be continued-------------------------------------

 

posted @ 2020-09-23 13:36  望星草  阅读(332)  评论(0编辑  收藏  举报