-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pap Lőrinc
committed
Mar 6, 2016
1 parent
d0a8e98
commit 72e565f
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/main/groovy/Ch06_MathAndLogicPuzzles/_06_09_Lockers.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package Ch06_MathAndLogicPuzzles | ||
|
||
/** Given closed lockers, by opening everyone, closing every second, opening every third, etc, which lockers will be open at the end? */ | ||
class _06_09_Lockers { | ||
static openLockers(int lockerCount) { | ||
def results = new BitSet() | ||
|
||
def lockerRange = 0..lockerCount | ||
for (step in lockerRange) | ||
lockerRange.step(step + 1) { results.flip(it) } | ||
|
||
results.stream().collect() | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/groovy/Ch06_MathAndLogicPuzzles/_06_09_LockersTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package Ch06_MathAndLogicPuzzles | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
import static Ch06_MathAndLogicPuzzles._06_09_Lockers.openLockers | ||
import static java.lang.Math.sqrt | ||
|
||
@Unroll class _06_09_LockersTest extends Specification { | ||
/*@formatter:off*/ | ||
def 'lockers?'() { | ||
expect: openLockers(lockerCount) == result | ||
where: lockerCount || result | ||
100 || (0..sqrt(lockerCount)).collect{ it**2 } | ||
} | ||
/*@formatter:on*/ | ||
} |