Skip to main content

Ralsina.Me — Roberto Alsina's website

Doing Your Homework, With Style

As usu­al in all pro­gram­ming list­s, ev­ery once in a while some­one will post a ques­tion in the Python Ar­genti­na list which is ob­vi­ous­ly his home­work. To han­dle that there are two schools of thought.

  1. Telling the stu­­dent how to do it is help­ing them cheat.

  2. Telling the stu­­dent how to do it is teach­ing him.

I tend more to­wards 1) but I think I have dis­cov­ered a mid­dle road:

1.5) Tell the stu­dent a so­lu­tion that's more com­pli­cat­ed than the prob­lem.

That way, if he fig­ures out the so­lu­tion, he has done the work, and if he does­n't fig­ure it out, it's go­ing to be so ob­vi­ous­ly be­yond his skill the teach­er will nev­er ac­cept it as an an­swer.

As an ex­am­ple, here's the prob­lem for which help was re­quest­ed:

Giv­en an un­sort­ed list of two-let­ter el­e­ments (une low­er­case, one up­per­case), for ex­am­ple:

['eD', 'fC', 'hC', 'iC', 'jD', 'bD', 'fH', 'mS', 'aS', 'mD']

Sort it by these cri­te­ri­a:

  1. Cre­ate sub­sets ac­cord­ing to the up­per­case let­ter, and sort them by the num­ber of mem­bers in as­cend­ing or­der, like this:

    ['fH', 'mS', 'aS', 'fC', 'hC', 'iC', 'jD', 'bD', 'eD', 'mD']
  2. Then sort each sub­set in as­cend­ing or­der of the low­er­case let­ter, like this:

    ['fH', 'aS', 'mS', 'fC', 'hC', 'iC', 'bD', 'eD', 'jD', 'mD']

Ig­nor­ing that the prob­lem is not cor­rect­ly writ­ten (there are at least two ways to read it, prob­a­bly more), I pro­posed this so­lu­tion, which re­quires python 3:

from collections import defaultdict
d1 = defaultdict(list)
[d1[i[1]].append(i) for i in  ['eD', 'fC', 'hC', 'iC', 'jD', 'bD', 'fH', 'mS', 'aS', 'mD']]
{i: d1[i].sort() for i in d1}
d2 = {len(d1[i]): d1[i] for i in d1}
print([item for sublist in [d2[i] for i in sorted(d2.keys())] for item in sublist])

This produces the desired result: ['fH', 'aS', 'mS', 'fC', 'hC', 'iC', 'bD', 'eD', 'jD', 'mD'] but it's done in such a way that to understand it, the student will need to understand roughly three or four things he has probably not been taught yet.

A Few Problems with A Song Of Ice and Fire

I read all of it, one book af­ter the oth­er, and end­ed a month ago. And since then, I have had a cou­ple of things about it both­er­ing me. Let's see if they make some sense. Mind you, I am go­ing to read vol­umes six and sev­en, be­cause these books are ad­dic­tive as crack in ebook-­for­m.

But, just like crack, they have some wor­ri­some fea­tures.

There May Be No Plan

We are five books (and a cou­ple of chap­ter­s) in­to it. It's sup­posed to be a sev­en book se­ries. And noth­ing has hap­pened. You may say a lot has, like "this char­ac­ter got killed" and "that oth­er char­ac­ter got killed" (and a hun­dred oth­er char­ac­ters got killed), yeah.

But what has changed in the five king­dom­s?

It's start­ing to feel, these many pages lat­er, as if ... well, who cares what hap­pen­s? The five king­doms will have a king, or an­oth­er. There will be drag­ons (which will sup­port a king or an­oth­er), there is war and ev­ery­one is hav­ing a crap­py time, but hey, all that hap­pened five times in the last hun­dred years or so al­ready.

The hand of the king was killed? Well, so were five of the last sev­en hand­s.

A Tar­garyen may come, lay waste to all the armies of the realm and be crowned? Well, that al­ready hap­pened in the field of fire, and they had Tar­garyens for a while, un­til they ran out of drag­on­s.

The Iron­men may con­quer the north? Well, they al­ready had con­quered it a cou­ple cen­turies ago, and then they lost it.

And so on: any of the pay­offs of the book se­ries has al­ready hap­pened, some of it more than once. So, what's spe­cial about this time around?

Does the au­thor have a plan, some­thing up his sleeve that's go­ing to be a shock? I don't know, but the tricks are start­ing to get repet­i­tive.

What would hap­pen if, af­ter sev­en book­s, it turns out that there's noth­ing spe­cial?

It's Too Earth-Like

The five king­dom­s. Scot­land, Eng­land, Wales, Ire­land and which one? Isle of Man? Be­cause, come on. There's these peo­ple who are al­most ex­act­ly Mon­gol­s, ex­cept they have bells in their hair. There's the pseu­do-viking, the pseu­do-s­cot­s, the er­satz-irish, the fake-i­tal­ian­s, the I-­can't-­be­lieve-it's-not-chi­nese and so on.

