Tips for the challenges

ALIENQUIZ

Reading process output

When interacting with processes we need a way to read their output. Since read is blocking it is important to read a finite amount of bytes (usually 1), until a certain condition is fulfilled. The following function reads until a pattern appears in the string.

Solution to the exercise

We use read_until to solve the exercise proposed here. Since the program writes outputs of the form Write n: , where n is a value, we use read_until(p.stdout,': ') to capture each output. Then, the value n is extracted using a simple regular expression 'Write ([0-9]+): ' and sent back to the program (encoded as bytes).

Lambdas

For this challenge you have to deal with functions, and lambdas might be of great help. Lambdas are one line, anonymous functions, that you might have seen in other languages, such as Javascript.

You can apply it right away or store it in a variable or data structure.

You can use it, for example, to make a dictionary of functions that returns the correct operator indexed by its name

Python provides a useful module called operator that does a similar thing, but using lambdas is more insightful.