事实之后的配件!


10

这个挑战是受数学启发的事实。编程不是


阶乘或事实的数学符号是感叹号!。感叹号也是not许多编程语言中的常用符号。

挑战:

以包含数字和字符的字符串+ !作为输入,并输出以下内容:

感叹号前面的所有内容都应被视为数学表达式,因此2+2也应如此4

单个感叹号之后的所有内容都应作为附件附加到其前面的内容中,因此:2+2!5应该给45,因为2+2=45是附件。2+2!5+5应该给410

由于!也意味着not,事实之后属于附件的任何东西都不应附加。因此,2+2!!5应该给4,因为5它不是附件。现在,not(not(true))==true所以2+2!!!5应该给452+2!!5!5+5应该给出:410,因为2+2=4,然后是阶乘和!5!5+5。第一个5不是事实,而是5+5在另一个感叹号之后,因此又是一个事实。

说明:

  • 感叹号+在任一侧都不会相邻。
  • 不会+以数字开头(5不是+5)。
  • 如果这是first前面的表达式的结果,则可以选择包含前导零!。既404用于输入接受输出:0+0!4

内容提要:评估每个总和(!作为分隔符)。然后丢弃出现在偶数之后的所有数字!(从字符串的开头算起)。然后全部删除!

测试用例:

!
   <- Empty string

5
5

12!
12

!87
87

!!5
   <- Empty string

5+5!2+2
104

5+5!!2+2
10

1!2!3!4!5!6!7!8!9
12468

10+10!!2+2!!3+3!4+4
208

2!!3!5
25

2!!3!5!7
25

10!!!!!!!5
105

这是因此以字节为单位(每种语言)的最短代码胜出!强烈建议您进行说明!


嗯...让我们说2 !! 3!5这里是5还是3的附件?
Officialaimm

3
@officialaimm是的25(请参阅添加的测试用例)。更重要的2!!3!5!7是仍会给出25,因为的!剩余数为偶数7(因此,您不仅要计算数字前面的游程,还要计算数字的!剩余数)。
Martin Ender

输出可以是Mathematica Row吗?
ngenisis

嗯...所以这个挑战实际上与析因无关吗?
DLosc

Answers:



5

JavaScript(ES6),58 56字节

感谢Martin Ender节省了两个字节。

let f =
x=>x.replace(/[^!]+/g,eval).replace(/!(\d*)!?\d*/g,"$1")
<input value="2+2!5+5" oninput="try{O.value=f(value)}catch(e){}"><br>
<input id=O value="410" disabled>

可能会有所改善...


很好地使用函数参数replace
尼尔,

@尼尔,谢谢,但是我找到了一个更好的方法:-)
ETHproductions '17

您的代码段给了我错误的答案1+1!5。我想你忘eval了之前的事了!
价值墨水

@ValueInk啊,我认为没有任何简单的方法可以解决此问题。
ETHproductions '17

2

果冻,16字节

ṣ”!µḢW;m2$VṾ$L¡€

在线尝试!

说明

这里的主要观察结果是,我们可以“无序”地运行步骤;除了评估总和然后忽略我们不喜欢的总和,我们可以忽略无效位置的总和,然后评估其余部分。

ṣ”!µḢW;m2$VṾ$L¡€
ṣ”!                Split input on '!'
   µ               Set as the new default for missing arguments
    Ḣ              Take the first element, removing it from the default
     W;  $         Cons with
       m2            every odd-numbered element of {the tail of the !-split input}
               €   For each remaining element
          VṾ$      Evaluate and de-evaluate it
             L¡      a number of times equal to its length

像这样求和"10+10"将对一个数字求值,例如20,然后对一个字符串进行反求值"20"。重复该过程没有其他影响(幂等)。因此,我们有效地评估了字符串的每个元素(空字符串除外),因为空字符串的长度为零,所以空字符串仍然未被评估。


这是有条件评估每个项目的绝妙技巧。您能以某种方式对每个项目及其评估值进行逻辑与吗?(我认为空字符串在Jelly中是虚假的)
ETHproductions's

