Blog:My code golf snippets (with full explanations!)

As stated in the 4th pointer of the Trivia header on my user page, I am a programmer. Programming is one of my most favourite things that I do. To me, personally, programming can be considered to be a form of art. The code you write — no matter how short, long, elegant or ugly it is — has a unique feel and beauty of its own. You could see it only if you look deep into the code... But enough of introduction. Let's get to the main point!

I am very interested in something called code golf. "What is code golf?", you might ask.

According to Wiki-pee-with-dick:
 * Code golf is a type of recreational computer programming competition sport (or game) in which participants strive to achieve the shortest possible source code that implements a certain algorithm. Playing code golf is known as "golf scripting".

And admittedly, I am the worst code golf player. Some might say there are worse, but I do not care.

My first code golf snippets (in Python 3) were my entries for a code golf contest hosted by Engineer Man Knowledge Centre (Yes, I'm using the British spelling, which I prefer. Don't judge me.), a website created by Engineer Man and his crews. They are all mediocre, even for code golf standards.

So, let's take a look at these snippets!

"Oh Sheet"
"Oh sheet indeed."

- Upperdecker2562

cat ~/codegolf/oh-sheet.cg.py

Summary
First, we have a JSON object, containing a list of named sheets, each containing a two-dimensional-array of cells and their values. To return the value of a particular cell, the user enters:.

Example
JSON object: Human readable version:

How It Works
converts a hexavigesimal (a.k.a. Base26) value into a decimal number. For example, A=1, B=2, and so on.

(seperator) returns the index of the first digit of the input.

(sheet) parses the JSON file (or in this case, input) and converts the parsed data into a Python dictionary.

(command) is the main input, which is divided into two separate strings by.


 * 1) The program parses the JSON object (as input) first, then it parses the second input.
 * 2) If the name of the table matches the name in the second input, it proceeds to find the requested cell based on the index in the second input.
 * 3) If it finds the requested cell, it prints the value of that cell, using one of Python's robust features — F-strings.

"Back to Morse"
cat ~/codegolf/back-to-morse.cg.py

Summary
This is a program that translates Morse codes into strings.

Example
Supposedly we have a message to TigerBlazer:

We use the program to translate the message, and the program outputs:
 * hey you should watch pekora s streams on youtube peko Hey, you should watch Pekora's streams on YouTube, peko!

How It Works
translates a string into Morse code using lookup tables *sigh*.

For every character:
 * 1) The program looks through the lookup table.
 * 2) Then it checks which Latin letter corresponds to the Morse letter it receives.
 * 3) Finally, it translates that letter and prints the output. And yes, the program translates every letter of the string and prints it out one at a time.

"Strong Prime"
This was not an entry for EMKC code golf contest. cat ~/codegolf/strong-prime.cg.py

Summary
According to Wiki-pee-poo:
 * In number theory, a strong prime is a prime number that is greater than the arithmetic mean of the nearest prime above and below (in other words, it's closer to the following than to the preceding prime). Or to put it algebraically, writing the sequence of prime numbers as (p1, p2, p3, ...) = (2, 3, 5, ...), pn is a strong prime if pn >.

Example
The first few strong primes are
 * 11, 17, 29, 37, 41, 59, 67, 71, 79, 97, 101, 107, 127, 137, 149, 163, 179, 191, 197, 223, 227, 239, 251, 269, 277, 281, 307, 311, 331, 347, 367, 379, 397, 419, 431, 439, 457, 461, 479, 487, 499.

How It Works
(prime) checks if the number is a prime number; whereas  (strong prime) checks if the sum of the digits of a prime number is also a prime number.

And now for something completely different
Are you confused after reading all of these? If so, please take a look at this image to relax.



Comments
What do you think? Leave a comment!