Skip to content

Commit c0d6cf7

Browse files
committed
add josephus_sort
1 parent 2390dd9 commit c0d6cf7

File tree

6 files changed

+223
-0
lines changed

6 files changed

+223
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# The default ``config.py``
2+
# flake8: noqa
3+
4+
5+
def set_prefs(prefs):
6+
"""This function is called before opening the project"""
7+
8+
# Specify which files and folders to ignore in the project.
9+
# Changes to ignored resources are not added to the history and
10+
# VCSs. Also they are not returned in `Project.get_files()`.
11+
# Note that ``?`` and ``*`` match all characters but slashes.
12+
# '*.pyc': matches 'test.pyc' and 'pkg/test.pyc'
13+
# 'mod*.pyc': matches 'test/mod1.pyc' but not 'mod/1.pyc'
14+
# '.svn': matches 'pkg/.svn' and all of its children
15+
# 'build/*.o': matches 'build/lib.o' but not 'build/sub/lib.o'
16+
# 'build//*.o': matches 'build/lib.o' and 'build/sub/lib.o'
17+
prefs['ignored_resources'] = ['*.pyc', '*~', '.ropeproject',
18+
'.hg', '.svn', '_svn', '.git', '.tox']
19+
20+
# Specifies which files should be considered python files. It is
21+
# useful when you have scripts inside your project. Only files
22+
# ending with ``.py`` are considered to be python files by
23+
# default.
24+
# prefs['python_files'] = ['*.py']
25+
26+
# Custom source folders: By default rope searches the project
27+
# for finding source folders (folders that should be searched
28+
# for finding modules). You can add paths to that list. Note
29+
# that rope guesses project source folders correctly most of the
30+
# time; use this if you have any problems.
31+
# The folders should be relative to project root and use '/' for
32+
# separating folders regardless of the platform rope is running on.
33+
# 'src/my_source_folder' for instance.
34+
# prefs.add('source_folders', 'src')
35+
36+
# You can extend python path for looking up modules
37+
# prefs.add('python_path', '~/python/')
38+
39+
# Should rope save object information or not.
40+
prefs['save_objectdb'] = True
41+
prefs['compress_objectdb'] = False
42+
43+
# If `True`, rope analyzes each module when it is being saved.
44+
prefs['automatic_soa'] = True
45+
# The depth of calls to follow in static object analysis
46+
prefs['soa_followed_calls'] = 0
47+
48+
# If `False` when running modules or unit tests "dynamic object
49+
# analysis" is turned off. This makes them much faster.
50+
prefs['perform_doa'] = True
51+
52+
# Rope can check the validity of its object DB when running.
53+
prefs['validate_objectdb'] = True
54+
55+
# How many undos to hold?
56+
prefs['max_history_items'] = 32
57+
58+
# Shows whether to save history across sessions.
59+
prefs['save_history'] = True
60+
prefs['compress_history'] = False
61+
62+
# Set the number spaces used for indenting. According to
63+
# :PEP:`8`, it is best to use 4 spaces. Since most of rope's
64+
# unit-tests use 4 spaces it is more reliable, too.
65+
prefs['indent_size'] = 4
66+
67+
# Builtin and c-extension modules that are allowed to be imported
68+
# and inspected by rope.
69+
prefs['extension_modules'] = []
70+
71+
# Add all standard c-extensions to extension_modules list.
72+
prefs['import_dynload_stdmods'] = True
73+
74+
# If `True` modules with syntax errors are considered to be empty.
75+
# The default value is `False`; When `False` syntax errors raise
76+
# `rope.base.exceptions.ModuleSyntaxError` exception.
77+
prefs['ignore_syntax_errors'] = False
78+
79+
# If `True`, rope ignores unresolvable imports. Otherwise, they
80+
# appear in the importing namespace.
81+
prefs['ignore_bad_imports'] = False
82+
83+
# If `True`, rope will insert new module imports as
84+
# `from <package> import <module>` by default.
85+
prefs['prefer_module_from_imports'] = False
86+
87+
# If `True`, rope will transform a comma list of imports into
88+
# multiple separate import statements when organizing
89+
# imports.
90+
prefs['split_imports'] = False
91+
92+
# If `True`, rope will remove all top-level import statements and
93+
# reinsert them at the top of the module when making changes.
94+
prefs['pull_imports_to_top'] = True
95+
96+
# If `True`, rope will sort imports alphabetically by module name instead
97+
# of alphabetically by import statement, with from imports after normal
98+
# imports.
99+
prefs['sort_imports_alphabetically'] = False
100+
101+
# Location of implementation of
102+
# rope.base.oi.type_hinting.interfaces.ITypeHintingFactory In general
103+
# case, you don't have to change this value, unless you're an rope expert.
104+
# Change this value to inject you own implementations of interfaces
105+
# listed in module rope.base.oi.type_hinting.providers.interfaces
106+
# For example, you can add you own providers for Django Models, or disable
107+
# the search type-hinting in a class hierarchy, etc.
108+
prefs['type_hinting_factory'] = (
109+
'rope.base.oi.type_hinting.factory.default_type_hinting_factory')
110+
111+
112+
def project_opened(project):
113+
"""This function is called after opening the project"""
114+
# Do whatever you like here!
6 Bytes
Binary file not shown.

