荷兰Burgerservicenummer(BSN)十一测试


29

介绍:

符合以下规则的荷兰BSN(BurgerServiceNummer)有效:

  • 它仅包含数字。
  • 长度应为8或9。
  • A通过索引数字时I,以下总和的结果:(请9xA + 8xB + 7xC + 6xD + 5xE + 4xF + 3xG + 2xH + -1xI注意-1而不是1!)应该被11整除,并且不应该为0。

挑战:

输入:代表BSN的字符串或字符数组。

输出:truthy或falsey结果输入是否是有效的BSN。

挑战规则:

  • 输入格式应为字符串或字符数组。不允许使用数字的整数数组或(可能是八进制)数字。(不过,您可以自己将其转换为数字的整数数组,但不能直接将其作为参数。)
  • 尽管上面的输入有限制,但您可以假定所有测试用例都将包含一个或多个数字([0-9]+
  • 关于长度为8而不是9的BSN,荷兰维基百科指出:“ 对于11次测试和其他实际用途,添加前导零以使长度为9。 ”(来源

一般规则:

  • 这是,因此最短答案以字节为单位。
    不要让代码高尔夫球语言阻止您发布使用非代码高尔夫球语言的答案。尝试针对“任何”编程语言提出尽可能短的答案。
  • 标准规则适用于您的答案,因此允许您使用STDIN / STDOUT,具有正确参数的函数/方法,完整程序。你的来电。
  • 默认漏洞是禁止的。
  • 如果可能,请为您的代码添加一个带有测试的链接。
  • 另外,如有必要,请添加说明。

测试用例:

// Truthy test cases:
111222333
123456782
232262536
010464554
10464554
44016773

// Falsey test cases:
000000000
192837465
247594057
88888888
73
3112223342
000000012

4
的确,如果有8位数字,那么A从给定的公式中忽略了一位吗?
isaacg

@isaacg我已经通过(荷兰语)维基百科页面的链接添加了与此相关的规则。您确实是对的,它A从公式中省略了(或基本上添加了导致0其长度为9 的前导,导致与省略相同的结果A)。
凯文·克鲁伊森

测试用例“ sum [...]不应为0。”:000000012
betseg

@betseg我已经将它添加到列表中
Kevin Cruijssen

Answers:


8

05AB1E23 21字节

`()DgLR*OD11Ö89¹gåP0Ê

在线尝试!或作为测试套件

说明

`                        # push input as individual chars onto stack
 (                       # negate top value
  )                      # wrap in list
   DgLR                  # range [len(input) ... 1]
       *O                # multiply with list of digits and sum
         D11Ö            # is evenly divisible by 11
             89¹gå       # len(input) is 8 or 9
                  P      # product of sum/divisible by 11/len in (8,9)
                   0Ê    # not equal to 0

可能是由于旧版本05AB1E的,但你可以改变现在保存3个字节DgLāĀ在线尝试。
凯文·克鲁伊森

12

JavaScript(ES6)57

输入为字符数组。reduceRight节省了一天!

s=>!(i=1,t=s.reduceRight((t,v)=>t-v*++i),!t|t%11|(i|1)-9)

测试

F=
s=>!(i=1,t=s.reduceRight((t,v)=>t-v*++i),!t|t%11|(i|1)-9)


;['111222333','123456782','232262536','010464554','10464554','44016773']
.forEach(t=>{
  var r=F([...t]);console.log(t,r)
})

;['000000000','192837465','247594057','88888888','73','3112223342','3112223342']
.forEach(t=>{
  var r=F([...t]);console.log(t,r)
})


1
总是很高兴看到reduceRight答案!
尼尔

最终找到了达到58的方法map(),只是意识到您的答案实际上是57字节长:-)
Arnauld

@Arnauld是的,我不敢相信我记错,谢谢
edc65

8

R,86 67字节

编辑:感谢Jarko Dubbeldam提出的点积产品!

l=length(x<-scan(,""));s=as.double(x)%*%c(l:2,-1);!s%%11&s&l>7&l<10

从stdin读取输入并将其存储为字符数组/向量。随后转换为数值,乘以向量9...2,-1并检查所有条件。


对我不起作用。您应该拆分x为矢量。
djhurio

@djhurio输入以空格分隔的值,它们隐式存储在字符向量中。或者,通过在两者之间打回车来一一输入。
Billywob '16

1
if(l<9)x=c(0,x);s=sum(as.double(x)*c(9:2,-1))可以变成s=sum(as.double(x)*c(l:2,-1))。同样,两个向量的成对乘积之和与它们的点乘相同%*%
2016年

@JarkoDubbeldam不错!点产品真的很聪明。
Billywob '16

7

JavaScript(ES6),61 60 59 58字节

将一个字符数组作为输入。返回false/ true

a=>!(a.map(c=>s-=--k?-c*k-c:c,k=a.length&9,s=0)|!s|k|s%11)

测试用例


6

电话:112 101 96 98 104字节

感谢@MartinEnder 在修复我的代码时节省了5 3个字节!

j,i,k,l;f(char*s){l=strlen(s);for(i=l,j=k=0;j<l;)k+=(s[j++]-48)*(i>1?i--:-1);return!(k%11)&&k&&(l^8)<2;}

如果无效,则返回0,如果有效,则返回1。在线尝试!


61即使长度不正确,也可以接受。
Christian Sievers,2016年

1
这不适用于我的个人BSN。
roberrrt-s

希望修复。
betseg

不固定。也不适用于我的。
DavidPostill

1
@Roberrrt,@DavidPostill; 现在可以吗,还是我应该放弃?=(
betseg 2013年

5

R,95 79 93字节

function(x){y=as.double(el(strsplit(x,"")));z=y%*%c((q<-length(y)):2,-1);(z&!z%%11&q>7&q<10)}

以字符串为参数的未命名函数。最初,我过度理解了使用字符串作为输入而不是数字的要求,但这很好,因为它在转换时节省了一些字节。

我不确定如何解释字符数组,但是如果这意味着您可以使用字符串数字矢量"1" "2" "3" "4" etc作为输入,那么它甚至会变得更短:

function(x){y=as.double(x);z=y%*%c((q<-length(y)):2,-1);(z&!z%%11&q>7&q<10)}

将x拆分为数值向量,如果长度为8,则附加0,然后计算向量y的点积 c(9,8,7,6,5,4,3,2,-1)。测试结果是否既非零又可被11整除。

@Enigma的逻辑节省了16个字节,在创建向量时隐式地将0附加 c(length(x):2,-1)

忘记添加长度为8/9的检查,所以+14个字节:(


4

Perl,58个字节(52 + 6)

@N=(-1,2..9);$r+=$_*shift@N for reverse@F;$_=$r&&/^\d{8,9}$/&&!($r%11)

与运行

perl -F// -lapE

输入通过STDIN

用法

echo 232262536 | perl -F// -lapE '@N=(-1,2..9);$r+=$_*shift@N for reverse@F;$_=$r&&/^\d{8,9}$/&&!($r%11)'

输出1为真值,0否则为假值。


您可以在开头保存一些字节:$r+=$_*(-1,2..9)[$i++]for reverse@F。另外,-F -pe(并且提供的输入没有最终的换行符,echo -n例如)就足够了(除非您的Perl太旧了,在这种情况下您将需要使用-a(但是在最近的Perls上,它由暗示-F)。最后,您的代码长70个字节,而不是52;)
Dada

3

C ++ 14、107 106字节

-1个字节int代替auto in for循环。

通过引用参数返回的未命名lambda。要求输入为std::string或的char容器,例如vector<char>

[](auto c,int&r){int i=c.size();r=7<i&&i<10?-2*c.back()+96:~1<<9;for(int x:c)r+=(x-48)*i--;r=r%11<1&&r>0;}

脱胶和用法:

#include<iostream>
#include<string>

auto f=
[](auto c, int& r){
 int i = c.size();
 //if the size is correct, init r to -2*I so we can add I safely later
 //otherwise such a big negative number, that the final test fails
 r = 7<i && i<10 ? -2*c.back()+96 : ~1<<9;
 for (auto x:c)
  r += (x-48)*i--;
 r = r%11<1 && r>0;
}
;

using namespace std;
using namespace std::literals;

int main(){
 int r;
 f("111222333"s,r); std::cout << r << std::endl;
 f("123456782"s,r); std::cout << r << std::endl;
 f("010464554"s,r); std::cout << r << std::endl;
 f("10464554"s,r); std::cout << r << std::endl;
 f("44016773"s,r); std::cout << r << std::endl;
 std::cout << std::endl;
 f("000000000"s,r); std::cout << r << std::endl;
 f("192837465"s,r); std::cout << r << std::endl;
 f("73"s,r); std::cout << r << std::endl;
 f("88888888"s,r); std::cout << r << std::endl;
 f("3112222342"s,r); std::cout << r << std::endl;
 std::cout << std::endl;
 f("99999999"s,r); std::cout << r << std::endl;
 f("999999999"s,r); std::cout << r << std::endl;
}

3

Befunge,72个字节

>+~>:0`v
^1\-*68_\2/4-!00p*8>1-10p\910gv
@.!+!\%+56:*g00$  _^#!:g01+*-<<

在线尝试!

说明

>+~>:0`v            Read characters from stdin until EOF, converting each digit into
^1\-*68_              a number on the stack, and keeping a count of the characters read.

      \2/4-!00p     Save !(count/2-4), which is only true for valid lengths (8 and 9).
               *    Multiply the EOF (-1) with the final digit; this is the initial total.

8>1-10p\910gv       Loop over the remaining 8 digits, multiplying each of them by 9-i and
 ^#!:g01+*-<<         add to the total; i goes from 7 down to 0, so 9-i goes from 2 to 9.

               $    Drop the loop counter.
           *g00     Multiply total by the length calculation (invalid lengths become 0).
      %+65:         Make a copy of the total, and calculate modulo 11.
    !\              Boolean not the other copy to check for zero. 
  !+                !(total%11 + !(total)) is only true for non-zero multiples of 11.
@.                  Output the result and exit.

3

MATL,36个字节

这不是我写过最长的MATL程序,但是我喜欢高尔夫语言中的if/ else语句变得非常冗长。我觉得此解决方案在MATL中可能不是最佳解决方案,但到目前为止,我无法对其进行进一步优化。我正在考虑在某个地方使用双精度0,也许在t所有地方都减少。

48-tn8=?0wh]tn9=?P[a2:9]*st11\~Y&}x0

在线尝试!说明:

48-                                  % Subtract 48 (ASCII '0')
   tn                                % Duplicate. Get length.
     8=?                             % If length equals 8
        0wh                          %     Prepend 0 to the duplicate
           ]                         % End if.
            t                        % Duplicate again.
             n9=?                    % If length equals 9.
                 P                   %     Reverse the duplicate
                  [a2:9]*            %     Element-wise product with [-1 2 ... 9]
                         s           %     Sum
                          t11\       %     Duplicate sum, modulus 11
                              ~Y&    %     Result on stack: modulus==0 AND sum!=0
                                 }   % Else
                                  x0 %     Remove the duplicate. Put 0 on stack.
                                     % Display implicitly.

