June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,52 +1,43 @@
|
|||
#include <numeric>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int weights[] = {1,3,1,7,3,9};
|
||||
const string valid_chars = "BCDFGHJKLMNPQRSTVWXYZ0123456789";
|
||||
|
||||
int char_value(char c){ return isalpha(c) ? c -'A' + 10 : c - '0'; }
|
||||
|
||||
int sedol_checksum(string const& sedol)
|
||||
template<typename result_sink_t>
|
||||
auto sedol_checksum(std::string const& sedol, result_sink_t result_sink)
|
||||
{
|
||||
//Check contents
|
||||
if(sedol.size() != 6)
|
||||
throw length_error("length of sedol string != 6");
|
||||
if(sedol.find_first_not_of(valid_chars) != std::string::npos)
|
||||
throw invalid_argument("sedol string contains disallowed characters");
|
||||
return result_sink(0, "length of sedol string != 6");
|
||||
|
||||
vector<int> char_values;
|
||||
transform(sedol.begin(), sedol.end(), back_inserter(char_values), char_value);
|
||||
const int weighted_sum = inner_product(char_values.begin(), char_values.end(), weights, 0);
|
||||
return (10 - (weighted_sum % 10)) % 10;
|
||||
const char * valid_chars = "BCDFGHJKLMNPQRSTVWXYZ0123456789";
|
||||
if(sedol.find_first_not_of(valid_chars) != std::string::npos)
|
||||
return result_sink(0, "sedol string contains disallowed characters");
|
||||
|
||||
const int weights[] = {1,3,1,7,3,9};
|
||||
auto weighted_sum = std::inner_product(sedol.begin(), sedol.end(), weights, 0
|
||||
, [](int acc, int prod){ return acc + prod; }
|
||||
, [](char c, int weight){ return (std::isalpha(c) ? c -'A' + 10 : c - '0') * weight; }
|
||||
);
|
||||
return result_sink((10 - (weighted_sum % 10)) % 10, nullptr);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace std;
|
||||
string inputs[] = {
|
||||
"710889",
|
||||
"B0YBKJ",
|
||||
"406566",
|
||||
"B0YBLH",
|
||||
"228276",
|
||||
"B0YBKL",
|
||||
"557910",
|
||||
"B0YBKR",
|
||||
"585284",
|
||||
"B0YBKT",
|
||||
"B00030"
|
||||
"710889", "B0YBKJ", "406566", "B0YBLH", "228276", "B0YBKL",
|
||||
"557910", "B0YBKR", "585284", "B0YBKT", "B00030"
|
||||
};
|
||||
const size_t n_inputs = sizeof(inputs) / sizeof(*inputs);
|
||||
|
||||
for(size_t i = 0; i != n_inputs; ++i)
|
||||
for(auto const & sedol : inputs)
|
||||
{
|
||||
cout << inputs[i] << sedol_checksum(inputs[i]) << "\n";
|
||||
sedol_checksum(sedol, [&](auto sum, char const * errorMessage)
|
||||
{
|
||||
if(errorMessage)
|
||||
cout << "error for sedol: " << sedol << " message: " << errorMessage << "\n";
|
||||
else
|
||||
cout << sedol << sum << "\n";
|
||||
});
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
29
Task/SEDOLs/Gambas/sedols.gambas
Normal file
29
Task/SEDOLs/Gambas/sedols.gambas
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
Public Sub Main()
|
||||
Dim byWeight As Byte[] = [1, 3, 1, 7, 3, 9, 1]
|
||||
Dim byCount, byCompute As Byte
|
||||
Dim siTotal As Short
|
||||
Dim sWork As New String[]
|
||||
Dim sToProcess As String[] = ["710889", "B0YBKJ", "406566", "B0YBLH", "228276", "B0YBKL",
|
||||
"557910", "B0YBKR", "585284", "B0YBKT", "B00030"]
|
||||
|
||||
For byCompute = 0 To sToProcess.Max
|
||||
For byCount = 1 To 6
|
||||
If IsLetter(Mid(sToProcess[byCompute], byCount, 1)) Then
|
||||
sWork.Add(Str(Asc(Mid(sToProcess[byCompute], byCount, 1)) - 55) * byWeight[byCount - 1])
|
||||
Else
|
||||
sWork.Add(Val(Mid(sToProcess[byCompute], byCount, 1)) * byWeight[byCount - 1])
|
||||
End If
|
||||
Next
|
||||
|
||||
For byCount = 0 To 5
|
||||
siTotal += Val(sWork[byCount])
|
||||
Next
|
||||
|
||||
siTotal = (10 - (siTotal Mod 10)) Mod 10
|
||||
|
||||
Print sToProcess[byCompute] & " = " & sToProcess[byCompute] & siTotal
|
||||
sWork.Clear()
|
||||
siTotal = 0
|
||||
Next
|
||||
|
||||
End
|
||||
21
Task/SEDOLs/Julia/sedols.julia
Normal file
21
Task/SEDOLs/Julia/sedols.julia
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using Base.Test
|
||||
|
||||
function appendchecksum(chars::AbstractString)
|
||||
if !all(isalnum, chars) throw(ArgumentError("invalid SEDOL number '$chars'")) end
|
||||
weights = [1, 3, 1, 7, 3, 9, 1]
|
||||
|
||||
s = 0
|
||||
for (w, c) in zip(weights, chars)
|
||||
s += w * parse(Int, c, 36)
|
||||
end
|
||||
return string(chars, (10 - s % 10) % 10)
|
||||
end
|
||||
|
||||
tests = ["710889", "B0YBKJ", "406566", "B0YBLH", "228276", "B0YBKL", "557910", "B0YBKR", "585284", "B0YBKT", "B00030"]
|
||||
csums = ["7108899", "B0YBKJ7", "4065663", "B0YBLH2", "2282765", "B0YBKL9", "5579107", "B0YBKR5", "5852842", "B0YBKT7", "B000300"]
|
||||
|
||||
@testset "Checksums" begin
|
||||
for (t, c) in zip(tests, csums)
|
||||
@test appendchecksum(t) == c
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue