Runoff is a ranked choice voting system. It has the benefit to provide a winner that better reflect the voters’ preference. To make that work, each voter rank the candidates in a preference list. If any candidate has a majority of the first vote preference, that candidate is declared the winner. If no candidate has more than 50% of the votes, an instant runoff takes place. The candidate that received the least amount of votes is eliminated from the election, anyone that had previously selected that candidate as their first preference now has their second preference considered. …
Plurality is a fairly simple introduction exercise to the next on the problem set, either you choose the less comfortable route with Runoff or the more comfortable with Tideman.
In Plurality we need to organize a simple election. The main function and headers are given by the course as a distribution code. We can’t change the distribution code or else we fail the staff tests. Our task is to implement the function that updates the voting count for each candidate and the function that print out the winner, or winners, as this simple model of election allows for frequent ties…
This time we’re preparing to code a substitution cipher. Instead of getting a number for key, we’ll be getting a string. A 26 character long string to be more exact, where each character will replace it’s equivalent index on the alphabet.
“A key, for example, might be the string
NQXPOMAFTRHLZGECYJIUWSKDVB. This 26-character key means that
A (the first letter of the alphabet) should be converted into
N (the first character of the key),
B (the second letter of the alphabet) should be converted into
Q (the second character of the key), and so forth.”
$ ./substitution YTNSHKVEFXRBAUQZCLWDMIPGJO
Although it may be obfuscated by the salad, the real Caesar did have his fair share of inventions. One of them, supposedly, is a simple cryptography system that consists in adding a number, or “key” to a letter, transforming it on another letter. A jump of positions based on said key. Here’s a working code example of “Hello” with key = 1.
$ ./caesar 1
Unencrypted text is generally called plaintext. Encrypted text is generally called ciphertext. And the secret used is called a key.
So, how it works for the code we’re supposed to write?
Readability applies The Coleman-Liau index formula to a text. The Coleman-Liau index of a text is designed to output what (U.S.) grade level is needed to understand the text. The formula keeps track of letters, words and sentences to return that index.
index = 0.0588 * L - 0.296 * S - 15.8
Here, L is the average number of letters per 100 words in the text, and S is the average number of sentences per 100 words in the text.
Here’s an example output:
Text: Harry Potter was a highly unusual boy in many ways. For one thing…
Credit is the point where things start to get complicated. We need to work with a lot of big numbers and not only do mathematics with then but learn how to travel trough the number to access specific digits in its composition. We learn a little about the cryptography behind each card number and how to use it to check if the card is valid and from which provider it comes.
As always we begin preparing our directory. Assuming you already have the pset1 directory, let’s type on our terminal:
~/ $ cd pset1
~/pset1/ $ mkdir credit
~/pset1/ $ cd credit
Cash is the easier project we work on the entire course (provided you’re using the CS50 library). It basically asks us to count how many coins we need to use to return a change. Here the cashier wants to use the least amount of coins possible, not mattering how many coins of each are in the cashier’s drawer.
To begin, we open CS50 IDE through the Menu in the course page or by typing ide.cs50.io on your browser. Then procced to create our cash project folder.
You’ll notice that now we have…