博客重定向至
http://www.cnblogs.com/unixfy
posted @
2013-09-14 23:49 unixfy 阅读(249) |
评论 (0) |
编辑 收藏
STL algorithm 中的 lower_bound and upper_bound
lower_bound 返回的结果 >= 参数 value
upper_bound 返回的结果 > 参数 value
具体明细可参见百科
lower_bound:http://baike.baidu.cn/view/4720650.htm
upper_bound:http://baike.baidu.cn/view/4163451.htm
测试代码:
1 #include <iostream>
2 #include <set>
3 #include <algorithm>
4 using namespace std;
5
6 int main()
7 {
8 set<int> si;
9 //cout << si.insert(3) << endl;
10 cout << *(si.insert(3).first) << endl;
11
12 for (int i = 1; i <= 10; i += 2)
13 {
14 si.insert(i);
15 }
16 for (set<int>::const_iterator cit = si.begin(); cit != si.end(); ++cit)
17 {
18 cout << *cit << ' ';
19 }
20 cout << endl;
21
22 cout << endl;
23 cout << *lower_bound(si.begin(), si.end(), 3) << endl;
24 cout << *lower_bound(si.begin(), si.end(), 4) << endl;
25 cout << *lower_bound(si.begin(), si.end(), 5) << endl;
26 cout << *lower_bound(si.begin(), si.end(), 6) << endl;
27
28 cout << endl;
29 cout << *upper_bound(si.begin(), si.end(), 3) << endl;
30 cout << *upper_bound(si.begin(), si.end(), 4) << endl;
31 cout << *upper_bound(si.begin(), si.end(), 5) << endl;
32 cout << *upper_bound(si.begin(), si.end(), 6) << endl;
33
34 system("PAUSE");
35 }
36
输出:
3
1 3 5 7 9
3
5
5
7
5
5
7
7
posted @
2013-05-30 20:55 unixfy 阅读(616) |
评论 (0) |
编辑 收藏
1 class CounterList(list):
2 def __init__(self, *args):
3 super(CounterList, self).__init__(*args)
4 self.counter = 0
5
6 def __getitem__(self, index):
7 self.counter += 1
8 return super(CounterList, self).__getitem__(index)
9
10
11 cl = CounterList(range(10))
12 print(cl)
13 cl.reverse()
14 print(cl)
15 del(cl[3:6])
16 print(cl)
17 print(cl.counter)
18 cl[4] + cl[2]
19 print(cl.counter)
输出:
>>>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 8, 7, 3, 2, 1, 0]
0
2
posted @
2013-05-21 22:14 unixfy 阅读(141) |
评论 (0) |
编辑 收藏
1 def checkIndex(key):
2 if not isinstance(key, (int,)):
3 raise TypeError
4 if key < 0:
5 raise IndexError
6
7 class ArithmeticSequence:
8 def __init__(self, start = 0, step = 1):
9 self.start = start
10 self.step = step
11 self.changed = {}
12
13 def __getitem__(self, key):
14
15 checkIndex(key)
16
17 print('__getitem__')
18
19 try:
20 return self.changed[key]
21 except KeyError:
22 return self.start + key * self.step
23
24 def __setitem__(self, key, value):
25
26 checkIndex(key)
27
28 print('__setitem__')
29
30 self.changed[key] = value
31
32
33 s = ArithmeticSequence(1, 2)
34 print(s[4])
35 s[4] = 2
36 print(s[4])
37 print(s[5])
38 s[3] = 100
39 print(s[3])
40 s.__setitem__(100, 3)
41 print(s.__getitem__(100))
输出:
>>>
__getitem__
9
__setitem__
__getitem__
2
__getitem__
11
__setitem__
__getitem__
100
__setitem__
__getitem__
3
posted @
2013-05-21 22:03 unixfy 阅读(233) |
评论 (0) |
编辑 收藏
1 class Filter:
2 def init(self):
3 self.blocked = []
4 def filter(self, sequence):
5 return [x for x in sequence if x not in self.blocked]
6
7 class SPAMFilter(Filter): # SPAMFilter 是 Filter 的子类
8 def init(self):
9 self.blocked = ['SPAM']
10
11 f = Filter()
12 f.init()
13 print(f.filter([1, 2, 3]))
14
15 s = SPAMFilter()
16 s.init()
17 print(s.filter(['SPAM', 'SPAM', 'SPAM', 'SPAM', 'eggs', 'bacon', 'SPAM']))
输出:
>>>
[1, 2, 3]
['eggs', 'bacon']
posted @
2013-05-19 11:39 unixfy 阅读(340) |
评论 (0) |
编辑 收藏
1 class MemberCounter:
2 member = 0
3 def init(self):
4 self.member += 1
5 MemberCounter.member += 1
6
7 m1 = MemberCounter()
8 m1.init()
9 print(m1.member)
10 print(MemberCounter.member)
11
12 m2 = MemberCounter()
13 m2.init()
14 print(m2.member)
15 print(MemberCounter.member)
16
17 print(m1.member is m2.member)
18 print(m1.member is MemberCounter.member)
19 print(m2.member is MemberCounter.member)
20
21 m3 = MemberCounter()
22 m3.init()
23 print(m3.member)
24 print(MemberCounter.member)
25 print(m2.member is MemberCounter.member)
26
27 print(m1.member)
28 print(MemberCounter.member)
输出:
>>>
1
1
2
2
False
False
True
3
3
False
1
3
posted @
2013-05-18 16:43 unixfy 阅读(421) |
评论 (0) |
编辑 收藏
1 __metaclass__ = type # 确定使用新式类
2
3 class Person:
4
5 def setName(self, name):
6 self.name = name
7
8 def getName(self):
9 return self.name
10
11 def greet(self):
12 print("Hello, world! I'm %s." % self.name)
13
14 def __foo(self):
15 print("FOO", self.name)
16 def _bar(self):
17 print("BAR", self.name)
18
19
20 foo = Person()
21 bar = Person()
22 foo.setName('Luke Skywalker')
23 bar.setName('Anakin Skywalker')
24 foo.greet()
25 bar.greet()
26 Person.greet(bar)
27
28 # foo.__foo()
29 foo._Person__foo()
30 foo._bar()
31 # foo._Person_bar()
输出:
>>>
Hello, world! I'm Luke Skywalker.
Hello, world! I'm Anakin Skywalker.
Hello, world! I'm Anakin Skywalker.
FOO Luke Skywalker
BAR Luke Skywalker
posted @
2013-05-18 16:31 unixfy 阅读(342) |
评论 (0) |
编辑 收藏
1 def my_range_2(start, stop = None, step = 1):
2 if stop == None:
3 if start > 0:
4 start, stop = 0, start
5 else:
6 start, stop = start, 0
7 result = []
8 if step > 0 and start < stop:
9 i = start
10 while (i < stop):
11 result.append(i)
12 i += step
13 elif step < 0 and start > stop:
14 i = start
15 while i > stop:
16 result.append(i)
17 i += step
18 return result
19
20 print(my_range_2(10))
21 print(my_range_2(5, 20, 2))
22 print(my_range_2(1, 10))
23 print(my_range_2(10, 0, -1))
24 print(my_range_2(100, 0, -9))
25 print(my_range_2(10, 20, -5))
26
27 print(my_range_2(100, 0, -10))
28 print(my_range_2(-10))
29 print(my_range_2(-10, -5, 1))
30 print(my_range_2(-10, -20, 1))
31 print(my_range_2(-50, -45))
>>>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 7, 9, 11, 13, 15, 17, 19]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[100, 91, 82, 73, 64, 55, 46, 37, 28, 19, 10, 1]
[]
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10]
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
[-10, -9, -8, -7, -6]
[]
[-50, -49, -48, -47, -46]
posted @
2013-05-18 14:32 unixfy 阅读(264) |
评论 (0) |
编辑 收藏
1 def search(sequence, number, lower = 0, upper = None):
2 if upper is None:
3 upper = len(sequence) - 1
4 if lower == upper:
5 assert(number == sequence[upper])
6 return upper
7 else:
8 middle = (lower + upper) // 2
9 if number > sequence[middle]:
10 return search(sequence, number, middle + 1, upper)
11 else:
12 return search(sequence, number, lower, middle)
13
14 seq = [34, 67, 8, 123, 4, 100, 95]
15 print(seq)
16 seq.sort()
17 print(seq)
18 print(search(seq, 34))
19 print(search(seq, 100))
输出:
[34, 67, 8, 123, 4, 100, 95]
[4, 8, 34, 67, 95, 100, 123]
2
5
posted @
2013-05-16 22:24 unixfy 阅读(329) |
评论 (0) |
编辑 收藏
1 def multiplier(factor):
2 def multiply_by_factor(number):
3 return number * factor
4 return multiply_by_factor
5
6
7 double = multiplier(2) # factor = 2
8 print(double(5)) # number = 5
9 triple = multiplier(3) # factor = 3
10 print(triple(4)) # number = 4
11 print(multiplier(5)(4))# factor = 5, number = 4
输出:
>>>
10
12
20
posted @
2013-05-16 21:18 unixfy 阅读(166) |
评论 (0) |
编辑 收藏