Python interview written test questions

1. Foreword: I haven't had an interview for two years. Today, I went out to try the market quotations. By the way, I have a look at the skills required by the company. I wrote a few questions today, and the answers were pretty good. The written test questions are not difficult to be technical, but they are a test of basic skills. Posting is the basic grammar and common operations of python. Today’s questions are also very interesting. I will keep updating the python interview questions I encountered later.
2.
Interview questions: 1.xxxx company (2020.10.29):
(1) Interview questions:
Insert picture description here
Insert picture description here

(2) Answer:

print(int(1.1))
print(int(1.9))

a = [1,2,3]
b = [3,4,5]
print(a+b)
print(set(a+b))

text = 'AbCdefG'
print(text.lower())

# 1-50奇数  51-100偶数求和
unevenNum = 0
evenNum = 0
for i in range(1,101):
    if i%2 != 0 and i<51:
        unevenNum+=i
    elif i%2 == 0 and i<101:
        evenNum+=i
print(unevenNum+evenNum)

a = 'hgaabcdef'
b = 'zxydaqr'
c = set(list(a+b))
s="".join((lambda x:(x.sort(),x)[1])(list(c)))
print(s)

# -123 -321
a = -123
c = -1*int(str(abs(a))[::-1])
print(c)
1
1
[1, 2, 3, 3, 4, 5]
{
    
    1, 2, 3, 4, 5}
abcdefg
3175
abcdefghqrxyz
-321

2. xxx company interview questions (2020.11.24)
(1) Written questions:
Insert picture description here
Insert picture description here

Insert picture description here
(2) Answer:

  1. A more advanced tuple is called namedtuple, which is translated into a named tuple in "Smooth Python". As the name implies, it is a tuple with a name. The namedtuple class is located in the collections module. After having namedtuple, accessing data through attributes can make our code more intuitive and better maintained.
    namedtuple can be used to create a data type similar to the ancestor. In addition to using indexes to access data, it can iterate, and it can also easily access data through attribute names.
    When defining a namedtuple, the first parameter is the name of the tuple, which is very similar to the class name in our custom class, and the second parameter is a string separated by spaces (or a list of strings), representing The 4 fields in the tuple are equivalent to the 4 attributes in the class. The initialization method is the same as the instance object of the class.
from collections import namedtuple
 
Friend=namedtuple("Friend",['name','age','email'])

f1=Friend('xiaowang',33,'[email protected]')
print(f1)
print(f1.age)
print(f1.email)
print(f1[0])

f2=Friend(name='xiaozhang',email='[email protected]',age=30)
print(f2)
name,age,email=f2
print(name,age,email)
[root@localhost demo01]# python3 text1124.py 
Friend(name='xiaowang', age=33, email='[email protected]')
33
xiaowang@163.com
xiaowang
Friend(name='xiaozhang', age=30, email='[email protected]')
xiaozhang 30 xiaozhang@sina.com

  1. The parameter types of python functions can be divided into: mandatory parameters, default parameters, variable parameters (variable length parameters), keyword parameters, combined parameters, a total of 5 types.
print(round(1.23456, 2))
print(round(1627731, -1))
print(round(6,-1))
print(round(5,-1))
print(round(4,-1))
print(round(80,-2))
print(round(40,-2))
[root@localhost demo01]# python3 text1124.py 
1.23
1627730
10
0
0
100
0

x = 10
a = lambda y: x+y
x=20
b=lambda y: x+y

print(a(10)) #1
print(b(10)) #2
[root@localhost demo01]# python3 text1124.py 
30
30

Lambda parameters are bound to values ​​at runtime, not at the time of definition. This is different from the default value parameter definition of a function. Introduction to Python lambda

prices = {
    
    
    'a': 45.23,
    'b': 612.78,
    'c': 205.55,
    'd': 10.75
}
min_prcice = min(zip(prices.values(), prices.keys()))
max_prcice = max(zip(prices.values(), prices.keys()))
prices_sorted = sorted(zip(prices.values(), prices.keys()))
print(min_prcice)
print(max_prcice)
print(prices_sorted)
[root@localhost demo01]# python3 text1124.py 
(10.75, 'd')
(612.78, 'b')
[(10.75, 'd'), (45.23, 'a'), (205.55, 'c'), (612.78, 'b')]

a=[1,5,2,1,9,1,5,10]
setlist = list(set(a))
setlist.sort(key=a.index)
print(setlist)

Note: If the two dictionaries are added without considering the same key, d1.update(d2) can be used to merge easily, but the value in the following dictionary will overwrite the value in dictionary d1.

for key, value in b.items():
    if key in a:
        a[key] += value
    else:
        a[key] = value
print(a)
  1. Find the common line of the two files:
alines = open('a.txt','r').readlines()
fw = open('c.txt','w')
for line in open('b.txt','r'):
    for al in alines:
        if al[:-1] in line:
            fw.write(line)
 
fw.close()

Guess you like

Origin blog.csdn.net/qq_34663267/article/details/109363161