RosettaCodeData/Task/SEDOLs/Phix/sedols.phix
2026-04-30 12:34:36 -04:00

29 lines
1.1 KiB
Text

with javascript_semantics
constant cmap = reinstate(reinstate(reinstate(repeat(-1,255),
tagset('9','0'),tagset(9,0)),
tagset('Z','B'),tagset(35,11)),"EIOU",-1)
function sedol(string t)
string s = filter(t,"out","-")
integer l = length(s)
string res = "invalid length"
if l=6 or l=7 then
sequence e = extract(cmap,s[1..6])
if find(-1,e) then
res = "invalid char"
elsif e[1]<=9 and max(e)>9 then
res = "no letters when starts with number"
else
integer n = sum(sq_mul(e,{1,3,1,7,3,9})),
ch = mod(10-mod(n,10),10)+'0'
res = iff(l=7 ? "invalid checksum"[iff(ch=s[7]?3:1)..$] : t&ch)
end if
end if
return res
end function
for t in {"710889","B0YBKJ","406566","B0YBLH","228276","B0YBKL","557910","B0YBKR",
"585284","B0YBKT","B00030","B000300","B000301","","12","12345","BOYBKR",
"B1F3M59","B-1H5-4P6","B-123-45","ZZZZZZ","1ZZZZZ","123456","1234567"} do
printf(1,"%s -> %s\n",{t,sedol(t)})
end for