Mon 10 March 2025
Flash Cards
A while back I found out that Jnr. Doctors are keen on using flash cards to help them revise for exams. It occurred to me that I could also use flash cards to drill some of the concepts that would come up in discussions about software systems and system design.
At the time I was developing on a raspberry-pi with a 7-inch screen and I was limited to how much I could install on the little SD card. At first I tried to use a browser based flash card tool, but I was in the mood for doing some deep dives to understanding how these actually work under the hood.
This led me to develop Kanki a bespoke flash card tool for the command line. In this blog post I break down the process I typically use to undertake a software project of this scale (i.e tiny scale).
Requirements
Like all good software projects we start with the high level expectations. What do we want this system to do? (Some of these are quoted straight out of my notebook.
- I want to have 1000+ flashcards based on software and system design
- I should be able to hide the answer, I should be able to reveal the answer.
- Can I input "correct", "close", "wrong" etc and update an SR algorithm to handle recall.
- Nice to have, filtering by topic.
In order to understand how flash cards operate you'd need to dig into figuring how the SR works:
Spaced Repetition: Newly introduced and more difficult flashcards are shown more frequently, while older and less difficult flashcards are shown less frequently in order to exploit the psychological spacing effect. The use of spaced repetition has been proven to increase the rate of learning. - Wikipedia
Entities
We need to know what things we will be working with:
Card |
---|
- Question (string) |
- Answer (string) |
- Topic (string) |
- DeckID (int) |
The card is the question and answer entity which I might have 1000 of (In the end I think I had about 60).
Deck |
---|
- Id (int) |
- Name (string |
The deck is the entity which groups cards, so a card belongs to a deck and this allows me to have cards around separate topics.
Interface
This isn't far off from what I drew in the notebook but clearly I had a vision and the following sort of captures it at the time:
I want to have a view which allows me to load a specified deck. Create a new deck or add cards to an existing deck.
Load: -> deck_a, deck_b
New: (deck)
Add: (cards)
I want to display the different inputs I can provide as answers to each card.
Again, Hard, Good, Easy
< 1min < 6min < 10min 4 days
These are some of the settings I'd like to fine tune as I get used to playing with the flashcard system.
config:
new cards / day
review cards / day
Spaced Repetition Algorithm
This is where things get slightly more involved since I had to understand how Anki and other flash card apps applied repetition. It was mostly finding the one that suited me best and felt the most natural to play with.
This is the algorithm I went for:
step 1: Cards have a queue
field (set to 1 for 'learning') and a
due
field (in minutes) determining when a card should be shown again.
step 2: review cards, already studied but are now due to
be relearnt. queue
field set to (2) and due less than or
equal to today.
step 3: 'new', queue is set to (0), these new cards are capped.
Essentially this determines how we populate the flash cards during a session. Using the three queue types:
- 0:
new
- 1:
learning
- 2:
review
- 3:
relearning
(This is essentially a card deemed as "learnt" but you've forgotten it and now it needs to be relearnt).
Properties of a card.
- due: the timestamp for when the card should be shown again.
- left: how many learning steps before the card graduates to review.
- reps: how many times the card has been shown.
- ivl: tracking the card interval (time to add when recalculating due).
Input
When answering the flash cards there's four inputs you can provide, each of which will cause a different effect, these decide the cards position in one of two or three queues. The result of these inputs also depends on which queue the card is currently in. You can see this here
Again
: Reset number of training repsHard
: +1 rep, show again in 1 minGood
: +1 rep, show again in 5 minEasy
: +1 rep, show again in 1 day
Development
Up until this point I hadn't done a single line of code. The research done above was undertaken before I started writing any code. This helps me have a clear understanding of what I'm actually trying to do. It's far easier to scribble away in my notebook that it is to change code.
There are a few things I needed, which I learnt after I'd written the tool. Things such as being able to edit a card, but these were not major features.
The other thing that I find interesting (or obvious) is solution for the queue. During a learning session you need to have the card queues be dynamic since you're popping the earliest due card and then you're sorting them by when they are next due so the implementation fits using a heap quite well. You can see me initialising the heap here.
Conclusion
I really enjoyed reading up on some of the complexities involved in getting the spaced repetition working in a way that allows new topics to be slowly introduced while working on cementing topics that were in the progress of being learnt. It feels a little exciting when you encounter something new during the learning session. I used kanki to prepare on some topics and every so often add new cards to system design.
One thing I found quite useful is getting someone else to ask you the questions verbally and they make a judgement on how close you are to the answer. This can get you more comfortable with talking about these topics and the person reading the questions can help keep you honest about how well you know a topic.
Lastly it's also interesting that learning is effectively broken down into three separate speeds. 1. This is new. 2. I'm actively learning this. 3. I should know this.
Mon 31 August 2020
Python Deque
This is now my third article on lists. As someone that uses the built-in python list on a fairly regular basis, I might have built up a false sense of security. I'm pretty familiar with these listy-boys. However, recently I found out that I was not thinking about them correctly. Readers might smack themselves if they're familiar with data-structures but don't know how lists are implemented internally. The built-in lists are dynamic arrays.
How else could they optimise a sweet O(1)
lookup time
on indexing: mylist[4]
. Especially when analysts are
trying to avoid the built-in iterator and cursing their code
with: for i in len(mylist): mylist[i]
.
Another trait an established data-structurer
with be familiar with when it comes to dynamic arrays
is that the append
and pop
methods are an amortised
O(1)
. Amortised; because occasionally you have to suffer
a cost of realloc(ating) memory across larger arrays.
Where the list starts to suffer is from pop
ing and
insert
ing at arbitrary positions.
Linked-List
The data-structurer will have had the linked-list
slammed into their head often enough that it will
pain them to hear about it again. So theory aside,
I'll give you that sweet O(1)
append
and last item pop
that you expect from a performant Stack
.
Python deque
provides a comparatively larger
performance hit on initialisation to list
and
has poor O(n)
performance when you want any arbitrary
item somewhere in the middle. It does, however, have
O(1)
; popleft
, pop
, append
and appendleft
. Due
to being a doubly-linked list (or double-ended queue to
get the abbreviation deque
)
Deque in the wild
I saw a nice little quote from an enginneer on Quora:
In 8 years of getting paid to write computer programs, this post is the only time I’ve typed ‘deque.’
There are many places deque
is used in the stdlib, most
commonly whenever someone needs a queue
or stack
such as
constructing a traceback, parsing python's sytax tree and
keeping track of context scope.
My little run-in with deque
was using it instead of a
recursive function to avoid python's
maximum recursion depth exceeded
This limit happens to be set to 10^4
. The solution was
to add child nodes to a deque
and when you were done with
analysing the current node, popleft
the next node.
Python Queue
You might be tempted to ask, well if deque
is for queues.
What on earth is from queue import Queue
.
These queues are different (although, still using deque
under
the hood). They are optimised for communication across threads,
which need to involve locking mechanisms and support methods like
put_nowait()
and join()
. These are not intended to be used
as a collective data-structure, hence the lack of support for
the in
operator.
More information
There is some neat documentation in the cpython repo which
contains more data-structures and other alternatives to
the standard built-in list
. Tools for working with
lists
References
- How are lists implemented:
- https://stackoverflow.com/a/15121933/3407256
- https://stackoverflow.com/a/23487658/3407256
Thu 06 December 2018
Chain of Responsibility
Easily one of my favourite patterns from the gang of four is the chain of responsibility pattern. It aims to avoid coupling the object that sends a request to the handlers of the request. This is especially useful if the structure to our handlers follows a sense of hierarchy.
I've had very few opportunities to implement design patterns, but gaming has really helped me to envision a context where several designs can be applied as handy solutions to managing complexity.
So we will apply the chain of responsibility to a situation where we have gathered four of the most powerful wizards in a room, each of whom will test their power by casting a spell.
For this example we have our wizards as the objects which send requests.
class Wizard:
"""Create a wizard."""
def __init__(self, name: str, intelligence: int):
#: Identify the wizard by name
self.name = name
#: Intelligence as a proxy of a wizard's power.
self.intelligence = intelligence
def cast_spell(self, spell: Spell):
"""Have the wizard cast a spell."""
print(f"{self.name} casts {spell.name} spell.")
spell.cast(self)
We will make each behaviour of the spell, which is determined by the power of the wizard casting it, as an object handling a request. If the wizard does not meet the handler's requirements it passes the request on, to it's successor. This is where we have applied a sense of hierarchy to the handler.
To begin we have an Abstract Handler:
from abc import ABC
from abc import abstractmethod
class AbstractHandler(ABC):
def __init__(self, successor=None):
self._successor = successor
def handle(self, creature):
reaction = self._handle(creature)
if not reaction:
self._successor.handle(create)
@abstractmethod
def _handle(self, spell):
raise NotImplementedError("Must provide implementation in subclass.")
Now we can define our handler hierarchy:
class LowPowerFireSpell(AbstractHandler):
def _handle(self, creature):
if creature.intelligence < 10:
print("Spell backfires.")
return True
class MediumPowerFireSpell(AbstractHandler):
def _handle(self, creature):
if creature.intelligence < 20:
print("Small fire ball is cast.")
return True
class HighPowerFireSpell(AbstractHandler):
def _handle(self, creature):
if creature.intelligence < 30:
print("A fire ball blazes across the room")
return True
class GodlikePowerFireSpell(AbstractHandler):
def _handle(self, creature):
print("A Massive column of fire burns through the room!")
return True
Finally a single object to identify the spell chain.
class Spell:
def __init__(self, name: str):
#: Identifying the spell by a name
self.name = name
#: How the spell behaves at differing levels of user's power
self.chain = LowPowerFireSpell(
MediumPowerFireSpell(
HighPowerFireSpell(
GodlikePowerFireSpell()
)
)
)
def cast(self, creature):
self.chain.handle(creature)
To test their skill, we have a spell which casts a "Fire Ball". We have the following wizards present:
if __name__ == '__main__':
fire_spell = Spell('Fire Ball')
merlin = Wizard("Merlin", 8)
albus = Wizard("Albus", 18)
howl = Wizard("Howl", 28)
gandalf = Wizard("Gandalf", 38)
They are all gathered in a room, and take turns casting the same spell.
room = [merlin, albus, howl, gandalf]
for wizard in room:
wizard.cast_spell(fire_spell)
print("")
To which we should see the spell behave according to their intelligence:
(myenv) pc-name ~ $ python script.py
Merlin casts Fire Ball spell.
Spell backfires.
Albus casts Fire Ball spell.
Small fire ball is cast.
Howl casts Fire Ball spell.
A fire ball blazes across the room
Gandalf casts Fire Ball spell.
A Massive column of fire burns through the room!
(myenv) pc-name ~ $
Thu 11 October 2018
Iterator Design Pattern
Before I started reading the gang of 4 ["Go4"], I was convinced I would not need an iterator. After all, in python, they are already implemented:
x = ['a', 'b', 'c']
for i in x:
print(i)
In the frame of python lists as aggregate objects, the intent of an iterator is satisfied.
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representations.
Displaying notes
Here I explain, how the iterator became my favourite design pattern.
I have written about representation of lists before, and I don't think this will be my last time. When implementing the drop-downs in foolscap to allow a user to see sections contained in their note, I ran into an issue of complexity where I had blocks of conditionals dictating what should be displayed to a user. I wanted the user to have an indication that a note contains sections, and for them to be able to toggle a drop-down to see each section. Similar to:
(+) | circleci |
| docker |
Where my "circleci"
note would contain sections
and the "docker"
note does not. Then expanding:
<-> | circleci |
└─ | -workflow |
| docker |
This is where I realised the abstraction power of an
iterator, And I could hide the collapsed sections
behind an "if"
conditional in an iterator.
class Menu:
def __init__(self, items):
self.items = items
def visible(self):
"""Yield the next appropriate item for display."""
for item in self.items:
yield item.title
if hasattr(item, 'expand') and item.expand:
for sub_item in item.sub_items:
yield sub_item.title
Supporting further traversal policies in my Menu
class is straightforward, and
drawing becomes absurdly simplified:
def draw(menu):
"""Draw all viewable menu items."""
for item in menu.visible():
draw_item(item)
After this I thought of a more complex aggregate structure that I could traverse, like a tree depth first.
Realising the abstraction strength of the iterator, one finds its definition of intent far more compelling.
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representations.
Tue 09 October 2018
Procedural Villages
My inspiration for any tool or project has always been the thought that it could be done better, either that or I'd feel like I would benefit a lot from a missing feature.
In my first embarked I attempted to create a dungeon crawler, full of all the creatures and characters I thought were missing from traditional games.
It was through brute-force of the Roguebasin tutorial where I learnt how to code.
I must have created the same game 4 or 5 times before I decided to scrap the libtcod library and create a game without an interface.
Armed with stdout
and turn-based printing, I
implemented feature after feature. One of the
features I was quite proud of was the randomly
generated villages I'd spawn my player into.
Objects
I was aiming for something simple, just populate
the map with houses that the player can interact
with, for this I deconstructed a house
into a
rectangle.
class Rect:
def __init__(self, x, y, h, v):
self.x1 = x
self.y1 = y
self.x2 = x + h
self.y2 = y + v
def center(self):
center_x = (self.x1 + self.x2) / 2
center_y = (self.y1 + self.y2) / 2
return (center_x, center_y)
def internal(self, x, y):
"""
[ ][ ][ ][ ]
[ ][X][X][ ]
[ ][X][X][ ]
[ ][X][X][ ]
[ ][ ][ ][ ]
"""
...
return bool()
def edges(self, x, y):
"""
[X][X][X][X]
[X][ ][ ][X]
[X][ ][ ][X]
[X][ ][ ][X]
[X][X][X][X]
"""
...
return bool()
def sides(self, x, y):
"""
[ ][X][X][ ]
[X][ ][ ][X]
[X][ ][ ][X]
[X][ ][ ][X]
[ ][X][X][ ]
"""
...
return bool()
The poorly defined object above could now provide boolean confirmation to a co-ordinates existence in appropriate sections of a rectangle.
From the above Rect
class I can place a door on
a Rect.sides()
and I can fill the area
defined by Rect.internal()
with items.
Empty space: | . |
Wall: | # |
Monster: | m |
Door: | + |
| . | . | . | . | . | . |
| . | # | # | + | # | . |
| . | # | m | . | # | . |
| . | # | . | . | # | . |
| . | # | . | . | # | . |
| . | # | # | # | # | . |
| . | . | . | . | . | . |
Throwing in some size variations and randomising the house position on the map, (making sure there are no houses intersecting each other). Allowed me to generate maps that looked like these:
Example 1
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | # | # | # | # | # | # | . | # | # | # | # | . | . | . |
| . | # | u | . | . | @ | # | . | # | . | m | # | . | . | . |
| . | + | . | . | . | . | # | . | # | . | . | + | . | . | . |
| . | # | # | # | # | # | # | . | # | # | # | # | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | > | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | # | # | + | # | # | # | . | . | . | . | . | . | . | . |
| . | # | . | . | r | . | # | . | . | . | . | . | . | . | . |
| . | # | . | . | . | . | # | . | . | . | . | . | . | . | . |
| . | # | # | # | # | # | # | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
Example 2
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | # | # | + | # | . | . | . | . |
| . | . | . | . | . | . | . | # | . | . | # | . | . | . | . |
| . | . | . | . | . | . | . | # | . | . | # | . | . | . | . |
| . | . | . | . | . | . | . | # | m | . | # | . | . | . | . |
| . | . | . | . | . | . | . | # | # | # | # | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | . | . | . | . | # | # | # | # |
| . | # | # | # | # | # | # | # | . | . | . | # | . | . | # |
| . | # | . | . | . | . | u | # | . | . | . | + | . | . | # |
| . | + | . | . | . | . | . | # | . | . | . | # | > | . | # |
| . | # | X | . | . | . | . | # | . | . | . | # | . | r | # |
| . | # | # | # | # | # | # | # | . | . | . | # | # | # | # |
| . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |
Unfortunately my code was not beautiful back then. I still think it was a neat idea