Skip to content

Commit 4624f93

Browse files
committed
better analysis
1 parent fa51951 commit 4624f93

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

maximum-ice-cream-bars/README.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Return the maximum number of ice cream bars the boy can buy with `coins` coins.
1414

1515
## Example 1
1616

17-
<table id="orgeabf3ff" border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
17+
<table id="orgb64d74a" border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
1818

1919

2020
<colgroup>
@@ -47,7 +47,7 @@ Return the maximum number of ice cream bars the boy can buy with `coins` coins.
4747

4848
## Example 2
4949

50-
<table id="org648cec1" border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
50+
<table id="org6eb7347" border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
5151

5252

5353
<colgroup>
@@ -80,7 +80,7 @@ Return the maximum number of ice cream bars the boy can buy with `coins` coins.
8080

8181
## Example 3
8282

83-
<table id="orgaedf5cd" border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
83+
<table id="orgb50ba5b" border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
8484

8585

8686
<colgroup>
@@ -188,7 +188,7 @@ So imagine moving down this list from left to right with `coins = 5` until the t
188188

189189
If you were to count the number of steps you took moving left toright you would get 3 as you should.
190190

191-
This seems to just be a simple loop in an iterator
191+
When considered in that way, this is just be a simple loop in an iterator. I love the loop facility so lets use that, and the nice thing about emacs generators is that because closures aren't "real" (everything is just a list after all) you can yield from pretty much anywhere - even from inside what in other languages would be higher order functions!
192192

193193
(iter-make
194194
(cl-loop for c in sorted-costs
@@ -199,7 +199,11 @@ This seems to just be a simple loop in an iterator
199199
do (iter-yield c)
200200
finally return nil))
201201

202-
And we can test that by passing in the example tables above and evaluating things.
202+
This will generate an iteration of all costs we have encountered before the runninig total was exceeded. Note that because we are passing back values with `iter-yield`, the `nil` value returned in the above code is immaterial, it is just necessary for the `cl-loop` syntax.
203+
204+
While we do not actually want the costs, we want just the total amount of costs that we passed, because we are using a generator here, there is no extra memory usage, we are simply streaming back values so adding this bit of functionality is effectively "free". To get the total number of ice cream bars the boy can get, we simply count the number (not total) of costs that are in the stream.
205+
206+
We can test that by passing in the example tables above and evaluating things.
203207

204208
(require 'generator)
205209
(let* ((expected (car (cdaddr data)))
@@ -213,21 +217,21 @@ And we can test that by passing in the example tables above and evaluating thing
213217
count 1)))
214218
(princ (if (equalp actual expected) "PASS" "FAIL")))
215219

216-
So call this against [Example 1](#orgd537517)
220+
So call this against [Example 1](#org09b5613)
217221

218222
1
219223
1
220224
2
221225
3
222226
PASS
223227

224-
Oh wow that seemeds to work. What about some others?
228+
Cool that seemeds to work. What about some others?
225229

226-
Lets run it on [Example 2](#org534f802)
230+
Lets run it on [Example 2](#org3551f66)
227231

228232
PASS
229233

230-
And now on [Example 3](#orgb05061f)
234+
And now on [Example 3](#orga73f0f4)
231235

232236
1
233237
1

maximum-ice-cream-bars/README.org

+7-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ So imagine moving down this list from left to right with ~coins = 5~ until the t
8686

8787
If you were to count the number of steps you took moving left toright you would get 3 as you should.
8888

89-
This seems to just be a simple loop in an iterator
89+
When considered in that way, this is just be a simple loop in an iterator. I love the loop facility so lets use that, and the nice thing about emacs generators is that because closures aren't "real" (everything is just a list after all) you can yield from pretty much anywhere - even from inside what in other languages would be higher order functions!
9090

9191
#+name: iterate-sorted-costs-you-could-afford
9292
#+begin_src emacs-lisp :eval no
@@ -100,7 +100,11 @@ This seems to just be a simple loop in an iterator
100100
finally return nil))
101101
#+end_src
102102

103-
And we can test that by passing in the example tables above and evaluating things.
103+
This will generate an iteration of all costs we have encountered before the runninig total was exceeded. Note that because we are passing back values with ~iter-yield~, the ~nil~ value returned in the above code is immaterial, it is just necessary for the ~cl-loop~ syntax.
104+
105+
While we do not actually want the costs, we want just the total amount of costs that we passed, because we are using a generator here, there is no extra memory usage, we are simply streaming back values so adding this bit of functionality is effectively "free". To get the total number of ice cream bars the boy can get, we simply count the number (not total) of costs that are in the stream.
106+
107+
We can test that by passing in the example tables above and evaluating things.
104108

105109
#+name: test---against-example--iterate-sorted-costs-you-could-afford
106110
#+begin_src emacs-lisp :results output :var data='() :eval no
@@ -128,7 +132,7 @@ So call this against [[Example 1]]
128132
: 3
129133
: PASS
130134

131-
Oh wow that seemeds to work. What about some others?
135+
Cool that seemeds to work. What about some others?
132136

133137
Lets run it on [[Example 2]]
134138

0 commit comments

Comments
 (0)