如果可以使用列向量:!U而不是48-
Luis Mendo


@LuisMendo太糟糕了。[a2:9]*会导致非元素乘积,因此!需要另一个会抵消初始增益的乘积。
桑契斯

3

MATL,26字节

!UGg*R!s0&)s-t11\~Gn8-tg=v

结果是一个非空的列向量,如果所有条目都不为零,则为

在线尝试!

要么 验证所有测试用例并将每个结果放在不同的行中。

说明

这将按以下顺序测试三个条件:

  1. 加权和不为零;
  2. 加权和可以除以11;
  3. 长度为8或9。

考虑输入'8925'以进行说明。;是矩阵的行分隔符。

!     % Implicit input. Transpose into a column vecvtor
      % STACK: ['8'; '9'; '2'; '5']
U     % Convert each digit to number
      % STACK: [8; 9; 2; 5]
Gg    % Push a row array of ones as long as the input
      % STACK: [8; 9; 2; 5], [1 1 1 1]
*     % Multiply, element-wise with broadcast
      % STACK: [8 8 8 8; 9 9 9 9; 2 2 2 2; 5 5 5 5]
R     % Upper triangular part
      % STACK: [8 8 8 8; 0 9 9 9; 0 0 2 2; 0 0 0 5]
!     % Transpose
      % STACK: [8 0 0 0;8 9 0 0;8 9 2 0;8 9 2 5]
