久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Python 找出出現(xiàn)次數(shù)超過數(shù)組長度一半的元素實(shí)例

瀏覽:79日期:2022-07-26 08:48:05

利用問題的普遍性和特殊性來求解,

代碼如下:

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ù)超過數(shù)組長度一半的元素 普遍性解法針對任何次數(shù)的統(tǒng)計(jì)均適用而不光只是針對出現(xiàn)次數(shù)超過數(shù)組長度一半的情況 ''' _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ù)都超過數(shù)組長度一半就會得出兩個(gè)元素出現(xiàn)的次數(shù)超出了數(shù)組長度的矛盾結(jié)果——所以超過數(shù)組長度一半的元素是唯一的 排序后在數(shù)組中間的一定是目標(biāo)解 特殊性解法只能針對元素出現(xiàn)次數(shù)超過數(shù)組長度一半的情況 ''' _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ù)超過數(shù)組長度一半的元素 suite = unittest.TestSuite() suite.addTest(GetFreqNumbersFromList(’test_normal_solution’)) suite.addTest(GetFreqNumbersFromList(’test_specific_solution’)) runner = unittest.TextTestRunner() runner.run(suite)

測試結(jié)果:

Python 找出出現(xiàn)次數(shù)超過數(shù)組長度一半的元素實(shí)例

補(bǔ)充知識:Python 用積分思想計(jì)算圓周率

早上起來突然想求圓周率,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ù)超過數(shù)組長度一半的元素實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 新巴尔虎右旗| 泸西县| 巴南区| 太谷县| 潮安县| 德庆县| 克山县| 佳木斯市| 高台县| 孝感市| 徐闻县| 金湖县| 大方县| 陇南市| 黑龙江省| 黔南| 长岛县| 工布江达县| 紫金县| 扎兰屯市| 开平市| 蒙自县| 巴彦县| 南昌县| 西林县| 册亨县| 府谷县| 拉孜县| 房山区| 正蓝旗| 故城县| 蒙阴县| 徐汇区| 永吉县| 东光县| 北川| 永福县| 石楼县| 静海县| 大英县| 安庆市|