Found a long ago forgotten Meyer. Used to learn OO from his books. Have now subscribed to this feed. Some things age really slow, if any.
Category: Coding
Useless but fun
As you, my dear reader, may have already noticed from my previous posts, I am currently teaching a course on how computers work for first year students. Very high level, simplified stuff since there’s only ten hours of lectures and ten hours of exercises for this.
One course exercise is about logic gates. After the lectures on the topic, students use the Digital Logic Sim tool to design some simple logic gates. Starting from AND and NOT gates, they build a NAND (the universal) gate and then OR, NOR and XOR gates using these gates in the project of theirs.

After this, students design two gates handling a byte (eight bits). Logic gate (or chip) ISZERO, checking if a byte has a zero eight bit integer value. ISEVEN gate checks if a byte is an even number.
Finally, they combine these two gates to check if the value in the byte is an even number and greater than zero. In the end they will meet the question whether the byte is actually signed or unsigned. Since then they need to take into account if the most significant bit (msb) is zero or one. In a signed byte, the msb value of 1 means the value of the byte is actually a negative integer.
Anyhows, in one of the lectures I mention that:
Anything that can be implement in hardware, can also be implement in software. And anything that is implementable in software, can also be implemented in hardware.
So a thought popped in my head out of nowhere: “Should I implement a demonstration in C that actually does implement these gates in software, starting from the boolean operators && and !, not using the ones already in the language?”
Of course I had to do that. Useless and fun. Suits me just fine.
Logic gates AND and NOT are quite simple to implement using boolean operators of C:
bool and(bool x, bool y) {
return x && y;
}
bool not(bool x) {
return !x;
}
Then implementing the NAND gate using NOT and AND:
bool nand(bool x, bool y) {
return not(and(x, y));
}
And the final example here, the OR operator implemented using three NANDS (compare this to the image of the OR gate above):
bool or(bool x, bool y) {
return nand(nand(x, x), nand(y, y));
}
The rest you can find following the link above, including a demo app in gates.c and an output file from the app.
What I would like to change in the implementation is to have two implementations of the ISZERO gate, since signed and unsigned bytes should implement this gate differently.
But enough of these uselessnesses. For today, at least.
Automation of boring tasks
My ongoing 1st year introductory course on computers has Moodle exams containing different kinds of automatically graded questions like:

We need to use automatically graded exams since there is 250-300 students each year, too few teachers participating, so evaluating the exams manually would take forever and would be very boring task to do.
Perhaps a necessary disclaimer: Everyone knows these conversions and arithmetics are, in practice, done oftentimes using software tools (programmer’s calculators) or web sites, when you need to. The actual learning goal here is not to say to students that doing this manually is something you have to learn to do for this profession. The learning goal is to expose the way data and simple arithmetics is handled in a computer and play around with these things to get an understanding of how computers work. And in time, you learn to see, for example, when debugging, some things that might be important in the hex, octal or binary values in memory.
Having a large set of questions for a course topic is a good thing to have. Then each exam generated for a student has different set of questions, randomly chosen by Moodle.
This poses another boring task problem. I would have to write tens or hundreds of these questions, choosing the values to convert or calculate, converting them to different radices, calculate the result and then use Moodle question editor to write the questions. And translate them to Finnish (or from Finnish to English), since I teach the course in two languages.
Well, what do you need when you have simple, boring repetitive tasks to do…?
A computer.
Well, a computer alone is just a bunch of metal, sand and plastics, so in addition, software would be needed.
Since I enjoy programming much more than writing Moodle exam questions, I implemented a command line tool using Swift. This tool generates random number conversion tasks (decimal to hex to binary) and arithmetic tasks (summing different values in different radices). Then these tasks are saved in a Moodle XML exam question format in a file.
./ConversionQs output-fi.xml 20 fi --verbose
The command above generates 20 conversion and arithmetic tasks each, in Finnish, outputs them to output-fi.xml file and shows me the progress in terminal window (--verbose flag). Below is an excerpt from an English output file:
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<question type="shortanswer">
<name>
<text>Convert between radixes</text>
</name>
<questiontext format="html">
<text><p>Convert the value 66 to radix: hexadecimal.</p> <p>Consider the values to be signed bytes, with eight bits.</p>
<p>Write in the answer the prefix for the radix (0x, 0b) asked, if it is not decimal.</p>
<p>Otherwise, use only the digits of the requested numbering system, no spaces or other punctuations.</p></text>
</questiontext>
<defaultgrade>2.0000000</defaultgrade>
<penalty>0.3333333</penalty>
<answer fraction="100" format="moodle_auto_format">
<text>0x42</text>
</answer>
</question>
...
Then I just import the XML file to Moodle, placing the questions into appropriate question category. Choosing a number of random questions from this set to an exam is an easy thing to do. Whenever I need a fresh set of questions, I just run the command again.
What a rise in productivity level of a teacher! Implementing this took around one working day. Now generating tens or even hundreds of exam questions happens in a fraction of a second, instead of hours and hours of mind consuming boredom!