s     % Sum of each column. This multiplies last element by 1, second-last by 2 etc
      % STACK: [32 27 4 5]
0&)   % Split into last element and remaining elements
      % STACK: 5, [32 27 4]
s     % Sum of array
      % STACK: 5, 63
-     % Subtract
      % STACK: -58. This is the result of condition 1
t11\  % Duplicate. Modulo 11
      % STACK: -58, 8
~     % Logical negation
      % STACK: -58, 0. This gives condition 2
Gn    % Push numnber of entries in the input
      % STACK: -58, 0, 4
8-    % Subtract 8. For valid lengths (8 or 9) this gives 0 or 1
      % STACK: -58, 0, -4
tg    % Duplicate. Convert to logical: set nonzero values to 1
      % STACK: -58, 0, -4, 1
=     % 1 if equal, 0 otherwise. Lenghts 8 or 9 will give 1. This is condition 3
      % STACK: -58, 0, 0
v     % Vertically concatenate the entire stack. This is truthy iff all values 
      % are non-zero. Implicitly display
      % STACK: [-58; 0; 0]

做得好。我认为不使用这种方法?可能会更有效,但是我无法弄清楚如何将长度缩短8或9。您Gn8-tg=非常聪明。
桑契斯,2013年

1
顺便说一句,列向量输入是否不可以作为表示BSN字符数组,从而为您节省了第一个!
桑契斯,2013年

