Sequence¶
Permutation¶
- calcpy.cycleperm(values, /, cycle)[source]¶
Permutate a list according to cycle notation.
- Parameters:
values (list) – Sequence to permutate.
cycle (list) – Permutation rule in cyclc notation.
- Returns:
Permutated sequence.
- Return type:
list
Examples
Permutate a list of strings.
>>> cycleperm(["a", "b", "c", "d", "e", "f", "g"], cycle=[1, 2, 4]) ['a', 'c', 'e', 'd', 'b', 'f', 'g']
Permutate a list of integers.
>>> cycleperm(list(range(6)), cycle=[0, 1, 2]) [1, 2, 0, 3, 4, 5]
- calcpy.swap(values, /, i=0, j=1)[source]¶
Swap two elements in a list.
- Parameters:
values (list) – Sequence to permutate.
i (int) – Index of the first element to swap.
j (int) – Index of the second element to swap.
- Returns:
Swapped sequence.
- Return type:
list
Examples
Swap two elements for a list of strings.
>>> swap(["a", "b", "c", "d", "e", "f", "g"], i=1, j=2) ['a', 'c', 'b', 'd', 'e', 'f', 'g']
Swap two elements for a list of integers.
>>> swap(list(range(6)), i=1, j=2) [0, 2, 1, 3, 4, 5]
- calcpy.prioritize(values, *, index, dup='multiple')[source]¶
Move some elements in the sequence to the beginning.
- Parameters:
values (list | tuple | str) – Sequence to permutate.
index (int | list[int]) – Index of the elements to move to the beginning. The index can be negative. If there are duplicated index values, the same element will appear multiple times.
dup (
{"multiple", "unique", "raise"}
) – Specify how to deal with the case that the same position is prioritized mutliple times."multiple"
: The same element will appear multiple times."unique"
: The same element will appear only once."raise"
: Raise an error.
- Return type:
list | tuple | str
Examples
Move a single positional argument to the beginning.
>>> prioritize(["a", "b", "c", "d"], index=-2) ['c', 'a', 'b', 'd']
Move multiple positional arguments to the beginning.
>>> prioritize(["a", "b", "c", "d"], index=[0, 2, -2]) ['a', 'c', 'c', 'b', 'd'] >>> prioritize(["a", "b", "c", "d"], index=[0, 2, -2], dup="unique") ['a', 'c', 'b', 'd']
Sequence Operations based on Comparers¶
- calcpy.same(values, key=None)[source]¶
Check whether all elements are the same.
- Parameters:
values (iterable)
key (callable)
- Return type:
bool
Examples
>>> same([{"a": 1}, {"a": 1}, {"a": 1}]) True >>> same([1, 1, 2, 2]) False
- calcpy.distinct(values, *, key=None)[source]¶
Check whether all elements are distinct.
- Parameters:
values (iterable)
key (callable)
- Return type:
bool
Examples
>>> distinct([[1, 2], [1, 3], [2, 3]]) True >>> distinct([[1, 2], [1, 3], [1, 3]]) False
Repetend Analysis¶
- calcpy.min_repetend_len(values, *, allow_frac=True, key=None)[source]¶
Get minimum length of repetends.
- Parameters:
values (list) – List of values as a sequence.
allow_frac (bool) – Whether to allow partial repetend at the end of sequence.
key – A object to be used to determine whether two values are the same.
- Returns:
Minimum length of repetends.
- Return type:
int
Examples
>>> min_repetend_len([1, 1, 1, 1]) 1 >>> min_repetend_len([1, 2, 1, 2]) 2 >>> min_repetend_len([1, 2, 1, 3]) 4 >>> min_repetend_len([1, 2, 1, 3, 1, 2, 1, 3]) 4 >>> min_repetend_len([1, 2, 1, 2, 1, 2, 1], allow_frac=False) 7
Sequence Generator¶
- calcpy.oeis.A276128(n=9223372036854775807)[source]¶
Generate OEIS sequence A276128.
- Parameters:
n (int) – Length of sequence
- Yields:
int – Value of the sequence
See also
- Explanations:
(Adapted from https://oeis.org/A276128) Definition of sequence: For a positive integer n, let the single-player game G[n] be as follows: x is a number in {0, 1, 2, …, n}, but unknown to the player. The player can guess as many times as he wants to determine the value of x. For each guess, the player can propose a possible value c in {0, 1, 2, …, n}, but such guess will cost the player c dollars. After each guess, the player will get response to show whether c<x, c=x, or c>x. A guess strategy will consist a series of guesses to determine x. The cost of multiple guesses is defined to be the sum of the cost of each guess. The cost of guess strategy is defined to be the worse case of the cost of the guess series. The optimal guess strategy for the game G[n] is the guess strategy that has the minimum cost. a[n] is the cost of the optimal guess strategy. It is indifference whether the set {0, 1, …, n} contains the element 0 since identifing this element takes no costs.
Algorithms: Dynamic programming
- Complexity:
- Generate \(a[n]\) when \((a[0],...,a[n-1])\) are available:
Time complexity: \(O(n)\). Space complexity: \(O(n)\).
- Generate \(a[0],...,a[n-1]\) entries (\(n\) entries in total):
Time complexity: \(O(n^2)\). Space complexity: \(O(n^2)\).
Examples
>>> list(A276128(14)) [0, 0, 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 21, 24]