r/dailyprogrammer – Yahtzee Upper Section Scoring

Here’s the source.

Description

The game of Yahtzee is played by rolling five 6-sided dice, and scoring the results in a number of ways. You are given a Yahtzee dice roll, represented as a sorted list of 5 integers, each of which is between 1 and 6 inclusive. Your task is to find the maximum possible score for this roll in the upper section of the Yahtzee score card. Here’s what that means.

For the purpose of this challenge, the upper section of Yahtzee gives you six possible ways to score a roll. 1 times the number of 1’s in the roll, 2 times the number of 2’s, 3 times the number of 3’s, and so on up to 6 times the number of 6’s. For instance, consider the roll [2, 3, 5, 5, 6]. If you scored this as 1’s, the score would be 0, since there are no 1’s in the roll. If you scored it as 2’s, the score would be 2, since there’s one 2 in the roll. Scoring the roll in each of the six ways gives you the six possible scores:

0 2 3 0 10 6

The maximum here is 10 (2×5), so your result should be 10.Examples

yahtzee_upper([2, 3, 5, 5, 6]) => 10
yahtzee_upper([1, 1, 1, 1, 3]) => 4
yahtzee_upper([1, 1, 1, 3, 3]) => 6
yahtzee_upper([1, 2, 3, 4, 5]) => 5
yahtzee_upper([6, 6, 6, 6, 6]) => 30

Opional Bonus

Efficiently handle inputs that are unsorted and much larger, both in the number of dice and in the number of sides per die. (For the purpose of this bonus challenge, you want the maximum value of some number k, times the number of times k appears in the input.)

yahtzee_upper(
    [1654, 1654, 50995, 30864, 1654, 50995, 22747, 1654, 1654, 1654, 1654, 1654, 30864, 4868, 1654, 4868, 1654, 30864, 4868, 30864]
) => 123456

Solution

Answering this problem is pretty trivial. All you need to do is a create another list that contains all numbers from 1 through the maximum number in the inputted list. Then multiply each number in the new list by the number of time it appears in the old list using the count function native to python. Here’s how it goes:

def yaht(t):
    return max([t.count(j) * j  for j in [i for i in range(max(t)+1)]])
    
# that's all there is to it!

The Foundation Series

This post is sponsored by sheer excitement. May contain spoilers.

It would be accurate to say that the defining aspect of this brilliant series is how ‘human’ everybody is. Human flaws, intellect, and experiences form every single plotline. Whether be it underestimation sanctioning the Mule’s rise to power, or the outrage at Seldon’s predictions of the collapse of the far-reaching Galactic Empire. Every single facet of this book is page-turning. It has everything, from planets coated in metal to spaceships that sync with the mind. The Foundation Series is a must-read for anyone out there.

What I also love is how absorbing the story is even though it spans a thousand years. Attachments to characters are disbanded. The only thing that does remain constant is the inevitability of psychohistory. This allows Asimov to toy around with dozens of characters, making the book pretty unique. Here are some characters I can recall off the top of my head:

  • Hari Seldon (psychohistorian)
  • Hober Mallow (Trader and later Mayor)
  • Salvor Hardin (First Mayor of the Foundation)
  • The Mule (Mutant with powers of emotional manipulation)
  • Bayta Darell (Civilian who stopped the Mule)
  • Arkady Darell (Played like a doll by the Second Foundation)
  • Golan Trevise (Big decision-maker)
  • Munn Li Compor (Councilman)
  • Bail Channis (unconverted military general)
  • Hann Pritcher (converted military general)
  • Bel Riose (ambitious military general)
  • Janov Pelorat (Historian trying to find Earth)
  • Preem Palmer (First Speaker)
  • Stor Gendibal (Speaker)
  • Ebling Mis (Psychologist)

The number of locations involved is fascinating as well. From planets near the galactic centre to those in the outer rim. From worlds abounding with radioactivity to kingdoms at Star’s End. From isolated global consciousness established by rogue robots to pleasure worlds like Kaglan.

Every action has a consequence. Everything plays a part in the larger scheme. Economic and social forces dictate everything. However, in an interesting twist, the only thing that threatens fate is individual actions. The ability to think, see the truth as it is, not as it should be, hampers the Seldon plan. For me, this is inspiring. After all, it does support the view that even one person can turn the world on its head. The Foundation series makes me appreciate certain qualities I never acknowledged before. It makes me desire the iron will of Salvor Hardin. It makes me search for the mental powers of the Second Foundation within myself. It makes me quest for the genius of Hari Seldon..

