Skip to main content

A tool to create linked list, binary tree and graph from array or dictionary for the ease of local test.

Project description

LeetCode Toolkit (lctk)

GitHub page: https://github.com/chengyutang/lctk

lctk is tool for creating LeetCode test cases for the ease of local testing.

Note: support for tree nodes with value 0 is fixed in version 0.0.5.

1. Introduction

LeetCode is a website where people can improve their coding skills and get prepared for techical interviews by solving coding problems and discussing with other people. Users can use the online judge (OJ) to run and test their codes within the brower. But the OJ could sometimes be slow, due to network limit or server overflow, which is not very convenient and efficient, especially when submitting frequently. Therefore, some users prefer to write and test their codes locally for a more convenient test (and a better looking submission history :p).

But for some of the problems, where the inputs are customised data structures, such as linked list, binary tree and graph, it's difficult to come up with test cases locally, while LeetCode uses built-in data structures to represent them, which makes it much easier to create test cases. For example, linked lists and binary trees are represented by arrays, and graphs are represented by dictionaries.

This tool helps users with creating linked list, binary tree, and graph locally from array or dictionary, for the ease of local testing.

2. Installation

If pip is installed, type the following command in the terminal to install this package

pip install lctk

To install pip, refer here.

3. Usage

First import this package simply using

import lctk

3.1 Linked List

API:

lctk.linkedList(arr, cyclePos = -1)

Linked list is represented by array in LeetCode's console. To create the equivalent linked list from an array, use the following command

head = lctk.linkedList(arr)

head would be the head node (a ListNode object) of the linked list represented by the input array arr.

Additionally, if you want to construct a linked list that has a cycle, just simply specify the position that the cycle begins (0-indexed). For example:

head = lctk.linkedList(arr, 2)

To print the values in a linked list, use the linkedList2Arr function, which also works with linked list that has a cycle.

arr = lctk.linkedList2Arr(linkedList)
print(arr)

PS: the definition of ListNode:

class ListNode:
	def __init__(self, x):
		self.val = x
		self.next = None

3.2 Binary Tree

Similar with linked list, a binary tree is also represented by an array in LeetCode's console, and the order is a layer-wise, left-to-right travesal of the tree.

Given an input array arr that represents a binary tree, the following command

root = lctk.binaryTree(arr)

where root would be the root node (a TreeNode object) of the equivalent binary tree.

You can also do the opposite, getting the array representation of a binary tree given a TreeNode root using

arr = lctk.binaryTree2Arr(root)

Example:

>>> inArr = [1, 2, 3, 4, None, 5, None, 6, None, None, None, 7]
>>> root = lctk.binaryTree(inArr)
>>> outArr = lctk.binaryTree2Arr(root)
>>> inArr == outArr
True

The definition of TreeNode:

class TreeNode:
	def __init__(self, x):
		self.val = x
		self.left = None
		self.right = None

3.3 Graph (Directed and Undirected)

In LeetCode, a graph is typically represented by a dictionary, just like that in Leetcode #133: Clone Graph.

root = lctk.graph(inDict)

root would be the root node (a GraphNode object) of the graph represented by the input dictionary inDict. The definition of graph node:

As always, you can also get the dictionary representation given a GraphNode root using

outDict = lctk.graph2Dict(root)

Example:

>>> inDict = {"$id":"1","neighbors":[{"$id":"2","neighbors":[{"$ref":"1"},{"$id":"3","neighbors":[{"$ref":"2"},{"$id":"4","neighbors":[{"$ref":"3"},{"$ref":"1"}],"val":4}],"val":3}],"val":2},{"$ref":"4"}],"val":1}
>>> root = lctk.graph(inDict)
>>> outDict = lctk.graph2Dict(root)
>>> inDict == outDict
True

The definition of GraphNode:

class GraphNode:
	def __init__(self, x):
		self.val = x
		self.neighbors = []

4. Conclusion

If there were any error or suggestions, please let me know through the GitHub repository page shown above.

Happy LeetCoding!

Version History

0.0.1: Initial version.

0.0.2: Added supported for linked list with cycle.

0.0.3: Fixed bugs.

0.0.4: Corrected the wrong usage description of linkedList API.

0.0.5: Fixed an error handling TreeNode with value 0 (@ew-git).

0.0.6: Fixed a typo (@roachsinai).

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

lctk-0.0.6.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

lctk-0.0.6-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file lctk-0.0.6.tar.gz.

File metadata

  • Download URL: lctk-0.0.6.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for lctk-0.0.6.tar.gz
Algorithm Hash digest
SHA256 306aec94fd2f15a9c9cd1da55a82de2c595f0144af554fcd4c0d620e9b4b69f9
MD5 ac207ccdfa1a62c1688952de647a6e8d
BLAKE2b-256 e16e82050bc35f0b78e812d4475b67d83a75a0dd31e261743a607915f4c9c67b

See more details on using hashes here.

File details

Details for the file lctk-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: lctk-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for lctk-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5ad3a1362ef7a3ffd2e083bca2a05ce6725e0c64b3d08bddc7c86d2e75e57a4d
MD5 4e7de3c625a09a30aeb60a51c4e1b9c6
BLAKE2b-256 4cb0429a3be531ee0cb4be21b8444dfbfb5d6305b3f1ab7daa593e02dcbd5957

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