-3
\$\begingroup\$

A loaded question is something like 'Have you stopped swearing?' in which both yes and no answers mean that I have swore, meaning it is impossible to answer the question if I haven't swore.

However, the constructed language Lojban has a word na'i, which means there is something wrong with what you just said.

Your input should be a list of newline-seperated strings containing the following tokens:

  1. start followed by any integer (e.g. -1, 0, 1)
  2. stop + integer
  3. haveYouStopped + integer

You can assume that each stop n is followed by the corresponding start n.

Then for each haveYouStopped n in the code:

  • Print Yes, I have. + a newline if the last stop n occurs before the last start n
  • Print No, I have not. + a newline if the last stop n occures after the last start n, or at least one start n but no stop n is found in the previous line
  • Print .i na'i + a newline if no start n occurs in the previous lines.

The last newline is optional.

Examples:

start 1
stop 1
haveYouStopped 1

Output: Yes, I have.

start 1
haveYouStopped 1

Output: No, I haven't.

haveYouStopped 1

Output: .i na'i

All code is counted in UTF-8 bytes. Codepoints outside the UTF-8 range are not allowed. Use any language, shortest code wins. Also, no external sources are allowed.

\$\endgroup\$
4
  • \$\begingroup\$ What is a "list of newline-separated strings"? Do you mean one string with portions delimited by newlines? \$\endgroup\$
    – Somebody
    Nov 21 at 2:24
  • 2
    \$\begingroup\$ Please add more testcases, for example, more than one queries, start after stop, multiple start without stop (is it allowed?), stop without start (is it allowed?), n other than 1, testcases with multiple different n... \$\endgroup\$
    – tsh
    Nov 21 at 8:27
  • 1
    \$\begingroup\$ In your rules you say the output is No, I have not., but in your test case the output is No, I haven't.. So should it be have not, or haven't, or can we choose either? \$\endgroup\$ Nov 21 at 9:56
  • 1
    \$\begingroup\$ This site has a special question called the Sandbox where you can post your challenges before you put them on the main site, so you can get feedback on how they can be improved. codegolf.meta.stackexchange.com/questions/2140/… \$\endgroup\$
    – noodle man
    Nov 21 at 12:20

4 Answers 4

2
\$\begingroup\$

Scala 3, 255 bytes

255 bytes. It can be golfed much more.


Golfed version. Attempt This Online!

def p(t:String,d:Map[String,String]=Map[String,String]()):Unit={t.split("\n").filterNot(_.isEmpty).foreach{l=>val Array(c,v)=l.split(" ");if(c.contains("s"))d(v)=if(c.contains("p"))"Yes, I have."else"No, I haven't."else println(d.getOrElse(v,".i na'i"))}}

Ungolfed version. Attempt This Online!

object Main {
  import scala.io.Source
  import scala.collection.mutable

  def processText(text: String, dict: mutable.Map[String, String] = mutable.Map[String, String]()): Unit = {
    val lines = text.split("\n").filterNot(_.isEmpty)
    for (line <- lines) {
      val Array(command, value) = line.split(" ")
      if (command.contains("s")) {
        dict(value) = if (command.contains("p")) "Yes, I have." else "No, I haven't."
      }
      else {
        println(dict.getOrElse(value, ".i na'i"))
      }
    }
  }

  def main(args: Array[String]): Unit = {
    val text =
      """
        |start 1
        |haveYouStopped 1
      """.stripMargin
    processText(text)
  }
}
\$\endgroup\$
2
\$\begingroup\$

05AB1E, (68 bytes) 107 bytesUTF-8

† Since the challenge states the answers should be counted in UTF-8 bytes for some reason.. -_-

|ηʒθ'hÅ?}ε€#R.¡θ}н€н€θDgi".i na'i"ëJ…dptQi“Yes, I€¡.“ë“No, I€¡€–.“]»

Try it online.

Explanation:

|                           # Get all inputs as a list of strings
 η                          # Pop and push its prefixes
  ʒ     }                   # Filter this list of prefixes, keeping those where:
   θ                        #  The last string of the prefix
      Å?                    #  ends with
    'h                     '#  an "h"
