r/PowerShell • • Sep 18 '19

Parenting Through PowerShell

My 8 year old decided to pretend to be sick so he would get sent home from school the other day. So after we finished all the work the teach gave us, I decided to make him some extra work. Since they are learning double digit addition I wrote a quick script that will generate a page with 25 problems on it. I kept printing him off new sheets until the end of the regular school day.

I thought I'd share it in case anyone else has a 2nd grader who likes to play hooky. https://www.dowst.dev/homework-double-digit-addition-without-carrying/

Does anyone else have any examples of parenting through PowerShell?

229 Upvotes

67 comments sorted by

View all comments

4

u/ihaxr Sep 18 '19

I got bored and decided to tweak the script a bit more to make it more flexible on the problem count and group sizes so you can adjust the number of problems and the group size to configure a PROBLEM x GROUP array (and if it's not a perfect square, it still works)

function Get-SumsUnder100 {
    $a = Get-Random -Minimum 10 -Maximum 50
    $b = Get-Random -Minimum 10 -Maximum 90
    if ($a + $b -gt 100) {
        Get-SumsUnder100
    } else { [PSCustomObject]@{"a" = $a; "b" = $b}
    }
}

function GenerateProblems ([int]$count) {
    $Results = 1..$count | ForEach-Object {
        Get-SumsUnder100
    }
    $Results
}

$problems = GenerateProblems -count 25
$groupSize = 5

$html = {
    "<style> td {text-align: right; font: 24px monospace; padding:0px 15px 50px 15px;} hr {border:1px solid black; display:block}</style><table>"
    for( $i=0; $i -lt $problems.Count; $i+=$groupSize ) {
        "<tr>"
        foreach ($problem in ($problems[$i..($i+($groupSize-1))]) ) {
            "<td>$($problem.a) <br/> + $($problem.b)<hr/></td>"
        }
        "</tr>"
    }
    "</table>"
}

$html.Invoke() | Out-File -Force -FilePath "output.html"

4

u/ka-splam Sep 18 '19 edited Sep 18 '19

Off topic; I was trying to do this in APL, and I came up with the idea of generating all combinations of two digit number addition into a table, filtering the ones below 100, taking the (x, y) axis indices of those as the available choices of addition problems for the worksheet, then taking nine pairs at random:

(⊢⌷⍨(⊂9?⍴)) ⍸100>∘.+⍨9+⍳90

e.g. these pairs.

61 1  23 55  35 33  11 21  1 48  52 3  5 30  47 29  15 52

Formatting them was as difficult and frustrating again, but I ended up with a neat grid. Room for the answer, and an underline, left as an exercise for the reader, lol.

      3 3 ⍴ {(⍪' +'),↑⍵}¨ (⊢⌷⍨(⊂9?⍴)) ⍸100>∘.+⍨9+⍳90

┌────┬────┬────┐
│  12│  55│  29│
│+ 22│+  6│+ 14│
├────┼────┼────┤
│   6│  47│  10│
│+ 33│+ 29│+ 27│
├────┼────┼────┤
│   3│  41│  40│
│+ 33│+ 26│+ 20│
└────┴────┴────┘

TryAPL.org link for 3 3 ⍴ {(⍪' +'),↑⍵}¨ (⊢⌷⍨(⊂9?⍴)) ⍸100>∘.+⍨9+⍳90

yes this took ages. yes this generates 8,100 pairs to find 9 random ones just to avoid using a loop around an if/else.

2

u/Lee_Dailey [grin] Sep 19 '19

/lee wonders ... is ka-splam insane, unsane, non-sane, or barely won a sanity/insanity roll freaky? [grin]

2

u/ka-splam Sep 19 '19 edited Sep 19 '19

Lol, I think I'm going mad spending so long trying to get this version working!

      {(⍪' +'),⊃⍵}¨1↓{⍵,⍣(100>+/⊃p)⊢p←⊂9+? 89 89}⍣{6=≢⍺}⊢⍬
   16     60     56     18     29 
 + 72   + 26   + 30   + 68   + 64 

It's fun though, in a way, like Regex is fun!

If this is the first nine numbers:

      ⍳9
1 2 3 4 5 6 7 8 9 

Then this is the addition table of each one, with each one:

      (⍳9) ∘.+ (⍳9)
 2  3  4  5  6  7  8  9 10
 3  4  5  6  7  8  9 10 11
 4  5  6  7  8  9 10 11 12
 5  6  7  8  9 10 11 12 13
 6  7  8  9 10 11 12 13 14
 7  8  9 10 11 12 13 14 15
 8  9 10 11 12 13 14 15 16
 9 10 11 12 13 14 15 16 17
10 11 12 13 14 15 16 17 18

And this is a bitmap on that table, of all the places where 10> value shown by a 1 (true), and 0 (false):

      10>(⍳9) ∘.+ (⍳9)
1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 0 0
1 1 1 1 1 1 0 0 0
1 1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0 0
1 1 1 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

And this is the coordinates (x, y) indexes into the matrix, where the 1 values are:

      ⍸10>(⍳9) ∘.+ (⍳9)
 1 1  1 2  1 3  1 4  1 5  1 6  1 7  1 8  2 1  2 2  2 3  2 4  2 5  2 6  2 7  3 1  3 2  3 3  3 4 
 3 5  3 6  4 1  4 2  4 3  4 4  4 5  5 1  5 2  5 3  5 4  6 1  6 2  6 3  7 1  7 2  8 1 

So those are all the pairs where the single digit addition would still be a single digit answer, no carrying. Smaller, simpler version.

Putting the same thing on both sides of a function can use ⍨ to echo it to both sides so (⍳9) ∘.+ (⍳9) -> ∘.+⍨ (⍳9) and the parens don't do anything there, so they can go..

Isn't there a certain regex-style novelty there?

1

u/Lee_Dailey [grin] Sep 19 '19

howdy ka-splam,

i've come to consider APL to be ... the equivalent of quantum mechanics. useful, interesting, bizarre, and a handy bit of evidence that really smart people tend to be more than slightly freaky ... [grin]

take care,
lee

2

u/ka-splam Sep 19 '19

Haha, yes I'd agree with a lot of that. I'm just hoping that if I go close enough, some of their smarts' dust will rub off on me :P

1

u/Lee_Dailey [grin] Sep 19 '19

[grin]

2

u/ka-splam Sep 19 '19

It's not working yet; Adám from Dyalog came up with 9+91(⊢,-)¨?5⍴90 to generate the pairs in about 3 minutes and 4 revisions, I've got a lonnggggg way to go. :D

2

u/Lee_Dailey [grin] Sep 19 '19

howdy ka-splam,

i hope you are having fun with it. [grin] i do enjoy digging thru a problem ... but APL is like an especially cryptic page of egyptian hieroglyphs.

take care,
lee