It would be foolish to express my awe in a single blog post. Pay up. Read this masterpiece of a series. It will open you to aspects of human nature that pass unnoticed. It advertises fallacies and advocates for morality yet sureness.

Here are some amazing quotes from the series.

“Violence is the last refuge of the incompetent.”

Hari Seldon

“Never let your sense of morals prevent you from doing what is right.”

Salvor Hardin

This one’s pretty chilling. Pretty applicable to the real world as well.

“Scientific truth is beyond loyalty and disloyalty”

Hari Seldon

“It’s always easy to explain the unknown by postulating a superhuman and arbitrary will”

Homir Munn

So true.

“The advance of civilization is an exercise in nothing but the limiting of privacy”

Janov Pelorat

September ’19 – Summary of Reading

Pebble in the Sky; Isaac Asimov – Went from a mundane storyline at the start to a brilliant ending bursting with energy. I found it really hard to read this book at the start, primarily due to the massive amount of time and content Asimov spent on setting up stuff. However, as soon as Schwartz escaped, the book magnificently picked up the pace and had me invorigated right till the end. I think it was interesting to see non-Earth people treating Earth people the same way some humans treat other humans, an eye-opener for me.

I, Robot; Isaac Asimov – A storyline composed out of distinct short stories didn’t look impressive to me before I started reading the book, but I was drastically wrong. I think the definitive quality of this book is the way it represents the future. Furthermore, the sheer amount of advancement that is palpable throughout the book excited me a lot. What I absolutely loved was how human elements were combined with robotics, leading to stuff like robopsychology, cyber-sentience, and paradoxes in reality. The last chapter was a bit boring, but on an overall basis, definitely one of the better books I’ve read.

The Godfather; Mario PuzioA well-written book, full of suspense and dynamics, and successful at creating a constant sense of danger. I enjoyed how Puzio switched from character to character and exploited every possible plotline – inevitably creating contrasts and ironies. Every single action had a consequence. That being said, there were some aspects I didn’t like that much. Luca Brasi’s death really hit me hard, and it seemed pretty avoidable and impossible as well. On an overall basis, one of the better books I’ve read, certainly not the best, but a memorable read nonetheless. Puzio’s repeated emphasis on the allure of maintaining composure and calm, and the foolishness of succumbing to anger and paranoia was one of the more concrete messages that got to me.

Automate The Boring Stuff; Al Sweigart – Nice, enthralling introduction to how the powerful the python language can be when used properly. I especially enjoyed the chapters on working with APIs and web browsers. That being said, as a consequence of the book having already become really old, there are various outdated practices that could prove to be misleading. I also don’t get why there’s so much on manipulating data when the far better alternative of pandas already exists.

The Stars, Like Dust; Isaac Asimov Just amazing. This book incorporated everything from rebellion planets to emotional manipulation, had treachery and plots running beneath pre-existing sub-plots. Every page offered a monumental event, every chapter a higher mountain to climb. The end did seem a bit cringy to me, with a breadcrumb being offered for US politics, but it was nonetheless inspiring. The characters were intricately crafted, and the entire affair of good guys being bad guys and bad guys being good guys had the effect of keeping one on the edge of his/her seat. Perhaps the most brilliant aspect of the book, the thought, and later truth, of the rebellion planet not being in a mysterious nebula but instead in plain sight, and the true nature of the director being hidden away in plain sight as well managed to enthrall me.

Locking and Unlocking .pdf files

Here, I’ll be showing you how to set the same password for all .pdf files in a directory simultaneously using python, instead of manually setting passwords for each pdf. This comes in handy for files with over hundreds of .pdf documents. The full code for setting passwords can be found here, and the same for unlocking files is available here.

We will be using the PyPDF2 and os modules. To start off, we’ll get the user to input their password and root directory. Then, we’ll use the os.walk()function to iterate over each file in that directory and its sub-directories.

for (root, dirs, files) in os.walk(path):
	for filename in files:
		if filename.endswith('.pdf'):
		    pdfFile = open(root + '/' + filename, 'rb')
			pdfReader = pypdf2.PdfFileReader(pdfFile)

Above, we store the result of each ‘walk’ as a three-element tuple. We then access each file in the current directory, and use an if statement to check whether or not the file concerned has a .pdf extension. Then, we open the file in a read binary format, and also initialise a pdfFileReader object for the file.

