Skip to content

Commit

Permalink
Merge f9c3451 into d5a8372
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Feb 27, 2019
2 parents d5a8372 + f9c3451 commit 26f85c7
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,4 @@ problems from
* [Day 184](https://github.com/vaskoz/dailycodingproblem-go/issues/378)
* [Day 185](https://github.com/vaskoz/dailycodingproblem-go/issues/381)
* [Day 186](https://github.com/vaskoz/dailycodingproblem-go/issues/382)
* [Day 188](https://github.com/vaskoz/dailycodingproblem-go/issues/388)
25 changes: 25 additions & 0 deletions day188/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package day188

// MakeFunctionsOriginal is the problem version.
func MakeFunctionsOriginal() []func() int {
var funcList []func() int
for _, i := range []int{1, 2, 3} {
funcList = append(funcList, func() int {
return i
})
}
return funcList
}

// MakeFunctionsCorrect is the corrected version.
func MakeFunctionsCorrect() []func() int {
var funcList []func() int
for _, i := range []int{1, 2, 3} {
funcList = append(funcList, func(i int) func() int {
return func() int {
return i
}
}(i))
}
return funcList
}
13 changes: 13 additions & 0 deletions day188/problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def make_functions():
flist = []

for i in [1, 2, 3]:
def print_i():
print(i)
flist.append(print_i)

return flist

functions = make_functions()
for f in functions:
f()
15 changes: 15 additions & 0 deletions day188/problem_correct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def make_functions():
flist = []

for i in [1, 2, 3]:
def build_print_i(i):
def print_i():
print(i)
return print_i
flist.append(build_print_i(i))

return flist

functions = make_functions()
for f in functions:
f()
23 changes: 23 additions & 0 deletions day188/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package day188

import "testing"

func TestMakeFunctionsOriginal(t *testing.T) {
t.Parallel()
funcs := MakeFunctionsOriginal()
for _, f := range funcs {
if result := f(); result != 3 {
t.Errorf("Expected every response to be 3. Got %v", result)
}
}
}

func TestMakeFunctionsCorrect(t *testing.T) {
t.Parallel()
funcs := MakeFunctionsCorrect()
for i, f := range funcs {
if result := f(); result != i+1 {
t.Errorf("Expected %v, got %v", i+1, result)
}
}
}

0 comments on commit 26f85c7

Please sign in to comment.