编写一个使用正整数的程序或函数。您可以假定输入有效,并且可以将其作为字符串。如果数字是以下任何一个
123
234
345
456
567
678
789
1
2
3
12
122
124
132
321
457
777
890
900
1011
1230
1234
必须全部导致虚假输出。(输入不会有前导零,因此您不必担心诸如此类的东西012。)
以字节为单位的最短代码获胜。
012吗?
012是虚假的,但是您可以假定它不是输入。
编写一个使用正整数的程序或函数。您可以假定输入有效,并且可以将其作为字符串。如果数字是以下任何一个
123
234
345
456
567
678
789
1
2
3
12
122
124
132
321
457
777
890
900
1011
1230
1234
必须全部导致虚假输出。(输入不会有前导零,因此您不必担心诸如此类的东西012。)
以字节为单位的最短代码获胜。
012吗?
012是虚假的,但是您可以假定它不是输入。
Answers:
range(123,790,111).count
一个匿名函数,输出0或1。它将创建列表[123, 234, 345, 456, 567, 678, 789]并计算输入出现的次数。
f=range(123,790,111).count
f(123)
=> 1
f(258)
=> 0
lambda n:n%111==12<n<900
只是很多条件链。
lambda我什至都不会猜到那是Python。太恐怖了
这个答案就是这个答案。我实际上并不十分了解我的答案如何工作,但DJMcMayhem在他的原始答案中给出了很好的解释,而我的答案是对他的修改。
([]<>)<>({}[({})]<>)<>(({}[({})()()()()()]<>{}{}<(())>)){{}{}(((<{}>)))}{}{}
它与-a ascii标志添加3个字节一起运行。
从原始的工作解决方案开始:
([]<>)<>({}[({})]<>)<>({}[({})]<>)({}{}[()()])({}<({}[()()()])>)(({}{}<(())>)){{}{}(((<{}>)))}{}{}
我通过编写的一个简单的高尔夫算法来运行此程序并得到:
([]<>)<>({}[({})]<>)<>(({}[({})]<>{}[()()]<({}[()()()])>{}<(())>)){{}{}(((<{}>)))}{}{}
从这里我看到该部分<({}[()()()])>{}实际上乘以一,这等于{}[()()()]将整个代码减少为:
([]<>)<>({}[({})]<>)<>(({}[({})]<>{}[()()]{}[()()()]<(())>)){{}{}(((<{}>)))}{}{}
最后,底片可以合并:
([]<>)<>({}[({})]<>)<>(({}[({})()()()()()]<>{}{}<(())>)){{}{}(((<{}>)))}{}{}
DI⁼1,1
DI⁼1,1 Main link. Argument: n (integer)
D Decimal; convert n to base 10.
I Increments; compute the differences of all pairs of adjacent digits.
1,1 Yield [1, 1].
⁼ Test the results to both sides for equality.
2Å1代替XX,,只是为了减少命令的数量(用4代替5)。
Å可以在我的键盘上写(而不是‚),这是有好处的:)
‚(不是,我用的)不具有书写键序列也同时Å是oA一间美式键盘上。
d1=tn2=*
这将打印1 1出真实的输入,并0在其中打印一个数组以显示伪造的值,因为这在MATL中是伪造的。
说明:
d % Calculate the difference between consecutive digits
1= % Push an array of which elements equal one
t % Duplicate this array
n % Push the length of this array
2= % Push a one if the length is 2, and a zero otherwise
% Now, if we have a truthy input, the stack looks like:
% [1 1]
% 1
% And if we have a falsy input, the stack looks something like this:
% [1 0]
% 1
% Or this:
% [1 1]
% 0
* % Multiply the top two elements
d1=Ep4=(我尚未进行彻底测试)
dTTX=5字节
T
{.chars==3&&'0123456789'.index: $_}
{$_ (elem) (123,*+111...789)}
{$_∈(123,*+111...789)}
*∈(123,*+111...789)
*∈(123,234...789)
# Whatever lambda ( the parameter is 「*」 )
*
∈ # is it an element of:
# this sequence
(
123,
234,
# deduce rest of sequence
...
# stop when you generate this value
789
)
my &code = *∈(123,234...789);
say code 123; # True
say code 472; # False
say ( *∈(123,234...789) )( 789 ); # True
+2个字节用于-nl标志。
在STDIN上输入并打印true或false。
p"123456789"[$_]&.size==3
在repl.it上查看它:https ://repl.it/DBn2/2 (单击▶️,然后在下面的控制台中输入输入。)
-a做了split,不是chop?另外,该&怎么办?我使用的是较旧的Ruby,它会引发错误。无论如何,没有它,它可以完美地以26字节工作。
-l不是-a。&.是Ruby 2.3中添加的“安全导航”运算符。如果没有它19,则输入,如果不是则不是子字符串"123456789",将引发NoMethodError。
([((()()()){}){}]{})(((()()()){}())<>)<>{({}<(({}[(((((()()()){}()){}){}){}){}]())){(<{}>)<>({}[()])<>}{}>[()])}<>
正确的版本(本着问题的精神):将整数用作输入,对于falsey,输出0,对于true,输出1。
这不是干净的堆栈。
让输入为n。
输出是正确的iff (n-123)(n-234)(n-345)(n-456)(n-567)(n-678)(n-789)=0。
我先减去12,再减去111 7次,计算出这七个数字,然后计算出这七个数字的逻辑双NOT并将它们相加。
对于真实结果,总和为6;对于假结果,总和为7。
然后,我从7中减去总和并输出答案。
第一次提交代码高尔夫球,请保持谨慎:)
a=>{return a>12&&a<790?a%111==12:false;};
a=>a>12&&a<790?a%111==12:false
a=>a>12&a<790&a%111==12
n=>1>(n-12)%111&n>99&n<790
这利用了以下事实:我对本质上为布尔值(基于位的布尔值)使用按位逻辑运算符。
感谢Titus的保存2。
(n-12)和n>99
基于Anastasiya-Romanova的答案,但返回Excel的TRUE/FALSE值。
=AND(LEN(N)=3,MID(N,2,1)-MID(N,1,1)=1,MID(N,3,1)-MID(N,2,1)=1)
此外,我们可以
=AND(LEN(N)=3,MID(N,2,1)-LEFT(N)=1,RIGHT(N)-MID(N,2,1)=1)
因为这两个RIGHT和LEFT默认返回单个字符。
并且,受到一些Python解决方案的启发:
=AND(LEN(N)=3,MOD(N,111)=12,N<>900)
感谢Neil提供4个字节...
=AND(N>99,MOD(N,111)=12,N<900)
N<900节省您一个字节,在这种情况下,您也可以使用N>99代替LEN(N)=3。
=REPT(LEFT(N),3)+12=N其中N是参考单元的名称。
=IF(LEN(N)<3,"Falsy",IF(AND(LEN(N)=3,MID(N,2,1)-MID(N,1,1)=1,MID(N,3,1)-MID(N,2,1)=1),"Truthy","Falsy"))
说明:
Excel中IF公式的语法为:
IF( condition, [value_if_true], [value_if_false] )
如果输入的长度N(小于等于3)是参考单元格的名称,则它将返回Falsy。否则,如果输入的长度N为3,并且第二位和第一位的差与第三位和第二位的差都等于1,则它将返回Truthy。
=REPT(LEFT(N),3)+12=N其中N是参考单元的名称。
接受字符串参数。
1 1≡¯2-/⍎¨
1 1≡ {1,1}是否等于
¯2-/ 的反向成对差
⍎¨ 每个字符都视为一个数字?
在线尝试APL!(出于安全原因⍎已被模拟e。)
($args[0]-12)/111-in1..7
使用与其他答案相同的“ 111加12的倍数”技巧,但使用其他方向。接受输入$args[0],减去12,除以111,然后检查是否是-in范围1..7。输出布尔值true / false。要求v3 +为-in运营商。
PS C:\Tools\Scripts\golfing> 123,234,345,456,567,678,789|%{.\easy-as-one-two-three.ps1 $_}
True
True
True
True
True
True
True
PS C:\Tools\Scripts\golfing> 1,2,3,12,122,124,132,321,457,777,890,900,1011,1230,1234|%{.\easy-as-one-two-three.ps1 $_}
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
十六进制转储(小端):
3803 d105 6808 ebc0 2010 b280 f2a0 1001 4770
此函数需要一个长度为字符串的指针对。输出为bash样式,其输出0为true,为非零值。在C语言中,该函数将声明为int oneTwoThree(size_t长度,char *字符串)。指令编码为thumb-2,具有2和4字节指令。在Raspberry Pi 3上测试。
非高尔夫装配:
.syntax unified
.text
.global oneTwoThree
.thumb_func
oneTwoThree:
@Input: r0 - the number of characters in the string
@r1 - A pointer to the (not necessarily NUL-terminated)
@string representation of the number (char*)
@Output: r1 - 0 if the number is in 123,234,...,789, else non-zero (bash-style)
subs r0,r0,#3
bne end @Return non-zero if r0!=3
ldr r0,[r1] @Remember that this is little endian
@So the first digit is the most siginificant byte
@I.e. if the input was 123 then r0 contains 0xXY010203 where XY is garbage
rsb r0,r0,r0,lsr #8 @r0=(r0>>8)-r0 (rsb is reverse subtract)
uxth r0,r0 @r0&=((1<<16)-1) (mask off top half)
@Now r0 is 0x0101 iff we have a matching number
sub r0,r0,#0x101
@Now r0 is 0 iff the string fit the specification
end:
bx lr @return
测试脚本(也是汇编):
.syntax unified
.text
.global main
.thumb_func
main:
push {r4,lr}
ldr r4,[r1,#4] @r0=argv[1]
mov r0,r4
bl strlen
@Now r0 is the length of the string argv[1]
mov r1,r4
bl oneTwoThree @oneTwoThree(strlen(argv[1]),argv[1])
cmp r0,#0
it ne
movne r0,#1 @Output through return code, 1 if false
pop {r4,pc}