@Sanchises问题是然后G推入列向量,我需要转置它以与g*
Luis Mendo

哦,对了。没关系!
桑契斯,2013年

3

Haskell中,116个 112 102字节

f x=div(length x)2==4&&g x>0&&h x
h=((==0).(`mod`11)).g
g=sum.zipWith(*)(-1:[2..]).map(read.(:[])).reverse

g计算在的11个有效位中使用的总和h,同时f还要检查正确的长度和11个有效位不为0。尤其是对f占用很多字节。

编辑:由于Lynn和div四舍五入而节省了10个字节。


1
怎么f x=div(length x)2==4&&g x>0&&h x
林恩

@Lynn:很好,谢谢。
Renzeee '16

2

果冻,21 字节

V€U×JN1¦µL:2=4×Sµ11ḍa

TryItOnline!运行所有测试用例

真实的返回值不为零(并且实际上是11 sum的倍数)。

怎么样?

V€U×JN1¦µL:2=4×Sµ11ḍa - Main link: string of digits  e.g. "111222333"
V€                    - eval each - effectively cast each to an integer (keeps leading zeros)
  U                   - upend                        e.g. [ 3, 3, 3, 2, 2, 2, 1, 1, 1]
    J                 - range(length)                e.g. [ 1, 2, 3, 4, 5, 6, 7, 8, 9]
   ×                  - multiply                     e.g. [ 3, 6, 9, 8,10,12, 7, 8, 9]
      1¦              - apply to index 1 (first element)
     N                -     negate                   e.g. [-3, 6, 9, 8,10,12, 7, 8, 9]
        µ             - monadic chain separation   e.g. z=[-3, 6, 9, 8,10,12, 7, 8, 9]
         L            - length(z)                    e.g. 9
          :2          - integer divide by 2          e.g. 4
            =4        - equals 4?                    e.g. 1
               S      - sum(z)                       e.g. 66
              ×       - multiply                     e.g. 66
                µ     - monadic chain separation   e.g. z=66
                 11ḍ  - divides(11, z)               e.g. 1
                    a - and z (for z=0 case)         e.g. 66 (truthy)