SalesData/.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"python.linting.pylintEnabled": false,
3+
"python.linting.flake8Enabled": true,
4+
"python.linting.enabled": true,
5+
"python.pythonPath": "D:\\technology\\VsCode\\miniconda3\\python.exe"
6+
}
944 Bytes
Binary file not shown.

SalesData/book.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3 -------------->在Windows、Unix-like(mac/linux/unix)上均可运行
2+
# -*- coding: utf-8 -*- -------------->文件编码格式 UTF-8
3+
4+
' Book sales information module '
5+
6+
__author__ = 'Chongsen Zhao'
7+
8+
from josephus_sort import cycle_sort
9+
10+
11+
class SalesData:
12+
"""Summary of class here.
13+
14+
书籍的销售情况,包括书籍的编号、销售单价以及销售量
15+
16+
Attributes:
17+
__book_no: 书籍编号
18+
__units_sold: 销售量
19+
__unit_price: 单价
20+
"""
21+
22+
def __init__(self, book_no, units_sold, unit_price):
23+
"""Inits SampleClass with blah."""
24+
self.__book_no = book_no
25+
self.__units_sold = units_sold
26+
self.__unit_price = unit_price
27+
# print("ISBN:", self.__book_no, ": constructor")
28+
29+
# def __del__(self):
30+
# """Destructor."""
31+
# print("ISBN:", self.__book_no, ": destructor")
32+
33+
def print_data(self):
34+
"""Export sales information."""
35+
print("ISBN:", self.__book_no, "\tUnits_sold:", self.__units_sold,
36+
"\tUnit_price:", self.__unit_price)
37+
38+
39+
if __name__ == '__main__':
40+
41+
data1 = SalesData("999-1", 111, 45)
42+
data2 = SalesData("999-2", 222, 30)
43+
data3 = SalesData("999-3", 333, 22)
44+
data4 = SalesData("999-4", 444, 10)
45+
46+
original = [data1, data2, data3, data4]
47+
print("\nThe sequence before sorting is:\n")
48+
for i in range(len(original)):
49+
original[i].print_data()
50+
51+
try:
52+
begin = int(input("\nPlease input a starting position:"))
53+
step = int(input("Please input a step number:"))
54+
except TypeError:
55+
print("\nPlease input an integer!")
56+
else:
57+
if begin <= 0 or begin > len(original) or step <= 0:
58+
raise IndexError("Out of range!")
59+
60+
final = cycle_sort(original, begin, step)
61+
print("\nThe sequence after sorting is:\n")
62+
for i in range(len(final)):
63+
final[i].print_data()
64+
65+
# *=====End File=====* #

SalesData/josephus_sort.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3 -------------->在Windows、Unix-like(mac/linux/unix)上均可运行
2+
# -*- coding: utf-8 -*- -------------->文件编码格式 UTF-8
3+
4+
' Circular sorting module '
5+
6+
__author__ = 'Chongsen Zhao'
7+
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+
# *=====End File=====* #

0 commit comments

Comments
 (0)