Teaching season launched, new apps
Yesterday was the first day of teaching in this Fall semester. Times are a bit challenging; teaching resources for the courses I teach in have shrinked down to about 58-66% of last year’s teaching hours, depending on a course. Also the number of teachers for a course is down. While number of students is going up rather than down.
Face-to-face teaching is not possible with these resources and number of students (225-300+ in two of the courses ongoing currently). We’d be in the classroom every day from 8am to 8pm and still have overlapping teaching events. So online it is. Hello Zoom.
One feedback item from students last year was that the terminology is challenging in the Devices and data network course. Understandable: in five two hour lectures I should be able to give an overview of how computers work and the components they contain. The same for the networking, ten hours of lectures and they should get a grasp on how the Internet and packet networks do their thing. This means a constant shower of new terminology unfamiliar to 1st year students. Other courses have similar issues.
So I developed a dictionary app for students to use. Or actually two. One with Java for desktop computers:

And another one using Swift and SwiftUI for macOS, iOS and iPadOS:

Both apps get the dictionaries from the same source, a remote git repository with a JSON file, using HTTPS. Thus the dictionaries can be edited, appended to and updates are then downloaded to the users’ devices.
In the user device the terms are stored into a SQLite database, so they can be used offline. Users can search for the terms and follow the URLs to sources in the Internet to find out more.
The desktop versions also have a feature that generates a graph of the relationships between the terms by searching the terms from other terms’ description. For this, GraphViz must be installed on the device.
The Swift version has a Widget for all Apple platforms. The widget randomly shows a couple of terms a day from the dictionary. Here’s the iPhone widget (along with the GitHub app widget on top):

And here is the macOS Widget:

