面试题汇总(七)
1.这个程序的出的结果是
class Singleton:
_instance = None
def __new__(cls,*args,**kwargs):
print('New')
if cls._instance is None:
print('Create')
cls._instance = super().__new__(cls,*args,**kwargs)
return cls._instance
def __init__(self):
print('Initialize')
self.prop = None
s1 = Singleton()
s2 = Singleton()
print(s1)
print(s2)
答案是:
New
Create
Initialize
New
Initialize
<__main__.Singleton object at 0x00000202F3E70CC0>
<__main__.Singleton object at 0x00000202F3E70CC0>
python自带的数据类型有哪些?
答: 列表(list) 字符串(str) 元祖(tuple) 字典(dict) 集合(set) 布尔值(bool)
请用1行python代码实现1-1000之间整数的求和
print(sum(range(1,1001)))
请使用python递归实现n的阶乘
def myfac(n):
if n==1:
return 1
else:
return myfac(n-1)*n
print(myfac(5))
请使用python实现冒泡排序
bubble_sort(alist):
for j in range(len(alist) - 1, 0, -1):
# j表示每次遍历需要比较的次数,是逐渐减小的
for i in range(j):
if alist[i] > alist[i + 1]:
alist[i], alist[i + 1] = alist[i + 1], alist[i]
li = [54, 26, 93, 17, 77, 31, 44, 55, 20]
bubble_sort(li)
print(li)
如何在linux下用一条命令,批量将某个目录下所有文件中指定字符串A(str A)转换为字符串B(str B)
不会
现有学生信息表student(字段项为stuld,stuName,class)和教师信息表teacher(字段项为teald,teaName,class),现虚查询每名教师(teaName)所教班级(class)数目及每个班级(class)中学生的数目,查询结果包含教师姓名(teaName),班级(class)和对应班级学生数目,请用一条sql语句实现
不会
请给出下短代码的执行结果
class B(object):
def fn(self):
print(B,'fn')
def __init__(self):
print(B,'INIT')
class A(object):
def fn(self):
print(A,'fn')
def __new__(cls,a):
print(a,'NEW')
if a > 10:
return super(A,cls).__new__(cls)
return B()
a1 = A(5)
a1.fn()
a2 = A(20)
a2.fn()
print(a1)
print(a2)
答案是
5 NEW
<class '__main__.B'> INIT
<class '__main__.B'> fn
20 NEW
<class '__main__.A'> fn
<__main__.B object at 0x000001DF8A5A7128>
<__main__.A object at 0x000001DF8A58C748>
一个父类和子类,有初始化函数,有销毁函数,有子类继承父类的函数即可
class Father(object):
def __init__(self, age):
self.age = age
print("age: %d" % (self.age))
def getAge(self):
print('父类的返回结果')
return self.age
class Son(Father):
def getAge(self):
print('子类的返回结果:')
return self.age
if __name__ == '__main__':
son = Son(18)
print(son.getAge())
答案是:
age: 18
子类的返回结果:18
班级表class (id,name) 学生表 student (id,name,classid,score)
(1).写sql语句建表
(2)写sql语句插入2个学生,2个班级
(3)写sql语句,分组查询每个班级的平均分数,并按降序排序,输出,班级编号,班级名,平均分数
python自带的数据类型有哪些?
list, string, dict, tuple, set, boolean
请用一行python代码实现1--1000之间整数的求和.
print(sum(range(1001)))
请使用python递归实现n的阶乘.
def f(x):
if x == 1:
return 1
else:
return f(x-1)*x
a = input(‘请输入一个大于0的整数:’)
print(f(a))
请用python实现冒泡排序
def bubble_sort(alist):
for j in range(len(alist)-1,0,-1):
for i in range(j):
if alist[i] > alist[i+1]:
alist[i], alist[i+1] = alist[i+1], alist[i]
li = [54,26,93,17,77,31,44,55,20]
bubble_sort(li)
print(li)
请给出下面代码的执行结果
new 5
B init
B fn
new 20
init 20
A fn
如何在linux下用一条命令,批量将某目录下所有文件中指定字符串 A替换为字符串B?
sed -i "s/A/B/g" `grep A -rl D` #百度的 看不懂
只能看清map 用法
def a(x):
return x**2
print(map(a,[1,2,3,4]))
#返回结果[1,4,9,16]
写一个父类和子类,有初始化函数,有销毁函数有子类继承父类的函数即可
class Father():
def __init__(self):
return ‘fathre’
def __del__(self):
return ‘father已经销毁’
class Son(Fathre):
def __init__(self):
return ‘son’
def __del__(self):
return ‘son已经销毁’
班级表 class(id,name)学生表student(id,name,class_id,score)
①写sql语句建表
②写sql语句加入2班级,2学生
③写sql语句,分组查询出每个班级的平均分数,并按降序排序,输出,班级编号,班级名,平均分数
①:CREATE TABLE IF NOT EXISTS class(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(50)
)
CREATE TABLE student (
id VARBINARY(20),
NAME VARCHAR(50),
class_id INT,
score DOUBLE
)
②:INSERT INTO class(NAME) VALUES('1803a');
INSERT INTO class(NAME) VALUES('1804a');
INSERT INTO student(id,NAME,class_id,score) VALUES('1001','张三',1,88.0);
INSERT INTO student(id,NAME,class_id,score) VALUES('1001','李四',1,99.0);
③:SELECT s.class_id,c.`name`,s.sco
FROM(
SELECT class_id,AVG(score) sco
FROM student
GROUP BY class_id
ORDER BY sco DESC)s JOIN class c
ON s.`class_id`=c.`id`