不幸的是,我只能接受一个答案,而不是两个,因为您的答案@Emigna的05AB1E答案具有相同的21个字节数。但是自从Enigma回答得更快(他对21个字节的编辑也很快)之后,我接受了他。
凯文·克鲁伊森

这对我来说听起来很公平!
乔纳森·艾伦,

2

Python 2,102字节

def f(i):S=sum(a*b for a,b in zip([-1]+range(2,10),map(int,i)[::-1]));return(7<len(i)<10)*(S%11<1)*S>0

2

Python 2,96字节

def g(s):u=7<len(s)<10and sum(x*int(('0'+s)[-x])for x in range(2,10))-int(s[-1]);print(u%11<1)*u

将字符串作为输入。'0'无论是否需要,该函数都会在字符串的开头添加a,并使用Python的负索引从字符串的末尾开始并从头到尾添加元素。

-1xI是单独处理的,使用到第二呼叫int()。我不知道如何避免这种情况而不花费比我节省的更多的字节数。

def g(s):u=7<len(s)<10and sum(x*int(('0'+s)[-x])for x in range(10))-2*int(s[-1]);print(u%11<1)*u它将同样有效,因为它将增加1时间s[-1],然后将其相减两次,并且还将增加0时间(某物),这当然不会影响总和。


2

脑高射炮,345个字节

包括+3 -a

([]){{}({}[((((()()()){}){}){}){}]<>)<>([])}{}<>([][(()()()()){}]){({}[()]){([]){{}{}([])}}}{}([{}])({}({}){})({}({})({}){})({}(({}){}){})({}(({})({})){}{})({}(({})({}){}){})({}((({}))({}){}){}{})({}((({}){}){}){})(({}(((({})){}){}){}{}{}<(((()()())()){}{})>)){{}({}(<>))<>{(({})){({}[()])<>}{}}{}<>([{}()]{}[{}]<(())>){((<{}{}>))}{}(<()>)}{}

Truthy为1,Falsy在堆栈顶部具有0。

在线尝试!

我敢肯定,有一个较短的方法可以在循环中进行乘法运算,但是我还没有找到它。

#reverse and subtract 48 from all numbers (ASCII -> decimal)
([]){{}({}[((((()()()){}){}){}){}]<>)<>([])}{}<> 

([][(()()()()){}])       #height - 8
{({}[()]){               #if not 0 subtract 1
   ([]){{}{}([])}        #if still not 0 pop everything
}}{}                     #this loop pops everything unless there are 8 or 9 digits

([{}])                   # -I
({}({}){})               # H*2
({}({})({}){})           # G*3
({}(({}){}){})           # F*4
({}(({})({})){}{})       # E*5
({}(({})({}){}){})       # D*6
({}((({}))({}){}){}{})   # C*7
({}((({}){}){}){})       # B*8
(({}(((({})){}){}){}{}{} # A*9 pushed twice with:
<(((()()())()){}{})>))   # 11 under it


{{} #if not 0
({}(<>))<>{(({})){({}[()])<>}{}}{}<>([{}()]{}   # mod 11
[{}]<(())>){((<{}{}>))}{}                       # logical not
(<()>)                                          # push 0 to exit loop
}{}
                                                # implicit print

