2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 59d9c6bc214c613ba73ff012
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: SEDOLs
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 5
|
|
|
|
|
videoUrl: ''
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: sedols
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
|
|
任务:
|
|
|
|
|
|
|
|
|
|
对于6位[SEDOL](https://en.wikipedia.org/wiki/SEDOL "wp:SEDOL")的每个数字列表,计算并附加校验和数字。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
也就是说,给定左侧的输入字符串,您的函数应返回右侧的相应字符串:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
<pre> 710889 => 7108899 B0YBKJ => B0YBKJ7 406566 => 4065663 B0YBLH => B0YBLH2 228276 => 2282765 B0YBKL => B0YBKL9 557910 => 5579107 B0YBKR => B0YBKR5 585284 => 5852842 B0YBKT => B0YBKT7 B00030 => B000300 </pre>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
还要检查每个输入是否正确形成,尤其是对于SEDOL字符串中允许的有效字符。您的函数应在无效输入时返回`null` 。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`sedol`是一个功能。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(typeof sedol === 'function');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`sedol('a')`应该返回null。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(sedol('a') === null);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`sedol('710889')`应返回'7108899'。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(sedol('710889') === '7108899');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`sedol('BOATER')`应该返回null。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(sedol('BOATER') === null);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`sedol('228276')`应该返回'228276'。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(sedol('228276') === '2282765');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function sedol(input) {
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
function sedol(input) {
|
|
|
|
|
const checkDigit = sedolCheckDigit(input);
|
|
|
|
|
if (checkDigit !== null) {
|
|
|
|
|
return input + checkDigit;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const weight = [1, 3, 1, 7, 3, 9, 1];
|
|
|
|
|
function sedolCheckDigit(char6) {
|
|
|
|
|
if (char6.search(/^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}$/) === -1) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let sum = 0;
|
|
|
|
|
for (let i = 0; i < char6.length; i++) {
|
|
|
|
|
sum += weight[i] * parseInt(char6.charAt(i), 36);
|
|
|
|
|
}
|
|
|
|
|
const check = (10 - (sum % 10)) % 10;
|
|
|
|
|
return check.toString();
|
|
|
|
|
}
|
|
|
|
|
```
|