高德軟件有限公司python試題 及 答案
本文地址: http://blog.csdn.net/caroline_wendy/article/details/25230835
by Spike 2014.5.7
本題目僅供學術交流, 嚴禁用于其他目的, 答案僅供參考.
1. 在python中, list, tuple, dict, set有什么區(qū)別, 主要應用在什么樣的場景?
解答:
定義:
list: 鏈表, 有序的項目, 通過索引進行查找, 使用方括號"[]";
tuple: 元組, 元組將多樣的對象集合到一起, 不能修改, 通過索引進行查找, 使用括號"()";
dict: 字典, 字典是一組鍵(key)和值(value)的組合, 通過鍵(key)進行查找, 沒有順序, 使用大括號"{}";
set: 集合,無序, 元素只出現(xiàn)一次, 自動去重, 使用"set([])";
應用場景:
list, 簡單的數(shù)據(jù)集合, 可以使用索引;
tuple, 把一些數(shù)據(jù)當做一個整體去使用, 不能修改;
dict, 使用鍵值和值進行關聯(lián)的數(shù)據(jù);
set, 數(shù)據(jù)只出現(xiàn)一次, 只關心數(shù)據(jù)是否出現(xiàn), 不關心其位置;
代碼:
- mylist = [1, 2, 3, 4, 'Oh']
- mytuple = (1, 2, 'Hello', (4, 5))
- mydict = {'Wang' : 1, 'Hu' : 2, 'Liu' : 4}
- myset = set(['Wang', 'Hu', 'Liu', 4, 'Wang'])
2. 靜態(tài)函數(shù), 類函數(shù), 成員函數(shù)的區(qū)別?
解答:
定義:
靜態(tài)函數(shù)(@staticmethod): 即靜態(tài)方法,主要處理與這個類的邏輯關聯(lián);
類函數(shù)(@classmethod): 即類方法, 更關注于從類中調(diào)用方法, 而不是在實例中調(diào)用方法, 可以用作方法重載, 傳入?yún)?shù)cls;
成員函數(shù): 實例的方法, 只能通過實例進行調(diào)用;
具體應用:
日期的方法, 可以通過實例化(__init__)進行數(shù)據(jù)輸出, 傳入?yún)?shù)self;
可以通過類的方法(@classmethod)進行數(shù)據(jù)轉(zhuǎn)換, 傳入?yún)?shù)cls;
可以通過靜態(tài)方法(@staticmethod)進行數(shù)據(jù)驗證;
代碼:
- # -*- coding: utf-8 -*-
-
-
- #eclipse pydev, python 3.3
- #by C.L.Wang
-
-
- class Date(object):
-
-
- day = 0
- month = 0
- year = 0
-
-
- def __init__(self, day=0, month=0, year=0):
- self.day = day
- self.month = month
- self.year = year
-
- def display(self):
- return "{0}*{1}*{2}".format(self.day, self.month, self.year)
-
- @classmethod
- def from_string(cls, date_as_string):
- day, month, year = map(int, date_as_string.split('-'))
- date1 = cls(day, month, year)
- return date1
-
- @staticmethod
- def is_date_valid(date_as_string):
- day, month, year = map(int, date_as_string.split('-'))
- return day <= 31 and month <= 12 and year <= 3999
-
- date1 = Date('12', '11', '2014')
- date2 = Date.from_string('11-13-2014')
- print(date1.display())
- print(date2.display())
- print(date2.is_date_valid('11-13-2014'))
- print(Date.is_date_valid('11-13-2014'))
3. a=1, b=2, 不用中間變量交換a和b的值
解答:
兩種形式: 加法或異或
代碼:
- a = 1
- b = 2
-
-
- a = a + b
- b = a - b
- a = a - b
- print ('a = {0}, b = {1}'.format(a, b))
-
-
- a = a ^ b
- b = a ^ b
- a = a ^ b
- print ('a = {0}, b = {1}'.format(a, b))
4. 寫一個函數(shù), 輸入一個字符串, 返回倒序排列的結(jié)果: 如: string_reverse(‘a(chǎn)bcdef’), 返回: ‘fedcba’
(請采用多種方法實現(xiàn), 并對實現(xiàn)方法進行比較)
解答:
5種方法的比較:
1. 簡單的步長為-1, 即字符串的翻轉(zhuǎn);
2. 交換前后字母的位置;
3. 遞歸的方式, 每次輸出一個字符;
4. 雙端隊列, 使用extendleft()函數(shù);
5. 使用for循環(huán), 從左至右輸出;
代碼:
- string = 'abcdef'
-
-
- def string_reverse1(string):
- return string[::-1]
-
-
- def string_reverse2(string):
- t = list(string)
- l = len(t)
- for i,j in zip(range(l-1, 0, -1), range(l//2)):
- t[i], t[j] = t[j], t[i]
- return "".join(t)
-
-
- def string_reverse3(string):
- if len(string) <= 1:
- return string
- return string_reverse3(string[1:]) + string[0]
-
-
- from collections import deque
- def string_reverse4(string):
- d = deque()
- d.extendleft(string)
- return ''.join(d)
-
-
- def string_reverse5(string):
- #return ''.join(string[len(string) - i] for i in range(1, len(string)+1))
- return ''.join(string[i] for i in range(len(string)-1, -1, -1))
-
-
- print(string_reverse1(string))
- print(string_reverse2(string))
- print(string_reverse3(string))
- print(string_reverse4(string))
- print(string_reverse5(string))
5. 請用自己的算法, 按升序合并如下兩個list, 并去除重復的元素:
list1 = [2, 3, 8, 4, 9, 5, 6]
list2 = [5, 6, 10, 17, 11, 2]
解答:
合并鏈表, 遞歸的快速排序, 去重鏈接;
代碼:
- import random
-
-
- list1 = [2, 3, 8, 4, 9, 5, 6]
- list2 = [5, 6, 10, 17, 11, 2]
-
-
- def qsort(L):
- if len(L)<2: return L
- pivot_element = random.choice(L)
- small = [i for i in L if i< pivot_element]
- large = [i for i in L if i> pivot_element]
- return qsort(small) + [pivot_element] + qsort(large)
-
-
- def merge(list1, list2):
- return qsort(list1 + list2)
-
-
- print(merge(list1, list2))
注: 如果使用set方法, list(set(list1 + list2)), 即可.
6. 請寫出打印結(jié)果
x = [0, 1]
i = 0
i, x[i] = 1, 2
print(x)
打印結(jié)果: [0, 2], python可以使用連續(xù)賦值, 從左至右.
g = lambda x, y=2, z : x + y**z
g(1, z=10) = ?
打印結(jié)果: 異常, 形參表末尾才可以有默認參數(shù), z需要提供默認參數(shù).
7. 說一下以下代碼片段存在的問題
- from amodule import * # amodule is an exist module
-
- class dummyclass(object):
- def __init__(self):
- self.is_d = True
- pass
-
- class childdummyclass(dummyclass):
- def __init__(self, isman):
- self.isman = isman
-
- @classmethod
- def can_speak(self): return True
-
- @property
- def man(self): return self.isman
-
- if __name__ == "__main__":
- object = new childdummyclass(True)
- print object.can_speak()
- print object.man()
- print object.is_d
解答: 1. 警告: object是python新形式(new style)的一個基礎類, 不應該被重新定義;
2. 警告: 類方法(classmethod)是類所擁有的方法, 傳入的參數(shù)應該是cls, 而不是self;
3. 錯誤: Python沒有new關鍵字, 如需修改new, 如單例模式, 可以重寫(override)__new__;
4. 錯誤: @property, 表示屬性, 不是方法, 則不需要加括號”()”, 直接調(diào)用object.man, 即可;
5. 錯誤: 如果想使用基類的成員, 則需要初始化基類, 如dummyclass.__init__(self), 即可;
6. 額外: 類名盡量使用大寫.
代碼:
- class dummyclass(object):
- def __init__(self):
- self.is_d = True
- pass
-
- class childdummyclass(dummyclass):
- def __init__(self, isman):
- dummyclass.__init__(self) #__init__
- self.isman = isman
-
- @classmethod
- def can_speak(cls): return True #cls
-
- @property
- def man(self): return self.isman
-
- if __name__ == "__main__":
- o = childdummyclass(True) #new, object
- print o.can_speak()
- print o.man #property
- print o.is_d
8. 介紹一下python的異常處理機制和自己開發(fā)過程中的體會
解答:
Python的異常處理機制:
try: 嘗試拋出異常;
raise: 引發(fā)異常;
except: 處理異常;
finally: 是否發(fā)生異常都需要做的事情;
創(chuàng)建新的異常類型, 需要繼承Exception類, 可以定義類的屬性, 便于處理異常;
開發(fā)體會:
異常主要處理讀取文件, 也可以使用with的方法讀取文件; 還可以用于網(wǎng)絡連接, 異常可以包含大量的錯誤信息, 進行錯誤處理.
代碼:
- class ShortInputException(Exception):
- def __init__(self, length, atleast):
- Exception.__init__(self)
- self.length = length
- self.atleast = atleast
-
- while True:
- try:
- text = raw_input('Enter somthing-->')
- if len(text) < 3:
- raise ShortInputException(len(text), 3)
- except EOFError:
- print('Why did you do an EOF on me')
- except ShortInputException as ex:
- print('ShortInputException The input was {0} long, \
- excepted at least {1}. '.format(ex.length, ex.atleast))
- else:
- print('No exception was raised. ')
- finally:
- print('Over')
高德軟件有限公司python試題 及 答案
本文地址: http://blog.csdn.net/caroline_wendy/article/details/25230835
by Spike 2014.5.7
本題目僅供學術交流, 嚴禁用于其他目的, 答案僅供參考.
1. 在python中, list, tuple, dict, set有什么區(qū)別, 主要應用在什么樣的場景?
解答:
定義:
list: 鏈表, 有序的項目, 通過索引進行查找, 使用方括號"[]";
tuple: 元組, 元組將多樣的對象集合到一起, 不能修改, 通過索引進行查找, 使用括號"()";
dict: 字典, 字典是一組鍵(key)和值(value)的組合, 通過鍵(key)進行查找, 沒有順序, 使用大括號"{}";
set: 集合,無序, 元素只出現(xiàn)一次, 自動去重, 使用"set([])";
應用場景:
list, 簡單的數(shù)據(jù)集合, 可以使用索引;
tuple, 把一些數(shù)據(jù)當做一個整體去使用, 不能修改;
dict, 使用鍵值和值進行關聯(lián)的數(shù)據(jù);
set, 數(shù)據(jù)只出現(xiàn)一次, 只關心數(shù)據(jù)是否出現(xiàn), 不關心其位置;
代碼:
- mylist = [1, 2, 3, 4, 'Oh']
- mytuple = (1, 2, 'Hello', (4, 5))
- mydict = {'Wang' : 1, 'Hu' : 2, 'Liu' : 4}
- myset = set(['Wang', 'Hu', 'Liu', 4, 'Wang'])
2. 靜態(tài)函數(shù), 類函數(shù), 成員函數(shù)的區(qū)別?
解答:
定義:
靜態(tài)函數(shù)(@staticmethod): 即靜態(tài)方法,主要處理與這個類的邏輯關聯(lián);
類函數(shù)(@classmethod): 即類方法, 更關注于從類中調(diào)用方法, 而不是在實例中調(diào)用方法, 可以用作方法重載, 傳入?yún)?shù)cls;
成員函數(shù): 實例的方法, 只能通過實例進行調(diào)用;
具體應用:
日期的方法, 可以通過實例化(__init__)進行數(shù)據(jù)輸出, 傳入?yún)?shù)self;
可以通過類的方法(@classmethod)進行數(shù)據(jù)轉(zhuǎn)換, 傳入?yún)?shù)cls;
可以通過靜態(tài)方法(@staticmethod)進行數(shù)據(jù)驗證;
代碼:
- # -*- coding: utf-8 -*-
-
-
- #eclipse pydev, python 3.3
- #by C.L.Wang
-
-
- class Date(object):
-
-
- day = 0
- month = 0
- year = 0
-
-
- def __init__(self, day=0, month=0, year=0):
- self.day = day
- self.month = month
- self.year = year
-
- def display(self):
- return "{0}*{1}*{2}".format(self.day, self.month, self.year)
-
- @classmethod
- def from_string(cls, date_as_string):
- day, month, year = map(int, date_as_string.split('-'))
- date1 = cls(day, month, year)
- return date1
-
- @staticmethod
- def is_date_valid(date_as_string):
- day, month, year = map(int, date_as_string.split('-'))
- return day <= 31 and month <= 12 and year <= 3999
-
- date1 = Date('12', '11', '2014')
- date2 = Date.from_string('11-13-2014')
- print(date1.display())
- print(date2.display())
- print(date2.is_date_valid('11-13-2014'))
- print(Date.is_date_valid('11-13-2014'))
3. a=1, b=2, 不用中間變量交換a和b的值
解答:
兩種形式: 加法或異或
代碼:
- a = 1
- b = 2
-
-
- a = a + b
- b = a - b
- a = a - b
- print ('a = {0}, b = {1}'.format(a, b))
-
-
- a = a ^ b
- b = a ^ b
- a = a ^ b
- print ('a = {0}, b = {1}'.format(a, b))
4. 寫一個函數(shù), 輸入一個字符串, 返回倒序排列的結(jié)果: 如: string_reverse(‘a(chǎn)bcdef’), 返回: ‘fedcba’
(請采用多種方法實現(xiàn), 并對實現(xiàn)方法進行比較)
解答:
5種方法的比較:
1. 簡單的步長為-1, 即字符串的翻轉(zhuǎn);
2. 交換前后字母的位置;
3. 遞歸的方式, 每次輸出一個字符;
4. 雙端隊列, 使用extendleft()函數(shù);
5. 使用for循環(huán), 從左至右輸出;
代碼:
- string = 'abcdef'
-
-
- def string_reverse1(string):
- return string[::-1]
-
-
- def string_reverse2(string):
- t = list(string)
- l = len(t)
- for i,j in zip(range(l-1, 0, -1), range(l//2)):
- t[i], t[j] = t[j], t[i]
- return "".join(t)
-
-
- def string_reverse3(string):
- if len(string) <= 1:
- return string
- return string_reverse3(string[1:]) + string[0]
-
-
- from collections import deque
- def string_reverse4(string):
- d = deque()
- d.extendleft(string)
- return ''.join(d)
-
-
- def string_reverse5(string):
- #return ''.join(string[len(string) - i] for i in range(1, len(string)+1))
- return ''.join(string[i] for i in range(len(string)-1, -1, -1))
-
-
- print(string_reverse1(string))
- print(string_reverse2(string))
- print(string_reverse3(string))
- print(string_reverse4(string))
- print(string_reverse5(string))
5. 請用自己的算法, 按升序合并如下兩個list, 并去除重復的元素:
list1 = [2, 3, 8, 4, 9, 5, 6]
list2 = [5, 6, 10, 17, 11, 2]
解答:
合并鏈表, 遞歸的快速排序, 去重鏈接;
代碼:
- import random
-
-
- list1 = [2, 3, 8, 4, 9, 5, 6]
- list2 = [5, 6, 10, 17, 11, 2]
-
-
- def qsort(L):
- if len(L)<2: return L
- pivot_element = random.choice(L)
- small = [i for i in L if i< pivot_element]
- large = [i for i in L if i> pivot_element]
- return qsort(small) + [pivot_element] + qsort(large)
-
-
- def merge(list1, list2):
- return qsort(list1 + list2)
-
-
- print(merge(list1, list2))
注: 如果使用set方法, list(set(list1 + list2)), 即可.
6. 請寫出打印結(jié)果
x = [0, 1]
i = 0
i, x[i] = 1, 2
print(x)
打印結(jié)果: [0, 2], python可以使用連續(xù)賦值, 從左至右.
g = lambda x, y=2, z : x + y**z
g(1, z=10) = ?
打印結(jié)果: 異常, 形參表末尾才可以有默認參數(shù), z需要提供默認參數(shù).
7. 說一下以下代碼片段存在的問題
- from amodule import * # amodule is an exist module
-
- class dummyclass(object):
- def __init__(self):
- self.is_d = True
- pass
-
- class childdummyclass(dummyclass):
- def __init__(self, isman):
- self.isman = isman
-
- @classmethod
- def can_speak(self): return True
-
- @property
- def man(self): return self.isman
-
- if __name__ == "__main__":
- object = new childdummyclass(True)
- print object.can_speak()
- print object.man()
- print object.is_d
解答: 1. 警告: object是python新形式(new style)的一個基礎類, 不應該被重新定義;
2. 警告: 類方法(classmethod)是類所擁有的方法, 傳入的參數(shù)應該是cls, 而不是self;
3. 錯誤: Python沒有new關鍵字, 如需修改new, 如單例模式, 可以重寫(override)__new__;
4. 錯誤: @property, 表示屬性, 不是方法, 則不需要加括號”()”, 直接調(diào)用object.man, 即可;
5. 錯誤: 如果想使用基類的成員, 則需要初始化基類, 如dummyclass.__init__(self), 即可;
6. 額外: 類名盡量使用大寫.
代碼:
- class dummyclass(object):
- def __init__(self):
- self.is_d = True
- pass
-
- class childdummyclass(dummyclass):
- def __init__(self, isman):
- dummyclass.__init__(self) #__init__
- self.isman = isman
-
- @classmethod
- def can_speak(cls): return True #cls
-
- @property
- def man(self): return self.isman
-
- if __name__ == "__main__":
- o = childdummyclass(True) #new, object
- print o.can_speak()
- print o.man #property
- print o.is_d
8. 介紹一下python的異常處理機制和自己開發(fā)過程中的體會
解答:
Python的異常處理機制:
try: 嘗試拋出異常;
raise: 引發(fā)異常;
except: 處理異常;
finally: 是否發(fā)生異常都需要做的事情;
創(chuàng)建新的異常類型, 需要繼承Exception類, 可以定義類的屬性, 便于處理異常;
開發(fā)體會:
異常主要處理讀取文件, 也可以使用with的方法讀取文件; 還可以用于網(wǎng)絡連接, 異常可以包含大量的錯誤信息, 進行錯誤處理.
代碼:
- class ShortInputException(Exception):
- def __init__(self, length, atleast):
- Exception.__init__(self)
- self.length = length
- self.atleast = atleast
-
- while True:
- try:
- text = raw_input('Enter somthing-->')
- if len(text) < 3:
- raise ShortInputException(len(text), 3)
- except EOFError:
- print('Why did you do an EOF on me')
- except ShortInputException as ex:
- print('ShortInputException The input was {0} long, \
- excepted at least {1}. '.format(ex.length, ex.atleast))
- else:
- print('No exception was raised. ')
- finally:
- print('Over')