挑战
给定一个整数,该整数可被9整除并有一个缺失的数字,请找到缺失的数字。
缺失的数字可以用数字0-9以外的任何字符表示,只要它是一致的即可。
如果缺少的数字可能是0或9,则输出一些内容来表示。
假设用户足够聪明,只能输入一位数字和一位数字等。
测试用例
在测试用例中,缺失的数字由问号表示
123? -> 3
?999 -> 0 or 9
?0023 -> 4
000?1 -> 8
?
可能的输入?
给定一个整数,该整数可被9整除并有一个缺失的数字,请找到缺失的数字。
缺失的数字可以用数字0-9以外的任何字符表示,只要它是一致的即可。
如果缺少的数字可能是0或9,则输出一些内容来表示。
假设用户足够聪明,只能输入一位数字和一位数字等。
在测试用例中,缺失的数字由问号表示
123? -> 3
?999 -> 0 or 9
?0023 -> 4
000?1 -> 8
?
可能的输入?
Answers:
/o&
\i@/+R9%
输出0
,如果结果可以是0或9。
/ Switch to Ordinal mode.
i Read all input as a string.
& Fold the next operation over the characters of this strings.
/ Switch to Cardinal mode (not an operation).
+ Add. Implicitly converts each character to the integer value it represents
and adds it to the top of the stack. The top of the stack is initially, implicitly
zero. When the character is "?", it is simply discarded, which adds the top of
the stack to another implicit zero beneath, so the "?" is effectively skipped.
R Negate. Multiplies the sum by -1.
9% Modulo 9. This gives the result.
\ Switch to Ordinal mode.
o Implicitly convert the result to a string and print it.
@ Terminate the program.
&
可以被移除,因为基数模式解释原始输入至多2点的整数。
如果可能为0或9,则输出9。
f=_=>9-([..._].reduce((a,b)=>a+~~b,0)%9)
f=
; 匿名函数是完全有效的。
%_s-Qd9
使用空格作为定界符,如果结果可能为0或9,则返回0。
%_s-Qd9
-Qd Remove the space from the input.
s Convert it to an integer.
%_ 9 Negate mod 9.
0*[].
X*[H|T]:-between(0,9,H),U is mod(X+H,9),U*T.
+X:-0*X.
是的,逻辑编程!
首先,我们创建一个谓词*
,该谓词在应用于零和空列表时成立。当列表的开头在0到9之间并且添加前导数字mod 9时,我们的谓词也会成立。
然后我们定义 +
,为0
作为第一个参数。也就是说,我们希望数字总和是9的倍数。
Prolog为实际为我们找到解决方案做了所有艰苦的工作。
1+!_#@3#.--9%~:#
James Holderness的Befunge答案的单行版本设法删除了两个字节。这实际上将代码压缩为一行,反转了方向,并利用了Befunge不会在行尾跳过的事实。他建议我发表一个单独的答案并作解释。该代码以*表示丢失的数字,并为0或9输出9。
1+!_ 3 --9% Initialises the pointer going right and initialises the digitsum with 3
1+!_ ~: Gets input and checks whether it is end of input (-1)
3 - Subtract three from the input.
This turns the ASCII values of digits into their mod 9 equivalent ("0"(48)=>45(mod 9)=>0)
-9% Subtract the value from the digitsum and mod it with 9
This keeps the digitsum as a negative digit greater than -9
Repeat this until the input ends
Now this is where it gets tricky.
At the end of input, the stack looks like this:
-(Digitsum%9), Copy of EOF input (-1)
The pointer is moving left from the _
+!_ Invert the -1 to get 0 and add it to the digitsum (does nothing but get rid of the -1)
1 %~ Adds 1 mod -1 (no input) = 0 to the stack
--9 Subtracts 9 from 0, yielding -9 and subtracts that from the digitsum.
This makes the digitsum positive and equal to 9-(digitsum%9), our desired value
@ . Finally, print and exit
*
(ASCII值42)被选为缺少字符,因为它抵消了数字和的初始值3。
\RequirePackage{expl3}
\ExplSyntaxOn
\tl_new:N \l_divnine_input_tl
\int_new:N \l_divnine_sum_int
\def \getnum {
\typein [ \l_divnine_input_tl ] { Input\space a\space number }
\tl_map_function:NN \l_divnine_input_tl \divnine_sum:n
\int_set:Nn \l_tmpa_int { 9 - \int_mod:nn { \l_divnine_sum_int } { 9 } }
\typeout
{ The\space missing\space digit\space is:\space
\int_use:N \l_tmpa_int
\int_compare:nNnT { \l_tmpa_int } = { 0 } { \space or\space 9 }
}
}
\cs_new:Nn \divnine_sum:n {
\regex_match:nnT { \d } { #1 } { \int_add:Nn \l_divnine_sum_int { #1 } }
}
\ExplSyntaxOff
\getnum
\stop
\RequirePackage{expl3}\ExplSyntaxOn\int_new:N\1\tl_new:N\2\def\3{\typein[\2]{Input\space a\space number}\tl_map_function:NN\2\4\int_set:Nn\1{9-\int_mod:nn{\1}{9}}\typeout{The\space missing\space digit\space is:\space\int_use:N\1\int_compare:nNnT{\1}={0}{\space or\space9}}}\def\4#1{\regex_match:nnT{\d}{#1}{\int_add:Nn\1{#1}}}\3\stop
\RequirePackage{expl3}\typein[\1].\newcount\2{\ExplSyntaxOn\int_gset:Nn\2{9-\int_mod:nn\19}}\typeout{\the\2 \ifnum\2=9or 0\fi}\stop
此代码中只能使用空格作为未知数字。
-2个字节,感谢@BolceBussiere
9|9-[:+/"."0
老实说,我不知道是什么原因"."0
解释?
为0,但是在我的解释器和TIO上都为0,所以我不会对此提出质疑。(更新:请参阅注释以了解原因)。
这种方法非常简单地获取数字的总和,通过减去9来取反,然后取模9。
|0SN%9
|0 # Read input while removing non-digits (question marks) and removing leading zeros
S # Sum the digits
N # Negate
%9 # Mod by 9
输出为0表示结果可能为0或9。
多亏了Xcoder先生,节省了2个字节。€
在评估过程中使用“每个快速”()时,将数字分成数字是多余的。
感谢Dennis节省了3个字节。可以将输入与0进行按位或运算,而不用手动将输入解析为数字,同时删除前导零和非数字。
|0
代替fØDV€
。
我意识到我不需要用它?
来代表缺席的数字,所以我在减去48后使用了9的倍数:x
这让我打高尔夫球了3+
,但是由于长度的原因我只节省了1个字节。有条件的第一行:(
我的Befunge-98答案的端口:另外
5个字节以检查我们是否已达到EOF,另外
1个字节按48("0"
vs '0
),另外
1个字节以打印答案.@
,
并 1个字节,因为第二个一行
总共有8个字节的空间。
~:0`!#|_"0"-+
@.%9-<
产出 0
如果丢失的数字可能是0或9。
由于以下原因,这仅在PyFunge解释器中有效。
许多此类解释是从我的Befunge-98解释中复制粘贴的,因为该程序与该程序非常相似。无耻的插头
在程序的第一行中,我们对包括x在内的数字求和,因为它是ASCII值,所以将其视为72。但是,一旦我们将其乘以9,总和将是相同的,因此这无关紧要。
~:0`!#|_"0"-+ THE FIRST LINE
~ Gets a character from input - If it is negative, we've reached EOF
:0`! Pushes 0 if the character is positive, 0 otherwise
#|_ Goes to the next line if the value if 0
This also gets the negative value off the stack by using a |
"0"- Subtracts 48 to account for taking in ASCII values
+ Adds this adjusted value to the sum
如果我们仅乘以9,我们将得到错误的数字,因为我们想要9 - (sum % 9)
。但是,我们可以做得比更好9\-
,它将从9中减去余数:如果在修改之前使总和为负数,则我们将得到正数结果,9 - (sum % 9)
在某些解释器中为9。这就是要求我们对Befunge 93和98使用PyFunge解释器的原因,因为它是TIO上唯一做到这一点的工具。其他的给我们一个介于-8和8之间的值,而不是0和8
@.%9-< THE SECOND LINE
< Redirects the IP onto this line
- Subtracts the sum from an implicit 0, making it negative
%9 Mods the sum by 9
@. Prints the digit and exits
我意识到,我并不需要使用?
到代表缺席的数字,所以我用一个是9减去48后的倍数:x
这让我的高尔夫球关闭3+
。
#v~'0-+
q>-9%
使用 x
作为缺少的数字,因为它的ASCII值在减去48后可以被9整除(这很好,因为它通常在数学中用作变量)。
通过退出代码输出(因为q
比短1个字节.@
)
输出0
,如果丢失的数位可以是0或9。
由于以下原因,这仅在PyFunge解释器中有效。
在程序的第一行中,我们对数字进行求和,包括x
,因为ASCII值而将其视为72。但是,一旦我们将其乘以9,总和将是相同的,因此这无关紧要。
#v~'0-+ THE FIRST LINE
#v~ Pushes the next character from input, and goes to the second line on EOF
'0- Subtracts 48 to account for taking in ASCII values
+ Adds this adjusted value to the sum
如果我们仅乘以9,我们将得到错误的数字,因为我们想要9 - (sum % 9)
。但是,我们可以做得比更好9\-
,它将从9中减去余数:如果在修改之前使总和为负数,则我们将得到正数结果,与9 - (sum % 9)
某些解释器等效。这就是要求我们对Befunge 93和98使用PyFunge解释器的原因,因为它是TIO上唯一做到这一点的工具。其他的给我们一个介于-8和8之间的值,而不是0和8。
q>-9% THE SECOND LINE
> Redirects the IP onto this line
- Subtracts the sum from an implicit 0, making it negative
9% Mods the sum by 9
q Outputs via exit code, ending the program
用途'
(与0
可以被“ 0”整除的都会这样做,包括0
其自身,都可以使用)。
输出0
表示0
或9
。
p -(gets.sum+6*~/$/)%9
p # print after inspecting
-( # unary negative, for reversing result of modulus (a-b%a)
gets.sum # ascii value of string
+ 6*~/$/ # add six for each size (in chars) of input, so "0" translates to 0
)%9 # mod 9
# find remainder after dividing 9
# except it's inverted so it's remainder to add to divide 9
必须归功于Mistah Figgins,其PyFunge答案告诉我,如果您只是确保ASCII值是9的倍数,则不需要特殊检查丢失的数字字符。
还要感谢乔·金(Jo King),他表明您不需要将字符完全转换为等效的数字,只需将3减去即可得到相对于基数9的值(ASCII 0
减3为45,是9的倍数)。 。
3_v#`0:~--
%9_@.+9
为此,您应该使用该字符*
作为缺少的数字(也可以使用其他字符,但这是最好的)。
输出9
如果丢失的数字可能是0或9。
说明
3 Push 3 onto the stack.
_ Since this is non-zero, drop the 3 and branch left.
3 Now executing right to left, push 3 again.
-- Negate twice, leaving the value unchanged as our starting sum.
~ Now we start the main loop, reading a character from stdin.
`0: Duplicate and check if it's > 0, i.e. not end-of-stream.
_ # If so, continue to the left.
3 - Subtract 3 to make the number relative to base 9.
- Then subtract it from the total, and repeat the loop.
v Once we reach the end of the input, we go down.
_ Drop the EOF character from the stack and go left.
%9 +9 Mod the total with 9, and add 9.
@. Output the result and exit.
本质上,我们是在计算所有数字的总和,外加每数字45(这将在我们对9进行调制时最终被抵消)。从3(我们的起始总数)中减去此总和,然后再用丢失的数字(ASCII)减去39*
减3)。同样,3减39是9的倍数,所以当我们用9进行模数修改时,它被抵消了。
所以最后我们要计算所有数字的负和,即mod 9,加上9,即
9 - (digitsum % 9)
这就是我们缺少的数字。
#q~3--9%
通过退出代码输出。与Mistah Figgin的答案一样,它仅适用于Pyfunge,其中负数mod 9变为正数。使用x作为缺少的数字。
param($a)0..9|?{!(($a-replace'x',$_)%9)}
需要输入像'123x'
成$a
。构造一个范围0
,9
并使用Where-Object
(在此缩写为|?
)拉出那些与该子句匹配的整数。该子句接受$a
,执行一个正则表达式-replace
以用x
当前数字替换,$_
并使用来获得mod 9 %9
。因此,如果9均分,则为零。我们将其取值为布尔值,而不是布尔值,将零变成真,将其他所有东西都变成假,从而满足Where-Object子句。这些结果留在管道上,并且输出是隐式的。
如果?
可以0 or 9
,则结果显示为9
。
.
$*
1{9}
^
9$*;
+`;1
.
. Sum of the digits in unary + 1
$*
1{9} Modulo 9
^ Prepend 9 semicolons, for subtracting
9$*;
+`;1 Subtract from 9
. Back to decimal
\d
可以更改为just,.
然后将其更改为$*
。
?
写这篇文章时还没有删除。
+>,[---[<-[<<]>[<+++++++++<]>>-],]-[<+>-----]<---.
打印0或9的9。缺少的字符由表示:
Tape Format:
0 Total Input 0
The total is represented as 9-digitSum%9
+>,[ Start loop with 1 as the total
--- Subtract 3 from the inputted byte to make the value%9 equal to the digit
[ While the input byte exists
<- Decrement the total
[<<]>[<+++++++++<] If the total is 0, reset it to 9
>>- Decrement the input byte
]
,] Continue loop until there is no input
-[<+>-----]<---. Add 48 to the total to convert the digit to the ascii value and output it
缺少的字符必须是mod 9为4,+ 3的字符,因为我们从正常数字中减去3,而将+1初始化为总数为1。
顺便说一句,为了打高尔夫球,代码中存在很多效率低下的问题,因为每个数字将重置总计5次,而不是如果我减去48而不是3,有时将重置一次。
0
吗?怎么样[0, 9]
(数组或2个数字的列表)?