Python面试题

当前位置: 面试问题网 > Python面试题 > 华为python面试题

华为python面试题

有两个序列a,b,大小都为n,序列元素的值任意整形数,无序;
   要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。
   1. 将两序列合并为一个序列,并排序,为序列Source
   2. 拿出最大元素Big,次大的元素Small
   3. 在余下的序列S[:-2]进行平分,得到序列max,min
   4. 将Small加到max序列,将Big加大min序列,重新计算新序列和,和大的为max,小的为min。
   Python代码
   def mean( sorted_list ):
   if not sorted_list:
   return (([],[]))
   big = sorted_list[-1]
   small = sorted_list[-2]
   big_list, small_list = mean(sorted_list[:-2])
   big_list.append(small)
   small_list.append(big)
   big_list_sum = sum(big_list)
   small_list_sum = sum(small_list)
   if big_list_sum > small_list_sum:
   return ( (big_list, small_list))
   else:
   return (( small_list, big_list))
   tests = [ [1,2,3,4,5,6,700,800],
   [10001,10000,100,90,50,1],
   range(1, 11),
   [12312, 12311, 232, 210, 30, 29, 3, 2, 1, 1]
   ]
   for l in tests:
   l.sort()
   print
   print “Source List: ”, l
   l1,l2 = mean(l)
   print “Result List: ”, l1, l2
   print “Distance: ”, abs(sum(l1)-sum(l2))
   print ‘-*’*40
   输出结果
   Python代码
   Source List: [1, 2, 3, 4, 5, 6, 700, 800]
   Result List: [1, 4, 5, 800] [2, 3, 6, 700]
   Distance: 99
   -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
   Source List: [1, 50, 90, 100, 10000, 10001]
   Result List: [50, 90, 10000] [1, 100, 10001]
   Distance: 38
   -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
   Source List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
   Result List: [2, 3, 6, 7, 10] [1, 4, 5, 8, 9]
   Distance: 1
   -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
   Source List: [1, 1, 2, 3, 29, 30, 210, 232, 12311, 12312]
   Result List: [1, 3, 29, 232, 12311] [1, 2, 30, 210, 12312]
   Distance: 21
   -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

【华为python面试题】相关文章

1. Python面试题:如何用Python来发送邮件

2. Python面试题:Python里面如何生成随机数

3. Python面试题:Python是如何进行内存管理的

4. 华为python面试题

5. Python面试题集

6. 软件测试LoadRunner面试题:What is think time? How do you change the threshold?

7. Python的两道面试题

8. Python文件操作的面试题

9. Python里面如何拷贝一个对象

10. Python中如何定义一个函数

本文来源:https://www.mianshiwenti.com/a13494.html

点击展开全部

《华为python面试题》

将本文的Word文档下载到电脑,方便收藏和打印

推荐程度:

进入下载页面

﹝华为python面试题﹞相关内容

「华为python面试题」相关专题

其它栏目

也许您还喜欢