2

PowerShell v2 +,96字节

param($n)$i=8-($n.count-eq8);!(($b=($n|%{(-"$_",(($i+1)*+"$_"))[!!$i--]})-join'+'|iex)%11)-and$b

好吧,我承认,这看起来像是一团糟。而且有点。但是,请忍受我,我们将克服它。

我们将输入$n(作为a char数组)并设置为$i等于8减去一个Boolean值,以了解中是否有8个项目$n。意思是,如果有8个项目,$i则将是7

下一节将计算与我们的输出结合起来。从里面工作,我们循环$n使用$n|%{...}。每次迭代,我们使用伪三元数得出两个结果之一- -"$_"(($i+1)*+"$_")。该指数是根据是否$i0或不是(即,我们已经打-1xI从挑战方程式的情况下),用于获取后减为下围棋轮。那些都被收集在括号里并和-join一起食用+。例如,到111222333了这一点,我们将有9+8+7+12+10+8+9+6+-3。在存储到中之前,先将其通过管道传递到iex(缩写Invoke-Expression并类似于eval$b。然后,我们采取%11并执行布尔非!(...)将该,(也就是说,如果被11整除,则此部分为$true)。加上-and$b 为了保证 $b不为零。该布尔结果留在管道上,并且输出是隐式的。

例子

PS C:\Tools\Scripts\golfing> 111222333,123456782,232262536,010464554,10464554,44016773|%{"$_ -> "+(.\dutch-burgerservicenummer.ps1 ([char[]]"$_"))}
111222333 -> True
123456782 -> True
232262536 -> True
10464554 -> True
10464554 -> True
44016773 -> True

PS C:\Tools\Scripts\golfing> 000000000,192837465,247594057,88888888,73,3112223342,000000012|%{"$_ -> "+(.\dutch-burgerservicenummer.ps1 ([char[]]"$_"))}
0 -> False
192837465 -> False
247594057 -> False
88888888 -> False
73 -> False
3112223342 -> False
12 -> False

2

PHP 139128字节

 $u=-1;$i=$argv[1];while($u<=strlen($i)){$c+=($u*(substr($i,-(abs($u)),1)));$u +=$u<0?3:1;}echo($c>0&&!($c%11)&&$u>8&&$u<11?1:0);

无法使CLI仅仅回显false的true。必须这样做。有任何想法吗?

128个字节:将“ true”和“ false”分别设为1和0。


2

C#,120 115 bytes

这将循环遍历char[]它接收的输入,并返回true或false:

bool b(char[]n){int r=0,k,i=0,l=n.Length;for(;i<l;i++){k=i==l-1?-1:l-i;r+=k*(n[i]-48);}return r>0&r%11<1&l<10&l>7;}

小提琴:https : //dotnetfiddle.net/3Kaxrt

我确定我可以抓取一些字节,尤其是在混乱的地方return。任何想法欢迎!

编辑:感谢凯文,节省了5个字节。我不知道我可以&代替&&


1
+1!r>0&&r%11==0&&l<10&&l>7可以golfed到r>0&r%11<1&l<10&l>7&&&r%11==0r%11<1)。并且-'0'可以打高尔夫球-48
凯文·克鲁伊森

2

PHP,86 85 84 83 82 79字节

注意:将PHP 7.1用于否定字符串索引。

for($l=log10($a=$argn);~$c=$a[-++$x];)$s+=$x>1?$x*$c:-$c;echo$s%11<1&$l>7&$l<9;

像这样运行:

echo 010464554 | php -nR 'for($l=log10($a=$argn);~$c=$a[-++$x];)$s+=$x>1?$x*$c:-$c;echo$s%11<1&$l>7&$l<9;';echo
> 1

PHP <7.1(+10字节)的版本

echo 010464554 | php -nR 'for($l=log10($a=$argn);~$c=$a[strlen($a)-++$x];)$s+=$x>1?$x*$c:-$c;echo$s%11<1&$l>7&$l<9;';echo

