精华帖 (0) :: 良好帖 (3) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-03-13
exoweb 要求挺高的么
|
|
返回顶楼 | |
发表时间:2010-03-14
1.平衡点问题
# -*- coding: utf-8 -*- data = [1, 3, 5, 7, 8, 25, 4, 20] def main() : dataIndex = [] length = len(data) total = sum(data) if (length > 2) : tempSum = data[0] for i in range(1, length - 1) : if (tempSum * 2 + data[i] == total) : dataIndex.append(i) tempSum = tempSum + data[i] return dataIndex if __name__ == "__main__" : print(main()) 2.支配点问题 # -*- coding: utf-8 -*- # 用dictionary实现,key存放所给的数,value为list,存放所给数在数组中的位置 # 一个数可能在数组中出现多次 data = [3, 3, 1, 2, 3] def main() : dataIndex = [] hashData = {} for i in range(0, len(data)) : key = data[i] if (key in hashData) : value = hashData[key] value.append(i) else : value = [] value.append(i) hashData[key] = value halfLength = int(len(data) / 2) if (len(data) % 2 == 1) : halfLength = halfLength + 1 for key in hashData : if (len(hashData[key]) >= halfLength) : dataIndex.append(hashData[key]) return dataIndex if __name__ == "__main__" : print(main()) |
|
返回顶楼 | |
发表时间:2010-03-15
如果不考虑运行效率,只考虑写代码的效率的话,为什么不使用api来完成呢...
比如sum, count什么的,很简单呢 比如第二题: a = [1, 2, 2, 3, 2] b = set(a) //去掉重复的元素,也可以不要 for i in b: if(a.count(i)>=len(a)/2): print i break |
|
返回顶楼 | |
发表时间:2010-03-17
第一题
算一个 写简单点,但是需要反复sum 效率要低 l = [1,3,5,7,8,25,4,20] for i in range(len(l)): if sum(l[:i]) == sum(l[i+1:]): print l[i] 取所有的,这个不要反复的sum l = [1,3,5,7,8,25,4,20] sum_of_list = sum(l) sum_of_head = 0 for i in range(len(l)): if sum_of_head*2 + l[i] == sum_of_list: print l[i] else: sum_of_head += l[i] |
|
返回顶楼 | |
发表时间:2010-03-17
最后修改:2010-03-17
tianling536 写道 取所有的,这个不要反复的sum l = [1,3,5,7,8,25,4,20] sum_of_list = sum(l) sum_of_head = 0 for i in range(len(l)): if sum_of_head*2 + l[i] == sum_of_list: print l[i] else: sum_of_head += l[i] 想起来最多也就一个。。 for i in range(len(l)): if sum_of_head*2 + l[i] == sum_of_list: print l[i] break elif sum_of_head*2 + l[i] > sum_of_list: break else: sum_of_head += l[i] |
|
返回顶楼 | |
发表时间:2010-03-18
最后修改:2010-03-18
joeycn 写道 如果不考虑运行效率,只考虑写代码的效率的话,为什么不使用api来完成呢...
比如sum, count什么的,很简单呢 比如第二题: a = [1, 2, 2, 3, 2] b = set(a) //去掉重复的元素,也可以不要 for i in b: if(a.count(i)>=len(a)/2): print i break 不考虑效率的话,两道题都只要一行就够了 第一题: >>> s = [1, 3, 5, 7, 8, 25, 4, 20] >>> filter(lambda x:sum(s[:s.index(x)])==sum(s[s.index(x)+1:]),s) [25] >>> s.append(30) >>> filter(lambda x:sum(s[:s.index(x)])==sum(s[s.index(x)+1:]),s) [] 第二题: >>> s = [1, 2, 2, 3, 2] >>> list(set(filter(lambda x:s.count(x)>len(s)/2,s))) [2] |
|
返回顶楼 | |
发表时间:2010-03-18
wayhome 写道 不考虑效率的话,两道题都只要一行就够了 第一题: >>> s = [1, 3, 5, 7, 8, 25, 4, 20] >>> filter(lambda x:sum(s[:s.index(x)])==sum(s[s.index(x)+1:]),s) [25] >>> s.append(30) >>> filter(lambda x:sum(s[:s.index(x)])==sum(s[s.index(x)+1:]),s) [] 第二题: >>> s = [1, 2, 2, 3, 2] >>> list(set(filter(lambda x:s.count(x)>len(s)/2,s))) [2] 受教了... |
|
返回顶楼 | |
发表时间:2010-03-19
wayhome 写道 不考虑效率的话,两道题都只要一行就够了 第一题: >>> s = [1, 3, 5, 7, 8, 25, 4, 20] >>> filter(lambda x:sum(s[:s.index(x)])==sum(s[s.index(x)+1:]),s) [25] >>> s.append(30) >>> filter(lambda x:sum(s[:s.index(x)])==sum(s[s.index(x)+1:]),s) [] s.index(x) 还得遍历list 用list comprehension 就够了 >>>l = [1,3,5,7,8,25,4,20] >>>[ l[i] for i in range(len(l)) if sum(l[:i]) == sum(l[i+1:]) ] [25] |
|
返回顶楼 | |
发表时间:2010-04-08
刚学习python,针对这两题,写出了我的解题答案。python要求代码简洁,写函数时就考虑到如何最简洁写出代码。
def balance(): li = [1,3,5,7,8,1,25,1,4,20] for i in range(len(li)-2): if(sum(li[:i+1])==sum(li[i+2:])): return li[i+1:i+2][0] def dominate(): li =[3,5,5,5,5,3,1,2,3] dict = {} index =0 maxcount=0 for i in li: tmp =dict.get(i) if(tmp): iarray = dict[i] iarray.append(index) if len(iarray)>maxcount: maxcount = i else: dict[i]=[index] index+=1 return "The number is %d,the all indexs are %s,and the first index is %d" \ % (maxcount,str(dict[maxcount]),dict[maxcount][0]) if __name__ == "__main__": print balance() print dominate() |
|
返回顶楼 | |
发表时间:2010-04-13
请多多指教~
#coding:utf-8 #1 li = [1,3,5,7,8,25,1,5,7,8,3] flag = 0 for i in range(1, --len(li)): if sum(li[:i]) == sum(li[i+1:]): print 'balance point', li[i]#可能有多个平衡点 flag = 1 if 0 == flag: print 'not find' #2 li = [1,3,4,3,3] foo = {} mid = len(li)>>1 for i in range(--len(li)): if li[i] not in foo.keys(): foo[li[i]] = [1,i] else: foo[li[i]] += [i] foo[li[i]][0] += 1 if foo[li[i]][0] > mid: print 'assign point', li[i], foo[li[i]][1:] break#只能有一个分配点 else: print 'not find' |
|
返回顶楼 | |