FreeChat Online

-2

FreeChat Online

It is 1985, and you are a humble Russian potato farmer. Sadly, your glorious homeland is going through a time of crisis and it needs your help! The KGB, with the "assistance" of your wife and children, has convinced you to build their up-and-coming government FreeChat Online service for high ranking soviet officials. Luckily, the Russian government has provided you with several items seized through routine surveillance of the US's technological innovations—a cutting edge Macintosh XL, and the prototype source code for LucasArts's Habitat, an advanced communication service with cutting edge graphics. Your task is simple: create a few modifications for the Russian government…

Due to Communism's tendency to coalesce toward collectivistic cultures, the Russian government wants this chat to embody their perfect world—one without silly notions of self or individualism. Alas, Macintosh compatibility leads you astray. Due to problems in the prototype, every other sentence in the chat buffer is corrupted, except for the ending period. Lucky for you, the Russian people make classification easy for you.

CHALLENGE: For a String s in the input, there are 1 or more sentences, separated by ". "(period, TWO spaces) The 2nd, 4th, 6th, and so on sentences are corrupted. The last sentence may have nothing, ".", ". ", or ". " as its end. Here's some examples of an input:

"L'etat c'est moi (I am the state) - King Louis XIV"

|----------------one sentence, ended by nothing--------------|

"Hello World. GojdGojog. How are you. "

|--three sentences, one corrupted, ended by ". "--|

"Hi. gfhyG. My favorite food is Baseball. jijhojh. Foobar. "

|---------------Five sentences, two corrupted, ended by ". "-----------|

You must process this string, replacing any capitalized word besides the first word of a sentence, words in parentheses (no nested, but there can be multiple sets in a sentence), or corrupted sentences with the string "The State".

Examples:

"L'etat c'est moi (I am the state) - King Louis XIV"

--->"L'etat c'est moi (I am the state) - The State The State The State"

"Hello World. GojdGojog. How are you. "

--->"Hello The State. GojdGojog. How are you. "

"Hi. gfhyG. My favorite food is Baseball. jijhojh. Foobar. "

--->"Hi. gfhyG. My favorite food is The State. jijhojh. Foobar. "

Rules:

  1. Standard Loopholes are prohibited
  2. ASCII Text Input

  3. If the input doesn't match the specifications set above, do whatever you please with the output!

  4. This is code-golf, so the shortest(valid)answer (in each language) wins!

  5. Bonus points for a one-liner (Not really tho, just cool points)

Michael

Posted 2018-11-14T03:49:48.427

Reputation: 133

Question was closed 2018-11-14T14:23:43.377

2I had thought this post is a spam when I read its title in questions list. – tsh – 2018-11-14T05:05:14.080

2Why Hi. gfhyG. My favorite food is Baseball. jijhojh. Foobar. is 4 sentences? Should it be 3? Hi., gfhyG., My favorite food is Baseball. jijhojh. Foobar. – tsh – 2018-11-14T05:12:01.650

@tsh u rite but 5 – Michael – 2018-11-14T05:13:46.013

2@Michael Why? Where are the another 2? Should dot followed by one space considered as stop of a sentence? – tsh – 2018-11-14T05:15:21.797

@tsh the code backticks remove trailing whitespace, its in my editor but not visible, just added commas – Michael – 2018-11-14T05:17:29.327

@JoKing I got u no nested, but there can be multiple sets in a sentence – Michael – 2018-11-14T05:30:22.960

2For the first example, is it unintended that the "C" is capitalized in the input? – Kamil Drakari – 2018-11-14T06:15:38.573

1You state "The 2nd, 4th, 6th, and so on sentences are corrupted.", but your first example is only a single sentence, so why are King Louis XIV replaced with 3x The State? Also, as mentioned by @KamilDrakari above, why isn't the C (or C'est) not replaced with The State while the King Louis XIV are? – Kevin Cruijssen – 2018-11-14T09:08:36.463

@KevinCruijssen I think it's one indexed – Jo King – 2018-11-14T09:22:01.470

