码迷,mamicode.com
首页 > 编程语言 > 详细

python kayb算法之从一组序列当中获取一组与目标值最接近的算法

时间:2018-06-30 23:44:28      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:img   需要   spl   one   als   class   ccf   gif   value   

python  kayb算法之从一组序列当中获取一组与目标值最接近的算法

思想:
1.考虑所有序列当中的所有元素相遇的情况进行迭代
2.在迭代过程当中维护一个与目标数值的最小差值
3.在迭代完之后剩余的那一项 即为与目标值最接近的元素序列
注意:
1.如果总和大于目标数需要判断此时大于目标数的和与前一个值总和哪个差值最小
2.考虑初始序列元素与目标值的最小差值
3.考虑序列当中所有元素的总和比目标值小
问题:
1.是否有最小可以接受的范围 可以大大提高时间

技术分享图片
 1 # -*- coding:utf-8 -*-
 2 from pprint import pprint
 3 
 4 def find_almost():
 5     """
 6     查找序列中的近似值
 7     :return:
 8     """
 9     min_diff_value = find_num = 1800000
10     num_list = [420, 423, 424, 460, 491, 491, 633, 634, 634, 636, 859, 872, 928, 985, 1013, 1061, 1064, 1064, 1096, 1106, 1133, 1135, 1144, 1176, 1208, 1218, 1228, 1228, 1246, 1247, 1265, 1270, 1278, 1352, 1378, 1542, 1561, 1570, 1677, 1709, 1777, 2020, 2022, 2126, 2225, 2400, 2486, 2772, 2856, 2867, 2905, 2938, 2944, 3338, 3448, 3464, 3476, 3483, 3817, 4112, 4482, 4482, 4599, 4681, 4686, 4707, 4888, 5185, 5419, 5428, 5447, 5507, 5976, 5977, 6264, 6278, 6315, 6402, 6406, 6631, 6633, 6698, 7014, 7058, 7762, 7792, 7919, 8203, 8203, 8236, 8250, 8406, 8444, 8451, 8460, 8564, 8565, 8575, 8577, 8580, 8598, 8614, 8630, 8669, 8669, 8686, 8704, 9027, 9062, 9065, 9069, 9080, 9082, 9088, 9089, 9095, 9102, 9108, 9133, 9144, 9152, 9154, 9154, 9155, 9160, 9161, 9171, 9173, 9195, 9198, 9229, 9229, 9231, 9247, 9264, 9269, 9298, 9553, 9874, 10018, 10073, 10377, 10485, 10708, 10740, 11166, 11375, 11824, 12353, 12360, 12828, 13059, 13066, 13081, 13136, 13227, 13234, 13244, 13282, 13283, 13527, 13565, 13569, 13576, 13581, 13633, 13640, 13650, 13667, 13681, 13708, 14763, 14998, 15066, 15703, 15748, 15778, 15836, 15906, 16077, 16158, 16278, 16311, 16382, 16453, 16700, 16839, 17046, 17562, 17620, 17856, 17859, 17893, 17908, 17912, 17914, 17914, 17920, 17929, 17931, 17933, 17953, 17973, 17977, 17984, 18000, 18003, 18044, 18063, 18067, 18104, 18107, 18109, 18112, 18118, 18150, 18216, 18226, 18249, 18258, 18260, 18268, 18274, 18277, 18308, 18459, 18581, 18741, 18774, 18789, 18804, 18850, 18851, 18851, 18851, 18854, 18854, 18854, 18854, 18854, 18854, 18957, 18961, 18969, 19212, 19478, 19525, 19537, 19539, 19550, 19552, 19557, 19611, 19688, 19792, 19911, 20256, 20361, 20514, 20532, 20693, 20695, 20742, 21041, 21164, 21813, 21865, 21988, 22065, 22087, 22131, 22172, 22186, 22199, 22222, 22271, 22430, 22566, 22615, 22618, 22977, 23069, 23069, 23316, 23661, 23715, 23734, 23789, 23910, 24012, 24022, 24084, 24427, 24547, 24789, 24819, 25060, 25589, 25871, 25894, 25915, 25915, 26051, 26101, 26105, 26112, 26115, 26117, 26117, 26117, 26119, 26134, 26175, 26215, 26238, 26367, 26490, 26631, 26734, 26845, 26863, 27059, 27061, 27178, 27191, 27205, 27211, 27265, 27396, 27405, 27411, 27489, 27493, 27504, 27513, 27537, 27571, 27704, 27727, 27767, 27884, 27895, 27898, 27905, 27980, 28013, 28020, 28092, 28101, 28117, 28166, 28241, 28244, 28246, 28293, 28389, 28408, 28458, 28473, 28667, 28676, 28800, 28800, 28812, 28850, 28968, 28969, 29145, 29180, 29248, 29271, 29703, 29721, 29736, 29869, 30033, 30103, 30150, 30427, 30555, 30895, 30990, 31095, 31259, 31330, 31514, 31516, 31529, 31529, 31552, 31575, 31576, 31587, 31601, 31606, 31619, 31630, 31650, 31651, 31656, 31660, 31673, 31681, 31702, 31744, 31748, 31752, 31770, 31780, 31801, 31808, 31809, 31873, 31877, 32042, 32184, 32296, 32344, 32370, 32385, 32453, 32527, 32534, 32552, 32562, 32577, 32588, 32588, 32611, 32614, 32619, 32628, 32629, 32633, 32645, 32650, 32651, 32651, 32661, 32661, 32673, 32675, 32675, 32681, 32724, 32740, 32745, 32756, 32777, 32781, 32813, 32877, 32914, 32994, 33105, 33186, 33300, 33357, 33408, 33646, 33649, 33760, 33987, 34008, 34117, 34354, 34386, 34397, 34479, 34518, 34663, 34696, 34766, 34905, 35432, 35564, 35566, 35625, 35810, 35857, 36176, 36240, 36268, 36408, 36446, 36655, 36815, 36858, 37004, 37087, 37143, 37177, 37179, 37274, 37339, 37431, 37477, 37708, 37771, 38152, 38316, 38771, 38805, 38926, 39029, 39072, 39110, 39140, 39390, 39419, 39477, 39573, 40067, 40391, 40487, 40639, 41011, 41135, 41239, 41245, 41317, 41329, 41630, 41957, 41985, 41987, 42020, 42048, 42216, 42279, 42466, 42473, 42642, 42679, 42705, 42781, 43241, 43492, 43635, 43701, 43719, 43892, 43911, 43961, 44096, 44171, 44172, 44199, 44240, 44240, 44242, 44446, 44585, 44633, 44879, 44929, 45150, 45482, 45809, 46303, 46993, 47555, 48150, 48231, 48336, 48424, 48576, 48581, 48596, 48602, 48606, 48645, 48809, 48866, 48930, 48981, 48994, 49261, 49691, 49740, 49771, 49842, 49846, 50057, 50187, 50262, 50338, 50436, 50477, 50956, 51053, 51317, 51410, 51753, 51783, 51949, 52101, 52109, 52133, 52138, 52239, 52294, 52440, 52451, 52499, 52788, 52900, 52987, 52999, 53009, 53023, 53386, 53677, 53830, 54057, 54201, 54262, 54285, 54817, 55063, 55337, 55703, 55922, 56099, 56159, 56163, 56344, 56477, 56486, 56776, 56837, 56925, 57031, 57107, 57309, 57412, 57541, 57670, 57855, 58036, 58058, 58279, 58320, 58359, 58419, 58506, 58668, 58825, 59202, 59295, 59541, 59557, 59974, 60149, 60285, 60330, 60368, 60510, 60530, 60610, 60613, 60642, 60871, 60955, 60959, 61392, 61755, 61779, 61837, 61943, 62034, 62039, 62089, 62190, 62231, 62257, 62496, 62800, 62822, 62859, 62860, 63025, 63209, 63231, 63263, 64322, 64560, 64975, 65020, 65181, 65593, 65956, 66190, 66332, 66403, 66545, 66731, 66823, 66987, 67064, 67299, 67510, 67804, 67896, 68255, 68318, 68419, 68429, 68467, 68484, 68674, 68740, 68837, 69016, 69044, 69203, 69350, 69542, 70286, 70321, 70579, 70704, 70898, 70950, 70988, 71003, 71112, 71114, 71122, 71266, 71294, 71444, 71477, 71630, 71837, 71853, 71993, 72249, 72637, 72691, 72715, 72792, 72894, 72987, 73062, 73348, 73904, 74014, 74347, 74868, 74903, 74938, 75031, 75078, 75428, 76079, 76317, 76425, 76503, 76510, 76651, 76696, 76710, 76750, 76892, 76903, 76955, 76977, 76987, 76987, 76987, 76995, 77057, 77181, 77189, 77336, 77404, 77531, 77594, 77684, 77689, 77838, 77840, 77875, 77876, 77918, 77925, 77933, 78101, 78102, 78142, 78164, 78188, 78253, 78297, 78305, 78328, 78365, 78406, 78779, 78801, 78812, 78861, 78899, 78911, 78976, 78982, 78982, 79031, 79071, 79121, 79162, 79214, 79349, 79499, 79514, 79515, 79517, 79542, 79550, 79552, 79573, 79579, 79582, 79606, 79608, 79621, 79645, 79646, 79649, 79659, 79670, 79674, 79684, 79686, 79694, 79752, 79755, 79764, 79769, 79780, 79781, 79782, 79796, 79802, 79803, 79809, 79810, 79836, 79851, 79881, 79882, 79895, 79915, 79931, 79937, 79939, 79953, 79960, 79970, 79981, 80008, 80013, 80031, 80032, 80034, 80039, 80068, 80071, 80118, 80122, 80194, 80215, 80254, 80293, 80310, 80321, 80368, 80376, 80389, 80421, 80449, 80463, 80477, 80490, 80548, 80584, 80645, 80696, 80720, 80735, 80770, 80915, 80919, 80961, 80974, 81000, 81021, 81024, 81067, 81069, 81089, 81104, 81199, 81215, 81344, 81352, 81391, 81508, 81540, 81547, 81586, 81952, 81967, 82312, 82347, 82533, 82786, 82872, 83088, 83122, 83180, 83447, 83532, 83555, 83596, 83921, 83934, 83940, 84133, 84189, 84257, 84715, 84755, 84846, 84866, 85110, 85129, 85135, 85169, 85223, 85640, 85664, 85935, 85944, 85991, 86098, 86313, 86411, 86485, 86614, 86668, 87101, 87372, 87700, 87786, 87994, 88048, 88281, 88316, 88336, 88373, 88558, 88698, 88709, 88721, 88803, 88806, 89010, 89062, 89147, 89163, 89324, 89454, 89544, 89628, 90050, 90238, 90688, 90776, 90979, 91054, 91134, 91409, 91482, 91597, 91620, 91626, 91667, 91940, 92028, 92133, 92352, 92570, 92708, 92744, 92802, 92807, 92816, 92858, 93318, 93364, 94273, 94417, 94515, 94584, 94950, 95122, 95234, 95643, 95860, 95937, 96005, 96168, 96425, 97040, 97181, 97820, 98052, 98404, 98468, 98475, 98511, 98553, 98656, 98848, 98888, 98996, 98997, 99021, 99190, 99191, 99345, 99384, 99673, 99848, 99987, 100001, 100534, 100741, 100808, 100943, 101215, 101266, 101325, 101568, 101571, 101596, 101606, 101695, 101716, 101835, 101909, 101919, 102076, 102116, 102149, 102187, 102511, 102550, 102790, 102969, 103056, 103190, 103455, 103480, 103564, 103610, 103664, 103824, 103988, 104013, 104045, 104187, 104336, 104391, 104460, 104492, 104533, 104539, 104545, 104657, 104883, 104997, 105043, 105459, 105608, 105612, 105905, 105935, 105981, 106191, 106205, 106253, 106352, 106450, 106504, 106529, 106577, 106579, 107017, 107084, 107131, 107230, 107286, 107328, 107389, 108056, 108188, 108221, 108623, 108641, 109012, 109023, 109042, 109049, 109287, 109403, 109500, 109541, 109603, 110011, 110481, 110668, 110690, 110821, 110906, 111109, 111258, 111408, 111430, 111582, 111584, 111746, 111967, 111977, 112349, 112352, 112382, 112399, 112430, 112444, 112613, 112709, 112815, 112852, 112876, 113005, 113153, 113191, 113267, 113317, 113422, 113488, 113597, 113815, 113866, 113959, 113988, 114076, 114190, 114194, 114434, 114536, 114673, 114755, 114914, 114957, 115004, 115009, 115044, 115090, 115105, 115148, 115288, 115402, 115449, 115518, 115571, 115822, 115847, 115987, 116001, 116031, 116035, 116049, 116099, 116136, 116195, 116198, 116201, 116217, 116362, 116371, 116424, 116455, 116468, 116486, 116736, 116778, 116779, 116853, 116915, 116917, 116936, 116976, 116999, 117012, 117054, 117082, 117269, 117308, 117323, 117352, 117368, 117763, 117922, 117978, 118185, 118271, 118299, 118522, 118663, 118710, 118739, 118782, 118846, 118866, 118920, 118930, 118978, 119030, 119033, 119177, 119236, 119361, 119463, 119653, 119683, 120278, 120426, 120745, 120836, 120904, 120936, 120944, 121132, 121260, 121383, 121640, 121888, 122051, 122065, 122112, 122269, 122313, 122503, 122507, 122841, 122852, 122890, 123129, 123148, 123264, 123275, 123316, 123389, 123613, 123749, 124271, 125081, 125416, 125461, 125582, 125634, 125726, 125780, 125961, 126079, 126102, 126244, 126295, 126312, 126408, 126519, 126924, 127023, 127065, 127234, 127274, 127281, 127440, 127474, 127651, 127751, 127920, 127974, 128434, 129076, 129159, 129189, 129353, 129373, 129453, 129519, 129673, 129680, 129764, 129803, 130092, 130170, 130194, 130266, 130374, 130487, 130600, 130656, 130829, 130983, 131069, 131432, 131642, 131662, 131707, 131743, 131757, 131886, 131920, 132003, 132078, 132091, 132106, 132320, 132323, 132680, 132694, 133124, 133202, 133271, 133361, 133377, 133387, 133403, 133468, 133733, 134080, 134211, 134312, 134357, 134443, 134643, 134771, 134812, 134984, 135439, 135869, 136055, 136475, 136592, 136648, 136751, 136871, 136886, 136900, 137040, 137463, 137689, 137700, 138027, 138484, 138704, 138862, 138990, 139056, 139171, 139333, 139526, 139866, 139872, 140141, 140234, 140237, 140243, 140281, 140622, 140650, 140824, 141158, 141178, 141392, 141551, 141974, 142082, 142130, 142423, 142610, 142697, 142984, 143136, 143335, 143806, 143843, 144076, 144164, 144707, 145375, 145379, 145421, 145424, 145536, 145613, 145675, 145806, 145817, 145892, 146335, 146577, 146695, 146799, 146854, 147094, 147206, 147571, 147644, 147702, 147708, 147750, 147770, 147896, 148044, 148110, 148124, 148150, 148195, 148205, 148472, 148489, 148645, 148711, 148774, 148778, 148912, 148923, 148992, 149010, 149018, 149114, 149195, 149244, 149282, 149591, 149788, 149797, 149974, 150031, 150068, 150145, 150243, 150264, 150275, 150277, 150354, 150477, 150484, 150723, 150881, 150917, 150936, 150981, 151068, 151073, 151093, 151157, 151178, 151602, 151609, 151626, 151635, 151688, 151760, 151783, 151838, 151881, 151889, 152029, 152088, 152108, 152113, 152125, 152191, 152282, 152290, 152312, 152799, 153145, 153157, 153236, 153299, 153345, 153582, 153586, 153633, 153658, 153904, 154072, 154166, 154189, 154563, 154606, 154968, 154998, 155168, 155230, 155247, 155299, 155398, 155551, 155586, 155588, 155772, 155807, 155818, 155893, 156031, 156206, 156346, 156515, 156600, 156602, 156706, 156760, 156817, 156850, 156851, 157150, 157208, 157421, 157607, 157954, 157976, 157983, 158009, 158010, 158035, 158227, 158269, 158368, 158384, 158496, 158681, 158874, 159083, 159101, 159157, 159195, 159740, 159863, 160197, 160335, 160425, 160498, 161090, 161383, 161398, 161467, 161501, 162051, 162387, 162401, 162581, 162870, 162908, 162916, 163005, 163114, 163393, 163520, 163582, 163682, 163829, 163837, 163849, 163865, 164083, 164122, 164136, 164142, 164230, 164238, 164292, 164312, 164451, 164543, 164551, 164635, 164644, 164647, 164938, 165028, 165140, 165248, 165407, 165452, 165641, 165720, 165854, 165974, 166009, 166228, 166257, 166314, 166368, 166470, 166633, 166640, 166667, 166672, 166782, 166927, 166998, 167003, 167127, 167547, 167684, 167909, 168090, 168491, 168713, 169337, 169416, 169447, 169451, 169555, 169605, 170049, 170125, 170236, 170270, 171182, 171219, 171307, 171458, 171695, 171773, 172040, 172398, 172489, 172785, 172870, 172965, 172980, 173001, 173425, 173479, 173541, 173719, 173795, 173869, 174188, 174708, 174758, 174868, 174920, 175191, 175654, 176017, 176087, 176098, 176169, 176472, 176594, 176632, 176635, 176686, 176692, 176711, 176819, 176897, 176900, 176975, 177080, 177189, 177284, 177288, 177376, 177397, 177453, 177464, 177602, 177649, 177684, 178034, 178051, 178052, 178363, 178420, 179260, 179604, 180046, 180064, 180279, 180389, 180981, 181764, 182177, 182300, 182366, 182628, 182869, 182966, 183085, 183100, 183405, 183485, 183913, 184477, 184672, 184799, 184850, 184867, 185255, 185394, 185809, 185872, 186620, 186801, 186985, 188368, 189227, 189572, 190491, 191866, 192388, 193037, 193550, 193773, 194160, 194186, 194289, 194296, 194299, 194624, 195106, 195500, 195531, 196291, 196464, 196563, 196606, 196607, 196616, 196696, 196701, 196818, 197241, 197247, 197314, 197390, 197479, 197486, 197578, 197581, 197649, 197792, 197962, 198058, 198117, 198376, 198546, 198707, 198768, 198843, 199045, 199152, 199496, 199654, 200110, 200283, 201403, 201731, 201752, 201814, 202008, 202016, 202362, 202381, 202472, 202607, 202807, 202996, 203250, 203279, 203472, 203647, 203823, 203978, 204727, 204943, 204962, 205476, 205570, 205719, 206299, 206433, 206436, 206585, 206608, 206882, 207007, 207081, 207345, 208542, 209338, 210558, 210652, 211098, 211587, 212055, 212205, 212323, 212720, 212842, 213312, 213420, 214159, 214163, 214334, 214534, 214553, 214572, 215153, 215283, 216156, 216191, 216341, 216399, 217003, 217047, 217254, 217407, 217567, 218071, 218192, 218214, 218260, 218456, 218680, 218891, 218962, 218974, 219023, 219065, 219424, 219434, 219520, 219601, 219856, 219996, 220044, 220181, 220523, 220546, 220764, 220868, 221137, 221217, 221346, 221376, 221392, 221536, 221835, 221987, 222011, 222157, 222190, 222241, 222754, 223173, 223285, 223318, 223723, 224163, 224322, 224515, 224516, 224694, 224876, 224883, 225244, 225249, 225426, 225528, 225577, 225628, 225876, 226465, 226569, 226631, 227164, 227365, 227712, 228026, 228058, 228078, 228310, 228491, 228669, 228711, 228809, 229307, 229659, 229864, 230486, 230614, 231082, 231221, 231354, 231519, 231950, 232157, 233827, 233929, 234255, 234704, 234867, 235131, 235160, 235225, 235305, 235441, 236069, 236161, 236170, 236293, 236306, 236347, 236351, 236472, 236572, 237156, 237219, 237373, 239227, 239856, 241770, 242454, 244000, 244107, 246068, 247390, 256324, 265148, 266636, 273227, 291459, 293413, 301047, 301424, 302001, 309915, 320948]
11     print(len(num_list))
12     # num_list = [1, 5, 10]
13     if sum(list(num_list)) <= find_num:
14         return sum(list(num_list)), list(num_list)
15     min_tuple = ()
16     for i in range(len(num_list)):
17         index_list = [i]
18         number_total = num_list[i]
19         if number_total >= find_num:
20             res = is_min_val(min_diff_value, number_total, find_num)
21             if res[0]:
22                 return number_total, index_list
23             elif res[2]:
24                 min_diff_value = res[1]
25                 min_tuple = (number_total, index_list)
26             continue
27         for j in range(i+1, len(num_list)):
28             index_list = [i]
29             number_total = num_list[i]
30             for h in range(j, len(num_list)):
31                 number_total += num_list[h]
32                 index_list.append(h)
33                 if number_total >= find_num:  # 如果总和大于目标数需要判断此时大于目标数的和与前一个值总和哪个差值最小
34                     _number_total = number_total-num_list[h]
35                     if abs(find_num-number_total) < abs(find_num-_number_total):
36                         _tuple = (number_total, index_list)
37                     else:  # 20 [0, 3, 4, 5, 6, 7]
38                         index_list.remove(h)
39                         _tuple = (_number_total, index_list)
40 
41                     res = is_min_val(min_diff_value, _tuple[0], find_num)
42                     if res[0]:
43                         return _tuple[0], index_list
44                     elif res[2]:
45                         min_diff_value = res[1]
46                         min_tuple = (_tuple[0], index_list)
47                     break
48                 if h == len(num_list)-1:
49                     res = is_min_val(min_diff_value, number_total, find_num)
50                     if res[0]:
51                         return number_total, index_list
52                     elif [2]:
53                         min_diff_value = res[1]
54                         min_tuple = [number_total, index_list]
55     return min_tuple
56 
57 def is_min_val(min_diff_val, now_val, find_val):
58     """
59 
60     :param min_diff_val:
61     :param now_val:
62     :param find_val:
63     :return: 是否需要暂停,当前差值, 是否为最小值
64     """
65     # print(min_diff_val, now_val, find_val)
66     if abs(now_val-find_val) == 0:
67         min_diff_val = abs(now_val-find_val)
68         return True, min_diff_val, False
69     elif abs(now_val-find_val) < min_diff_val:
70         min_diff_val = abs(now_val-find_val)
71         return False, min_diff_val, True
72     else:
73         return False, min_diff_val, False
74 
75 
76 if __name__ == __main__:
77     import time
78     a = time.time()
79     print(find_almost())
80     print(time.time() - a)
展开查看 Code

沟通交流方式

微信:w1530697775  期待与你的交流   --__<_>__--

python kayb算法之从一组序列当中获取一组与目标值最接近的算法

标签:img   需要   spl   one   als   class   ccf   gif   value   

原文地址:https://www.cnblogs.com/kayb/p/9248981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!