咨询回答者


12
  • 原文:https//james-iry.blogspot.co.at/2009/05/brief-incomplete-and-mostly-wrong.html

    Alain Colmerauer设计了逻辑编程语言Prolog。他的目标是创建一种与两岁的孩子一样聪明的编程语言。为了证明自己已经成功实现了目标,他提出了一个Prolog程序,该程序可以为所有查询提供足够的答案“否”。
    问我任何事!
    ?-

  • (他当然没有。)您的任务是创建一个比Alain Colmerauer的程序更智能的程序。不必在Prolog中。

细节

  • 如果输入以结尾?并且至少有一个,,则从最后一个返回文本,,直到最后一个?

  • 否则,如果输入以?return 结尾No

  • 否则,返回Yes

规则

  • 没有标准漏洞。
  • 输入/输出将通过我们的标准输入/输出方法进行。
  • 您的程序必须至少查询1次。
  • 您应该输出已处理的查询。
  • 在示例中,YesNo区分大小写。
  • 您可以确保,如果输入包含a ?,则输入将只有一个?,并且它将始终是最后一个字符。
  • 输入将始终是短语/句子。这句话/句话绝不会只包含字符,?,如,?,?无效投入。(尽管提交仍然可以实现,因为在这种情况下,短语/句子是一个空字符串。)
  • 此外,输入将永远不会以结尾,?
  • 如果紧接在末尾,或之前有空格?,则应将其包含在输出中。

例子

Hmm. -> Yes
Alright, -> Yes
Ask me anything! -> Yes
Ask me a question, please! -> Yes
Are you okay? -> No
No? -> No
Hey,does this program work? -> does this program work

Quotes in the following test cases should not be outputted.
They are used as a delimiter here.

Okay, so this does work ? -> " so this does work "
Please, add, a, test, case, containing, multiple, commas? -> " commas"

计分

这是,因此最短答案以字节为单位。


6
我们是否保证如果输入包含a ?,那么将只有一个,并且始终是最后一个字符?
毛茸茸的

3
请添加一个包含多个逗号的测试用例。
manatwork's

8
尽早接受答案可能会阻止其他用户发布新答案,因为看起来挑战已经完成。
Arnauld

3
You are guaranteed that if the input includes a ?, the input will only have one ? and it will always be the last character.因此,这些测试用例是不必要的。

8
ends with ,?一个有效的输入?
GammaFunction

Answers:


6

05AB1E20 19 字节

'?åi',¡”€–”0ǝθ¨ë”…Ü

-1个字节感谢@Grimy

在线尝试验证所有测试用例

说明:

'?åi          '# If the (implicit) input contains a "?":
    ',¡       '#  Split the (implicit) input on ","
       ”€–”    #  Push dictionary string "Not"
           0ǝ  #  Insert it at the first position (index 0) in the list
       θ       #  Then get the last item of the list
        ¨      #  And remove the last character
               #  (either the "?" of the original input; or the "t" in "Not")
      ë        # Else:
       ”…Ü     #  Push dictionary string "Yes"
               # (after which the top of the stack is output implicitly as result)

请参阅我的05AB1E技巧(如何使用字典?部分),以了解为什么”€–”"Not"”…Ü"Yes"



@肮脏的哦,聪明的想法。谢谢!:)
Kevin Cruijssen

8

Python 3,62个字节

lambda s:['Yes',*s[:-1].split(','),'No'][~(','in s)*('?'in s)]

在线尝试!

表达式~(','in s)*('?'in s)的计算结果为0(即'Yes')如果字符串不包含'?',否则-1(即,'No')如果字符串不包含',',否则-2(即,字符串的最后一个逗号分隔部不包括最后一个字符)。


5

JavaScript(ES6), 53  52字节

s=>(m=s.match(/(,?)([^,]*)\?/))?m[1]?m[2]:'No':'Yes'

在线尝试!

已评论

s =>                  // s = input string
  ( m = s.match(      // m is the result of matching in s:
  //     +------------>    an optional comma
  //     |     +------>    followed by a string containing no comma
  //     |     |   +-->    followed by a question mark
  //   <--><-----><>     
      /(,?)([^,]*)\?/
  )) ?                // if m is not null:
    m[1] ?            //   if the comma exists:
      m[2]            //     output the string following it
    :                 //   else:
      'No'            //     output 'No'
  :                   // else:
    'Yes'             //   output 'Yes'

Welp正在寻找JS解决方案,但最终得到的东西是这种大小的两倍。
随机的家伙

4

木炭23 22字节

¿№θ?¿№θ,⁻⊟⪪θ,¦?¦No¦Yes

