Pretty Printing C++ Archives from Emails

I’m just putting this here because I nearly managed to lose it. This is a part of a pretty unvarnished BASH script for a very specific purpose, taking an email file containing a ZIP of submitted C++ code from students. This script produces pretty printed PDFs of the source files named after each author to facilitate marking and annotation. It’s not a thing of beauty. I think I’ll probably write a new cleaner version in future.

 

 

Installing Android Nougat on a Stock Galaxy Tab 10.1

My daughter uses an Android Samsung tablet (coded GT-P7510) which ended official support on Android 4.0.4. Unfortunately I didn’t pay any attention to this issue until the apps she most wanted to use, namely Netflix and YouTube stopped working on it as the Android version was too low.

I found a ROM to upgrade to Android 7.1 (Nougat) with some cost – for instance, the camera doesn’t work, but Aimee doesn’t care about that. So I decided to try upgrading it since the tablet was otherwise now utterly useless.

To make things more difficult, most of the information on upgrading this tablet on the Internet is outdated or wrong, or pre-supposes that the device was long since updated. I also don’t run Windows, and ran into some problems with the Heimdall alternative.

So this quick article is the result of a couple of evenings running into dead ends. It might help someone else. Certainly if I ever need to do it again it’ll help me.

But as usual, if you break something, you own all the parts. These instructions are completely specific to this particular tablet, and the wifi only version at that. Make sure your device is fully charged before you start.

A new recovery image had to be installed first, and some steps had to be undertaken just to get that far.

Heimdall

First of all there’s supposed to Windows software called Odin that is used to update the ROM, especially from a stock start. I can’t run that without emulation since I don’t run Windows, and in any case, I suspect it might behave badly in a virtual machine, and probably wouldn’t run correctly on modern Windows.

So I installed a Free and Open Source alternative known as Heimdall. For me, this was simple as it was Debian packaged. I couldn’t get the frontend to be useful, and I couldn’t get the Java version of the frontend to work online or offline. So I defaulted to the command line.

So, as root on Debian GNU/Linux:

This is also ensuring all the command line tools for android debugging are installed (I already had these).

Receiving TWRP

The device needs to be made ready for Odin / Heimdall upload. Turn the device off, and then hold Power and Volume Down till it appears with two icon choices. You want the one on the right. Use Volume Up to select, and use Volume Up again to bypass the dire warnings.

I had no success in using the Heimdall frontend, your mileage may vary. I got the correct archive for my purposes from here.

I downloaded the archive, and used tar xvf to extract the contents. You will find two .img files, recovery.img and hidden.img. You’ll need both.

Note that the partition target on the device for recovery is not called recovery but is called SOS at least on my device.

Because of the no-reboot option note that the tablet will continue to warn you not to restart it. You’ll need to watch the command line progress carefully to ensure that it is on. Now reboot the machine once again into the Odin / Heimdall mode again. I.e. power it off, and turn it on with Power and Volume Down.

Now flash

For me this successfully got TWRP 3.0.3 loaded. It was a major odyssey of conflicting information to get this far. When you reboot make sure you hold down volume down to get to the recover menu, (and now choose the left hand option). If you don’t do this, the stock ROM overwrites the new one and you’ll need to start again.

Using TWRP

From here, things were relatively plain sailing. I got the ROM from here. Incidentally, I’d tried other recovery ROMs I got onto the device before when I couldn’t get TWRP onto it, they did not allow the following steps to work.

I then used TWRP’s wipe option to wipe Cache, Data, and Dalvik Cache.

I used the Advanced button and put the device into sideload mode.

I then, from the Linux command prompt executed

I then did not reboot but went back in TWRP and selected sideload again, this time I was careful to uncheck the wipe data and cache items since I’m loading other items on top of the basic ROM image.

and I repeated the same for the last package

finally I selected to reboot the tablet. It took a pretty long time to boot. Don’t forget it’s a relatively underpowered device.

The device is up and running and now runs the apps my daughter wants again.

The Deceptiveness of Coincidence

A friend of mine recently posted about a chain of events – people sharing birthdays – that was so unlikely that a lottery ticket purchase was called for. Most people might make similar comments as the oddity of these events struck them. There followed some discussion about these problems and it made me think of trying to set out the issues more carefully.

I’ve done a lecture, in some variations (for school audiences), almost every year about the deceptiveness of common sense in mathematics. Indeed, it provided the title of this blog.

The  Birthday Problem

One of the problems I examined is the famous birthday problem. Let’s look at two aspects of this.

How many people must be in a room before we are certain that two of them share the same birthday?

This can be answered by using the so called pigeon hole principle. In this case, the pigeon holes are all the days of the calendar in a year, 365, or let’s say 366 to allow for the worst case scenario of a leap year. The people in the room are the pigeons. You can place 366 pigeons into the holes and keep them all separate, but any more ensures that at least two must share one of the holes. So, similarly, you need 367 people to be absolutely sure that one pair in the room shares a birthday.

Let’s look at a more subtle question.

How many people must be in a room so that it is more likely than not that two of them share the same birthday?

It turns out this number is deceptively low. We reach a 50% chance (more likely than not) with just 23 people in the room. Perhaps even more surprisingly we reach a 99.9% chance with 70 people in the room. The calculations for the probabilities are set out in the link to the lecture, and the wikipedia article as mentioned above, but I don’t want to focus on that. I want to focus on why the result is so shocking to most people.

The issue is the collision of relatively rare events (a birthday) with numerous combinations. For instance if you want to consider how many pairs one can form in the room we can see this.

If there are three people in the room, A, B and C, there are three possible pairs: AB, AC, and BC. If there are four people in the room, A, B, C and D, there are six possible pairs: AB, AC, AD, BC, BD, and CD. There’s actually a nice formula for this and you can work this out for more examples.

