RosettaCodeData/Task/SEDOLs/Ruby/sedols.rb

38 lines
758 B
Ruby
Raw Permalink Normal View History

2013-10-27 22:24:23 +00:00
Sedol_char = "0123456789BCDFGHJKLMNPQRSTVWXYZ"
2019-09-12 10:33:56 -07:00
Sedolweight = [1,3,1,7,3,9]
2013-10-27 22:24:23 +00:00
2013-04-10 23:57:08 -07:00
def char2value(c)
2019-09-12 10:33:56 -07:00
raise ArgumentError, "Invalid char #{c}" unless Sedol_char.include?(c)
2013-04-10 23:57:08 -07:00
c.to_i(36)
end
def checksum(sedol)
2015-02-20 00:35:01 -05:00
raise ArgumentError, "Invalid length" unless sedol.size == Sedolweight.size
2019-09-12 10:33:56 -07:00
sum = sedol.chars.zip(Sedolweight).sum{|ch, weight| char2value(ch) * weight }
2013-10-27 22:24:23 +00:00
((10 - (sum % 10)) % 10).to_s
2013-04-10 23:57:08 -07:00
end
2019-09-12 10:33:56 -07:00
data = %w(710889
2013-10-27 22:24:23 +00:00
B0YBKJ
406566
B0YBLH
228276
B0YBKL
557910
B0YBKR
585284
B0YBKT
B00030
C0000
1234567
2019-09-12 10:33:56 -07:00
00000A)
2013-10-27 22:24:23 +00:00
2019-09-12 10:33:56 -07:00
data.each do |sedol|
2013-10-27 22:24:23 +00:00
print "%-8s " % sedol
begin
2013-04-10 23:57:08 -07:00
puts sedol + checksum(sedol)
2013-10-27 22:24:23 +00:00
rescue => e
p e
end
2013-04-10 23:57:08 -07:00
end