Skip to content
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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Conversation

kakusuke
Copy link

@kakusuke kakusuke commented Nov 7, 2013

配列のメソッドがまだイマイチうまく使いこなせません。。。

@Sixeight
Copy link
Member

Sixeight commented Nov 8, 2013

LGTM!
LGTM
👍 👎

def guess(numbers)
@numbers = numbers.each_char.to_a

'+' * hit_count + '-' * match_count
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hit_count の処理が2回実行されることになりますね。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メモイズすれば良さげです

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メモイズって初めて聞きました。
そういう実装があるんですねー。勉強になります

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メモイズするとgessメソッド中で初期化処理書かないといけない気がするので、
あまり処理速度気にする必要がなければあえてメモイズしないのもアリじゃないでしょうか?

Copy link
Member

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)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、文字列の上書きができればそんな感じのやり方でもうちょっとうまく書ける気がしてきました

Copy link
Author

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

うーん、でもやっぱり読みにくいですね。。。

Copy link
Member

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 { '+' }

この方法も面白いですね。

Copy link
Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この初期化は不要です。インスタンスメソッド存在しなくてもnilが返るので hit_count の中でいきなり書けばOKです。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2回目gessるとバグりませんか?

ちょっとテスト書いてみます

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

おお、ほんとですね。すみません。
そうするとメモイズがだいぶダサいので最初のコードが美しいと思います。

Copy link
Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この実装面白いですね。なるほどー、って感じです。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

頭ひねってこのやり方しか出てこなかったです
どう実装するのが自然でしょうかね。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants