Lottery as a Smart Contract: The Business Logic

Lottery as a Smart Contract: The Business Logic

Writing a lottery contract is a great way to learn important Smart Contract concepts such as State, Event and Visibility. In this 3 part series, I will describe the logic and codes behind a Lottery Smart Contract. In developing my Lottery Contract, I used Oraclize, a data carrier service that executes external JSON web services.

Motivation

Codility before lunch

Codility before lunch

Like how new recruits in the army are required to do a few pull-ups before lunch, I make myself complete at least 2 Codility challenges a week. If nothing, it helps to keep the mind thinking algorithmically - which I believe is a good thing.

This challenge requires one to cycle through a series of numbers in an array for N times. For example, given the array A = [3,8,9,7,6], to cycle this function once, it becomes A = [6,3,8,9,7]. Notice that the last element becomes the first, and then every other element moves 1 place up. 

To cycle it twice, it becomes A = [7,6,3,8,9].

More Codility

More Codility

The 2nd Codility lesson is to search for the odd number of elements in an array that cannot be paired with any other element. Codility provided this example.

A[0] = 9, A[1] = 3, A[2] = 9
A[3] = 3, A[4] = 9, A[5] = 7
A[6] = 9

Apart from A[5], every other element has a match. The first thought is to iterate through the array and look for the pair and remove both elements once it is found. When an element cannot find its pair, break from the loop and return the element. Simple enough, so I coded. 

Learning on Codility

Learning on Codility

Codility is where programmers receive programming tests as interview questions. This is also where programmers could learn to code. I am at lesson 1 on Iterations, and here's my answer. It finds the binary gap of a number. A binary gap is the longest sequence of 0 of a number converted to binary. For example, the number 5 is 101 is binary, and it contains 1 zero, so it's binary gap is 1. The number 1041 is 10000010001 in binary. It contains 2 sequences of 0, the first of which is 5 zeros long and the second is 0 zeros long.