Python interview questions collection (three)

Python interview questions collection (3): Python classic interview questions (1)

1. Floating point calculation###

topic####

Determine whether the running results of floating-point numbers are equal:

 a =0.1 
b =0.2 
c =0.3 
assert a + b == c

Question analysis:

This question examines the knowledge points of computer floating point operations. Not only the python language, but other programming languages also face the same problem: when performing floating-point operations, the decimal representation will lose its precision, causing the results of the operations to be inconsistent with the actual results.

This is due to the characteristics of the underlying CPU and computing standard when performing arithmetic through its own floating-point unit. Decimals that seem to be infinite are infinite in the binary representation of a computer.

Therefore, you must pay attention when performing high-speed floating-point operations, especially when assertions are required during automated testing, which is very error-prone.

Question answer:

The assertion fails and an AssertionError is thrown.

solution:

Use Decimal module:

from decimal import Decimal  
a =0.1 
b =0.2 
c =0.3
assert Decimal(str(a))+Decimal(str(b))==Decimal(str(c))

Decimal() can maintain the accuracy of floating-point numbers, and is very useful in industries with high accuracy requirements such as the financial field and the testing field. But we must pay attention: Decimal() must be a string type parameter, if it is a data type, the precision will be lost again.

Decimal(0.1)+Decimal(0.2)==Decimal(0.3)  # False   
Decimal('0.1')+Decimal('0.2')==Decimal('0.3')  # True

2. List flattening and dimensionality reduction###

topic####

There is a two-dimensional list, reduced to a normal one-dimensional. For example, in the Lemon Class, there will be students in groups, and we want to obtain the names of all the students through the group information.

 groups =[['huahua','xiaojian'],['musen','yuze'],['keyou']] 
# got the answer['huahua','xiaojian','musen','yuze','keyou']

method one####

The simplest way can be extracted one by one through the for loop:

 names =[]for group in groups:for name in group:         
  names.append(name)print(names)

Method Two####

However, some restrictions may be added during the interview, such as allowing you to implement it with one line of code. At this time, you need to have a further understanding of python basics, such as using the sum function:

 names =sum(groups,[])

Very simple to achieve the same function. Why can the sum function do it? Let's look at a simple example:

 a =sum([1,2,3])

The result is 6, which means that the sum function will add up each element in a list. But the sum function can also have a second parameter: the starting value, which is 0 by default, so the above process can actually be written as:

 a =sum([1,2,3],0) # 0+1+2+3

Based on the same principle, you can perform dimensionality reduction operations on lists:

a =sum([['yuze'],['is'],['a']],[])
# []+[' yuze']+['is']+['a']
# List splicing

Method Three

It can also be easily solved by list comprehension:

a =[e for group in groups for e in group]

Three, multiple inheritance

classA:
 def run(self):print("a is running")classB:
 def run(self):print("b is running")classC(A, B):
 pass

# What will be printed? ? ?
C().run()

Multiple inheritance is often used to ask interview questions. In this exam question, both the parent classes A and B of class C have implemented the run method, which one is called first in the C object?

The answer is: a running

Diamond problem####

classA:
 def run(self):print("a running")classB(A):
 pass

classC(A):
 def run(self):print("C running")classD(B, C):
 pass

# What will be printed? ? ?
D().run()

Their inheritance diagram can be simplified as follows. This problem is also known as the diamond problem or the diamond problem:

The answer is: c running

What if we don't let C class inherit A?

classA:
 def run(self):print("a running")classB(A):
 pass

classC:
 def run(self):print("C running")classD(B, C):
 pass

# What will be printed? ? ?
D().run()

Their inheritance diagram can be simplified as follows, which can be referred to as a v-type problem:

The answer will become a running

Question analysis

The specific reason is that the inheritance of python will follow a specific order, the priority is in the front, and the subclass will be used first. How to quickly check this order of inheritance? Go to view through D.__mro__

For example, in the diamond problem, the order of d is as follows:

(< class'__main__.D'>,<class'__main__.B'>,<class'__main__.C'>,<class'__main__.A'>,<class'object'>)

D is at the top. If D defines the run method, it will be used first, then B, then C, then A, and finally the object base class.

In the case of line V, the order of d becomes this:

(< class'__main__.D'>,<class'__main__.B'>,<class'__main__.A'>,<class'__main__.C'>,<class'object'>)

A and C reversed the order. The answers to the 2 questions are inconsistent.

Recommended Posts

Python interview questions collection (three)
Python interview questions
Python interview questions summary
Python classic interview questions two
Python interview questions: string concatenation
Python answers questions
Python interview assault
Python garbage collection mechanism
Python classic interview question one​
XTU programming Python training three
LeetCode brushing questions summary python3
Python string three formatted output
Collection of Python Common Modules
Learn about garbage collection in Python
Python classic programming questions: string replacement