aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/algorithms/inputgen.py
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-06-24 01:54:13 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-06-24 01:54:13 +0300
commit82a674e409fce161299efeb43e1176f869af64af (patch)
tree3b544bd96688f3847233e011452c754c38755116 /algorithms/inputgen.py
parentadd Pylint configuration (diff)
downloadsorting-algorithms-82a674e409fce161299efeb43e1176f869af64af.tar.gz
sorting-algorithms-82a674e409fce161299efeb43e1176f869af64af.zip
major refactoring
With the focus on (re)usability. That includes adding separate modules for plotting, input generation and things like that.
Diffstat (limited to 'algorithms/inputgen.py')
-rw-r--r--algorithms/inputgen.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/algorithms/inputgen.py b/algorithms/inputgen.py
new file mode 100644
index 0000000..2659ffc
--- /dev/null
+++ b/algorithms/inputgen.py
@@ -0,0 +1,30 @@
+# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com>
+# This file is licensed under the terms of the MIT License.
+# See LICENSE.txt for details.
+
+from array import array
+from enum import Enum
+from random import seed, sample
+
+seed()
+
+class InputKind(Enum):
+ BEST, AVERAGE, WORST = 'best', 'average', 'worst'
+
+ def __str__(self):
+ return self.value
+
+def _gen_input_from(xs):
+ return array('l', xs)
+
+def gen_input_for_sorting(n, case=InputKind.AVERAGE):
+ if n < 0:
+ raise ValueError('input length must not be a negative number')
+ if case is InputKind.BEST:
+ return _gen_input_from(range(n))
+ elif case is InputKind.AVERAGE:
+ return _gen_input_from(sample(range(n), n))
+ elif case is InputKind.WORST:
+ return _gen_input_from(range(n - 1, -1, -1))
+ else:
+ raise NotImplementedError('invalid input kind: ' + str(case))