Python出出现次数超过数组长度⼀半的元素实例
利⽤问题的普遍性和特殊性来求解,
代码如下:
importunittest
fromdatetime importdatetime
classGetFreqNumbersFromList(unittest.TestCase):
defsetUp(self):
print("\n")
self.start_time =w()
print(f"{self._testMethodName} start: {self.start_time}")
deftearDown(self):
print(f"{self._testMethodName} end: {d_time}")
exec_time =(d_time -self.start_time).microseconds
print(f"{self._testMethodName} exec_time: {exec_time}")
defnormal_solution(self, _list, _debug=False):
"""
普遍性解法
利⽤字典记录每个元素出现的次数——然后出元素出现次数超过数组长度⼀半的元素
普遍性解法针对任何次数的统计均适⽤⽽不光只是针对出现次数超过数组长度⼀半的情况
"""
_target =len(_list) //2
_dict ={}
for_member in_list:
if_member notin_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
defspecific_solution(self, _list, _debug=False):
"""
特殊性解法
假设有两个元素出现的次数都超过数组长度⼀半就会得出两个元素出现的次数超出了数组长度的⽭盾结果——所以超过数组长度⼀半的元素是唯⼀的
排序后在数组中间的⼀定是⽬标解
特殊性解法只能针对元素出现次数超过数组长度⼀半的情况
"""
_list.sort()
if_debug:
print(_list[len(_list) //2])
return_list[len(_list) //2]
deftest_normal_solution(self):
actual_result =al_solution([2,2,2,2,2,2,1,1,1,1,1], False)
self.assertEqual(actual_result[0], 2)
deftest_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__":
# 出出现次数超过数组长度⼀半的元素
suite =unittest.TestSuite()
suite.addTest(GetFreqNumbersFromList('test_normal_solution'))
suite.addTest(GetFreqNumbersFromList('test_specific_solution'))
runner =unittest.TextTestRunner()
runner.run(suite)
测试结果:
补充知识:Python ⽤积分思想计算圆周率
早上起来突然想求圆周率,1单位时圆的⾯积。
代码如下:
frommath importpow, sqrt
defcalc_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
whilex_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)
运⾏结果接近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 出出现次数超过数组长度⼀半的元素实例就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
利⽤问题的普遍性和特殊性来求解,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60import unittest
from datetime import datetime
class GetFreqNumbersFromList(unittest.TestCase):
def setUp(self):
print("\n")
self.start_time =w()
print(f"{self._testMethodName} start: {self.start_time}")
def tearDown(self):
print(f"{self._testMethodName} end: {d_time}")
exec_time =(d_time -self.start_time).microseconds
print(f"{self._testMethodName} exec_time: {exec_time}")
def normal_solution(self, _list, _debug=False):
"""
普遍性解法
利⽤字典记录每个元素出现的次数——然后出元素出现次数超过数组长度⼀半的元素
普遍性解法针对任何次数的统计均适⽤⽽不光只是针对出现次数超过数组长度⼀半的情况
"""
python获取数组长度_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):
"""
特殊性解法
假设有两个元素出现的次数都超过数组长度⼀半就会得出两个元素出现的次数超出了数组长度的⽭盾结果——所以超过数组长度⼀半的元素是唯⼀的排序后在数组中间的⼀定是⽬标解
特殊性解法只能针对元素出现次数超过数组长度⼀半的情况
"""
_list.sort()
if_debug:
print(_list[len(_list) //2])
return_list[len(_list) //2]
测试结果:
补充知识:Python ⽤积分思想计算圆周率
早上起来突然想求圆周率,1单位时圆的⾯积。
代码如下:
运⾏结果接近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 出出现次数超过数组长度⼀半的元素实例就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持脚本之家。
12345678910111213141516from  math import  pow , sqrt  def  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)