-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Codebreaker解いてみました #2
base: master
Are you sure you want to change the base?
Conversation
def guess(numbers) | ||
@numbers = numbers.each_char.to_a | ||
|
||
'+' * hit_count + '-' * match_count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hit_count の処理が2回実行されることになりますね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
メモイズすれば良さげです
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
メモイズって初めて聞きました。
そういう実装があるんですねー。勉強になります
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
メモイズするとgessメソッド中で初期化処理書かないといけない気がするので、
あまり処理速度気にする必要がなければあえてメモイズしないのもアリじゃないでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
現状 readable ですしね。
僕だと、↓ みたいな感じで書いちゃいますけど、readable じゃなくなるんですよね。。。
'+' * (h = hit_count) + '-' * (total_count - h)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、文字列の上書きができればそんな感じのやり方でもうちょっとうまく書ける気がしてきました
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こんな感じに書けばメソッド呼び出し1回ずつでいけそうです
('+' * total_count).each_char.to_a.tap {|marks|
marks.fill('-', hit_count..-1)
}.join
うーん、でもやっぱり読みにくいですね。。。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
('+' * total_count).each_char.to_a
は、↓ で良さそうです。
total_count.times.map { '+' }
この方法も面白いですね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、ちょっとすっきりしますね
total_count.times.map { '+' }.tap {|marks|
marks.fill('-', hit_count..-1)
}.join
文字列を一旦配列にしてうんぬんが無駄な気がしますね
文字列の一部を直接指定した文字で埋められたら良さそうですが。。。
('+' * total_count).fill('-', hit_count..-1)
みたいなのが理想です
|
||
def guess(numbers) | ||
@numbers = numbers.each_char.to_a | ||
@hit_count = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この初期化は不要です。インスタンスメソッド存在しなくてもnil
が返るので hit_count
の中でいきなり書けばOKです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2回目gessるとバグりませんか?
ちょっとテスト書いてみます
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
おお、ほんとですね。すみません。
そうするとメモイズがだいぶダサいので最初のコードが美しいと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます
戻しました
def total_count | ||
(0..9).map(&:to_s).reduce(0) do |count, n| | ||
count + [@secret.count(n), @numbers.count(n)].min | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この実装面白いですね。なるほどー、って感じです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
頭ひねってこのやり方しか出てこなかったです
どう実装するのが自然でしょうかね。。。
配列のメソッドがまだイマイチうまく使いこなせません。。。