-
-
Save nikolat/1154136 to your computer and use it in GitHub Desktop.
お題:FizzBuzz(Nパターン) solved by Groovy
This file contains hidden or 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
// http://d.hatena.ne.jp/fumokmm/20110815/1313405510 | |
def fizzBuzzN(nums, list) { | |
def toMap = { lst -> (0..<lst.size()/2).collectEntries{ lst[it*2..it*2+1] } } | |
toMap(list).collect{ k, v -> | |
v = nums.collect{ it % k == 0 ? v : '' } | |
}.transpose()*.join().eachWithIndex{ it, idx -> | |
println(it ? it : idx+nums.from) | |
} | |
} | |
fizzBuzzN(1..100, [3, 'Fizz', 5, 'Buzz', 7, 'Hoge']) |
This file contains hidden or 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
def fizzBuzzN(nums, list): | |
dic = {} | |
while len(list) > 0: | |
dic[list.pop()] = list.pop() | |
for num in nums: | |
p = '' | |
for (k, v) in dic.items(): | |
if num % k == 0: | |
p += v | |
if p == '': | |
p = num | |
print p | |
if __name__ == '__main__': | |
fizzBuzzN(range(1, 101), [3, 'Fizz', 5, 'Buzz', 7, 'Hoge']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment