Generates visualizations for arrays
Project description
AlgoViz
Creates table for python list and highlights indexes on index access.
Installation
pip install algoviz
Examples
Example 1 : Coin Change Problem
from algoviz.vizlist import VizList
def coinChange(coins, amount):
dp = [0] * (amount + 1)
dp[0] = 1
dp = VizList(dp, title_name='Coin Change')
for coin in coins:
for val in range(amount + 1):
if coin <= val:
dp.print(f'Coin = {coin} | dp[{val}] += dp[{val} - {coin}] =', f'#[{val}] + #[{val} - {coin}]')
'''
dp.print :
Param 1 : Normal Print
Param 2: eval is run on this string. Replace array name with # to access original array.
'''
dp[val] += dp[val - coin]
return dp[-1]
if __name__ == '__main__':
coins = [1, 2]
amount = 3
res = coinChange(coins, amount)
print(f'Output : {res}')
Output:
Coin Change Init
┏━━━┳━━━┳━━━┳━━━┓
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃
┡━━━╇━━━╇━━━╇━━━┩
│ 1 │ 0 │ 0 │ 0 │
└───┴───┴───┴───┘
Coin = 1 | dp[1] += dp[1 - 1] = 1
Coin Change
┏━━━┳━━━┳━━━┳━━━┓
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃
┡━━━╇━━━╇━━━╇━━━┩
│ 1 │ 1 │ 0 │ 0 │
└───┴───┴───┴───┘
Coin = 1 | dp[2] += dp[2 - 1] = 1
Coin Change
┏━━━┳━━━┳━━━┳━━━┓
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃
┡━━━╇━━━╇━━━╇━━━┩
│ 1 │ 1 │ 1 │ 0 │
└───┴───┴───┴───┘
Coin = 1 | dp[3] += dp[3 - 1] = 1
Coin Change
┏━━━┳━━━┳━━━┳━━━┓
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃
┡━━━╇━━━╇━━━╇━━━┩
│ 1 │ 1 │ 1 │ 1 │
└───┴───┴───┴───┘
Coin = 2 | dp[2] += dp[2 - 2] = 2
Coin Change
┏━━━┳━━━┳━━━┳━━━┓
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃
┡━━━╇━━━╇━━━╇━━━┩
│ 1 │ 1 │ 2 │ 1 │
└───┴───┴───┴───┘
Coin = 2 | dp[3] += dp[3 - 2] = 2
Coin Change
┏━━━┳━━━┳━━━┳━━━┓
┃ 0 ┃ 1 ┃ 2 ┃ 3 ┃
┡━━━╇━━━╇━━━╇━━━┩
│ 1 │ 1 │ 2 │ 2 │
└───┴───┴───┴───┘
Output : 2
Example 2 : Count number of palindromes
def countSubstrings(s):
n = len(s)
dp = [[0] * n for _ in range(n)]
dp = VizList(dp, column_index=list(s), row_index=list(s), title_name='DP Table', sleep_time=1, set_highlight_color='blue')
res = 0
for i in range(n - 1, -1, -1):
for j in range(i, n):
dp[i][j] = s[i] == s[j] and ((j - i + 1) < 3 or dp[i + 1][j - 1])
res += dp[i][j]
dp.clear_highighlight_data() # Clears highlight tracking data. It's done automatically on list assignment operators
# The second parameter is evaluated using eval and printed.
# Note : to access the internal array, the array name should be replace by a #
return res
if __name__ == '__main__':
s = 'aab'
res = countSubstrings(s)
print('Output : {}'.format(res))
The above code will generate
DP Table Init
┏━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┓
┃ ┃ a [0] ┃ a [1] ┃ b [2] ┃
┡━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━┩
│ a [0] │ 0 │ 0 │ 0 │
│ a [1] │ 0 │ 0 │ 0 │
│ b [2] │ 0 │ 0 │ 0 │
└───────┴───────┴───────┴───────┘
DP Table
┏━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┓
┃ ┃ a [0] ┃ a [1] ┃ b [2] ┃
┡━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━┩
│ a [0] │ 0 │ 0 │ 0 │
│ a [1] │ 0 │ 0 │ 0 │
│ b [2] │ 0 │ 0 │ True │
└───────┴───────┴───────┴───────┘
DP Table
┏━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┓
┃ ┃ a [0] ┃ a [1] ┃ b [2] ┃
┡━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━┩
│ a [0] │ 0 │ 0 │ 0 │
│ a [1] │ 0 │ True │ 0 │
│ b [2] │ 0 │ 0 │ True │
└───────┴───────┴───────┴───────┘
DP Table
┏━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┓
┃ ┃ a [0] ┃ a [1] ┃ b [2] ┃
┡━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━┩
│ a [0] │ 0 │ 0 │ 0 │
│ a [1] │ 0 │ True │ False │
│ b [2] │ 0 │ 0 │ True │
└───────┴───────┴───────┴───────┘
DP Table
┏━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┓
┃ ┃ a [0] ┃ a [1] ┃ b [2] ┃
┡━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━┩
│ a [0] │ True │ 0 │ 0 │
│ a [1] │ 0 │ True │ False │
│ b [2] │ 0 │ 0 │ True │
└───────┴───────┴───────┴───────┘
DP Table
┏━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┓
┃ ┃ a [0] ┃ a [1] ┃ b [2] ┃
┡━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━┩
│ a [0] │ True │ True │ 0 │
│ a [1] │ 0 │ True │ False │
│ b [2] │ 0 │ 0 │ True │
└───────┴───────┴───────┴───────┘
DP Table
┏━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━┓
┃ ┃ a [0] ┃ a [1] ┃ b [2] ┃
┡━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━┩
│ a [0] │ True │ True │ False │
│ a [1] │ 0 │ True │ False │
│ b [2] │ 0 │ 0 │ True │
└───────┴───────┴───────┴───────┘
Output : 4
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
algoviz-0.2.4.tar.gz
(6.5 kB
view details)
File details
Details for the file algoviz-0.2.4.tar.gz
.
File metadata
- Download URL: algoviz-0.2.4.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.6.0.post20210108 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3845ea526b75a3e12e9f1da71b6a72831ed87709467610f926c25941797b4347 |
|
MD5 | 9f1d4a650471639fbff47e7052770c0c |
|
BLAKE2b-256 | cbed6dc3227500250acd1de20a95603c9d2941bd0907df8926c79d19f4a0ab75 |