Now, to make an encrypted pdf using PyPDF2, we’ll read one file and copy over its contents into another file, then encrypt the second file and delete the first. Do note that opening the first file in a write binary format won’t delete it, it’ll delete the data inside the file, but not the actual file. To delete files, we can use the os.remove() function.

# inside 'endswith('.pdf')' if

if pdfReader.isEncrypted == False:
    pdfWriter = pypdf2.PdfFileWriter()
    for pageNum in range(pdfReader.numPages):
		pdfWriter.addPage(pdfReader.getPage(pageNum))

	pdfWriter.encrypt(password)

	if filename.endswith('decrypted.pdf'):
		resultPdf = open(root + '/' + filename[:-13] + 'encrypted.pdf', 'wb')
	else:
		resultPdf = open(root + '/' + filename[:-4] + 'encrypted.pdf', 'wb')

	pdfWriter.write(resultPdf)
	os.remove(root + '/' + filename)
	resultPdf.close()
	pdfFile.close()
	

Now, we firstly check whether the file in question is encrypted or not. If not, we initialise a PdfFileWriter object, and copy over the contents of the file to the object. Using the user-inputted password, we encrypt the new file and then save it with a suffix of ‘_encrypted.pdf.’ Then, the os.remove function is used to delete the starting file.

Unlocking files has the same methodology. Using a python program also saves a lot of time when say unlocking hundreds of files. Once again, we use os.walk() to traverse our root directory. Here, we also check for whether the file is encrypted, and proceed only if it is.

if pdfReader.decrypt(password) == 1:
    pdfWriter = pypdf2.PdfFileWriter()
    for pageNum in range(pdfReader.numPages):
        pdfWriter.addPage(pdfReader.getPage(pageNum))
	
	if filename.endswith('encrypted.pdf'):
	    resultPdf = open(root + '/' + filename[:-13] + 'decrypted.pdf', 'wb')
	else:
	    resultPdf = open(root + '/' + filename[:-4] + 'decrypted.pdf', 'wb')
	pdfWriter.write(resultPdf)
	os.remove(root + '/' + filename)
	resultPdf.close()
	pdfFile.close()
else:
    print('password incorrect for {}'.format(root + '/' + filename))
    

If the inputted password works for a file, the file is unlocked, then its contents are copied over into a new file with no password. Then, the encrypted file is deleted, and the new file replaces it. If the inputted password is incorrect, the program outputs the name of the file.

Application-wise, these programs are very efficient. For example, if there is a website with hundreds of free resources (many of which are password protected PDFs), you can download all these files and use the program above to unlock all files simultaneously. Using a brute-force attack (coming soon), we can also download hundreds of files and try to hack them using a spin-off of the program above.

Using Python to make Multiplication Tables in Excel

A really easy problem. Here’s the full code.

Now, we need to create an n by n multiplication table in excel using a python program, where n is an arbitrary, positive integer. We will do this using the openpyxl module. Firstly, we’ll initialise our notebook and set our current sheet to Sheet, which is the default active sheet.

wb = openpyxl.Workbook()
sheet = wb['Sheet'] # or wb.active
n = int(input('enter n| '))

For the purposes, a ‘bold’ font style object has also been created. I will assign this to the row and column headings later.

for i in range(2, 2+n):
	sheet['A' + str(i)].value = i-1
	sheet['A' + str(i)].font = boldFont
	sheet[get_column_letter(i) + '1'].value = i-1
	sheet[get_column_letter(i) + '1'].font = boldFont

Above, all I’ve done is create row and column headings for all integers upto and including n, with each heading being written in bold characters. Now, time to create the table.

for i in range(2, sheet.max_row + 1):
	for j in range(2, sheet.max_column+1):
		sheet[get_column_letter(j) + str(i)].value = sheet[get_column_letter(j) \ 
		        + '1'].value * sheet['A' + str(i)].value

Making the table is really simple. What I’ve done is iterate over each row heading, and then over each column heading inside the row, then find the product of these two and assign it to the required cell. You could also do this using excel formulas, but I think this solution is reasonably efficient.

Here’s another solution that I found on github.

for rowNum in range(1, number+2):
	for colNum in range(1, number+2):
		if rowNum==1 and colNum==1:
			sheet.cell(row=rowNum, column=colNum).value=''
		elif rowNum==1:
			sheet.cell(row=rowNum, column=colNum).value = colNum-1
			sheet.cell(row=rowNum, column=colNum).font = boldFont
		elif colNum==1:
			sheet.cell(row=rowNum, column=colNum).value=rowNum-1
			sheet.cell(row=rowNum, column=colNum).font = boldFont
		else:
			sheet.cell(row=rowNum, column=colNum).value = (rowNum-1)*(colNum-1)

