Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
38
Task/CUSIP/Ruby/cusip-1.rb
Normal file
38
Task/CUSIP/Ruby/cusip-1.rb
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
def check_cusip(cusip)
|
||||
abort('CUSIP must be 9 characters') if cusip.size != 9
|
||||
|
||||
sum = 0
|
||||
cusip.split('').each_with_index do |char, i|
|
||||
next if i == cusip.size - 1
|
||||
case
|
||||
when char.scan(/\D/).empty?
|
||||
v = char.to_i
|
||||
when char.scan(/\D/).any?
|
||||
pos = char.upcase.ord - 'A'.ord + 1
|
||||
v = pos + 9
|
||||
when char == '*'
|
||||
v = 36
|
||||
when char == '@'
|
||||
v = 37
|
||||
when char == '#'
|
||||
v = 38
|
||||
end
|
||||
|
||||
v *= 2 unless (i % 2).zero?
|
||||
sum += (v/10).to_i + (v % 10)
|
||||
end
|
||||
|
||||
check = (10 - (sum % 10)) % 10
|
||||
return 'VALID' if check.to_s == cusip.split('').last
|
||||
'INVALID'
|
||||
end
|
||||
|
||||
CUSIPs = %w[
|
||||
037833100 17275R102 38259P508 594918104 68389X106 68389X105
|
||||
]
|
||||
|
||||
CUSIPs.each do |cusip|
|
||||
puts "#{cusip}: #{check_cusip(cusip)}"
|
||||
end
|
||||
11
Task/CUSIP/Ruby/cusip-2.rb
Normal file
11
Task/CUSIP/Ruby/cusip-2.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
TABLE = ("0".."9").chain("A".."Z", %w(* @ #)).zip(0..).to_h
|
||||
|
||||
def valid_CUSIP?(str)
|
||||
sum = str[0..-2].chars.each_slice(2).sum do |c1,c2|
|
||||
TABLE[c1].divmod(10).sum + (TABLE[c2]*2).divmod(10).sum
|
||||
end
|
||||
str[-1].to_i == (10 - (sum % 10)) % 10
|
||||
end
|
||||
|
||||
CUSIPs = %w(037833100 17275R102 38259P508 594918104 68389X106 68389X105)
|
||||
CUSIPs.each{|cusip| puts "#{cusip}: #{valid_CUSIP? cusip}"}
|
||||
Loading…
Add table
Add a link
Reference in a new issue