You don’t need to know the formula, but if you want it is  \frac{n!}{(n-2)!2!}.

People in Room Possible Pairs
3 3
4 6
10 45
15 105
20 190
23 253
30 435
50 1225
70 2415

You can see just how rapidly the number of pairs increases. It might seem un-obvious that 23 people is as likely as not to produce a duplicate birthday, but it’s perhaps less so when you realise that we are not predicting which pigeon hole has two pigeons in it to go back to our analogy. But given 253 possible pairings, it seems less unlikely even intuitively that there will be “collisions” within one of those pairs.

Coin Tosses

Let me talk about a similar problem, flipping a (fair) coin to get a Head (H) or a Tail (T). Let’s consider that we flip the coin three times. The possible outcomes are:

HHH, HHT, HTH, THH, TTH, THT, TTH, TTT.

These are all absolutely equally likely, and even this is sometime unintuitive. The coin has no memory of how it performed, and no desire to conform to some concept such as a “law of averages“. But we tend to categorise these 8 possible events into 4 categories.

  • Three Heads: (one way this can happen, so 1/8 probability)
  • Two Heads, One Tail: (three ways this can happen, so 3/8 probability)
  • One Head, Two Tails: (three ways this can happen, so 3/8 probability)
  • Three Tails: (one way this can happen so 1/8 probability).

In doing so, we can see why the HHH and TTT events look particularly exciting and rare. They are no more rare than any other individual outcome, but it’s the groupings of how one can get two heads that makes that collection of events more unexciting.

To take this to more of an extreme. If we flip a coin ten times, then these outcomes are all exactly equally likely.

  1. HHHHHHHHHH
  2. HTHTHTHTHT
  3. HHTHTTHTTH
  4. THTTHHHTHT
  5. TTTTTTTTTT

But some will be far more remarked upon that others. But even if we have an experiment that flips a coin 10 times over and over again enough times, we will expect to see all of these (more or less, eventually) equally often, but we will probably not consider these exciting except maybe outcomes 1,5 and maybe 2.

Genealogy

Another example, closer to home. Let’s assume that there have been 7,500 generations of Homo Sapiens since its emergence as a species.

The probability that in each of those 7,500 generations in both your paternal and maternal line that all the pairings took place between the right people at the right time in the right circumstances to give rise to you is astronomical. You have two parents, four grand parents, 8 great grand parents and so on. Just for that collection of people. If we add you into the mix, then the number of pairings involved over these 7500 generations is

2^{7501}-1

That number has 2259 digits, you can see them here on Wolfram Alpha.

That’s the number of people involved to get to you, including you. They all had to survive and meet at the right time, and um, do everything else at exactly the right time in the right way. What are the odds? And yet you are here… You might find that exceptional, and of course in a way it is, but the point is, you are not more likely or unlikely than most other human beings that have been born, or countless potential humans who might have been but were not born because their parents never even met. The fact that you specifically exist is unlikely, but the fact that if you did not someone else would exist instead is highly likely and predictable.

Naturally, however, we tend to think of our own existence rather more than some random person on the other side of the planet. But each one of us is an example of the coin flipping perfectly as needed every single time. Sometimes a Head, sometimes a Tail, but that exact perfect sequence essential to produce you. Naturally we are each quite taken with the outcome that produces us and also our nearest and dearest. But if it had not happened, we wouldn’t be here to wonder about it.

Availability and Psychology

As you can see there is more to this than mathematics and probability, there is also perception and psychology, specifically a concept called the Availability Heuristic. Some things stand out to us as being significant when they are no more significant than other events we ignore.

You might live in a city of a million people. You might personally know only a hundred. You might pass a thousand on your commute to work each day. You do that hundreds of times a year. The number of possible combinations of people you will see over a year is enormous, but you won’t remember it as a special event when you bump into all the people who aren’t dear to you.

The Anthropic Principle

A nice example of this thinking is the question that many people have asked which is essentially: why is the Universe, and our Earth, set up so nicely to allow humans to exist? If certain physical constants were just a bit different then matter as we know it couldn’t form. If we were just a bit closer to the Sun we couldn’t survive and so on.

These things are all true. But in a Universe with inappropriate physical constants there will be no matter by definition, and certainly no sentient matter like us to notice.

If the Earth was closer to the Sun, homo sapiens would not exist as it currently does, but like as not some other species would, and it would also think how perfectly the world was formed for them. This concept, that we tend to think the Universe is so special to have created us, despite the fact that if not we wouldn’t be here to think about it, is often called the Anthropic Principle.

We believe that the improbability of life itself or us as a species is an example of the collision between billions of individually improbable events with the billions of events than could have occurred, and the billions of years in which they might have done so.

We end up having to think about such things with great care because our evolution on the African savannah didn’t really equip us to consider them. So we shouldn’t be surprised that we are surprised, but we still are.

It’s the old cliché: you are unique and special; just like everyone else.

You are unique, just like everyone else

There are always dangers in imagining our special place as a species.

“This is rather as if you imagine a puddle waking up one morning and thinking, ‘This is an interesting world I find myself in — an interesting hole I find myself in — fits me rather neatly, doesn’t it? In fact it fits me staggeringly well, must have been made to have me in it!’ This is such a powerful idea that as the sun rises in the sky and the air heats up and as, gradually, the puddle gets smaller and smaller, frantically hanging on to the notion that everything’s going to be alright, because this world was meant to have him in it, was built to have him in it; so the moment he disappears catches him rather by surprise. I think this may be something we need to be on the watch out for.”

Douglas Adams, The Salmon of Doubt