LoginSignup
0
0

More than 5 years have passed since last update.

等差数列

Posted at
# 等差数列
# http://nabetani.sakura.ne.jp/hena/ord11arithseq/
# http://qiita.com/Nabetani/items/c206fbc645c255cb7de6
# 
CHAR_TBL = [*"0".."9", *"a".."z"]
CHAR2NUM_TBL = Hash[
  *CHAR_TBL
    .map.with_index
    .to_a
    .flatten
]
SEQ_MAX = CHAR_TBL.size

def com_diff_seq_max_length(input)
  seq = input.split("").map { |c| CHAR2NUM_TBL[c] }
  (1...SEQ_MAX).map { |diff|
    (0...diff).map { |ini|
      seq
        .map { |n|
          n - ini
        }
        .select { |n|
          n >= 0 && n % diff == 0
        }
        .map.with_index { |n, i|
          (n / diff) - i
        }
        .group_by { |n|
          n
        }
        .values
        .map { |v|
          v.size
        }
        .max
    }
  }
  .flatten
  .select { |n|
    n != nil
  }
  .max

end

def test(input, expected)
  result = com_diff_seq_max_length(input).to_s
  print (result == expected) ? "." : "E"
end

test( "12345abcz", "5" )                                 # 0 
test( "012abku", "4" )                                   # 1 
test( "01245689cdeghik", "6" )                           # 2 
test( "0", "1" )                                         # 3 
test( "m", "1" )                                         # 4 
test( "01", "2" )                                        # 5 
test( "az", "2" )                                        # 6 
test( "0az", "2" )                                       # 7 
test( "0ak", "3" )                                       # 8 
test( "05ak", "3" )                                      # 9 
test( "01349acdrsuv", "2" )                              # 10
test( "01245789efgipqstux", "3" )                        # 11
test( "0123456789abcdefghijklmnopqrstuvwxyz", "36" )     # 12
test( "02468acegikmoqsuwy", "18" )                       # 13
test( "0369cfilorux", "12" )                             # 14
test( "048cgkosw", "9" )                                 # 15
test( "05afkpuz", "8" )                                  # 16
test( "0123456789abcdefghjklmnopqrstuvwxyz", "18" )      # 17
test( "0123456789bcdefghijklmopqrstuvwxyz", "12" )       # 18
test( "0156abfgklpquv", "7" )                            # 19
test( "0167cdijopuv", "6" )                              # 20
test( "0178eflmst", "5" )                                # 21
test( "0189ghopwx", "5" )                                # 22
test( "019aijrs", "4" )                                  # 23
test( "012567abcfghklmpqruvw", "7" )                     # 24
test( "012678cdeijkopquvw", "6" )                        # 25
test( "012789efglmnstu", "5" )                           # 26
test( "01289aghiopqwxy", "5" )                           # 27
test( "0129abijkrst", "4" )                              # 28
test( "01235678abcdfghiklmnpqrsuvwx", "7" )              # 29
test( "01236789cdefijklopqruvwx", "12" )                 # 30
test( "0123789aefghlmnostuv", "5" )                      # 31
test( "012389abghijopqrwxyz", "5" )                      # 32
test( "01239abcijklrstu", "4" )                          # 33
test( "368acdknouvz", "4" )                              # 34
test( "369chikmnopqruwx", "6" )                          # 35
test( "05689cdefghijklmnopqrstvwy", "18" )               # 36
test( "2489abdeiklrsuvwz", "4" )                         # 37
test( "678bhijklnpqrsuvwxyz", "6" )                      # 38
test( "1246cfjkopquxz", "5" )                            # 39
test( "123459abcefhilmotuvx", "6" )                      # 40
test( "02578acdefikmopqsuvwxz", "8" )                    # 41
test( "135abdefghijlopstuwz", "7" )                      # 42
test( "0126789fgjnotuvxy", "5" )                         # 43
test( "2345678defjkmnoqrtvwxy", "7" )                    # 44
test( "02568bdemnostw", "5" )                            # 45
test( "145689bdfhilnqrstvwxz", "6" )                     # 46
test( "4aghjrtuvwxyz", "7" )                             # 47
test( "158achklmqstwy", "3" )                            # 48
test( "012346abceghjknortv", "5" )                       # 49
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0