SEDOLs: Difference between revisions
m
→{{header|langur}}
(Added solution for Action!) |
Langurmonkey (talk | contribs) |
||
(17 intermediate revisions by 10 users not shown) | |||
Line 46:
{{trans|Python}}
<
assert(c !C ‘AEIOU’, ‘No vowels’)
R Int(c, radix' 36)
Line 69:
L(sedol) sedols.split("\n")
print(sedol‘’checksum(sedol))</
{{out}}
Line 86:
=={{header|Action!}}==
<
BYTE ARRAY weights=[1 3 1 7 3 9]
INT i,sum
Line 144:
Test("12345")
Test("1234567")
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/SEDOLs.png Screenshot from Atari 8-bit computer]
Line 165:
=={{header|ActionScript}}==
<
//ActionScript does not have a character type, so 1-digit strings
//are used instead
Line 214:
printWithCheck("585284");
printWithCheck("B0YBKT");
printWithCheck("B00030");</
=={{header|Ada}}==
<
procedure Test_SEDOL is
Line 256:
Put_Line (Test (Index) & Character'Val (Character'Pos ('0') + Check (Test (Index))));
end loop;
end Test_SEDOL;</
The function Check raises Constraint_Error upon an invalid input. The calculated sum is trimmed using (-''sum'') mod 10, which is mathematically equivalent to (10 - (''sum'' mod 10)) mod 10.
Line 274:
{{trans|C}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386 - ''char in string'', ''is alpha'', ''is digit'' and ''to upper'' are not in the standard's prelude}}
<
STRING reject = "AEIOUaeiou";
Line 331:
OD;
done: SKIP
)</
Output:
<pre>7108899
Line 345:
=={{header|ALGOL W}}==
<
% returns the check digit for the specified SEDOL %
string(1) procedure sedolCheckDigit ( string(6) value sedol ) ;
Line 387:
testCheckDigit( "B0YBKT", "7" );
testCheckDigit( "B00030", "0" )
end.</
{{out}}
<pre>
Line 405:
=={{header|AppleScript}}==
<
if ((count sedol) is not 6) then ¬
return {false, "Error in appendCheckDigitToSEDOL handler: " & sedol & " doesn't have 6 characters."}
Line 447:
set output to output as text
set AppleScript's text item delimiters to astid
return output</
{{output}}
<
B0YBKJ7
4065663
Line 460:
5852842
B0YBKT7
B000300"</
=={{header|Arturo}}==
<
ord7: to :integer `7`
c2v: function [c][
Line 488:
loop sedols 'sed ->
print [sed "->" sed ++ checksum sed]</
{{out}}
Line 508:
===Full===
<
Loop, Parse, codes, `,
output .= A_LoopField "`t-> " SEDOL(A_LoopField) "`n"
Line 526:
Return "Invalid character."
Return code . Mod(10-Mod(check_digit,10),10)
}</
===Short===
{{works with|AutoHotkey 1.1}}
<
MsgBox % SEDOL("B0YBKJ") ;B0YBKJ7
MsgBox % SEDOL("406566") ;4065663
Line 546:
s += ((c := Asc(A_LoopField)) >= 65 ? c - 65 + 10 : c - 48) * weights[A_Index]
return w Mod(10 - Mod(s, 10), 10)
}</
=={{header|AWK}}==
Validate or calculate checksum of SEDOL codes read from standard input (one per line)
<
{
return amap[a]
Line 598:
print sedol sedolcheck
}
}</
=={{header|BASIC}}==
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<
DO
INPUT a$
Line 632 ⟶ 633:
NEXT i
getSedolCheckDigit = (10 - (total MOD 10)) MOD 10
END FUNCTION</
=={{header|BBC BASIC}}==
<
PRINT FNsedol("B0YBKJ")
PRINT FNsedol("406566")
Line 655 ⟶ 656:
s% += (a% + 7 * (a% > 9)) * weights%(i%)
NEXT
= d$ + CHR$(&30 + (10 - s% MOD 10) MOD 10)</
'''Output:'''
<pre>
Line 674 ⟶ 675:
'''Notes''': it reads the codes from standard input, one per line (linefeed terminated); the input encoding must meet the following specifications: single byte encoding, digits (0-9) must have codes that follow the same order of the digits (0, 1, 2, ...) and similar for letters, the encoding must match the one used with the compiled source (likely, ASCII based encodings). This should happen 99% of the time (for ASCII, ISO-8859 family and UTF-8 have the same byte encoding for alphanumeric characters).
<
#include <ctype.h>
#include <string.h>
Line 721 ⟶ 722:
}
return 0;
}</
Fed the input list from the task description, the output is:
Line 737 ⟶ 738:
=={{header|C sharp|C#}}==
<
static int sedolChecksum(string sedol)
{
Line 763 ⟶ 764:
return (10 - (sum % 10)) % 10;
}</
=={{header|C++}}==
<
#include <numeric>
#include <cctype>
Line 810 ⟶ 811:
return 0;
}
</syntaxhighlight>
=={{header|Caché ObjectScript}}==
<
{
Line 832 ⟶ 833:
}
}</
{{out|Examples}}
<pre>USER>For { Read s Quit:s="" Write ": "_##class(Utils.Check).SEDOL(s), ! }
Line 850 ⟶ 851:
=={{header|Clojure}}==
<
(letfn [(sedol [ys] (let [weights [1 3 1 7 3 9]
convtonum (map #(Character/getNumericValue %) ys)
check (-> (reduce + (map * weights convtonum)) (rem 10) (->> (- 10)) (rem 10))]
(str ys check)))]
(map #(sedol %) xs)))</
=={{header|COBOL}}==
{{works with|GNU Cobol|2.0}}
<
IDENTIFICATION DIVISION.
PROGRAM-ID. sedol.
Line 937 ⟶ 938:
CLOSE sedol-file
.
END PROGRAM sedol.</
=={{header|Common Lisp}}==
Implemented from scratch using the description on Wikipedia as specification.
{{Works with|ClozureCL}}
<
(assert (<= 0 start end (length sedol)))
(assert (= (- end start) 6))
Line 953 ⟶ 954:
(head (subseq sedol start end))
(tail (digit-char posn)))
(return (concatenate 'string head (list tail))))))</
=={{header|D}}==
===Functional Version===
<
char checksum(in char[] sedol) pure @safe /*@nogc*/
Line 976 ⟶ 977:
B0YBKL 557910 B0YBKR 585284 B0YBKT".split)
writeln(sedol, sedol.checksum);
}</
{{out}}
<pre>7108899
Line 991 ⟶ 992:
===Imperative Version===
Longer, faster lower-level version, same output.
<
char sedolChecksum(in char[] sedol) pure nothrow @safe /*@nogc*/
Line 1,028 ⟶ 1,029:
"585284", "B0YBKT"])
writeln(s, s.sedolChecksum);
}</
===Short Version===
Same output.
<
import std.stdio, std.algorithm, std.string, std.numeric,std.ascii;
Line 1,040 ⟶ 1,041:
.map!(c => c.isDigit ? c - '0' : c - 'A' + 10)
.dotProduct([1, 3, 1, 7, 3, 9]) % 10);
}</
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 1,123 ⟶ 1,124:
readln;
end.
</syntaxhighlight>
Output:
Line 1,139 ⟶ 1,140:
=={{header|E}}==
<
def Digit := ('0'..'9')
def Letter := ('B'..'D'|'F'..'H'|'J'..'N'|'P'..'T'|'V'..'Z')
Line 1,175 ⟶ 1,176:
B0YBKT".trim().split("\n") {
println(addChecksum(sedol.trim()))
}</
=={{header|EasyLang}}==
<syntaxhighlight>
weights[] = [ 1 3 1 7 3 9 ]
func$ chksum sedol6$ .
if len sedol6$ <> 6
return ""
.
for i to 6
c$ = substr sedol6$ 1 1
if strpos "AEIOU" c$ <> 0
return ""
.
h = strcode substr sedol6$ i 1
if h >= 48 and h <= 57
h -= 48
elif h >= 65 and h <= 90
h -= 65 - 10
else
return ""
.
sum += h * weights[i]
.
return strchar ((10 - (sum mod 10)) mod 10 + 48)
.
repeat
s$ = input
until s$ = ""
print s$ & chksum s$
.
input_data
710889
B0YBKJ
406566
B0YBLH
228276
B0YBKL
557910
B0YBKR
585284
B0YBKT
B00030
</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Ruby}}
<
@sedol_char "0123456789BCDFGHJKLMNPQRSTVWXYZ" |> String.codepoints
@sedolweight [1,3,1,7,3,9]
Line 1,221 ⟶ 1,265:
e in ArgumentError -> IO.inspect e
end
end)</
{{out}}
Line 1,250 ⟶ 1,294:
{{Works with|Office 365 Betas 2021}}
<
=LAMBDA(s,
IF(6 = LEN(s),
Line 1,282 ⟶ 1,326:
"Expected a 6-character SEDOL"
)
)</
and also assuming that the names ELEM and MUL are bound to the following reusable lambdas in Name Manager:
<
=LAMBDA(x,
LAMBDA(xs,
Line 1,295 ⟶ 1,339:
MUL
=LAMBDA(a, LAMBDA(b, a * b))</
{{Out}}
Line 1,391 ⟶ 1,435:
=={{header|F Sharp|F#}}==
<
let Inputs = ["710889"; "B0YBKJ"; "406566"; "B0YBLH"; "228276"; "B0YBKL"
"557910"; "B0YBKR"; "585284"; "B0YBKT"; "B00030"]
Line 1,418 ⟶ 1,462:
addCheckDigit Inputs |> List.iter (printfn "%s")
with
ex -> printfn "ERROR: %s" ex.Message</
=={{header|Factor}}==
<
math math.parser regexp sequences unicode ;
IN: rosetta-code.sedols
Line 1,462 ⟶ 1,506:
input [ dup sedol-check-digit "%-6s %s\n" printf ] each ;
MAIN: sedol-demo</
{{out}}
<pre>
Line 1,485 ⟶ 1,529:
=={{header|Forth}}==
<
: char>num ( '0-9A-Z' -- 0..35 )
Line 1,512 ⟶ 1,556:
sedol" B0YBKR" B0YBKR5 ok
sedol" 585284" 5852842 ok
sedol" B0YBKT" B0YBKT7 ok</
=={{header|Fortran}}==
{{Works with|Fortran|90 and later}}
<
IMPLICIT NONE
CONTAINS
Line 1,558 ⟶ 1,602:
END DO
END PROGRAM SEDOLTEST</
Output
710889 7108899
Line 1,572 ⟶ 1,616:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,664 ⟶ 1,708:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>Calculated checksum
Line 1,702 ⟶ 1,746:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=905f91c785f1f15a360726717731862f Click this link to run this code]'''
<
Dim byWeight As Byte[] = [1, 3, 1, 7, 3, 9, 1]
Dim byCount, byCompute As Byte
Line 1,730 ⟶ 1,774:
Next
End</
Output:
<pre>
Line 1,747 ⟶ 1,791:
=={{header|Go}}==
<syntaxhighlight lang="go">
package main
Line 1,810 ⟶ 1,854:
}
}
</syntaxhighlight>
Output:
<pre>
Line 1,838 ⟶ 1,882:
=={{header|Groovy}}==
<
assert text.size() == 6 && !text.toUpperCase().find(/[AEIOU]+/) : "Invalid SEDOL text: $text"
Line 1,847 ⟶ 1,891:
text + (10 - (sum % 10)) % 10
}
String.metaClass.sedol = { this.&checksum(delegate) }</
Test Code:
<
'228276': '2282765', 'B0YBKL': 'B0YBKL9', '557910': '5579107', 'B0YBKR': 'B0YBKR5',
'585284': '5852842', 'B0YBKT': 'B0YBKT7', 'B00030': 'B000300'].each { text, expected ->
println "Checking $text -> $expected"
assert expected == text.sedol()
}</
Output:
<pre>Checking 710889 -> 7108899
Line 1,869 ⟶ 1,913:
=={{header|Haskell}}==
<
-------------------------- SEDOLS ------------------------
Line 1,917 ⟶ 1,961:
"BOYBKT", -- Ill formed test case - illegal vowel.
"B00030"
]</
{{Out}}
<pre>7108899
Line 1,933 ⟶ 1,977:
=={{header|Icon}} and {{header|Unicon}}==
<
every write(sedol("710889"|"B0YBKJ"|"406566"|"B0YBLH"|"228276"|
"B0YBKL"|"557910"|"B0YBKR"|"585284"|"B0YBKT"|"B00030"))
Line 1,951 ⟶ 1,995:
return x || (10 - (t%10)) % 10 # complete
}
end</
=={{header|J}}==
There are several ways to perform this in J. This most closely follows the algorithmic description at Wikipedia:
<
ac0 =: (, 10 | 1 3 1 7 3 9 +/@:* -)&.(sn i. |:)</
However, because J is so concise, having written the above, it becomes clear that the negation (<tt>-</tt>) is unnecessary.
Line 1,962 ⟶ 2,006:
Which leads us to this more efficient formulation:
<
which reduces to:
<
Which is just as concise as <tt>ac0</tt>, but faster.
Line 1,970 ⟶ 2,014:
Which leads us to:
<
Which is more than twice as fast as even the optimized formulation (<tt>ac2</tt>), though it is slightly longer.
=={{header|Java}}==
<
public class SEDOL{
Line 2,004 ⟶ 2,048:
return (str.length() == 6) && !str.toUpperCase().matches(".*?[AEIOU].*?");
}
}</
=={{header|JavaScript}}==
===Imperative===
<
return input + sedol_check_digit(input);
}
Line 2,048 ⟶ 2,092:
print("error: " + e);
}
}</
output
<pre>7108899
Line 2,066 ⟶ 2,110:
===Functional===
<
'use strict';
Line 2,229 ⟶ 2,273:
// MAIN ---
return main();
})();</
{{Out}}
<pre>7108899
Line 2,246 ⟶ 2,290:
{{works with|jq|1.4}}
This implementation accepts strings with lowercase letters, but converts them to uppercase.
<
explode | map( if 97 <= . and . <= 122 then . - 32 else . end) | implode;
Line 2,288 ⟶ 2,332:
else $ans
end ;
sedolize</
{{Out}}
# Assuming sedol.txt contains the input in the task description
Line 2,297 ⟶ 2,341:
{{works with|Julia|0.6}}
<
function appendchecksum(chars::AbstractString)
Line 2,317 ⟶ 2,361:
@test appendchecksum(t) == c
end
end</
{{out}}
Line 2,324 ⟶ 2,368:
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">
val weights = arrayOf(1, 3, 1, 7, 3, 9, 1)
val validChars = (('0'..'9') + ('A'..'Z')).toSet() - "AEIOU".toSet()
fun sedol7(sedol6: String): String {
require(sedol6.length == 6) { "Length of argument string must be 6" }
require(sedol6.all { it in validChars }) { "Argument string contains an invalid character" }
val sum = sedol6.map { it.digitToInt(36) }.zip(weights, Int::times).sum()
val check = (-sum).mod(10)
}
fun main(
val sedol6s = listOf(
"557910", "B0YBKR", "585284", "B0YBKT", "B00030"
)
for (sedol6 in sedol6s)
println("$sedol6 -> ${sedol7(sedol6)}")
}
</syntaxhighlight>
{{out}}
Line 2,367 ⟶ 2,408:
=={{header|langur}}==
{{trans|Go}}
<syntaxhighlight lang="langur">val .csd = fn(.code) {
case 0:
return "nada, zip, zilch"
Line 2,376 ⟶ 2,416:
}
if
return "invalid character(s)"
}
Line 2,384 ⟶ 2,424:
val .nums = s2n .code
val .sum = for[=0] .i of .nums {
_for += .nums[.i]
}
}
val .h =
# invalid...
"": 0,
Line 2,420 ⟶ 2,460:
writeln .input, ": ", .d
} else {
val .expect =
write .input, .d
writeln if .expect == .d {""} else {
$" (SEDOL test failed; expected check digit \.expect;)"}
}
}
</syntaxhighlight>
{{out}}
Line 2,450 ⟶ 2,491:
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
'adapted from BASIC solution
mult(1) = 1: mult(2) = 3: mult(3) = 1
Line 2,482 ⟶ 2,523:
getSedolCheckDigit = (10 - (total MOD 10)) MOD 10
END FUNCTION
</syntaxhighlight>
=={{header|M4}}==
<
changequote(`[',`]')
define([_bar],include(sedol.inp))
Line 2,501 ⟶ 2,542:
])
divert
eachline(_bar,[checksum])</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Code<>ToString[10-Mod[ToExpression[Quiet[Flatten[Characters[Code]
/.x_?LetterQ->(ToCharacterCode[x]-55)]]].{1,3,1,7,3,9},10]]
Scan[Print[SEDOL[#]] &, {"710889","B0YBKJ","406566","B0YBLH","228276","B0YBKL","557910","B0YBKR","585284","B0YBKT","B00030","DUMMY"}]</
{{out}}
<pre>
Line 2,524 ⟶ 2,565:
=={{header|Mercury}}==
<syntaxhighlight lang="text">:- module sedol.
:- interface.
Line 2,576 ⟶ 2,617:
is_vowel('I').
is_vowel('O').
is_vowel('U').</
=={{header|Modula-3}}==
<
IMPORT IO, Fmt, Text, Stdio;
Line 2,622 ⟶ 2,663:
| BadSedol(text) => IO.Put(text & "\n", Stdio.stderr);
END;
END SEDOL.</
Output:
<pre>7108899
Line 2,636 ⟶ 2,677:
=={{header|MUMPS}}==
<
NEW A,B
SEDOL1
Line 2,656 ⟶ 2,697:
NEW UP,LO
SET UP="ABCDEFGHIJKLMNOPQRSTUVWXYZ",LO="abcdefghijklmnopqrstuvwxyz"
QUIT $TRANSLATE(X,LO,UP)</
Examples:
<pre>USER>D SEDOL^ROSETTA
Line 2,688 ⟶ 2,729:
=={{header|Nim}}==
<
assert c notin "AEIOU"
if c < 'A': ord(c) - ord('0') else: ord(c) - ord('7')
Line 2,703 ⟶ 2,744:
"228276", "B0YBKL", "557910", "B0YBKR",
"585284", "B0YBKT", "B00030"]:
echo sedol, " → ", sedol & checksum(sedol)</
{{out}}
Line 2,719 ⟶ 2,760:
=={{header|OCaml}}==
<
assert (not (String.contains "AEIOU" c));
match c with
Line 2,746 ⟶ 2,787:
"B0YBKR";
"585284";
"B0YBKT" ]</
=={{header|Oforth}}==
<
[ 1, 3, 1, 7, 3, 9 ] s
zipWith(#[ dup isDigit ifTrue: [ '0' - ] else: [ 'A' - 10 + ] * ]) sum
10 mod 10 swap - 10 mod
StringBuffer new s << swap '0' + <<c ;</
{{out}}
Line 2,775 ⟶ 2,816:
=={{header|Pascal}}==
{{works with|Free_Pascal}}
<
function index(c: char): integer;
Line 2,819 ⟶ 2,860:
writeln(codes[i], ' -> ', seforl);
end;
end.</
Output:
<pre>% ./Sedols
Line 2,836 ⟶ 2,877:
=={{header|Perl}}==
This program reads from standard input.
<
use POSIX qw(strtol);
sub zip :prototype(&\@\@) {
my $f = shift;
my @a = @{shift()};
Line 2,849 ⟶ 2,890:
my @weights = (1, 3, 1, 7, 3, 9);
sub
my $s = shift;
$s =~ /[AEIOU]/ and die "No vowels";
Line 2,861 ⟶ 2,902:
chomp;
print sedol($_), "\n";
}</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">type</span> <span style="color: #000000;">string6</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004080;">string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">6</span>
Line 2,897 ⟶ 2,938:
<span style="color: #0000FF;">?</span><span style="color: #000000;">sedol</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 2,912 ⟶ 2,953:
"B000300"
</pre>
=={{header|Phixmonti}}==
{{trans|RPL}}
<syntaxhighlight lang="Phixmonti">include ..\Utilitys.pmt
def ->7DOL
( 1 3 1 7 3 9 ) var weights
0 >ps
6 for >ps
tps get
dup 65 >= if 7 - endif 48 -
weights ps> get nip * ps> + >ps
endfor
10 ps> 10 mod - 10 mod tostr chain
enddef
( "710889" "B0YBKJ" "406566" "B0YBLH" "228276" "B0YBKL" "557910" "B0YBKR" "585284" "B0YBKT" "B00030" )
getid ->7DOL map
pstack</syntaxhighlight>
{{out}}
<pre>
[["7108899", "B0YBKJ7", "4065663", "B0YBLH2", "2282765", "B0YBKL9", "5579107", "B0YBKR5", "5852842", "B0YBKT7", "B000300"]]
=== Press any key to exit ===</pre>
=={{header|PHP}}==
<
assert(stripos('AEIOU', $c) === FALSE);
return intval($c, 36);
Line 2,939 ⟶ 3,006:
'585284',
'B0YBKT') as $sedol)
echo $sedol, checksum($sedol), "\n";</
=={{header|PicoLisp}}==
<
(pack Str
(char
Line 2,962 ⟶ 3,029:
(for S '("710889" "B0YBKJ" "406566" "B0YBLH" "228276" "B0YBKL" "557910" "B0YBKR" "585284" "B0YBKT" "B00030")
(prinl (sedol S)) )</
=={{header|PL/I}}==
<
sedol: procedure options (main); /* 3 March 2012 */
declare alphabet character (36) static initial
Line 2,987 ⟶ 3,054:
put edit (s, v) (x(2), a, f(1)); put edit (' ') (a);
end;
end sedol;</
{{out}}
Line 3,006 ⟶ 3,073:
=={{header|Potion}}==
No extra credit.
<
if ("0" ord <= c ord and c ord <= "9" ord): c number integer.
else: 10 + c ord - "A" ord.
Line 3,018 ⟶ 3,085:
.
(str, (10 - (sum % 10)) % 10) join
.</
=={{header|PowerShell}}==
<
{
Param ( # Validate input as six-digit SEDOL number
Line 3,068 ⟶ 3,135:
{
Add-SEDOLCheckDigit -SixDigitSEDOL $PartialSEDOL
}</
{{out}}
<pre>7108899
Line 3,083 ⟶ 3,150:
=={{header|PureBasic}}==
<
Protected i, j, sum, c, m
For i=1 To Len(rawstring$)
Line 3,122 ⟶ 3,189:
Data.s "710889","B0YBKJ","406566","B0YBLH","228276"
Data.s "B0YBKL","557910","B0YBKR","585284","B0YBKT","B00030"
EndDataSection</
[[Image:PB SEDOL.png]]
=={{header|Python}}==
<
assert c not in 'AEIOU', "No vowels"
return int(c, 36)
Line 3,137 ⟶ 3,204:
sedol, sedolweight)
)
return str((
for sedol in '''
Line 3,151 ⟶ 3,218:
B0YBKT
'''.split():
print sedol + checksum(sedol)</
Line 3,157 ⟶ 3,224:
{{Works with|Python|3.7}}
<
from functools import reduce
Line 3,181 ⟶ 3,248:
Right(0)
)
)(lambda d: Right(str((
Line 3,281 ⟶ 3,348:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>SEDOL checksum digits:
Line 3,299 ⟶ 3,366:
=={{header|Q}}==
<
v:{("i"$x) - ?[("0"<=x) & x<="9"; "i"$"0"; -10+"i"$"A"]} each x; / Turn characters of SEDOL into their values
w:sum v*1 3 1 7 3 9; / Weighted sum of values
Line 3,306 ⟶ 3,373:
}
scd each ("710889";"B0YBKJ";"406566";"B0YBLH";"228276";"B0YBKL";"557910";"B0YBKR";"585284";"B0YBKT";"B00030")</
=={{header|Quackery}}==
<syntaxhighlight lang="Quackery"> [ 48 - dup 9 > if [ 7 - ] ] is chvalue ( c --> n )
[ 0 over witheach
[ chvalue
' [ 1 3 1 7 3 9 ]
i^ peek * + ]
10 mod 10 swap - 10 mod
48 + join ] is ->sedol ( $ --> $ )
$ "710889 B0YBKJ 406566 B0YBLH
228276 B0YBKL 557910 B0YBKR
585284 B0YBKT B00030" nest$
witheach [ ->sedol echo$ cr ]</syntaxhighlight>
{{out}}
<pre>7108899
B0YBKJ7
4065663
B0YBLH2
2282765
B0YBKL9
5579107
B0YBKR5
5852842
B0YBKT7
B000300</pre>
=={{header|R}}==
<
datalines <- readLines(tc <- textConnection("710889
B0YBKJ
Line 3,345 ⟶ 3,443:
#Print in format requested
writeLines(withchkdig)</
=={{header|Racket}}==
<
;;; Since the Task gives us unchecksummed and checksummed SEDOLs, and
;;; we'll just take a list of the output SEDOLs and remove their last
Line 3,410 ⟶ 3,508:
(check-false (invalid-SEDOL? S))
(check-equal? (SEDOL-append-checksum (substring S 0 6))
S (format "test SEDOL for ~a" S))))</
Output:
Line 3,433 ⟶ 3,531:
{{trans|Perl}}
{{Works with|rakudo|2015-12-17}}
<syntaxhighlight lang="raku"
die 'No vowels allowed' if $s ~~ /<[AEIOU]>/;
die 'Invalid format' if $s !~~ /^ <[0..9B..DF..HJ..NP..TV..Z]>**6 $ /;
Line 3,458 ⟶ 3,556:
B0YBKT
B00030
>;</
=={{header|REXX}}==
Line 3,469 ⟶ 3,567:
╚════════════════════════════════════════════════════════════════════╝
</pre>
<
@abcU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' /*the uppercase Latin alphabet. */
alphaDigs= '0123456789'@abcU /*legal characters, and then some. */
Line 3,510 ⟶ 3,608:
sed: say; say 'SEDOL:' sedol; say; return
ser: say; say '***error***' arg(1); call sed; exit 13
swa: say; say '***warning***' arg(1); say; return</
'''output''' when using the defaults:
<pre>
Line 3,527 ⟶ 3,625:
=={{header|Ring}}==
<
see sedol("710889") + nl
see sedol("B0YBKJ") + nl
Line 3,552 ⟶ 3,650:
next
return d + (10 - (s % 10)) % 10
</syntaxhighlight>
Output:
<pre>
Line 3,566 ⟶ 3,664:
B0YBKT7
B000300
</pre>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪
{ 1 3 1 7 3 9 } → weights
≪ 0 1 6 '''FOR''' j
OVER j DUP SUB NUM
'''IF''' DUP 65 ≥ '''THEN''' 7 - '''END''' 48 -
weights j GET * +
'''NEXT'''
10 SWAP OVER MOD - 10 MOD →STR +
≫ ≫ ''''→7DOL'''' STO
|
'''→7DOL''' ''( "SEDOL6" -- "SEDOL7" ) ''
store weights
sum=0 ; for j = 1 to 6
get jth character of SEDOL
convert it to its index
sum += index * weight
check = mod(10-mod(sum,10),10)
.
|}
{{in}}
<pre>
≪ { }
{ "710889" "B0YBKJ" "406566" "B0YBLH" "228276" "B0YBKL" "557910" "B0YBKR" "585284" "B0YBKT" "B00030" }
1 OVER SIZE FOR j DUP j GET →7DOL SWAP + SWAP NEXT DROP
≫ EVAL
</pre>
{{out}}
<pre>
1: { "7108899" "B0YBKJ7" "4065663" "B0YBLH2" "2282765" "B0YBKL9" "5579107" "B0YBKR5" "5852842" "B0YBKT7" "B000300" }
</pre>
=={{header|Ruby}}==
<
Sedolweight = [1,3,1,7,3,9]
Line 3,605 ⟶ 3,742:
p e
end
end</
{{out}}
<pre>
Line 3,625 ⟶ 3,762:
=={{header|Rust}}==
<
fn sedol(input: &str) -> Option<String> {
let weights = vec![1, 3, 1, 7, 3, 9, 1];
Line 3,672 ⟶ 3,809:
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,689 ⟶ 3,826:
=={{header|Scala}}==
<
require(s.size == 6 || s.size == 7, "SEDOL length must be 6 or 7 characters")
require(s.size == 6 || s(6).asDigit == chksum, "Incorrect SEDOL checksum")
Line 3,695 ⟶ 3,832:
def chksum = 10 - ((s zip List(1, 3, 1, 7, 3, 9) map { case (c, w) => c.asDigit * w } sum) % 10)
override def toString = s.take(6) + chksum
}</
Test cases:
Line 3,736 ⟶ 3,873:
=={{header|Seed7}}==
<
const func char: sedolCheckDigit (in string: sedol) is func
Line 3,770 ⟶ 3,907:
writeln(sedol <& sedolCheckDigit(sedol));
end for;
end func;</
{{out}}
Line 3,789 ⟶ 3,926:
=={{header|Sidef}}==
{{trans|Raku}}
<
die 'No vowels allowed' if (s ~~ /[AEIOU]/);
Line 3,817 ⟶ 3,954:
).each { |s|
say sedol(s);
}</
{{out}}
<pre>
Line 3,835 ⟶ 3,972:
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<
includesAnyOf: aSet [
aSet do: [ :e | (self includes: e) ifTrue: [ ^true ] ].
^false
]
].</
<
|weight charList|
Line 3,884 ⟶ 4,021:
]
]
].</
<
sedol := SEDOL new.
{ '710889'.
Line 3,897 ⟶ 4,034:
'B0YBKR'.
'585284'.
'B0YBKT' } do: [ :c | (sedol checked: c) displayNl ]</
{{works with|Smalltalk/X}}
<
'710889'
'B0YBKJ'
Line 3,921 ⟶ 4,058:
check := (10 - (sum%10)) % 10.
Transcript showCR: ( in,(Character digitValue:check))
].</
{{out}}
7108899
Line 3,938 ⟶ 4,075:
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<
--#SET TERMINATOR @
Line 4,003 ⟶ 4,140:
RETURN TEXT || CHECK;
END @
</syntaxhighlight>
Output:
<pre>
Line 4,051 ⟶ 4,188:
=={{header|Standard ML}}==
<
if List.exists (fn x => x = c) (explode "AEIOU") then raise Fail "no vowels"
else if Char.isDigit c then ord c - ord #"0"
Line 4,076 ⟶ 4,213:
"B0YBKR",
"585284",
"B0YBKT" ];</
=={{header|Tcl}}==
<
variable chars {0 1 2 3 4 5 6 7 8 9 "" B C D "" F G H "" J K L M N "" P Q R S T "" V W X Y Z}
variable weight {1 3 1 7 3 9 1}
Line 4,116 ⟶ 4,253:
assert {$sedol eq $answer} "assertion failed: $sedol ne $answer"
puts $sedol
}</
=={{header|Transact-SQL}}==
Line 4,122 ⟶ 4,259:
Returns empty string if invalid.
<
( @SEDOL varchar(50) )
RETURNS varchar(7)
Line 4,184 ⟶ 4,321:
-- Return the result of the function
RETURN @SEDOL
END</
Examples:
Line 4,212 ⟶ 4,349:
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
check="1'3'1'7'3'9"
Line 4,233 ⟶ 4,370:
PRINT input, " checkdigit: ", checksum
ENDLOOP
</syntaxhighlight>
Output:
<pre>
Line 4,257 ⟶ 4,394:
to the list of character values.
<
#import nat
Line 4,264 ⟶ 4,401:
charval = -:@rlXS num alphabet
iprod = sum:-0+ product*p/weights+ charval*
checksum = difference/10+ remainder\10+ iprod</
An optimization following the J solution avoids a run-time subtraction
by complementing the coefficients at compile time using these
definitions in place of those above.
<
checksum = remainder\10+ iprod</
A further performance improvement subsumes the character value lookup
Line 4,276 ⟶ 4,413:
the version shown below.
<
iprod = sum:-0+ lookup*p/weights</
To optimize further, we can build a separate smaller multiplication table for
Line 4,284 ⟶ 4,421:
index directly into the input list.
<
iprod = sum:-0+ gang +^|(~&,~)*lNrXXK9 ^(~&,&h!)* lookups</
Here is a test program.
<
examples = ^T(~&,~&h+ %nP+ checksum)*t
Line 4,301 ⟶ 4,438:
B0YBKR
585284
B0YBKT]-</
output:
<pre>7108899
Line 4,315 ⟶ 4,452:
=={{header|VBA}}==
<
Dim mult(6) As Integer
mult(1) = 1: mult(2) = 3: mult(3) = 1
Line 4,340 ⟶ 4,477:
getSedolCheckDigit = Input1 + CStr((10 - (Total Mod 10)) Mod 10)
End Function</
=={{header|VBScript}}==
Derived from the BASIC version.
<syntaxhighlight lang="vbscript">
arr = Array("710889",_
"B0YBKJ",_
Line 4,388 ⟶ 4,525:
Next
getSEDOLCheckDigit = (10 - total Mod 10) Mod 10
End Function</
{{out}}
Line 4,406 ⟶ 4,543:
=={{header|Visual FoxPro}}==
<
#DEFINE ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#DEFINE VOWELS "AEIOU"
Line 4,480 ⟶ 4,617:
RETURN EMPTY(tcMsg)
ENDFUNC
</syntaxhighlight>
{{out}}
<pre>
Line 4,490 ⟶ 4,627:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Conv
var sedol = Fn.new { |s|
Line 4,526 ⟶ 4,663:
var ans = (a = sedol.call(test)) ? a : "not valid"
System.print("%(test) -> %(ans)")
}</
{{out}}
Line 4,544 ⟶ 4,681:
=={{header|XPL0}}==
<
func CheckDigit(Str); \Return the check digit for a SEDOL
Line 4,583 ⟶ 4,720:
CrLf(0);
];
]</
{{out}}
Line 4,601 ⟶ 4,738:
=={{header|Yabasic}}==
<
do
Line 4,626 ⟶ 4,763:
NEXT
return d$ + CHR$(48 + mod(10 - mod(s, 10), 10))
end sub</
=={{header|zkl}}==
<
( text.len()!=6 or (text..matches("*[AEIOUaeioua-z]*")) ) and
throw(Exception.ValueError("Invalid SEDOL text: "+text));
Line 4,637 ⟶ 4,774:
else c.toAsc()-55;
}).zipWith('*,T(1,3,1,7,3,9)).sum() % 10) % 10;
}</
It sure does look like that trailing %10 is extraneous. It also seems like lower case is implicitly invalid.
<
"B0YBKL","557910","B0YBKR","585284","B0YBKT","B00030")
.apply(checksum).println();</
{{out}}
<pre>
|