Skip to content

Commit b5890c0

Browse files
committed
添加类(class Person),对其对象进行约瑟夫排序
1 parent ff0c1a2 commit b5890c0

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

josephus.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,54 @@ def cycle_sort(target, begin, step):
3535
return result
3636

3737

38-
if __name__ == "__main__":
39-
original = ['a', 'b', 'c', 'd']
40-
final = cycle_sort(original, 2, 2)
41-
print(final)
38+
class Person:
39+
"""Summary of class here.
40+
41+
人的基本信息,包括姓名、年龄
42+
43+
Attributes:
44+
__name: 姓名
45+
__age: 年龄
46+
"""
47+
def __init__(self, name, age):
48+
"""Inits SampleClass with blah."""
49+
self.__name = name
50+
self.__age = age
51+
# print("name:", self.__name, ": constructor")
52+
53+
# def __del__(self):
54+
# """Destructor."""
55+
# print("name:", self.__name, ": destructor")
56+
57+
def print_data(self):
58+
"""Export sales information."""
59+
print("name:", self.__name, "\tage:", self.__age)
60+
61+
62+
if __name__ == '__main__':
63+
64+
Person1 = Person("zhangsan", 30)
65+
Person2 = Person("lisi", 35)
66+
Person3 = Person("wangwu", 42)
67+
Person4 = Person("zhouliu", 10)
68+
69+
original = [Person1, Person2, Person3, Person4]
70+
print("\nThe sequence before sorting is:\n")
71+
for i in range(len(original)):
72+
original[i].print_data()
73+
74+
try:
75+
begin = int(input("\nPlease input a starting position:"))
76+
step = int(input("Please input a step number:"))
77+
except ValueError:
78+
print("\nPlease input an integer!")
79+
else:
80+
if begin <= 0 or begin > len(original) or step <= 0:
81+
raise IndexError("Out of range!")
82+
83+
final = cycle_sort(original, begin, step)
84+
print("\nThe sequence after sorting is:\n")
85+
for i in range(len(final)):
86+
final[i].print_data()
4287

4388
# *=====End File=====* #

0 commit comments

Comments
 (0)