@ETHproductions:果冻的默契使得它不太可能节省字节;它宁愿避免在可能的情况下两次使用相同的值,并且如果您确实想重用一个值,通常必须至少在一个µ地方放置一个多余的值(并且µ在循环内不起作用,这意味着您需要一些东西)更详细)。我确实设法使它起作用,ṣ”!µḢW;m2$ȧVṾ$$€但它并没有那么短(并且当您将Jelly推入其嵌套控制结构的能力的边缘时,往往会出现美元符号堆积的特征。)

2

果冻,18 字节

ṣ”!µḊm2;@ḢW$LÐfVṾ€

在线尝试!

怎么样?

ṣ”!µḊm2;@ḢW$LÐfVṾ€ - Main link: string
ṣ”!                - split on '!' characters
   µ               - monadic chain separation (call that x)      e.g. ['1+1','0+0','0+0','0+0','','1+0','','','']
    Ḋ              - dequeue x (all but the leftmost entry of x) e.g.       ['0+0','0+0','0+0','','1+0','','','']
     m2            - modulo 2 index into that result             e.g.       ['0+0',      '0+0',   '1+0',   '']
           $       - last two links as a monad
         Ḣ         -     head x (the leftmost entry of x)        e.g.  '1+1'
          W        -     wrap                                    e.g. ['1+1']
       ;@          - concatenate with reversed arguments         e.g. ['1+1','0+0',      '0+0',   '1+0',   '']
             Ðf    - filter keep:
            L      -     length (keep that have non-zero length) e.g. ['1+1','0+0',      '0+0',   '1+0']
               V   - eval as jelly code (vectorises)             e.g. [  2,    0,          0,       1]
                      Yes, addition is just + and decimal numbers are just strings of digits in Jelly believe it or not!
                Ṿ€ - uneval €ach (creates a string from each one)e.g. [ '2',  '0',        '0'     ,'1']
                      without the € it would uneval the list and hence yield commas too)
                   - implicit print (prints the resulting list [of characters and possibly
                      lists of characters] as if it were all one string.)

我认为这不适用于0+0输入中间的文字(在不被丢弃的地方);它产生的空字符串,即使它应该产生一个数字0

啊,是的-我将不得不采用更长的解决方法:(
Jonathan Allan

应该固定(现在可以打高尔夫球)。
乔纳森·艾伦


1

红宝石58 56 + 1 = 59 57字节

使用-p标志。来自Tutleman的 -2个字节。

i=0;$_=' '+$_;gsub(/!?([^!]*)/){eval$1if(2>i+=1)||i%2<1}

在线尝试!(添加了一行额外的代码,这样它将占用所有输入行,并在不同的行中打印输出。)


我想你可以在括号里放上括号eval$1,不是吗?
Tutleman '17

@图特曼吧。我不知道我遇到了什么问题使我添加了括号(当我开始编写程序时它们不存在),但是看来我真的可以删除它们。
价值墨水

0

批,192个 184字节

@echo off
set/ps=
call:c . "%s:!=" "%"
echo(%s%
exit/b
:c
set s=
if not %2=="" set/as=%2
:l
shift
shift
if "%1"=="" exit/b
if %1=="" goto l
set/at=%1
set s=%s%%t%
goto l

必须处理空字符串很不方便。


0

,18字节

我认为这是最短的时间...尽管我也在大约三遍之前说过。

{VaX++v%2+!v}Ma^'!

将输入作为命令行参数。在线尝试!

说明

                    a is 1st cmdline arg; global variable v is -1 (implicit)
              a^'!  Split a on !
{           }M      Map this function to the resulting list (note that inside function,
                    a is the function arg):
    ++v              Increment v (so that v tracks the 0-based index of the current
                     element)
       %2            We want to keep the elements where v%2 is 1...
         +!v         ... and also v=0, where v%2 is 0, but adding !v makes it 1
  aX                 String-multiply the argument by the above quantity (turning elements
                     we don't want into empty string)
 V                   Eval it (eval'ing empty string gives nil, but that's okay because
                     nil doesn't output anything)
                    Autoprint the resulting list, concatenated together (implicit)

0

R,95个字节

function(x)for(e in strsplit(gsub('!([^!]*)![^!]*','\\1!',x),'!')[[1]])cat(eval(parse(text=e)))

可能还有一些改进的空间,但是目前这是我能提出的最好的。

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.