Write pseudocode in repl.it. Execute it as Python. Learn Paper 1, learn computational thinking.
- Launch a GitPod workspace
- Once you're reached the workspace, add a new file
new.pseudo
- Enter pseudocode (for example
output "Hello World"
), save and exit - Type
pseudo execute new
and the code in the filenew.pseudo
executes - In the interpreter, enter
pseudo transpile new
and it outputs the python code that gets executed - Execute multiple files, it concats them for you
Practice writing pseudocode by actually running it, ensuring that it works. It also could be a way to learn Python.
The IB Pseudocode (formally specified here) is actually quite close to Python; it only takes about 200 lines of Python code to convert (transpile) it. This is why it might be a general purpose way to get your feet wet into programming.
The IB pseudocode specification includes four data structures, and students are expected to be able to interact with them to solve problems, based on a prompt.
It is common for the IB, and for other programming classes, to specify a programmable object that either 1) you are given or 2) you make yourself. In both cases, you need to interact with such objects. We'll call the former "given" objects and the latter "my" objects.
Here are some examples of simple prompts that show both GIVEN*
and MY*
objects:
- Create a collection called
MYCOLLECTION
that holds all prime numbers from 1 to 100. - Output all the names in the list called
GIVENNAMES
- You have a list of names called
GIVENNAMES
that contain 20 string values and you need their last names inMYLASTNAMES
- You have a list called
GIVENNUMBERS
of 100 random integers from 1 to 100 (inclusive) and you need to find the sum - You have a collection of names called
GIVENANGELS
and you need to count how many of them there are
In the actual exam, when giving answers, you won't have to create or populate these GIVEN*
objects. You can write code as if they are already set up correctly. In the actual exam, however, you'll obviously need to create and populate the datastructures.
In order go run code that actually works, we need to create and populate the GIVEN*
objects with example values in our environment, even though we don't have to do that in the target environment (the actual exam).
We'll also need to initialize MY*
objects so that they can be populated in our code.
The section below explains this in more detail.
The IB Pseudocode code specifies four datastructures: List
, Collection
(SL & HL), Stack
, and Queue
(HL). This repo provides classes according to the specification, so you can create such objects (which under the hood, uses Python's OOP approach).
For example, in #1 above, you need some way of creating a collection called MYCOLLECTION
that has a bunch of names. In this case, we just need to use this:
MYCOLLECTION = Collection()
And then you can do this later in your program:
MYCOLLECTION.addItem(3) // add prime number"
To set up our given object in example #2 above, you could do this:
GIVENNAMES = List()
GIVENNAMES[0] = "Aaron"
GIVENNAMES[1] = "Adam"
...
and so on, but that would be painful. Instead, wouldn't it be great if we could just make a new file and type the names we need on twenty lines? You can do that:
GIVENNAMES = List.from_file('students.txt')
Make a new file, call it students.txt
and type one name per line, and GIVENNAMES
will be populated with that information upon execution.
For #4 above, you do it with another convenience function:
GIVENNUMBERS = List.from_x_integers(100, min=1, max=100)
Finally, here is the full solution for problem #5:
GIVENANGELS = Collection()
loop while GIVENANGELS.hasNext()
ITEM = GIVENANGELS.getNext()
output ITEM
In the actual exam, you wouldn't need to write the first line.
All four data structures have the following convenience functions to easily populate a data structure.
Where Class
below is either Array
, Collection
, Stack
or Queue
:
item = Class.from_file(name)
item = Class.from_x_integers(how_many, min=1, max=100)
item = Class.from_array(list)
If you are curious how this all works:
- The repo which implements
transpile
,execute
, andrun
is written as a command line utility, using the great click package - The transpiling itself is just a bunch of regexp with some defined classes
- The excution occurs with Python's
exec
function, where for the globals parameter includes a link to the four classes that represent the four data structures (which allows us to initialize and interact with such objects)
Why did I do it that way?
- Students can actually program "real" code that actually executes on the computer
- It started out as an experiment on how to share code with students
- Since it downloads the repo from github, it's portable: I can improve it and all forked projects (student-created) can benefit from improvements
How did I test it to ensure correctness?
- All of the example code runs correctly with the example code
- If there are any issues, I can fix and all subsequent instances will enjoy the benefits