在线尝试!链接是详细版本的代码。编辑:感谢@KevinCruijssen,节省了1个字节。说明:

¿№θ?

字符串是否包含?s?

¿№θ,

它包含任何,s吗?

⊟⪪θ,

,s 上分割字符串,然后取最后一个。

⁻...?

删除?并输出结果。

No

如果没有,,则输出No

Yes

如果没有,?则输出Yes


-1更改Print(Join(Split(Pop(Split(q, ",")), "?"), w)Print(Minus(Pop(Split(q, ",")), "?");
Kevin Cruijssen,

@KevinCruijssen谢谢,我忘了Minus那样做。此外,我为自己节省了两个分隔符而感到高兴。
尼尔


3

Pyth,25个字节

?qeQ\??}\,QPecQ\,"No""Yes

在线尝试!

?q                          # if       ==
  eQ\?                      #    Q[-1]    "?":
      ?}                    #   if     in   
        \,Q                 #      ","    Q:
             cQ\,           #     return split(Q, ",")
            e               #                         [-1] (last element)
           P                #                             [:-1] (remove the trailing ?)
                 "No"       #   else: return "No"
                     "Yes"  # else: return "Yes" (last " implicit)



3

视网膜32 28字节

^'?K`Yes
.+,(.*)\?
$1
'?K`No

-4个字节,带有@Neil的提示。

在线尝试。

说明:

   K`       # Replace any (implicit) input, which does
^           # NOT
 '?        '# contain a "?"
     Yes    # with "Yes"

.+          # Match 1 or more characters
  ,         # followed by a comma
    .*      # followed by zero or more characters,
   (  )     # (captured in capture group 1)
       \?   # followed by a (trailing) "?"
$1          # And replace it with just the match of capture group 1,
            # (so everything between the last comma and trailing question mark)

  K`        # Replace any remaining string, which does
'?         '# contain a "?"
    No      # with "No"

            # (after which the result is output implicitly)

您知道视网膜1的K舞台具有内置条件吗?我没有 它可能可以为您节省一些字节。
尼尔

@Neil我不确定如何将K和条件链接起来&,以说实话。我知道如何使用K与正则表达式匹配这样的,但我怎么与它相结合的条件来模拟三元的if-else为Yes/ No
凯文·克鲁伊森

您不需要&,这让我感到惊讶,并且您可以匹配一个字符串,或者甚至可以匹配一个字符,因为您知道任何字符都?必须在末尾。
尼尔

@Neil这样吗?显然也是32个字节。
凯文·克鲁伊森

1
不完全的; 您仍然可以在正则表达式上进行匹配,但是可以使用字符串或字符代替。
尼尔

2

IBM / Lotus Notes公式,79个字节

@If(@Ends(i;"?");@If(@Contains(i;",");@Left(@RightBack(i;",");"?");"No");"Yes")

公式没有TIO,所以...

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明


2

Python 3中98 87个字节

-9字节归功于ElPedro

x=input();o="Yes"
if"?"==x[-1]:o=x[(x.rfind(",")+1):-1]if x.count(",")else"No"
print(o)

在线尝试!

这已经被其他答案打败了,但是我正在尝试更多地使用Python打高尔夫球。意见表示赞赏!


1
您可以使用一个打印,避免分配,然后使用lambda,因为它现在只是一个声明,然后缩短内嵌if语句列表索引,而不是再缩短条件下使用in,然后把它全部,因为它太接近的现有的答案
乔金

在不改变原始方法的情况下进行几次高尔夫球练习,将其降至87点,在线尝试!
ElPedro

您可以替换if x.count(",")if~x.find(","),节省一个字节。str.find计算-1是否找不到该字符串。如果使用一元运算符对此进行补充,则0只有当子字符串不存在时,它才会产生。或者,您可以将其替换if","in x 为少4个字节。
吉特(Jitse)







1

Python 2中66个 63 62字节

lambda i:("Yes",("No",i[i.rfind(",")+1:-1])[","in i])["?"in i]

在线尝试!

-3发现更新的说明后,“输入将只有一个?,它将始终是最后一个字符。”

-1感谢@ChasBrown

基本上是我的Lotus Notes答案端口。奇怪的是,上述说明对Notes的回答无济于事,因为@Ends它比便宜4个字节@Contains。现在,只要有一个@In功能...


2
保存1个字节使用rfind
Chas Brown


1

C ++(GCC) 120个 118字节

类似函数的宏:

#include<string>
#define f(s)({int i{},j{};for(;s[i];)j=s[++i]-44?j:i;s[--i]-63?"Yes":j?std::string(s-~j,i+~j):"No";})

在线尝试!

-2个字节,感谢@ceilingcat

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.