Skip to main content

A small Python 3.5+ library to make debugging LeetCode binary tree, linked list and matrix problems more convenient.

Project description

LeetNode

A small Python 3.5+ library to make debugging LeetCode binary tree, linked list and matrix problems more convenient.

Binary Trees

from leetnode import TreeNode, build_btree

Creating a binary tree node

btree_node = TreeNode('val')

Creating a binary tree from a list

btree1 = build_btree([3, 9, 20, 8, 16, 15, 7, 1, 2, None, None, 3, None, None, None, None, None, 12])

or

btree2 = TreeNode.from_list(['a', 'b', 'cd', None, 'ef', 'gh', 'i', None, None, None, None, 'jkl', 'mn', 'o'])

or, from a JSON string

btree3 = TreeNode.from_list('[1, null, 555555, null, 43, 1]')

Printing a list representation of a binary tree

>>> print(btree1)
[3, 9, 20, 8, 16, 15, 7, 1, 2, None, None, 3, None, None, None, None, None, 12]

>>> print(btree2)
['a', 'b', 'cd', None, 'ef', 'gh', 'i', None, None, None, None, 'jkl', 'mn', 'o']

>>> print(btree3)
[1, None, 555555, None, 43, 1]

Printing a tree representation of a binary tree

>>> print(btree1.tree_string())
           ___3_____
          /         \
    _____9          _20
   /      \        /   \
  8___     16     15    7
 /    \          /
1     _2        3
     /
    12

>>> print(btree2.tree_string())
   ______'a'______
  /               \
'b'_            __'cd'___________
    \          /                 \
    'ef'     'gh'              __'i'_
                              /      \
                          _'jkl'     'mn'
                         /
                       'o'

>>> print(btree3.tree_string())
1__
   \
  555555__
          \
           43
          /
         1

Linked Lists

from leetnode import ListNode, build_linked_list

Creating a linked list node

list_node = ListNode('val')

Creating a linked list from a list

llist1 = build_linked_list(['a', 'b', 'c', 'd'])

or

llist2 = ListNode.from_list([1, 2, 3, 4, 5])

or, from a JSON string

llist3 = ListNode.from_list('["e", "f", "g", "h"]')

Printing a linked list

>>> print(llist1)
['a' -> 'b' -> 'c' -> 'd']

>>> print(llist2)
[1 -> 2 -> 3 -> 4 -> 5]

>>> print(llist3)
['e' -> 'f' -> 'g' -> 'h']

Iterating over a linked list

>>> for node in llist1:
...     print(node.val + node.next.val if node.next is not None else node.val)
...
ab
bc
cd
d

Matrices

Printing a matrix

>>> mat1 = [[1.2, 2.33, 3.0], [4.5, 6.7777, 8], [9.0, 10.98, 111.42]]
>>> mat2 = [[3, 0, 8, 4], [2, 2340, 5, 7], [97, 432, 6, 3], [0, 3, 1, 13]]
>>> mat3 = mat3 = '[["a", "b", "ccc"], ["dd", "e", "ff"], ["g", "h", "i"]]'

>>> print(matrix_to_string(mat1))
[ [ 1.2   , 2.33  , 3.0    ],
  [ 4.5   , 6.7777, 8      ],
  [ 9.0   , 10.98 , 111.42 ] ]

>>> print(matrix_to_string(mat2))
[ [    3,    0,    8,    4 ],
  [    2, 2340,    5,    7 ],
  [   97,  432,    6,    3 ],
  [    0,    3,    1,   13 ] ]

>>> print(matrix_to_string(mat3))
[ [ 'a'  , 'b'  , 'ccc' ],
  [ 'dd' , 'e'  , 'ff'  ],
  [ 'g'  , 'h'  , 'i'   ] ]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

leetnode-1.0.3.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

leetnode-1.0.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file leetnode-1.0.3.tar.gz.

File metadata

  • Download URL: leetnode-1.0.3.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.0

File hashes

Hashes for leetnode-1.0.3.tar.gz
Algorithm Hash digest
SHA256 6b17c108343ba8762640a387bc94f9d1f9b732bab7b9d612d015f5d2136fe75a
MD5 b426cd07488df76da65399075c0a5638
BLAKE2b-256 90b9aa3dd87181fddfc931b0000709085cf7539b9c9066a1fc08121816a2e2a1

See more details on using hashes here.

File details

Details for the file leetnode-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: leetnode-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.0

File hashes

Hashes for leetnode-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 99f9ddaa0966dfa869098c68f24767ed035d6e392699b6797c5f037c1d1f2b5f
MD5 fc00f29e65974db199af37bdc9b08853
BLAKE2b-256 819bb808447ce3658ce818ad6e655ede302bb76ae906f44a3d49347ab1e43aef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page