1
1
#!/usr/bin/env python3
2
2
# -*- coding: utf-8 -*-
3
3
4
- ' Circular sorting module '
4
+ ' Josephus sorting module '
5
5
6
6
__author__ = 'Chongsen Zhao'
7
7
8
8
9
- def cycle_sort (target , begin , step ):
10
- """ 对原列表重新排序生成新列表
11
-
12
- 在原列表中从指定位置开始,按照步进每次取出一个元素放入新列表
13
- 并把该元素从原列表中弹出,不参与下次排序
14
-
15
- Args:
16
- target: 原列表
17
- begin: 起始位置
18
- step: 步进
19
-
20
- Returns:
21
- 排序后的新列表
22
-
23
- Raises:
24
- TypeError: An error occurred accessing wrong data type.
25
- IndexError: An error occurred accessing invalid index.
26
- """
27
- pos = begin - 1
28
- result = []
29
-
30
- for i in range (len (target )):
31
- pos = (pos + step - 1 ) % len (target )
32
- result .append (target [pos ])
33
- target .pop (pos )
34
-
35
- return result
36
-
37
-
38
9
class Person :
39
10
"""Summary of class here.
40
11
@@ -55,34 +26,62 @@ def __init__(self, name, age):
55
26
# print("name:", self.__name, ": destructor")
56
27
57
28
def print_data (self ):
58
- """Export sales information."""
29
+ """Export person information."""
59
30
print ("name:" , self .__name , "\t age:" , self .__age )
60
31
61
32
62
- if __name__ == '__main__' :
33
+ class RingSort :
34
+ """Summary of class here.
35
+
36
+ 对约瑟夫环中的Person对象按照起始位置和步进依次抽取
37
+
38
+ Attributes:
39
+ people: 容器,元素为Person对象
40
+ id: 容器的索引
41
+ step: 步进
42
+ """
43
+ def __init__ (self ):
44
+ """constructor."""
45
+ self .people = []
46
+ self .id = 0
47
+ self .step = 1
63
48
64
- Person1 = Person ("zhangsan" , 30 )
65
- Person2 = Person ("lisi" , 35 )
66
- Person3 = Person ("wangwu" , 42 )
67
- Person4 = Person ("zhouliu" , 10 )
49
+ def reset (self , start , step ):
50
+ """设置起始位置和步进"""
51
+ self .id = start - 1
52
+ self .step = step
53
+
54
+ def append (self , target ):
55
+ """容器里添加Person对象"""
56
+ self .people .append (target )
57
+
58
+ def next (self ):
59
+ """返回下一个要抽取的Person对象"""
60
+ self .id = (self .id + self .step - 1 ) % len (self .people )
61
+ ret = self .people .pop (self .id )
62
+ return ret
63
+
64
+
65
+ if __name__ == '__main__' :
68
66
69
- original = [Person1 , Person2 , Person3 , Person4 ]
70
- print ("\n The sequence before sorting is:\n " )
71
- for i in range (len (original )):
72
- original [i ].print_data ()
67
+ ring = RingSort ()
68
+ ring .append (Person ("Jorn" , 25 ))
69
+ ring .append (Person ("Tom" , 30 ))
70
+ ring .append (Person ("Jerry" , 35 ))
71
+ ring .append (Person ("Mike" , 40 ))
73
72
74
73
try :
75
- begin = int (input ("\n Please input a starting position:" ))
74
+ start = int (input ("\n Please input a starting position:" ))
76
75
step = int (input ("Please input a step number:" ))
77
76
except ValueError :
78
77
print ("\n Please input an integer!" )
79
78
else :
80
- if begin <= 0 or begin > len ( original ) or step <= 0 :
79
+ if start <= 0 or step <= 0 :
81
80
raise IndexError ("Out of range!" )
82
81
83
- final = cycle_sort ( original , begin , step )
82
+ ring . reset ( start , step )
84
83
print ("\n The sequence after sorting is:\n " )
85
- for i in range (len (final )):
86
- final [ i ] .print_data ()
84
+ for i in range (len (ring . people )):
85
+ ring . next () .print_data ()
87
86
88
87
# *=====End File=====* #
0 commit comments