Skip to content

Instantly share code, notes, and snippets.

@masakikku
Last active May 29, 2019 09:25
Show Gist options
  • Save masakikku/16b4227396b9890ff64ef8301dd4f171 to your computer and use it in GitHub Desktop.
Save masakikku/16b4227396b9890ff64ef8301dd4f171 to your computer and use it in GitHub Desktop.
名前間違えました。
# 標準入力から得られた数字までの FizzBuzz を出力
# 引数に 1 ~ 100 の数値を指定してコマンドラインで以下を実行
$ ruby -e 'raise "illegal arguments. $ {this code} 1 to 100 number argument." unless [*1..100].include?(ARGV[0].to_i); 1.upto(ARGV[0].to_i){|i| out="#{[:Fizz][i%3]}#{[:Buzz][i%5]}"; p out.empty? ? i : out}'
♯ 標準入力から与えられた2つの値の最大公約数
# 引数に任意の数値を2つ指定してコマンドラインで以下を実行
$ ruby -e "seeds = ARGV[0].to_i > ARGV[1].to_i ? ARGV : [ARGV[1],ARGV[0]]; rcv=lambda{|a,b| if (a%b) == 0 then p b else rcv.call(b,a%b) end }; rcv.call(seeds[0].to_i,seeds[1].to_i) "
#!/usr/bin/env ruby
CARD_SUIT = %w|S H D C|
CARD_NUMBER = [*1..12]
POKER_HANDS = %w|_onepair _twopair _threecard _straight _flash _fullhouse _fourcard _straightflash|
def main(args)
out = nil
out = _validate(args)
if out
p out
exit
end
out = :HighCard
POKER_HANDS.each do |hand|
if v = self.send(hand, args)
out = v
end
end
p out
end
#validate
def _validate(args)
if args.size != 5
return :HighCard
end
args.map{|v| v.chars[0]}.each do |suit|
return :HighCard unless CARD_SUIT.include?(suit)
end
args.map{|v| v.chars[1]}.each do |num|
return :HighCard unless CARD_NUMBER.include?(num)
end
end
#Straight flash : 同じスートの連番5枚 (S2 S3 S4 S5 S6)
def _straightflash(args)
:StraightFlash if _straight(args) && _flash(args)
end
#Straight : 連番5枚 (S2 D3 H4 C5 S6)
def _straight(args)
ary = args.map{|v| v.chars[1].to_i }.sort
last = ary[0].to_i + 4
:Straight if ( ary & [*ary[0].to_i..last] ).size == 5
end
#Flash : 同じスートが5倍 (S2 S4 S9 S5 S1)
def _flash(args)
[:Flash][args.map{ |v| v.chars[0] }.uniq.size - 1]
end
#Four card : 同じ数字が4枚 (S2 S6 H6 C6 D6)
def _fourcard(args)
:FourCards if _counter(args, 4) > 0
end
#Three cards : 同じ数字3枚 (S2 H3 H6 C6 D6)
def _threecard(args)
:ThreeCards if _counter(args, 3) > 0
end
#Two pair : 同じ数字2枚の組み合わせ2個(S2 H2 H10 C6 D6)
def _twopair(args)
:TwoPair if _counter(args, 2) > 1
end
#One pair : 同じ数字が2枚 (S2 H2 H10 C6 D4)
def _onepair(args)
:OnePair if _counter(args, 2) > 0
end
#Full house : 同じ数字3 枚と2枚の組み合わせ (S2 H2 H6 C6 D6)
def _fullhouse(args)
:FullHouse if _counter(args, 2) > 0 && _counter(args, 3) > 0
end
def _counter(args, number)
k = Hash.new(0)
args.map{|v| v.chars[1] }.each{|x| k[x] += 1 }
k.map{|key,value| value}.count(number)
end
main($*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment