Literate Programming in Leo Phase 1: a program to detect balanced parentheses

34 views
Skip to first unread message

The Living Cosmos

unread,
Oct 6, 2019, 12:10:14 PM10/6/19
to leo-editor
# Title

I want to learn how to truly develop code in literate programming style. To this end, I have decided to work on some coding challenges. The existing code is here:

# Writing a Valid Parens Program in Literate LEO Programming

A few days ago someone posted a [coding challenge](https://www.reddit.com/r/Python/comments/dd1efi/im_on_a_mission_to_explain_all_popular_coding/) that I knew I could solve, but I knew I would be better off if I could specify all the logical cases.

# So what is a program that validates a string for matching parentheses?

In my eyes, it breaks down into 2 modes. In mode 1: you accept an open paren or an empty string. This screenshot shows this logic node: 

In mode 2: you accept the complementary closed paren or another open parenthesis. You balk on empty string. This screenshot show this logic:

# Now that I have my logic, how do I intertwine Python code with it?

I had hoped to put Python code as children nodes of my logic nodes, but I ended up creating a separate node and flipping back and forth to transliterate the logic into Python code. Screenshot of separate node:

# So my questions are:

1. How to test my Python code in Leo?
2. What is a better way to structure this .leo file so that the logic and code are interspersed?





Edward K. Ream

unread,
Oct 6, 2019, 12:26:17 PM10/6/19
to leo-editor
On Sun, Oct 6, 2019 at 11:10 AM The Living Cosmos <thequie...@gmail.com> wrote:



This is a silly, made up problem, which doesn't tell you much about a candidate, but let's ignore that :-)

It's also a very easy (but presumably tricky) problem, because it is completely self contained. For such problems, you can, and should, start with unit tests.

In Leo, here is the gist:

1. Create an @test node.
2. Within that node, create a table of inputs and expected outputs.
3. For each entry in the table, assert that the expected output matches the output of your code.

For a really simple problem like that, you can define the code in the @test node :-)

There are lots of unit tests like this in unitTest.leo.

HTH.

Edward
------------------------------------------------------------------------------------------
Edward K. Ream: edre...@gmail.com Leo: http://leoeditor.com/
------------------------------------------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages