Shirley

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

At the BAA webinar on 20th. September 2020 Ray Polivka described this programming test set 1n the 1950s in the course "Programs1" in the Iliac work at Illinois University. "Shirley's weight at birth was X tons stored in location A, For the first 20 weeks her weight increase linearly by 8oz per week. After that the rate of growth declines by a constant factor, held in location B, every four weeks. What is Shirley's weight at 16 weeks and at 52 weeks?". Ray observed that since the pupils were writing binary code with a handful of registers and a generous 1K of memory this was quite a stiff problem to encounter on your first course. The webinar participants discussed this for quite a while at the end of which Curtis Jones said Adam's approach was a good example of APL as a tool of thought. I commented that we were in effect dealing with the sum of a geometric series here. The function "shirley" shows how I coded a solution using the series. Starting with a birth weight of 100 ounces (six pounds four ounces), using a decay factor of 0.9 (meaning that growth in this month is .9 times the growth in last month) and using UK Imperial tons I get this answer:

```      0⍕((100÷16×2240) .9) #.shirley 16 52    ⍝ calculate Shirley's weight in ounces
228 424
```

This is the function

wt←xb shirley w;a;b;c;k;m;n;r ⍝ weight of Shirley in ounces after "w" weeks ("w" may be a vector or whatever)

```a b←(16×2240)1×2↑xb     ⍝ birth weight in ounces, monthly growth decay factor
a←(⌊0.5+16×a)÷16        ⍝ to nearest dram
m←w÷4          ⍝ weeks to lunar months
c←⌊m           ⍝ complete months
n←0⌈c-5        ⍝ number of complete months after 20 weeks
r←b*n          ⍝ growth factor to apply in last complete month
k←32           ⍝ growth per month in ounces for the first 20 weeks
wt←+⌿a⍪↑(k×5⌊m)(k×b×(n×b=1)+(1-r)÷(b=1)+1-b)(b×r×(m>5)×k×m-c)
→0
```
``` ⍝ wt=sum of (birth, first 20 weeks growth, the next complete months, the last month part)
```
``` ⍝ requires ⎕ML<2
⍝ line [9] was ⍝ wt←+/a(k×5⌊m),(k×b×(n×b=1)+(1-r)÷(b=1)+1-b),b×r×(m>5)×k×m-c
```
``` ⍝ for the "n" complete months after month 5 ...
⍝ the total weight increase is k×+/b*⍳n   (when 1=⎕IO)
⍝ which we know is k×b×(1-b*n)÷1-b        (sum of a geometric series)
```
``` ⍝ I use UK Imperial units where an ounce is 16 drams, a pound is 16 ounces,
⍝ a stone is 14 pounds, a hundreweight is 8 stone, a ton is 20 hundredweight.
⍝ So my ton is the "long ton" of 2240 pounds (2240=20×8×14).
⍝ In the US they use the short ton of 2000 pounds.
⍝ The metric ton (or tonne) has 2204.6226 pounds, it is 1000 kilograms.
```
```⍝ testing for birth weight 100oz (6 pounds, 4 ounces) and factor 0.9
⍝ ' 228 424'≡0⍕((100÷16×2240) .9) #.shirley 16 52
⍝ 100    ≡((100÷16×2240) .9) #.shirley 0
⍝ 108    ≡((100÷16×2240) .9) #.shirley 1
⍝ ....and several more tests
```

This is a traditional function which I might have written in 1970 (except for the mix on line [9]). I doubt if it would be faster, even if Shirley lives to be 100, than Adam's solution that does a +/ over all the weeks. I suppose it reflects the unreconstructed way I think which has not changed that much since 1970. Notice however that my years of going to BAA London meetings are responsible for the fact that most of the functions I write now have unit tests at the bottom.

```      +unit '#.shirley'
```

All of 23 unit tests in #.shirley passed

```     100 14 16 16⊤⌊0.5+16×((100÷16×2240)0.93)shirley 0 16 52
```

0 1 1 6 0 13 4 4 15 0 0 4 So we see, with these assumptions, Shirley grows from 6 pounds and 4 ounces at birth to 1 stone and 4 ounces at 16 weeks and to 1 stone, 13 pounds, 15 ounces and 4 drams when a year old.