- !/usr/bin/python
import nose
def remove_neg(lst): Remove the negative numbers from the list lst. for i in range(len(lst)-1,-1,-1):
if lst[i] < 0: lst.remove(lst[i])
def bubblesort(lst): Sorts list in place. for p in range(len(lst)-1, 0, -1):
for i in range(p): if lst[i] > lst[i + 1]: lst[i], lst[i + 1] = lst[i + 1], lst[i]
def find_dups(lst): find duplicates in a list. Return a set of duplicate items result=set() count={} for i in lst:
count[i] = count.get(i,0) + 1
for k in count:
if count[k] > 1: result.add(k)
return result
def test_remove_neg(): Test for funtion remove_neg l1 = [] l2 = [1] l3 = [-1] l4 = [-1, -2, -3, -4] l = [l1,l2,l3,l4]
for x in l: remove_neg(x) assert l == [[],[1],[],[]]
def test_find_dups(): test for function find_dups assert find_dups([]) == set([]) assert find_dups([1]) == set([]) assert find_dups([1, 1]) == set([1]) assert find_dups([1, -1]) == set([]) assert find_dups([2,2,2,2,4,3,3,6,5,5]) == set([2,3,5])
def test_bubblesort(): l1 = [] l2 = [1] l3 = [3,-2,0,9] l4 = [0.1, -0.9, 8, -9] l5 = ['ff', 'sdf', 'g', 'lkj']
l = [l1,l2,l3,l4,l5] for x in l: bubblesort(x) assert l == [[],[1],[-2,0,3,9],[-9,-0.9,0.1,8],['ff', 'g', 'lkj', 'sdf']]
if __name__ == '__main__' :
nose.runmodule()