DEV Community

Cover image for Practicing algorithms using Polyglot Notebooks - part 1 (setup)
Krzysztof Koziarski
Krzysztof Koziarski

Posted on

Practicing algorithms using Polyglot Notebooks - part 1 (setup)

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)

Polyglot Notebooks extension
Polyglot Notebooks can be used for practicing algorithms. In this article I will show you how to install, configure and troubleshoot Polyglot Notebooks.

Installation

Polyglot Notebooks is a VS Code extension. To use it you need to:

  1. Install the latest .NET 7 SDK
  2. Install the Polyglot Notebooks extension from the VS Code marketplace.

Getting started - create new notebook

To create a new polyglot notebook, open the Command Palette(Ctrl+Shift+P) on Windows or (Cmd+Shift+P) on MacOS, and select Polyglot Notebook: Create new blank notebook -> Create as .dib -> C#

In the new cell write:

$"Hello world at {DateTime.Now}".Display();
Enter fullscreen mode Exit fullscreen mode

and press CTRL+Enter to execute it. You can also execute current cell by clicking the play icon.

The .Display() method is a special extension method which shows the result in an output cell. It’s available only in Polyglot Notebook.
In the above example it is the same as writing

Console.WriteLine($"Hello world at {DateTime.Now}")
Enter fullscreen mode Exit fullscreen mode

There are two types of cells: code and markdown.

Polyglot notebook - code cell

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.

polyglot notebooks - previous values

If you don't, variables will not be available. Similarly, if you change values or method implementation in previous cells without executing, then only old values will be available in next cells.

polyglot notebooks - previous values error

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 mine:

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        "omnisharp.enableAsyncCompletion": true,
        "omnisharp.useModernNet": true,
        "interactiveWindow.collapseCellInputCode": "always",
        "notebook.showFoldingControls": "always",
        "notebook.cellToolbarVisibility": "hover",
        "notebook.cellFocusIndicator": "border",
        "notebook.diff.ignoreMetadata": false,
        "notebook.diff.ignoreOutputs": true,
        "notebook.dragAndDropEnabled": false,
        "notebook.lineNumbers": "on",
        "notebook.outline.showCodeCells": true,
        "notebook.diff.enablePreview": false,
        "notebook.consolidatedOutputButton": false,
        "notebook.compactView": false,
        "notebook.output.textLineLimit": 50,
        "vsicons.associations.files": [
            { "icon": "pddl_happenings", "extensions": ["dib"], "format": "svg" }
        ],
        "files.exclude": {
            "**/.gitattributes": true,
            "**/.gitignore": true,
            "**/*.code-workspace": true
        }
    },
    "extensions": {
        "recommendations": [
            "vscode-icons-team.vscode-icons",
            "ms-dotnettools.dotnet-interactive-vscode",
        ]
    }
}
Enter fullscreen mode Exit fullscreen mode

There are some settings for notebook.* that I prefer, but it's up to you to experiment with those settings.

As you can see, there is also a recommended vscode-icons extension. This is to set an icon for .dib files:

"vsicons.associations.files": [
    { "icon": "pddl_happenings", "extensions": ["dib"], "format": "svg" }
]
Enter fullscreen mode Exit fullscreen mode

The result will be:
polyglot notebook icon

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"
},
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

Infinity loop

While practicing algorithms, it is very likely that you will write an infinity loop like below.

polyglot notebooks - infinity loop

Unfortunately, the stop button (1) doesn't work in that case and will not stop executing the cell. To stop an execution, you need to click Restart button on the top (2) or open Command Palette(Ctrl+Shift+P) and type Polyglot Notebook: Restart the current notebook's kernel. In that case, variables from previous cells will not be available and you will need to execute the previous cells again.

Broken layout

Sometimes when you open a bigger notebook with many code and markdown cells you can get a broken view like this:

polyglot notebooks - broken layout

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 Practicing algorithms using Polyglot Notebooks - part 2 - introduction

Gist for this article can be found here

Top comments (0)