12
\$\begingroup\$
  • Original: https://james-iry.blogspot.co.at/2009/05/brief-incomplete-and-mostly-wrong.html

    Alain Colmerauer designed the logic programming language Prolog. His goal was to create a programming language that is as intelligent as a two-year-old child. In order to prove that he had succeeded in his goal, he presented a Prolog program that answers "No" resourcefully for all inquiries.
    Ask me anything!
    ?-

  • (Of course he didn't.) Your task is to create a program that is more intelligent than Alain Colmerauer's program. This does not have to be in Prolog.

Specifics

  • If input ends with ? and has at least one ,, return text from the last , until before the last ?.

  • Else, if input ends with ? return No.

  • Else, return Yes.

Rules

  • No standard loopholes.
  • Input/output will be taken via our standard input/output methods.
  • Your program has to take at least 1 inquiry.
  • You should output the processed inquiry.
  • The Yes and No's are case-sensitive in the examples.
  • You are guaranteed that if the input includes a ?, the input will only have one ? and it will always be the last character.
  • The input will always be a phrase/sentence. This phrase/sentence will never only contain the characters , and ?, e.g. ,, ?, and ,? are not valid inputs. (Although submissions might implement it anyway, since the phrase/sentence is an empty string in these cases.)
  • In addition, the input will never end with ,?.
  • If there is whitespace immediately after the last , or immediately before the ?, they should be included in the output.

Examples

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"

Scoring

This is , so shortest answer in bytes wins.

\$\endgroup\$
  • 6
    \$\begingroup\$ Are we guaranteed that if the input includes a ?, there will only be one and it will always be the last character? \$\endgroup\$ – Shaggy Sep 16 '19 at 9:47
  • 3
    \$\begingroup\$ Please add a test case containing multiple commas. \$\endgroup\$ – manatwork Sep 16 '19 at 10:19
  • 8
    \$\begingroup\$ Accepting an answer early may discourage other users from posting new answers, because it kind of looks like the challenge is done. \$\endgroup\$ – Arnauld Sep 16 '19 at 11:58
  • 3
    \$\begingroup\$ You are guaranteed that if the input includes a ?, the input will only have one ? and it will always be the last character. Therefore these test cases are unneccecary. \$\endgroup\$ – user85052 Sep 16 '19 at 14:28
  • 8
    \$\begingroup\$ Is ends with ,? a valid input? \$\endgroup\$ – GammaFunction Sep 16 '19 at 16:11

20 Answers 20

6
\$\begingroup\$

05AB1E, 20 19 bytes

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

-1 byte thanks to @Grimy.

Try it online or verify all test cases.

Explanation:

'?å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)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ”€–” is "Not" and ”…Ü is "Yes".

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 20 \$\endgroup\$ – Grimmy Sep 16 '19 at 11:10
  • \$\begingroup\$ @Grimy Oh, smart thinking. Thanks! :) \$\endgroup\$ – Kevin Cruijssen Sep 16 '19 at 11:12
8
\$\begingroup\$

Python 3, 62 bytes

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

Try it online!

The expression ~(','in s)*('?'in s) evaluates to 0 (i.e. 'Yes') if the string does not contain a '?', else -1 (i.e. 'No') if the string does not contain a ',', and otherwise -2 (i.e. the last comma-separated section of the string excluding the last character).

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

JavaScript (ES6),  53  52 bytes

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

Try it online!

Commented

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'
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Welp, was looking for a JS solution, ended up with something double this size. \$\endgroup\$ – The random guy Sep 16 '19 at 9:26
4
\$\begingroup\$

Charcoal, 23 22 bytes

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

Try it online! Link is to verbose version of code. Edit: Saved 1 byte thanks to @KevinCruijssen. Explanation:

¿№θ?

Does the string contain any ?s?

¿№θ,

Does it contain any ,s?

⊟⪪θ,

Split the string on ,s and take the last.

⁻...?

Delete the ? and output the result.

No

If there are no ,s then output No.

Yes

