Jump to content

CUSIP: Difference between revisions

1,330 bytes added ,  7 years ago
m (added whitespace and highlighting to the task's preamble.)
Line 473:
68389X106: False
68389X105: True</pre>
 
=={{header|Phix}}==
<lang Phix>sequence cch = {}
 
function CusipCheckDigit(string cusip)
integer s = 0, c, v
if length(cch)=0 then
cch = repeat(-1,256)
for i='0' to '9' do
cch[i+1] = i-'0'
end for
for i='A' to 'Z' do
cch[i+1] = i-55
end for
cch['*'+1] = 36
cch['@'+1] = 37
cch['#'+1] = 38
end if
if length(cusip)!=9 then return 0 end if
for i=1 to 8 do
c := cusip[i]
v := cch[c+1]
if v=-1 then ?{v,c} return 0 end if
if remainder(i,2)=0 then
v *= 2
end if
s += floor(v/10)+mod(v,10)
end for
return cusip[9]=mod(10-mod(s,10),10)+'0'
end function
 
sequence tests = {"037833100", -- Apple Incorporated
"17275R102", -- Cisco Systems
"38259P508", -- Google Incorporated
"594918104", -- Microsoft Corporation
"68389X106", -- Oracle Corporation (incorrect)
"68389X105"} -- Oracle Corporation
 
for i=1 to length(tests) do
string ti = tests[i]
printf(1,"%s : %s\n",{ti,{"invalid","valid"}[CusipCheckDigit(ti)+1]})
end for</lang>
{{out}}
<pre>
037833100 : valid
17275R102 : valid
38259P508 : valid
594918104 : valid
68389X106 : invalid
68389X105 : valid
</pre>
 
=={{header|Racket}}==
7,820

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.