For the fun of it, showing beginners how to solve an everyday problem, here’s a Consensus Algorithm pseudo code. Pseudo code is a mixture of everyday talk and programming languages, to get your point across without it actually adhering to the syntax of one particular language.

For many years, MIT has inspired kids of all ages to learn to code using Scratch: https://scratch.mit.edu/. This has been translated to many languages and is a brilliant, free tool to get used to coding!

Apple has recently launched, in iOS 10, “Swift Playgrounds”. This seems absolutely brilliant and I can’t wait to show this to a couple of kids which I have high hopes for! http://www.apple.com/swift/playgrounds/

My little task is to let 10 participants choose between 3 options of ice cream. My code needs to check that the answer is a valid one.

The dictionary Merriam-Webster defines consensus as a «general agreement», not a «unison agreement». Therefore, if there is one flavour that has a larger vote than the other flavours, we have a democratic winner! http://www.merriam-webster.com/dictionary/consensus We want democracy, after all!

The program will only finish if there is actually a winner. Any tie of numbers will make it repeat the question again up until a limit of 5 ties have happened. After that it closes.

The main logic is to compare the numbers against each other. If 1 is not bigger than 2, it is either because 2 is bigger or because they are the same size. Compare against the 3rd number and again check if there are 2 of the same size. If all are different sizes then we have a bigger number in one of the 3 options. Even if we have two numbers that are the same size, is the 3rd number the largest? If not, if we don’t have a winner, we need to vote again.

- Is choice 1 bigger than 2? In that case, is 1 bigger than 3 also? If yes, 1 is the winner as it is larger than the others. If no, is 1 = 3? In that case, we have a tie. If 1 is
__not__equal to 3, then choice 3 is the biggest of all and is the winner.

- If 1 is
__not__bigger than 2, it is either because 2 is equal to 1, or because 2 is bigger. First check if 2 is bigger than 3. If yes, is 1=2? If 1=2 there is a tie. If not, 2 is bigger than both and is the winner. But, if 2 is not bigger than 3 because they are equal, then there is a tie. If 2 is not bigger than 3 and they are not equal, than 3 is simply the largest number and is the winner.

I have also created this algorithm as a Scratch project, just for fun. It was nice to test it! https://scratch.mit.edu/projects/88141292/

In order to have less repetition of code and maybe a slightly faster algorithm, pre-recorded messages for flavour comparison have been put into variables. Message1 receives the flavour name and inserts it into the message.

Procedure IcecreamChoice

Valid input list Items: «Strawberry, Vanilla, Chocolate»

Winner = false

FinishCount = 0

Message1 = “<flavour> is the chosen flavour.”

Message2 =”There is a tie. Please vote again.”

Message3 =”Error! Limit of tries reached. This procedure will now close.”

Repeat until Winner = true OR FinishCount > 5

Strawberry = 0

Vanilla = 0

Chocolate = 0

Counter = 0

Repeat until Counter = 10

Ask for Input

If Input is valid (compare to valid input list Items)

Set matching Item to Item + 1

Set Counter to Counter + 1

List current votes:

«Strawberry has » Strawberry « votes so far»

«Vanilla has » Vanilla « votes so far»

«Chocolate has » Chocolate « votes so far»

Else type «Not a valid input. Please try again.»

(end Repeat until Counter = 10)

If Strawberry > Vanilla then

If Strawberry > Chocolate then

Strawberry Message1

Set Winner = true

Else

If Strawberry = Chocolate then

Message2

Counter = 0

Set FinishCount to FinishCount + 1

Else

Chocolate Message1

Set Winner = true

Else

If Vanilla > Chocolate then

If Strawberry = Vanilla then

Message2

Counter = 0

Set FinishCount to FinishCount + 1

Else

Vanilla Message1

Set Winner = true

Else

If Vanilla = Chocolate then

Message2

Counter = 0

Set FinishCount to FinishCount + 1

Else

Chocolate Message1

Set Winner = true

(end Repeat until Winner = true OR FinishCount > 5)

If FinishCount > 5

Message3

(end Procedure IcecreamChoice)

Image source: Kerbstone, Pixabay, Public domain CC licence. https://pixabay.com/en/icecream-background-dessert-summer-993678/