From d90a93d7f1224a43ef4baf24281e0df7bdde5413 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 29 May 2017 06:55:50 +0300 Subject: algorithms.inputgen -> algorithms.input_kind --- algorithms/algorithm.py | 12 ++++++------ algorithms/input_kind.py | 31 +++++++++++++++++++++++++++++++ algorithms/inputgen.py | 31 ------------------------------- algorithms/params.py | 2 +- 4 files changed, 38 insertions(+), 38 deletions(-) create mode 100644 algorithms/input_kind.py delete mode 100644 algorithms/inputgen.py (limited to 'algorithms') diff --git a/algorithms/algorithm.py b/algorithms/algorithm.py index 673a525..74817de 100644 --- a/algorithms/algorithm.py +++ b/algorithms/algorithm.py @@ -3,7 +3,7 @@ # For details, see https://github.com/egor-tensin/sorting-algorithms. # Distributed under the MIT License. -from . import inputgen +from . import input_kind class Algorithm: def __init__(self, codename, display_name, f): @@ -12,13 +12,13 @@ class Algorithm: self.function = f @staticmethod - def gen_input(n, case=inputgen.InputKind.AVERAGE): - #raise NotImplementedError('input generation is not defined for generic algorithms') - return inputgen.gen_input_for_sorting(n, case) + def gen_input(n, case=input_kind.InputKind.AVERAGE): + #raise NotImplementedError('input generation is not defined for a generic algorithm') + return input_kind.gen_input_for_sorting(n, case) class SortingAlgorithm(Algorithm): def __init__(self, codename, display_name, f): super().__init__(codename, display_name, f) - def gen_input(self, n, case=inputgen.InputKind.AVERAGE): - return inputgen.gen_input_for_sorting(n, case) + def gen_input(self, n, case=input_kind.InputKind.AVERAGE): + return input_kind.gen_input_for_sorting(n, case) diff --git a/algorithms/input_kind.py b/algorithms/input_kind.py new file mode 100644 index 0000000..998f1d6 --- /dev/null +++ b/algorithms/input_kind.py @@ -0,0 +1,31 @@ +# Copyright (c) 2016 Egor Tensin +# This file is part of the "Sorting algorithms" project. +# For details, see https://github.com/egor-tensin/sorting-algorithms. +# Distributed under the MIT License. + +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 cannot be less than zero') + 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)) diff --git a/algorithms/inputgen.py b/algorithms/inputgen.py deleted file mode 100644 index 998f1d6..0000000 --- a/algorithms/inputgen.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright (c) 2016 Egor Tensin -# This file is part of the "Sorting algorithms" project. -# For details, see https://github.com/egor-tensin/sorting-algorithms. -# Distributed under the MIT License. - -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 cannot be less than zero') - 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)) diff --git a/algorithms/params.py b/algorithms/params.py index bf87976..edd1361 100644 --- a/algorithms/params.py +++ b/algorithms/params.py @@ -6,7 +6,7 @@ from enum import Enum from numbers import Integral -from .inputgen import InputKind +from .input_kind import InputKind from .plotter import PlotBuilder from . import registry from .timer import Timer -- cgit v1.2.3