Clicking or tapping on the term on the widget opens the app and shows the description as well as English term for that specific ICT term.
The Java version is already public and available for installation. Swift version is still under development, I just need to decide how to distribute it to the various types of devices and prepare the App Store information with screenshots, descriptions and all. Then there is the inevitable App Store review waiting. Hopefully it’ll pass. This is yet another app for the category “downloads and renders JSON in a GUI”.
Hopefully the students find the apps useful for learning. Next I should focus on finishing the dictionary for the basic Internetworking part of my course and put also that available for the apps to download…
Mind the map
Another post about data structures, time performance and the count-the-words-in-a-book-file-fast case I’ve been writing about before.
I did a C++ implementation of the books and words problem. Earlier, I have implemented several solutions for the problem using Swift and Java. This time I used C++ standard library std::map and wanted to see if parallel processing in several threads would speed up the processing.
Obviously it did. Execution time of the multithreaded version was 74% of the single threaded version. The sample files were processed by the single threaded version in 665 ms, while the multithreaded version took only 491 ms. Nice!
But then I saw, from the documentation of the std::map, that it keeps the key values in the dictionary in order while elements are added to the map/dictionary.
But this is not needed in my case! Surely this also takes time and gives me additional possibilities in optimising the time performance.
I changed, in the single threaded implementation, the std::map to std::unordered_map, and behold, it was faster than the multithreaded version with 446 ms execution time!
So mind the map. There are many, and some of those may be more suitable to your use case than the others.
For details, see the project in GitHub.
Tabs or spaces
I used to be a tabs guy ages ago. Then switched to spaces. I don’t actually remember why and when. Maybe after arguing about this among fellow programming teachers at the university.
The debate around tabs versus spaces, some times quite heated, has been mostly about programming style. But today I saw a tweet that has a relevant argument for using tabs. Programmers using screen readers or braille readers lose valuable space because of spaces (pun intended).
Understandably it takes more space (!) and time to read e.g. nine spaces than three tabs, assuming the tab is configured to match three characters. It is all about accessibility.
So, I will be converting back to using tabs when indenting code.
New version of SortSpectacle
My sorting algorithm learning tool / demo app SortSpectacle now includes a new method, Block sort.
Block sort is a variant of Merge sort. Merge sort uses additional arrays to recursively divide the original array into smaller arrays which are then sorted and combined to a final sorted array.
Block sort is an in-place variant of Merge sort. It uses a small cache to move elements around. Thus it is more memory efficient than Merge sort. And according to the demo app, also quite fast.
I still need to implement the step-by-step version of the algorithm — without it the execution is not animated. But if you take a look a the implementation (a Swift port I did based on the Java code WikiSort implemented by Mike McFadden), you can see that it will be quite a challenge to do…
Also it was quite nice to read in Twitter that someone is actually using my sorting algorithms in their own Swift/SwiftUI app to demonstrate the new SwiftUI Charts in action!
Anyhows, here’s a YouTube demo video showing the new version in action:
Coverage fight
Well it was a fight but got the code coverage working with VS Code / Java and Coverage Gutters extension. Doing this for an exercise under preparation for the next Fall course on Data Structures and Algorithms. Probably this is just a demo or something students may take a look at, not a required task.

What I have in the Maven pom.xml currently is
<argLine>-Xmx2048m</argLine>
within the properties struct, and in the build/plugins:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>${argLine} -Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
</configuration>
</plugin>
and the whole jacoco plugin structure is here, under the build/plugins structure (with some commented out sections):
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!-- Ensures that the code coverage report for unit tests is created
after unit tests have been run. -->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${sonar.jacoco.reportPath}</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${jacoco.path}</outputDirectory>
</configuration>
</execution>
<execution>
<id>default-report</id>
<goals></goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.xml</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
</configuration>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.0</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
and finally, you need to build from the command line with this Maven command:
mvn jacoco:prepare-agent package jacoco:report
So when viewing the code then, you can see the code coverage colors in the code editor sidebar on the left.
Busy busy busy
I’ve been really busy with evaluating student coursework in two courses. Some of this work was postponed in Feb/March since I participated in the international MSc program selection process then.
I have been considering posting shorter news like things or just small snippets of work I have been doing. Just to keep this blog alive.
So here is a screenshot of something I did in between the course evaluations — potentially a new exercise to be used in the Data structures and algorithms course next Fall. A small utility that converts plain Finnish text to braille symbols.

The job for the students is to generate the braille version faster than the naive lookup table looping implementation they get ready, using hash tables.
Another thing I did was for a course on GUI design & programming:

The purpose of this stupid card guessing game was to demonstrate how to implement a simple GUI test automation and execute the tests in simulator environment.
That’s it for today. Now back to evaluating those student programming assingments…
Update to the previous post code
Just in case anyone is reading, I updated the code related to the previous post. It was about using Swift async tasks to process data in parallel, comparing it to the single threaded functional programming implementation. Due to updates to Swift (I assume?), the code started to fail (printout was messy, nothing too bad) so I fixed it. Probably my mistake, revealed by some change in the language or libraries I use.
Now the code in FunctionalParallel is using the async version of Swift ArgumentParser library. It is now also otherwise improved and (at least now) works correctly. I will measure the speed on the M1 Mini when I get to it and make any changes, if necessary, in the performance comparison table in the repository readme file.
Now I am out of home, looking after the granddaughter as she sleeps. Her parents (including my daughter) are out, celebrating my son’s birthday. Happy birthday son ?!