@JoKing That's what I assumed as well, so why is the first test cases which is a single sentence (1 in 1-indexed) corrupted with its capitalized words replaced? – Kevin Cruijssen – 2018-11-14T09:30:42.987

@KevinCruijssen You're meant to ignore corrupted sentences, so it's okay to make replacements in the first sentence – Jo King – 2018-11-14T09:48:10.050

@Michael Can an input-sentence end with a trailing dot an two spaces? – Kevin Cruijssen – 2018-11-14T12:40:05.100

@Michael Should I in the first example be replaced with The State as well? Or does the single letter I not count as a capitalized word? – Kevin Cruijssen – 2018-11-14T12:43:36.947

@KevinCruijssen No replacing words in parenthesises – Jo King – 2018-11-14T20:52:29.050

@JoKing Yeah, someone else already replied that to me, but I see (s)he now deleted their message.. – Kevin Cruijssen – 2018-11-14T21:58:01.723

@KevinCruijssen I think I fixed all the vague areas in my examples, can I has main page pls – Michael – 2018-11-16T03:28:29.563

Answers

4

Retina 0.8.2, 77 bytes

(?<=^(([^.]*)\. +([^.]*)\. +)*[^ .][^.]*)(?<!\([^)]*)[A-Z][A-Za-z]*
The State

Try it online! Link includes test cases. Explanation:

(?<=^(([^.]*)\. +([^.]*)\. +)*[^ .][^.]*)

Only match in the middle of alternate sentences.

(?<!\([^)]*)

Don't match inside parentheses.

[A-Z][A-Za-z]*

Match words beginning with uppercase letters.

The State

Replace them as desired.

Neil

Posted 2018-11-14T03:49:48.427

Reputation: 95 035

3

Java 10, 164 bytes

s->{var S=s.split(".  ",-1);for(int l=S.length,i=0;i<l;)System.out.print((i%2<1?S[i].replaceAll("(?!^)[A-Z]\\w*(?<!\\([^)]*)","The State"):S[i])+(++i<l?".  ":""));}

Try it online.

Explanation:

s->{                       // Method with String as both parameter and return-type
  var S=s.split(".  ",-1); //  Split the input by ".  " to get the list of sentences
                           //  (the -1 is in case the input ends in a trailing ".  ")
  for(int l=S.length,      //  Amount of sentences
      i=0;i<l;)            //  Loop `i` in the range [0, amount_of_sentences):
    System.out.print(      //   Print (without newline):
     (i%2<1?               //    If `i` is even:
       S[i]                //     Print the sentence,
        .replaceAll(       //      After we've replaced all
          "(?!^)           //       (except the very first)
           [A-Z]\\w*       //       capitalized words,
           (?<!\\([^)]*)"  //       that are not within parenthesis
          "The State")     //      With "The State"
      :                    //    Else:
       S[i])               //     Print the sentence as is
            +(++i<l?       //    And if it's not the last iteration:
               ".  "       //     Append ".  "
              :            //    Else:
               ""));}      //     Append nothing more

Kevin Cruijssen

Posted 2018-11-14T03:49:48.427

Reputation: 67 575

2

Perl 6, 91 bytes

{S:g/\s<:Lu><-[.\s]>+<?{!grep {$/.prematch.comb($_)%2},'.  ',/\(|\)/,/\.\s$/}>/ The State/}

Try it online!

Regex based solution.

Explanation:

 S:g/                  # Do a global substitution
     \s<:Lu><-[.\s]>+  # On all words starting with capital letters
               <?{                                        }>  # Where:
                  !grep {                       } # None of:
                                                 ,'.  '   # Sentence terminators
                                                 ,/\(|\)/ # Parenthesises
                                                 ,/\.\s$/ # Is first word of sentence?
                                    .comb($_)%2   # Have an odd amount of matches
                         $/.prematch              # From the string before
                                                           / The State/  # And replace

Jo King

Posted 2018-11-14T03:49:48.427

Reputation: 38 234