There are knight­s, whose ar­mour is ex­act­ly me­dieval ar­mour. There's the sea­far­ing raider­s, on their long­ship­s. Etceter­a, etceter­a, et-f­reak­ing-ceter­a. It's like when­ev­er the au­thor needs to add an "ex­otic" char­ac­ter, he just throws a dart at the map, then an­oth­er, cre­ates a mix 80% one, 20% the oth­er, makes up some sil­ly or­tog­ra­phy rule for names, and that's it.

The Magic is Lazy

So, drag­on­s. And of course, drag­ons cre­ate mag­ic (y­ou can see how lots of mag­i­cal giz­mos start work­ing since the drag­ons came).

So, let's make mag­ic ev­ery­thing. Want to have leg­endary sword­s? Then they are made of Va­lyr­i­an steel. That's mag­i­cal steel, which is why it seems to nev­er need sharp­en­ing. That's why you can have fam­i­ly heir­loom sword­s. Be­cause they are mag­i­cal.

And there's a mag­i­cal door, made of mag­i­cal wood. The mag­i­cal wood comes, of course, from mag­i­cal trees.

And there's fake mag­i­cal sword­s, made by re­al east­ern mag­ic. And there's mag­i­cal as­sas­sin­s. And mag­i­cal coin­s, mag­i­cal can­dles. And so on, and so forth. You can't paint your­self in­to cor­ners when you can count on there be­ing a mag­i­cal paint­brush that lays down paint that does­n't stain the mag­i­cal shoes of the painters of the mag­i­cal land of Painthe­ri­a, whose names al­ways have a dou­ble la­ryn­geal con­so­nant in the mid­dle.

More is More is More is Less

The first book man­ages to tell rough­ly a (earth) year of sto­ry. The fourth and fifth, to­geth­er be­cause they hap­pen si­mul­ta­ne­ous­ly, cov­er per­haps three month­s. And there are char­ac­ters we have not seen since book three, when they were just about to say some­thing. We are cur­rent­ly en­ter­ing the third book wait­ing to know what the maid­en of Tarth said at that mo­men­t.

There are books that are about one char­ac­ter, there are those that are about a dozen, there are those that are about one hun­dred. None of the lat­ter is any good. The sto­ry keeps ex­pand­ing and slow­ing down. At this step, all of book sev­en is go­ing to be about a sin­gle day in the lifes of 50 first-per­son char­ac­ter­s, and each one will de­scribe their break­fast, be­fore we un­ex­pect­ed­ly get promised (very soon now) an eight book which will cov­er their post-break­fast craps and clear ev­ery ques­tion we may have had about the sub­jec­t.

Fan Service

You, who know what was in the pies served in the feast at Win­ter­fell in vol­ume 5, you are be­ing spoon­fed that kind of thing to make you feel smart and knowl­edge­able. If you don't know what was in that pie... well, YOU MISSED IT.

And how is it a good idea to write three pages that (if you have a good mem­o­ry) shout what was in that pie, when it's a sto­ry about a third-­line and fourth-­line char­ac­ters whose names noone will re­mem­ber?

Well, it's a good idea be­cause it's fan ser­vice, and fans love be­ing served. But it's a cyn­i­cal, cal­cu­lat­ing move. You are be­ing served bad pie there, fan­s.

So, Are the Books Good?

They are awe­some. I can't wait for the sixth vol­ume. George RR Mar­t­in, here's my mon­ey. Tell me a sto­ry.

Twitter Off

A week ago, I took a de­ci­sion I had not seen com­ing. I shut down com­ments on this blog un­less the post was tech­ni­cal. Be­cause I could not stand some of the com­ments any­more. I said "com­ments down for a mon­th, and then I'll see if I miss them".

Well, so far I don't, as you can see by this post hav­ing com­ments dis­abled.

To­day, I am re­mov­ing the com­ment track of my life. I an shut­ting down my twit­ter ac­coun­t. Some au­to­mat­ic posts will go out, but I am not read­ing it and am not no­ti­fied of any­thing. Again, it's "twit­ter down for a mon­th, and then I'll see if I miss it΅.

Hope­ful­ly I will not.

Python Trick: the Fundict (or the Diction)

Sup­pose you have made the choice in the past of ex­pos­ing a dic­tio­nary as part of an ob­jec­t's in­ter­face. So, peo­ple are do­ing things like:

object.data[foo]['bar']

And now you want people to not have to specify foo because it can be obtained from somewhere else. In fact, what you want now is to expose something like this:

object.data(bar, foo='foo')

Here's an idea on how to do that with­out break­ing the old code:

class fundict(dict):
    def __call__(self, bar, foo='foo'):
        return self[foo][bar]

That's a dic­tio­nary that's al­so a callable, and thus in­dis­tin­guish­able from a func­tion. A func­tion-­dic­tionary. A fun­dic­t. And of couse, you could al­so do it the oth­er way around, and im­ple­ment a func­tion that works as a dic­tio­nary. A dic­tio­nary-­func­tion, a dic­tio­n. But since that's more work, I used this one.


Contents © 2000-2020 Roberto Alsina