Python 找出出現(xiàn)次數(shù)超過(guò)數(shù)組長(zhǎng)度一半的元素實(shí)例
利用問(wèn)題的普遍性和特殊性來(lái)求解,
代碼如下:
import unittestfrom datetime import datetimeclass GetFreqNumbersFromList(unittest.TestCase): def setUp(self): print('n') self.start_time = datetime.now() print(f'{self._testMethodName} start: {self.start_time}') def tearDown(self): self.end_time = datetime.now() print(f'{self._testMethodName} end: {self.end_time}') exec_time = (self.end_time - self.start_time).microseconds print(f'{self._testMethodName} exec_time: {exec_time}') def normal_solution(self, _list, _debug=False): ''' 普遍性解法 利用字典記錄每個(gè)元素出現(xiàn)的次數(shù)——然后找出元素出現(xiàn)次數(shù)超過(guò)數(shù)組長(zhǎng)度一半的元素 普遍性解法針對(duì)任何次數(shù)的統(tǒng)計(jì)均適用而不光只是針對(duì)出現(xiàn)次數(shù)超過(guò)數(shù)組長(zhǎng)度一半的情況 ''' _target = len(_list) // 2 _dict = {} for _member in _list: if _member not in _dict: _dict.setdefault(_member, 1) else: _dict[_member] += 1 _ret = [_member for _member in _dict if _dict[_member] > _target] if _debug: print(_ret) return _ret def specific_solution(self, _list, _debug=False): ''' 特殊性解法 假設(shè)有兩個(gè)元素出現(xiàn)的次數(shù)都超過(guò)數(shù)組長(zhǎng)度一半就會(huì)得出兩個(gè)元素出現(xiàn)的次數(shù)超出了數(shù)組長(zhǎng)度的矛盾結(jié)果——所以超過(guò)數(shù)組長(zhǎng)度一半的元素是唯一的 排序后在數(shù)組中間的一定是目標(biāo)解 特殊性解法只能針對(duì)元素出現(xiàn)次數(shù)超過(guò)數(shù)組長(zhǎng)度一半的情況 ''' _list.sort() if _debug: print(_list[len(_list) // 2]) return _list[len(_list) // 2] def test_normal_solution(self): actual_result = self.normal_solution([2,2,2,2,2,2,1,1,1,1,1], False) self.assertEqual(actual_result[0], 2) def test_specific_solution(self): actual_result = self.specific_solution([2,2,2,2,2,2,1,1,1,1,1], False) self.assertEqual(actual_result, 2)if __name__ == '__main__': # 找出出現(xiàn)次數(shù)超過(guò)數(shù)組長(zhǎng)度一半的元素 suite = unittest.TestSuite() suite.addTest(GetFreqNumbersFromList(’test_normal_solution’)) suite.addTest(GetFreqNumbersFromList(’test_specific_solution’)) runner = unittest.TextTestRunner() runner.run(suite)
測(cè)試結(jié)果:
補(bǔ)充知識(shí):Python 用積分思想計(jì)算圓周率
早上起來(lái)突然想求圓周率,1單位時(shí)圓的面積。
代碼如下:
from math import pow, sqrtdef calc_circle_s_with(r, dy, x_slices): x_from_start_to_cc = sqrt(1 - pow(dy, 2)) dx = x_from_start_to_cc / x_slices x_to_edge = 1 - x_from_start_to_cc quarter_circle_s = 0 while x_to_edge < 1: rect_s = dy * dx quarter_circle_s += rect_s x_to_edge = x_to_edge + dx dy = sqrt(1 - pow((1 - x_to_edge), 2)) circle_s = 4 * quarter_circle_s print(circle_s)calc_circle_s_with(1, 0.0001, 10000000)
運(yùn)行結(jié)果接近3.1415926,dy傳的越小,x_slices傳的越大,就越接近。
半徑為:1
初始小矩形到圓周的距離:1 - x_from_start_to_cc
其中dy代表四分之一圓中初始小矩形的高度,x_slices代表小矩形的寬度:(1 - x_from_start_to_cc) / x_slices
四分之一圓的面積積分為:quarter_circle_s
以上這篇Python 找出出現(xiàn)次數(shù)超過(guò)數(shù)組長(zhǎng)度一半的元素實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 利用CSS3新特性創(chuàng)建透明邊框三角2. html清除浮動(dòng)的6種方法示例3. CSS代碼檢查工具stylelint的使用方法詳解4. Vue3使用JSX的方法實(shí)例(筆記自用)5. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程6. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. 使用css實(shí)現(xiàn)全兼容tooltip提示框9. JavaScript數(shù)據(jù)類(lèi)型對(duì)函數(shù)式編程的影響示例解析10. 不要在HTML中濫用div