It is shorter, but I don’t think it’s that elegant due to the usage of if/else statements. But that’s just personal bias. Instead of iterating over headings and content cells differently, the author here iterates every single cell (including 0,0) in one for loop.

Here’s a sample multiplication table for n = 18:

Arduino Crystal Ball

This is the 11th project in the Arduino Starter Kit. Do note that I have skipped the 9th and 10th projects because my DC motor isn’t working.

Setup

Here’s what we need:

  • Arduino UNO + Breadboard
  • Jumper wires
  • LCD Screen
  • Potentiometer
  • Tilt Sensor
  • 1 x 10 kΩ Resistor
  • 1 x 220 Ω Resistor

Layout

Here’s our schematic, as per the Arduino Projects Book.

And here’s my incredibly messy build:

Code

We will be using the LCD Display (Arduino) library to control our screen. While setting up, all we need to do is print out the initial message we want the user to see.

void setup() {
 lcd.begin(16, 2);
 pinMode(switchPin, INPUT);
 lcd.print("Crystal Ball");
 lcd.setCursor(0, 1);
 lcd.print("At your service");
}

Then, using the random() method, we will give our output.

reply = random(8);
switch(reply) {
    case 0:
    lcd.print("Yes");
    break;
    case 1:
    lcd.print("Probabale");
    break;
    case 2:
    // ...

That’s all there is to it. You can see the full code here.

Metrics for Analyzing Urban Societies

If we want to analyze specific regions or communities within cities, there must be some indicators that act as differentiators. For example, carbon dioxide concentrations, literacy rates, water hardness levels, diabetes levels etc. We can use multiple quantitative metrics. However, the hard part is finding relationships between these metrics and interpreting them.

With regards to Jaipur, there are various ways to collect data and use metrics. The first, simpler method is to set up your own grid of sensors around the city, each sending data in files based on its latitude and longitude rather than identification number. The second would be to get in touch with say the state government, or a sufficiently large private company.

Now, along with collecting numerical data, we will also need some qualitative data (and assign classes) to establish patterns. For example, if a specific region with a certain pincode has an abnormal concentration of carbon dioxide and a high usage of cigarettes, we would expect severe health, or even comfort problems there.

Getting the data is the hard part. Kaggle has a database on Jaipur, but only regarding weather data, which sucks. There are various other governmental sources, but most are written in hindi and are consequently very hard to input into a model as a language to language model/framework will be required. If all the sheets have a standard template, we could type in the data manually for the first few sheets, and then figure out a scraper to do the job for us.

Summary of Reading – August ’19

The Glass Palace; Amitav Ghosh – A book reliant on emotions and relationships. Really didn’t bode well with me. The end was depressing and cruel, with characters having been developed throughout the book being separated and consequently dying. I usually don’t read books in this category. This book just ensured that i’ll continue to do so.

The Hitchhiker’s Guide to the Galaxy; Douglas Adams – One of the best books I’ve ever read in my life. Challenges the borders of imagination. The one thing that stands out throughout is just how absurd, and surprisingly realistic everything seems to be. Adams’s use of humour and sarcasm is also a defining aspect of the book. From mice being ‘pan-dimensional’ beings to depressed robots with brains the size of the planets, this book is one hell of a read for those who love what lies beyond. His little digs at some of our defining aspects also avoid failing to grab attention. There were two that I distinctly remember. The first being the one about construction crews demolishing homes, inflated to gargantuan levels by starting off the book with an intergalactic construction crew that demolishes the Earth (the dolphins knew!). The second is much more subtle – Dent’s use of various ‘stylistic devices’ to provide a feedback for Vogon poetry.

The Restaurant at the End of the Universe; Douglas Adams – This book takes a more poignant tone, but not without degrading the humour. Zaphod entire argument of anyone wanting to be a president not being a good fit for president comes true, dramatically, with the ‘man who controls the universe.’ However, Ford and Arthur’s stay on prehistoric earth grabbed the headlines for me. I adored how Adams manages to use the Golgafrinchans to convey both humor and anger at us humans. Their insane amounts of stupidity, which was responsible for tree-dwellers dying out and being named cave-men, and war being declared on regions with no people did manage to capture the essence of what humans are. Personally, I feel Adams uses this to take a hit at the entire concept of war and egoism.

Life, the Universe and Everything; Douglas Adams – An absolutely brilliant read. Adams hilariously transforms the popular game of cricket into an intergalactic war that kills over 2 ‘grillion’ people. The element of realism makes this worth reading. Personally, I think there were strong connotations between the people of Krikkit and religion. Both behave similarly in the sense that they meet circumstances/events that oppose their ideology or beliefs, and then choose to destroy rather than embrace those self same circumstances. The way Adams uses the white krikkit robots as symbols of death and loyalty (in my opinion) is also spectacular. In all, another stunning book in the series.

So Long, and Thanks for All the Fish; Douglas Adams – This book takes on quite a different tone and setting from the previous books. Surprisingly, Arthur gets a girlfriend. This rounds off his character arc well. He’s settled, got back his planet, and has a person to relax and connect with. It brings out Arthur’s social side, while providing answers to some plaguing questions (but not providing THE question).

Mostly Harmless; Douglas Adams – In typical Douglas Adams fashion, Douglas Adams kills Fenchurch (not exactly kill, but shift Arthur to a universe where Fenchurch doesn’t exist). In addition to being uncannily depressing, it also brings about the destruction of the old guide. Furthermore, to compound this misery, Earth is destroyed yet again, and all of the main characters die. As I said, depressing. It does round off the series pretty well. Adams started by destroying the Earth and ensuring all of the main characters survive, and he ends by destroying the Earth and ensuring all of the main characters die. Perhaps Martin got his inspiration from here.

Arduino Light Theremin (Starter Kit Project #6)

We will use a phototransistor as a light sensor to control a piezo and produce sound – resembling the functionality of an actual theremin.

Setup

Here’s what we need:

  • Arduino UNO + Breadboard
  • Jumper wires/cables
  • 1 10kΩ resistor
  • Piezo
  • 1 phototransistor

Layout

Here’s what the instrument should look like, as per the Arduino projects book.

And here is what I made:

Code

The calibration of the sensor is the most important part in this mini-project.

int sensLow = 1023;
int sensHigh = 0;

void setup() {
    while (millis() < 5000) {
        sensVal = analogRead(A0);
        if (sensVal > sensHigh) {
            sensHigh = sensVal;
        }
        if (sensVal < sensLow) {
            sensLow = sensVal;
        }
    }
}

The millis function returns the amount of time for which our board has been working. Thus, we calibrate for the first five seconds by taking in the minimum and maximum possible sensor input values. These will be used to scale our input values later on.

void loop() {
  sensVal = analogRead(sensPin);
  int pitch = map(sensVal, sensLow, sensHigh, 50, 4000);
  tone(8, pitch, 20);
  delay(10);
}

Here, we take in our input value and map it to the appropriate frequency. Then, using the tone function, we output sound using our piezo (which vibrates as per the frequency provided by tone()). You can play around the frequency and mapping values just for fun.

Arduino Mood Cue (Starter Kit Project #5)

Here, we will turn a potentiometer to consequently rotate a simple servo meter. The arduino will collect the data a

Setup

Here are the components we need:

  • Arduino UNO + Breadboard
  • 2 100 μF Capacitors
  • Jumper wires/cables
  • Potentiometer
  • Servo Motor (with arm)

Layout

Here’s what it should look like, as per the Arduino projects book.

And here is my build:

You might notice that there are some differences between the schematic and what I’ve made. Firstly, the potentiometer is just too large to be placed on the same strip as a capacitor and power/ground connections, which is why I have used orange jumpers to elongate the entire setup. Also note that the capacitors, if connected incorrectly, could potentially explode. I would google up how to connect these and then proceed! It took me a lot of time to find out which pin is the anode and which pin is the cathode. Turns out the pin which has a grey arrow full of zeros pointing downwards is the negatively charged electrode, or the cathode.

Code

You can find the full code here.

#include <Servo.h>
Servo myServo;

To start, we import the library required to interact with the servo motor, and create an instance of the Servo object to handle our motor. Then, all we need is 4 lines of functional code.

void loop() {
    potVal = analogRead(potPin);
    angle = map(potVal, 0, 1023, 0, 179);
    myServo.write(angle);
    delay(15);
}

Firstly, we read our analogue potentiometer value and convert it into an angle for rotating our servo motor. Then, using the servo library, we turn our motor. The delay of 15 milliseconds is to accomodate the time used up in rotating the motor.

Demo