If there are no ?s then output Yes.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ -1 changing Print(Join(Split(Pop(Split(q, ",")), "?"), w) to Print(Minus(Pop(Split(q, ",")), "?"); \$\endgroup\$ – Kevin Cruijssen Sep 16 '19 at 12:53
  • \$\begingroup\$ @KevinCruijssen Thanks, I'd forgotten that Minus did that. Besides, I was feeling pleased with myself for saving two separators. \$\endgroup\$ – Neil Sep 16 '19 at 16:05
4
\$\begingroup\$

Perl 5 + -plF/,|\?/, 25 bytes

$_=/\?$/?/,/?pop@F:No:Yes

Try it online!

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Pyth, 25 bytes

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

Try it online!

?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)
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Zsh, 51 bytes

<<<${${${1#*\?}:+Yes}:-${${${${1##*,}%$1}:-No}%\?}}

Try it online!

A byte can be saved if ends with,? and similar are invalid.

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Stax, 16 bytes

ëw‼◘╔╤▬n→ª▒¡Γ╟ï¿

Run and debug it

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Retina, 32 28 bytes

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

-4 bytes with tips from @Neil.

Try it online.

Explanation:

   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)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Did you know that Retina 1's K stage has a built-in conditional? I didn't. It could probably save you a few bytes. \$\endgroup\$ – Neil Sep 16 '19 at 13:06
  • \$\begingroup\$ @Neil I'm not sure how to chain the K and conditional & to be completely honest. I know how to use K with a regex to match like this, but how do I combine it with the conditional to mimic a ternary if-else for the Yes/No? \$\endgroup\$ – Kevin Cruijssen Sep 16 '19 at 13:38
  • \$\begingroup\$ You don't need &, which is what surprised me, and you can just match on a string, or better still, a character, since you know that any ? must be at the end. \$\endgroup\$ – Neil Sep 16 '19 at 16:01
  • \$\begingroup\$ @Neil So, like this? Which is also 32 bytes apparently. \$\endgroup\$ – Kevin Cruijssen Sep 16 '19 at 16:32
  • 1
    \$\begingroup\$ Not quite; you're still matching on a regex, but you're allowed a string or character instead. \$\endgroup\$ – Neil Sep 16 '19 at 21:59
2
\$\begingroup\$

IBM/Lotus Notes Formula, 79 bytes

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

No TIO for Formula so...

enter image description here

enter image description here

enter image description here

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Python 3, 98 87 bytes

-9 bytes thanks to ElPedro

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

Try it online!

This has been solidly beaten by other answers, but I'm trying out golfing in Python a bit more. Advice is appreciated!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ You can use one print to avoid the assignment, then use a lambda since it's now just one statement, then shorten the inline if statements to a list indexing instead and then shorten the conditions to use in, then throw it all out because it's too close to an existing answer \$\endgroup\$ – Jo King Sep 16 '19 at 23:44
  • \$\begingroup\$ A couple of golfs without changing your original approach to get it down to 87 Try it online! \$\endgroup\$ – ElPedro Sep 17 '19 at 7:32
  • \$\begingroup\$ You can replace if x.count(",") with if~x.find(","), saving one byte. str.find evaluates to -1 if the string is not found. If you complement this with the unary operator, it results in 0 if and only if the substring is not present. Alternatively, you can replace it with if","in x for 4 bytes fewer. \$\endgroup\$ – Jitse Sep 18 '19 at 7:23
1
\$\begingroup\$

PHP, 58 bytes

<?=$argn[-1]=='?'?substr(strrchr($argn,','),1,-1)?:No:Yes;

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Red, 78 bytes

func[s][r:"Yes"parse s[to","copy t to"?"(r: last split t",")| to"?"(r:"No")]r]

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 40 bytes

{m/[\,(.*?)]*\?$/??$0.tail||"No"!!"Yes"}

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Gema, 34 characters

*,*\?=@subst{\*,=;$2}
*\?=No
*=Yes

Sample run:

bash-5.0$ echo -n 'Hey,does this program work?' | gema '*,*\?=@subst{\*,=;$2};*\?=No;*=Yes'
does this program work

Try it online! / Try all test cases online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Japt, 23 bytes

ø'? ?Uq, hoinu¹Ìk'?:`Y

Try it

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 72 bytes

x=>x.Last()!=63?"Yes":x.Contains(',')?x.Split(',').Last().Trim('?'):"No"

Saved 2 bytes thanks to @someone

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ SkipLast(1) -> Trim('?') for -??? bytes as far as I see (all my attempts to calculate the difference result in different numbers) \$\endgroup\$ – the default. Sep 17 '19 at 3:51
1
\$\begingroup\$

Python 2, 66 63 62 bytes

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

Try it online!

-3 after spotting the updated clarification "the input will only have one ? and it will always be the last character."

-1 with thanks to @ChasBrown

Basically a port of my Lotus Notes answer. Curiously, the clarification noted above does not help the Notes answer because @Ends is 4 bytes cheaper than @Contains. Now if only there was an @In function...

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Jelly, 20 bytes

Ṗṣ”,Ṫ⁾NoḊ?Ɗ“Yes”i?”?

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C++ (gcc), 120 118 bytes

Function-like macro:

#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";})

Try it online!

-2 bytes thanks to @ceilingcat

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy