There are many ways to practice algorithms: using online editors or by writing console apps and even more complicated programs with unit tests. You can also practice it on LeetCode.
I would like to show you tools I use to write and resolve algorithmic problems. I hope you’ll find it useful too.
I'm .NET developer and I will focus on C#/.NET, but you can use it for JavaScript as well.
Polyglot Notebooks (formerly .NET Interactive)
You can add them by clicking + in the top bar buttons (1) or the one appearing on hover cell (2).
At the bottom right corner of each cell (3), you can see if this is a markdown cell or code cell with its language.
Reusing previous cells
Variables, methods and classes from previous cells can be accessed in next cells but only if you execute the previous cells first.
Configuration
The default configuration is good enough to start working with the notebooks. Below, I share some overrides that I think will make your work with Polyglot Notebooks much easier.
Create workspace
In the same folder where your notebook is placed, create a VS Code workspace file. I will name it dotnet.code-workspace.
You can use
Add some keyboard shortcuts
I recommend adding some keyboard shortcuts to make it easier to operate notebook cells. Open the Command Palette(Ctrl+Shift+P) on Windows or (Cmd+Shift+P) on MacOS, and type Preferences: Open Keyboard Shortcuts (JSON), then add the following configuration
{
"key": "o",
"command": "notebook.cell.collapseCellInput",
"when": "notebookCellListFocused && !inputFocus && !notebookCellInputIsCollapsed"
},
{
"key": "o",
"command": "notebook.cell.expandCellInput",
"when": "notebookCellInputIsCollapsed && notebookCellListFocused"
},
{
"key": "q",
"command": "notebook.cell.collapseCellInput",
"when": "notebookCellListFocused && !inputFocus && !notebookCellInputIsCollapsed"
},
{
"key": "q",
"command": "notebook.cell.expandCellInput",
"when": "notebookCellInputIsCollapsed && notebookCellListFocused"
},
{
"key": "w",
"command": "notebook.cell.clearOutputs",
"when": "notebookCellEditable && notebookCellHasOutputs && notebookEditable && notebookEditorFocused && !inputFocus"
},
This will basically allow you collapse/expand cells by simply pressing o or q and clear cell output by pressing w
.gitignore
Set line ending for *.dib files:
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
# Never modify line endings of dib files
*.dib text eol=lf
Troubleshooting
Infinity loop
While practicing algorithms, it is very likely that you will write an infinity loop like below.
To fix this you need to collapse all cell inputs by opening Command Palette(Ctrl+Shift+P) and typing Notebook: Collapse All Cell Inputs. You can then expand all cell inputs if you need to.
Next post
SOCIAL SHARE CARD GENERATOR