CUSIP: Difference between revisions

188 bytes removed ,  15 days ago
(→‎{{header|Modula-2}}: Added MiniScript)
 
(5 intermediate revisions by 2 users not shown)
Line 55:
<syntaxhighlight lang=11l>F cusip_check(=cusip)
I cusip.len != 9
X.throw ValueError(‘CUSIP must be 9 characters’)
 
cusip = cusip.uppercase()
Line 2,988:
=={{header|langur}}==
If we don't strictly follow the pseudo-code, we can do this.
<syntaxhighlight lang=langur>val .isCusip = ffn(.s) {
 
if s is not isString(.s)string or len(.s) != 9 {
{{works with|langur|0.8.5}}
<syntaxhighlight lang=langur>val .isCusip = f(.s) {
if not isString(.s) or len(.s) != 9 {
return false
}
 
val .basechars = cp2s('0'..'9') ~ cp2s('A'..'Z') ~ "*@#"
 
val .sum = for[=0] .i of 8 {
var .v = index(s2s(.s, .i), .basechars)
if not .v: return false
.v = .v[1]-1
if .i div 2: .v x*= 2
_for += .v \ 10 + .v rem 10
}
 
.s[9]-'0' == (10-(.sum rem 10)) rem 10
}
 
val .candidates = wfw/037833100 17275R102 38259P508 594918104 68389X106 68389X105/
 
for .c in .candidates {
writeln .c, ": ", if(.isCusip(.c): "good" ; "bad")
}
}</syntaxhighlight>
 
Following the pseudo-code would look more like the following.
 
{{works with|langur|0.8.5}}
{{trans|Go}}
<syntaxhighlight lang=langur>val .isCusip = ffn(.s) {
if s is not isString(.s)string or len(.s) != 9 {
return false
}
 
val .sumbasechars = for[='0] '.i.'9' of~ 'A'..'Z' 8~ {"*@#"
val .c = .s[.i]
var .v = 0
 
val sum = for[=0] giveni .cof 8 {
val .c = .s[.i]
# for given, default op between conditions is "and"
var .v = 0
# for switch, default op between conditions is "or"
 
switch[and] c {
case >= '0', <= '9':
.v = .c-'0'
 
case >= 'A', <= 'Z':
.v = .c-55 # .c-'A'+10
 
case '*': .v = 36
case '@': .v = 37
case '#': .v = 38
 
default: return false
}
 
if .i div 2: .v x*= 2
_for += .v \ 10 + .v rem 10
}
 
.s[9]-'0' == (10-(.sum rem 10)) rem 10
}
 
val .candidates = wfw/037833100 17275R102 38259P508 594918104 68389X106 68389X105/
 
for .c in .candidates {
writeln .c, ": ", if(.isCusip(.c): "good" ; "bad")
}
}</syntaxhighlight>
 
{{out}}
1,007

edits