ε                           # Map over each prefix ending with "haveYouStopped"
 €#                         #  Split each inner string by spaces
   R                        #  Reverse this list of pairs
    .¡ }                    #  Then group it by:
      θ                     #   The last item of the pair (the numbers)
        н                   #  Keep the first group (of the reversed list)
 €н                         #  Keep the strings of each inner pair
   €θ                       #  Keep the last letter of each string
     Dg                     #  Duplicate, pop and push the amount of characters in this list
       i                    #  If this length is 1 (thus only "haveYouStopped"):
        ".i na'i"           #   Push string ".i na'i"
       ë                    #  Else:
        J                   #   Join the list of 2 or 3 characters to a string
         …dptQi             #   If it's equal to "dpt" (thus "haveYouStopped,stop,start"):
               “Yes, I€¡.“  #    Push dictionary string "Yes, I have."
              ë             #   Else:
               “No, I€¡€–.“ #    Push dictionary string "No, I have not."
]                           # Close both if-else statements and map
 »                          # Join the list of strings by newlines
                            # (after which it is output implicitly with additional trailing newline)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why “Yes, I€¡.“ is "Yes, I have." and “No, I€¡€–.“ is "No, I have not.".

\$\endgroup\$
4
  • 1
    \$\begingroup\$ It just says "Codepoints outside the UTF-8 range are not allowed", but these are all perfectly fine Unicode characters, so I think this should count. \$\endgroup\$
    – noodle man
    Nov 21 at 12:17
  • \$\begingroup\$ @noodleman Ah wait. I mixed up UTF-8 with ASCII.. -_- My bad! \$\endgroup\$ Nov 21 at 12:36
  • \$\begingroup\$ It still says "all code should be counted in UTF-8 bytes" however. \$\endgroup\$
    – noodle man
    Nov 21 at 12:37
  • \$\begingroup\$ @noodleman reverted that part.. And added an explanation. \$\endgroup\$ Nov 21 at 12:49
2
\$\begingroup\$

Retina, 92 75 74 bytes

L$ms`d( \S+)$(?<=([pt])\1$.+?)?
$2.i na'i
p.+
YesI.
t.+
NoIn't.
I
, I have

Try it online! Link includes test cases. Explanation:

L$ms`d( \S+)$(?<=([pt])\1$.+?)?
$2.i na'i

For each haveYouStopped line, look for a previous start or stop line with that signed integer, and if so prefix the output with the final t or p, otherwise just output .i na'i.

p.*
YesI.
t.*
NoIn't.
I
, I have

If a start or stop was found then decode it to the desired output string.

Edit: Saved 17 bytes and corrected the Yes/No order thanks to @tsh.

Since this question counts in UTF-8, I didn't post a Charcoal answer, but had it been allowed it would have been 59 SBCS bytes:

≔⦃⦄θWS¿№ιs§≔θΣι⪫⪪⎇№ιp¦Yes,.¦No,n't.¦,¦, I have⟦∨§θΣι.i na'i

Attempt This Online! Link is to verbose version of code. Takes input as a list of newline-terminated strings. Explanation:

≔⦃⦄θ

Start with all questions being impossible.

WS

Loop through the input strings.

¿№ιs

Does this string resolve a question?

§≔θΣι⪫⪪⎇№ιp¦Yes,.¦No,n't.¦,¦, I have

If so then save the appropriate answer.

⟦∨§θΣι.i na'i

Otherwise, output the saved answer, or the impossible answer if one wasn't saved.

\$\endgroup\$
3
0
\$\begingroup\$

Python, 160 158 bytes

def d(t,i={}):
 for l in t.strip().split("\n"):
  c,v=l.split()
  if"s"in c:i[v]="Yes, I have."if"p"in c else"No, I haven't."
  else:print(i.get(v,".i na'i"))

-2 thanks to UndoneStudios

(Assumes that you are referring to taking input as a newline-delimited string)

Ungolfed version

def decode(text):
    data = {}
    for line in text.strip().split("\n"):
        command, value = line.split()
        if "s" in command:
            data[value] = "Yes, I have." if "p" in command else "No, I haven't."
        else:
            print(data.get(v, ".i na'i"))
\$\endgroup\$
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.