Skip to content

Repository files navigation

Custom-Python-Structures

Practice custom data structures.

Also testing Github Actions on that repo.

Badges

TestsBlackMyPyPyLintCoverage

Structures

LoopList

Sequence structure, implements list with feature, where indexes go by cycle.

Default List sequence:

graph LR
A --> B --> C --> D --> E
Loading

Loop List

graph LR
A --> B --> C --> D --> E --> A
Loading

So, if in default List you try to get 5th element from that sequence, you'll get IndexError: list index out of range.

But with LoopList 5th element will be A again, 6th = B, 9th = E, 10th = A again and etc.

Same with negative indexes: -1th element == E, -2th = D, -5th == A, -6th == E again and etc.

That structure also supports slices, but currently without step, so you can try get slise [-2:7] and that will return list [D,E,A,B,C,D,E,A,B].

Idea was inspired by Josephus Problem and with that structure solution will look like that:

ring=LoopList(range(1, n+1))
whilelen(ring) >1:
ring.rotate(k-1)
delring[0]
returnring[0]

Matrix

That structure implements matrix interface on nested list.

UML

classDiagram
Matrix <|-- NumericMatrix
Matrix <|-- BitMatrix
class Matrix{
-_values : [[T]]
-_width : int
-_height : int
+width int
+height int
+size tuple
+is_square bool
+rotated_... Matrix
+mirrored_... Matrix
+main_diagonal [T]
+__getitem__(key)
+__setitem__(key, value)
+generate(...)
+from_nested_list(list of lists)
+from_joined_lists(w, h, list)
+from_lists(*lists)
+input_matrix(...)
+transpose()
+get_minor(i, j)
}
class NumericMatrix{
+zero_matrix(n, [m]) NumericMatrix
+identity(n) NumericMatrix
+trace : int or float
+determinant : int or float
+__add__(other)
+__sub__(other)
+__mul__(other)
+__div__(other)
+__invert__()
+__neg__()
}
class BitMatrix{
+zero_matrix(n, [m]) BitMatrix
+identity(n) BitMatrix
+__and__(other)
+__or__(other)
+__xor__(other)
+__sub__(other)
+__neg__()
}
MatrixIterator --o Matrix
MatrixIterator: matrix
MatrixIterator: WALKTHROW_TYPE
MatrixIterator: +__init__(Matrix)
MatrixIterator: +__iter__()
MatrixIterator: +__next__()
Loading

Usage

# Creating examplesm1=NumericMatrix.from_joined_lists(3, values=range(9))
m2=Matrix(2, 2, [['A', 'B'], ['C', 'D']])
m3=NumericMatrix.zero_matrix(size=4)
m4=BitMatrix.from_lists([True, False], [False, True])
# Comparingassertm1!=m2# Supports compating between matrixassertm2== [['A', 'B'], ['C', 'D']] # And directly with nested list# Math# A, B : NumericMatrixassertA+B==B+A==CassertA+=BassertA==C# Same with subtraction# Multiplication implements matrix multiplication, so:assertA*B!=B*A# Trace and Determinant:A.traceA.determinant# TransformationsA.transponate()
assertA.rotated_clockwise.rotated_counterclockwise==AassertA.mirrored_horizontaly.mirrored_horizontaly==A# Bit operations - implements bit logic between elements# A, B : BitMatrixassertA&B==B&AassertA|B==B|AassertA^B==B^Aassert-(-A) ==A# Indexing and slicingA[0,0]
A[1,:]
A[:,2]
A[4:7,2:4]

About

Practice custom data structures

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages