面試題彙總(七)

 

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語句建表python

②寫sql語句加入2班級,2學生linux

③寫sql語句,分組查詢出每一個班級的平均分數,並按降序排序,輸出,班級編號,班級名,平均分數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`
相關文章
相關標籤/搜索