说明

for(
  $l=log10(         # Take the log of the input number.
    $a=$argn        # Set input to $a
  );
  ~$c=$a[-++$x];    # Iterate over digits of input (reverse). Negate to
                    # change every char to extended ASCII (all truthy),
                    # without changing empty sting (still falsy, ending
                    # the loop).
)
  $s+=$x>1?         # Add current char to the sum...
     ?$x*$c:-$c;    # multiplied by $x, unless $x is 1; subtract it.
echo
  $s%11<1 &         # Check if sum is divisible by 11, and
  $l>7   &          # log of the input is greater than 7, and
  $l<9;             # log of the input is less than 9. Outputs 0 or 1.

调整

  • 区分空字符串和的更短方法"0",保存了一个字节
  • 由于10000000无效,因此无需与进行比较greater than or equalsgreater than就可以节省一个字节
  • 减去最低有效数字的更短方法
  • 取反char而不是XOR,保存一个字节
  • 通过使用节省3个字节-R,使$argn可用

2

Java 8,115 98字节

b->{int l=b.length,i=0,r=0,x;for(;l>7&l<10&i<l;r+=(b[i++]-48)*(x<2?-1:x))x=l-i;return r>0&r%11<1;}

我很惊讶还没有人发布Java答案,所以这里是一个。

说明:

在这里尝试。

b->{                  // Method with character-array as parameter and boolean return-type
  int l=b.length,     //  Length of the array
      i=0,            //  Index-integer, starting at 0
      r=0,            //  The result-sum, starting at 0
      x;              //  Temp integer `x`
  for(;l>7&l<10       //  Start looping if the length is either 8 or 9
       &i<l;          //  And continue looping while the index is smaller than the length
      r+=             //    After every iteration, increase the result-sum by:
         (b[i++]-48)  //     The current digit
         *(           //     Multiplied by:
           x<2?       //      If `x` is 1:
            -1        //       Multiply by -1
           :          //      Else:
            x))       //       Simply multiply by `x` 
    x=l-i;            //   Set `x` to the length minus the current index
                      //  End of loop (implicit / single-line body)
  return r>0          //  Return if the result-sum is larger than 0,
    &r%11<1;          //   and if the result-sum is divisible by 11
}                     // End of method

1

Clojure,114个字节

好吧,这是-从第一个参数中减去其余参数,以便处理weight的特殊情况-1nil对于无效长度的输入,此函数返回,但在if子句上,它们的作用与相同false。如果长度不是8或9 ,则(#{8 9}(count v))返回。nilv

(fn[v](if(#{8 9}(count v))(#(and(< % 0)(=(mod % 11)0))(apply -(map *(range 1 10)(reverse(map #(-(int %)48)v)))))))

测试用例:

(pprint (group-by f (map str [123456782 232262536 "010464554" 10464554 44016773 "000000000" 192837465 247594057 88888888 73 3112223342 "000000012"])))
{true  ["123456782" "232262536" "010464554" "10464554" "44016773"],
 false ["000000000" "192837465" "247594057" "88888888" "000000012"],
 nil   ["73" "3112223342"]}


1

Stax,23 个字节

╪╦µΘç}<╔▼◘╞i∟~¿≥←║▐√ 4u

在线运行和调试!

说明

使用解压后的版本进行解释。

i%8A:byr{]ei^*mBN+|+c11%!L|A
i                               Suppress implicit eval
 %8A:b                          Length is 8 or 9 (Element #1 on the final stack)
      yr                        Reverse input
        {     m                 Map each element with
         ]e                         Its numerical value
           i^*                      Multiplied current 1-based loop index
               BN+              Negate the first element
                  |+            Sum (Element #2 on the final stack)
                    c11%!       Sum is multiple of 11 (Element #3 on the final stack)
                         L|A    Collect all the three elements and `and` them.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.