仲間はずれの判定
30分プログラム、その134。仲間はずれの判定。
眠いので、あまり調子がよくない。明日、別の言語でやりなおす。
とりあえず動いてるけど、なにか漏れがある気がする。
使い方
$ ruby class.rb [1, 1, 1, 1, 1] = homo [1, 1, 2, 1, 1] = quasi_homo [2, 1, 1, 1, 1] = quasi_homo [2, 1, 4, 1, 1] = hetero
ソースコード
#! /opt/local/bin/ruby -w # -*- mode:ruby; coding:utf-8 -*- # # class.rb - # # Copyright(C) 2007 by mzp # Author: MIZUNO Hiroki <hiroki1124@gmail.com> # http://mzp.sakura.ne.jp/ # # Timestamp: 2007/09/15 23:25:35 # # This program is free software; you can redistribute it and/or # modify it under the same terms as Ruby itself. # def classify(xs) ys = xs.sort if ys[0] != ys[-1] then if ys[0] != ys[-2] then :hetero else :quasi_homo end else :homo end end [[1,1,1,1,1],[1,1,2,1,1],[2,1,1,1,1],[2,1,4,1,1]].map{|xs| puts "#{xs.inspect} = #{classify xs}" }