SICP – cap. 2 – Dati gerarchici e closure – esercizi – 23

Continuando da qui ecco un altro esercizio, qui.

Exercise 2.19: Consider the change-counting program of 1.2.2. It would be nice to be able to easily change the currency used by the program, so that we could compute the number of ways to change a British pound, for example. As the program is written, the knowledge of the currency is distributed partly into the procedure first-denomination and partly into the procedure count-change (which knows that there are five kinds of U.S. coins). It would be nicer to be able to supply a list of coins to be used for making change.

We want to rewrite the procedure cc so that its second argument is a list of the values of the coins to use rather than an integer specifying which coins to use. We could then have lists that defined each kind of currency:

(define us-coins 
  (list 50 25 10 5 1))

(define uk-coins 
  (list 100 50 20 10 5 2 1 0.5))

We could then call cc as follows:

(cc 100 us-coins)

To do this will require changing the program cc somewhat. It will still have the same form, but it will access its second argument differently, as follows:

(define (cc amount coin-values)
  (cond ((= amount 0) 
        ((or (< amount 0) 
             (no-more? coin-values)) 
         (+ (cc 
             (- amount

Define the procedures first-denomination, except-first-denomination and no-more? in terms of primitive operations on list structures. Does the order of the list coin-values affect the answer produced by cc? Why or why not?

Uh! la prima parte è davvero immediata:

(define (no-more? coin-values)
  (if (null? coin-values) true false))

(define (except-first-denomination coin-values)
  (cdr coin-values))

(define (first-denomination coin-values)
  (car coin-values))

Metto tutto nel file coins.rkt per comodità:

Poi la seconda parte 👿 non ci provo perché poi mi deprimo 👿 passo subito a Bill the Lizard 🚀 , il mio nerd di riferimento 😉

OOPS! 😳 dubbio atroce, forse ho frainteso –niente dimostrazione matematica– devo stare più attento; vista fatta sembra banale: Finally, we’re asked if the order of the list coin-values affects the answer produced by cc. We can find out quickly enough by experiment.

We can see that the order doesn’t matter. This is because the procedure recursively evaluates every sub-list after subtracting the value of the first coin from the target amount. It doesn’t matter if that value is the smallest, largest, or even if the values are shuffled.

Interessanti le osservazioni di sicp-ex 🚀

Devo affrontare in modo diverso questi esercizi; questo è alla terza modifica con riscrittura parziale 😯


NumPy – 37 – introduzione agli oggetti Pandas – 3

Continuo da qui copiando qui.

L’oggetto Index di Pandas
We have seen here that both the Series and DataFrame objects contain an explicit index that lets you reference and modify data. This Index object is an interesting structure in itself, and it can be thought of either as an immutable array or as an ordered set (technically a multi-set, as Index objects may contain repeated values). Those views have some interesting consequences in the operations available on Index objects. As a simple example, let’s construct an Index from a list of integers:

Index come array immutabile
The Index in many ways operates like an array. For example, we can use standard Python indexing notation to retrieve values or slices:

Index objects also have many of the attributes familiar from NumPy arrays:

One difference between Index objects and NumPy arrays is that indices are immutable–that is, they cannot be modified via the normal means:

This immutability makes it safer to share indices between multiple DataFrames and arrays, without the potential for side effects from inadvertent index modification.

Index come set ordinato
Pandas objects are designed to facilitate operations such as joins across datasets, which depend on many aspects of set arithmetic. The Index object follows many of the conventions used by Python’s built-in set data structure, so that unions, intersections, differences, and other combinations can be computed in a familiar way:

These operations may also be accessed via object methods, for example


cit. & loll – 36

Giovedì e allora ecco qua 💥

Turing machine
::: FactsOfMath

Happy Birthday RMS 🌺 🎵 🚀
::: climagic

Acing the technical interview
::: Aphyr

Ridiculing technical interviews is good fun, and in many ways well deserved, but…
::: yminsky

Clean code always looks like it was written by someone who cares
::: CodeWisdom

Explained Tabs vs. spaces to my son
::: lizardbill

Mouse not found
::: blusewillis

Typical code comments
::: rvagg

so this afternoon i am learning about ftrace
::: b0rk

Can liberty survive the digital age?
::: jilliancyork

Bash History
::: {}

Whiteboard problems in pure Lambda Calculus
ci sarebbe anche un aspetto serioso ma il FizzBuzz è epico 😜
::: algoritmic

here are my PS #bash #prompt envvars — made by hand years ago
hronir rockz! 🚀
::: hronir

Email subject line of the day
::: wallingf

Ma ogni volta è un passo in avanti
::: medeoTL

Well said! “I am less afraid of artificial intelligence than of human stupidity.”
::: marcelsalathe

An AI class in school left me with the professor’s mantra
::: scanlime

roses are red
::: mergeconflict

Computer programming is the task of writing code
::: agfitzp

JavaScript 7 – struttura dei programmi – 5

Continuo da qui a copiare qui.

Indentare il codice
Cioè scriverlo bene, in modo che sia leggibile, come fanno i nerds.
Non è richiesto; si può scrivere tutto il programma in una sola lunga linea ma poi è difficile leggerlo.
With proper indentation, the visual shape of a program corresponds to the shape of the blocks inside it. I like to use two spaces (io di solito 4) for every open block, but tastes differ—some people use four spaces, and some people use tab characters.

Ciclo for
Many loops follow the pattern seen in the previous while examples. First, a “counter” variable is created to track the progress of the loop. Then comes a while loop, whose test expression usually checks whether the counter has reached some boundary yet. At the end of the loop body, the counter is updated to track progress.

Because this pattern is so common, JavaScript and similar languages provide a slightly shorter and more comprehensive form, the for loop (file f0.js).

for (var number = 0; number <= 12; number = number + 2)

This program is exactly equivalent to the earlier even-number-printing example. The only change is that all the statements that are related to the “state” of the loop are now grouped together.

The parentheses after a for keyword must contain two semicolons. The part before the first semicolon initializes the loop, usually by defining a variable. The second part is the expression that checks whether the loop must continue. The final part updates the state of the loop after every iteration. In most cases, this is shorter and clearer than a while construct.

Here is the code that computes 210, using for instead of while (file f1.js):

var result = 1;
for (var counter = 0; counter < 10; counter = counter + 1)
  result = result * 2;

Note that even though no block is opened with a {, the statement in the loop is still indented two spaces to make it clear that it “belongs” to the line before it.

Uscire dal ciclo
Having the loop’s condition produce false is not the only way a loop can finish. There is a special statement called break that has the effect of immediately jumping out of the enclosing loop.

This program illustrates the break statement. It finds the first number that is both greater than or equal to 20 and divisible by 7 (f2.js).

for (var current = 20; ; current++) {
  if (current % 7 == 0)

Using the remainder (%) operator is an easy way to test whether a number is divisible by another number. If it is, the remainder of their division is zero.

The for construct in the example does not have a part that checks for the end of the loop. This means that the loop will never stop unless the break statement inside is executed.

If you were to leave out that break statement or accidentally write a condition that always produces true, your program would get stuck in an infinite loop. A program stuck in an infinite loop will never finish running, which is usually a bad thing.

If you create an infinite loop in one of the examples on these pages, you’ll usually be asked whether you want to stop the script after a few seconds. If that fails, you will have to close the tab that you’re working in, or on some browsers close your whole browser, in order to recover.

The continue keyword is similar to break, in that it influences the progress of a loop. When continue is encountered in a loop body, control jumps out of the body and continues with the loop’s next iteration.

Visto che Marijn non fa l’esempio per continue lo faccio io: scrivo solo i numeri pari da 0 a 10, file f3.js:

for (var current = 0; current < 11 ; current++) {
  if (current % 2 == 1)


NumPy – 36 – introduzione agli oggetti Pandas – 2

Continuo da qui copiando qui.

L’oggetto DataFrame di Pandas
The next fundamental structure in Pandas is the DataFrame. Like the Series object discussed in the previous section, the DataFrame can be thought of either as a generalization of a NumPy array, or as a specialization of a Python dictionary. We’ll now take a look at each of these perspectives.

DataFrames come array di NumPy generalizzato
If a Series is an analog of a one-dimensional array with flexible indices, a DataFrame is an analog of a two-dimensional array with both flexible row indices and flexible column names. Just as you might think of a two-dimensional array as an ordered sequence of aligned one-dimensional columns, you can think of a DataFrame as a sequence of aligned Series objects. Here, by “aligned” we mean that they share the same index.

To demonstrate this, let’s first construct a new Series listing the area of each of the five states discussed in the previous section:

Now that we have this along with the population Series from before, we can use a dictionary to construct a single two-dimensional object containing this information:

Like the Series object, the DataFrame has an index attribute that gives access to the index labels:

Additionally, the DataFrame has a columns attribute, which is an Index object holding the column labels:

Thus the DataFrame can be thought of as a generalization of a two-dimensional NumPy array, where both the rows and columns have a generalized index for accessing the data.

DataFrame come dictionary specializzato
Similarly, we can also think of a DataFrame as a specialization of a dictionary. Where a dictionary maps a key to a value, a DataFrame maps a column name to a Series of column data. For example, asking for the ‘area’ attribute returns the Series object containing the areas we saw earlier:

Notice the potential point of confusion here: in a two-dimesnional NumPy array, data[0] will return the first row. For a DataFrame, data['col0'] will return the first column. Because of this, it is probably better to think about DataFrames as generalized dictionaries rather than generalized arrays, though both ways of looking at the situation can be useful. We’ll explore more flexible means of indexing DataFrames in Data Indexing and Selection.

Costruire oggetti DataFrame
A Pandas DataFrame can be constructed in a variety of ways. Here we’ll give several examples.

da un singolo oggetto Series
A DataFrame is a collection of Series objects, and a single-column DataFrame can be constructed from a single Series:

da una lista di dictionaries
Any list of dictionaries can be made into a DataFrame. We’ll use a simple list comprehension to create some data:

Even if some keys in the dictionary are missing, Pandas will fill them in with NaN (i.e., “not a number”) values:

da un dictionary di oggetti Series
As we saw before, a DataFrame can be constructed from a dictionary of Series objects as well:

da un array NumPy bidimensionale
Given a two-dimensional array of data, we can create a DataFrame with any specified column and index names. If omitted, an integer index will be used for each:

da un array NumPy strutturato
We covered structured arrays in Structured Data: NumPy’s Structured Arrays. A Pandas DataFrame operates much like a structured array, and can be created directly from one:


Ancora sulle conversioni di JavaScript

OK, quasi lolloso, anzi certamente ma chissà 😙

Su Twitter si divertono con i calcoli aritmetici strani di JavaScript –troppo volonteroso– per esempio qui e qui.

Io sono del parere che dovrebbe imparare a dire di no, quando necessario.
M siccome non lo fa tocca al programmatore controllarlo, non è censura ma richiamo alla deontologia –secondo me 🐙

C’è un caso che a prima vista mi lascia perpluto

Eh!? 🤖  cosa succede a complessificare l’espressione (solo la prima parte, il resto è sensato):

non ci sono; sembra venire da qui

ma attenzione:

No, non ci sono 😡

Anche con altri linguaggi posso avere risultati errati:

Perl 5

🤖 sbagliato; la versione 6 non funziona (con questo script).

Python invece

🤖 sbagliato anche questo; tutti sanno che la risposta giusta è

41 monkeys and 1 fat snake

(qui) 🐙


SICP – cap. 2 – Dati gerarchici e closure – esercizi – 22

Continuo da qui, un esercizio, qui.

Exercise 2.18: Define a procedure reverse that takes a list as argument and returns a list of the same elements in reverse order:

(reverse (list 1 4 9 16 25))
(25 16 9 4 1)

Non ci sono riuscito, provo a giustificarmi 😯

La mia idea è di usare last-pair definita nell’esercizio precedente … c’è però il problema … non ho idea come farlo … mi sa che sbircio Bill the Lizard 😊

Arghhh!!! 👿
Come ho fatto a non pensarci! Bisogna ragionare ricorsivamente e basta appendere il car della lista al reverse del cdr della lista 😊

See how easy it is to start thinking in Scheme? dice Bill, rockz! 🚀
La mia stessa difficoltà la trovo in sicp-ex, non sono solo io 😙

OK 😡 sono troppo condizionato all’abitudine di ragionare à la Algol 🐙

Ma forse il problema (mio) è un altro: l’esercizio è semplicissimo, basta applicare quello detto “a lezione”; il guaio (mio) è che sono solo, non sono a lezione, non ho qualcuno cui confrontarmi.
E così ci ho ripensato parecchio, troppo, anche non volendo 😯


NumPy – 35 – introduzione agli oggetti Pandas – 1

Continuo da qui iniziando Pandas, copio qui.

At the very basic level, Pandas objects can be thought of as enhanced versions of NumPy structured arrays in which the rows and columns are identified with labels rather than simple integer indices. As we will see during the course of this chapter, Pandas provides a host of useful tools, methods, and functionality on top of the basic data structures, but nearly everything that follows will require an understanding of what these structures are. Thus, before we go any further, let’s introduce these three fundamental Pandas data structures: the Series, DataFrame, and Index.

We will start our code sessions with the standard NumPy and Pandas imports:

Gli oggetti Series
A Pandas Series is a one-dimensional array of indexed data. It can be created from a list or array as follows:

As we see in the output, the Series wraps both a sequence of values and a sequence of indices, which we can access with the values and index attributes. The values are simply a familiar NumPy array:

The index is an array-like object of type pd.Index, which we’ll discuss in more detail momentarily.

Like with a NumPy array, data can be accessed by the associated index via the familiar Python square-bracket notation:

As we will see, though, the Pandas Series is much more general and flexible than the one-dimensional NumPy array that it emulates.

Series come NumPy array generalizzato
From what we’ve seen so far, it may look like the Series object is basically interchangeable with a one-dimensional NumPy array. The essential difference is the presence of the index: while the Numpy Array has an implicitly defined integer index used to access the values, the Pandas Series has an explicitly defined index associated with the values.

This explicit index definition gives the Series object additional capabilities. For example, the index need not be an integer, but can consist of values of any desired type. For example, if we wish, we can use strings as an index, and the item access works as expected:

We can even use non-contiguous or non-sequential indices:

Series come dictionary specializzato
In this way, you can think of a Pandas Series a bit like a specialization of a Python dictionary. A dictionary is a structure that maps arbitrary keys to a set of arbitrary values, and a Series is a structure which maps typed keys to a set of typed values. This typing is important: just as the type-specific compiled code behind a NumPy array makes it more efficient than a Python list for certain operations, the type information of a Pandas Series makes it much more efficient than Python dictionaries for certain operations.

The Series-as-dictionary analogy can be made even more clear by constructing a Series object directly from a Python dictionary:

By default, a Series will be created where the index is drawn from the sorted keys. From here, typical dictionary-style item access can be performed:

Unlike a dictionary, though, the Series also supports array-style operations such as slicing:

We’ll discuss some of the quirks of Pandas indexing and slicing in Data Indexing and Selection.

Costruire oggetti Series
We’ve already seen a few ways of constructing a Pandas Series from scratch; all of them are some version of the following:

pd.Series(data, index=index)

where index is an optional argument, and data can be one of many entities.

For example, data can be a list or NumPy array, in which case index defaults to an integer sequence:

data can be a scalar, which is repeated to fill the specified index:

data can be a dictionary, in which index defaults to the sorted dictionary keys:

In each case, the index can be explicitly set if a different result is preferred:

Notice that in this case, the Series is populated only with the explicitly identified keys.


JavaScript 6 – struttura dei programmi – 4

Continuo da qui a copiare qui.

Cicli while e do
Consider a program that prints all even numbers from 0 to 12. One way to write this is as follows:


lo salvo nel file c0.js e lo eseguo nel terminale, viene meglio

That works, but the idea of writing a program is to make something less work, not more. If we needed all even numbers less than 1,000, the previous would be unworkable. What we need is a way to repeat some code. This form of control flow is called a loop (ciclo):

Looping control flow allows us to go back to some point in the program where we were before and repeat it with our current program state. If we combine this with a variable that counts, we can do something like this (come sopra, lo metto nel file c1.js):

var number = 0;
while (number <= 12) {
  number = number + 2;

A statement starting with the keyword while creates a loop. The word while is followed by an expression in parentheses and then a statement, much like if. The loop executes that statement as long as the expression produces a value that is true when converted to Boolean type.

In this loop, we want to both print the current number and add two to our variable. Whenever we need to execute multiple statements inside a loop, we wrap them in curly braces ({ and }). Braces do for statements what parentheses do for expressions: they group them together, making them count as a single statement. A sequence of statements wrapped in braces is called a block.

Many JavaScript programmers wrap every single loop or if body in braces. They do this both for the sake of consistency and to avoid having to add or remove braces when changing the number of statements in the body later. In this book, I will write most single-statement bodies without braces, since I value brevity. You are free to go with whichever style you prefer.

The variable number demonstrates the way a variable can track the progress of a program. Every time the loop repeats, number is incremented by 2. Then, at the beginning of every repetition, it is compared with the number 12 to decide whether the program has done all the work it intended to do.

Nota: si può scrivere la { su una riga a sé come fanno i C-isti, così:

var number = 0;
while (number <= 12) 
  number = number + 2;

As an example that actually does something useful, we can now write a program that calculates and shows the value of 210 (2 to the 10th power). We use two variables: one to keep track of our result and one to count how often we have multiplied this result by 2. The loop tests whether the second variable has reached 10 yet and then updates both variables (file c2.js). Ovviamente si tratta solo di un esempio, lo stesso risultato si ottiene con Math.pow(2, 10).

// file c2.js (questo è un commento)
var result = 1;
var counter = 0;
while (counter < 10) {
  result = result * 2;
  counter = counter + 1;

The counter could also start at 1 and check for <= 10, but, for reasons that will that will become apparent in Chapter 4, it is a good idea to get used to counting from 0.

The do loop is a control structure similar to the while loop. It differs only on one point: a do loop always executes its body at least once, and it starts testing whether it should stop only after that first execution. To reflect this, the test appears after the body of the loop:

do {
  var yourName = prompt("Who are you?");
} while (!yourName);

This program will force you to enter a name. It will ask again and again until it gets something that is not an empty string. Applying the ! operator will convert a value to Boolean type before negating it, and all strings except “” convert to true. This means the loop continues going round until you provide a name that is not the empty string.

L’esempio può essere eseguito solo nel browser perché c’è la chiamata a prompt; propongo di rifare il precedente usando do al posto di while, file c3.js:

// file c3.js

var result = 1;
var counter = 0;

do {
  result = result * 2;
  counter = counter + 1;
} while (counter < 10)



NumPy – 34 – manipolare dati con Pandas

Continuo da qui iniziando a copiare da un capitolo nuovo, qui.

Finora abbiamo visto gli oggetti di ndarray forniti da NumPy. Ma c’è il package Pandas: Pandas is a newer package built on top of NumPy, and provides an efficient implementation of a DataFrame. DataFrames are essentially multidimensional arrays with attached row and column labels, and often with heterogeneous types and/or missing data. As well as offering a convenient storage interface for labeled data, Pandas implements a number of powerful data operations familiar to users of both database frameworks and spreadsheet programs.

As we saw, NumPy’s ndarray data structure provides essential features for the type of clean, well-organized data typically seen in numerical computing tasks. While it serves this purpose very well, its limitations become clear when we need more flexibility (e.g., attaching labels to data, working with missing data, etc.) and when attempting operations that do not map well to element-wise broadcasting (e.g., groupings, pivots, etc.), each of which is an important piece of analyzing the less structured data available in many forms in the world around us. Pandas, and in particular its Series and DataFrame objects, builds on the NumPy array structure and provides efficient access to these sorts of “data munging” tasks that occupy much of a data scientist’s time.

We will focus on the mechanics of using Series, DataFrame, and related structures effectively. We will use examples drawn from real datasets where appropriate, but these examples are not necessarily the focus.

Installare Pandas
Basta seguire le istruzioni della documentazione.
Once Pandas is installed, you can import it and check the version:

Just as we generally import NumPy under the alias np, we will import Pandas under the alias pd:

Nota sulla documentazione
IPython gives you the ability to quickly explore the contents of a package (by using the tab-completion feature) as well as the documentation of various functions (using the ? character). (Refer back to Help and Documentation in IPython if you need a refresher on this.)
For example, to display all the contents of the pandas namespace, you can type pd.<TAB>:

Questo è solo l’inizio, lunghissimissimo l’elenco.
And to display Pandas’s built-in documentation, you can use this:

More detailed documentation, along with tutorials and other resources, can be found here.