Help golfing this solution further We are doing a PCG challenge at work, and I'm submitting answers in C#.  This is a modification of the "M'Kay" challenge.  My solution has been deemed correct by the requirements, I'm just trying to shrink it further if possible.


  A filler word is an apparently meaningless word, phrase, or sound that marks a pause or hesitation in speech.
  
  Your task is to create a function that has a 30%-50% chance of adding a random filler word to a block of text after the punctuation marks , ; . ? and !; and when added is followed by the same punctuation. The same filler word must be used for each replacement within a single execution of the function.
  
  If the filler word occurs after a sentence completing punctuation mark such as . ? or ! the filler word must have the first letter capitalized.
  If the filler word occurs after a sentence pausing punctuation mark such as , or ; the filler word must be in lowercase.
  
  Fillers: um, literally, you know, er, okay, right, ah, like


My submitted code:

string p(string i,int z=-1){string w="Um|Literally|You know|Er|Okay|Right|Ah|Like".Split('|')[i.Length %8],s="?,!;.",o="";foreach(var c in i){var x=s.IndexOf(c);o+=(z&gt;0&amp;&amp;i[z]&gt;'9'&amp;&amp;x&gt;-1&amp;&amp;(1&amp;z)&lt;1?c+" "+(x%2&gt;0?" "+w.ToLower():w):"")+c;z++;}return o;}



  Original input text: We heard tales of Americans like Coast Guard Petty Officer Ashlee Leppert, who is here tonight in the gallery with Melania.  Ashlee was aboard one of the first helicopters on the scene in Houston during Hurricane Harvey.  Through 18 hours of wind and rain, Ashlee braved live power lines and deep water, to help save more than 40 lives.  Thank you, Ashlee.
  
  With fillers added: We heard tales of Americans like Coast Guard Petty Officer Ashlee Leppert, okay, who is here tonight in the gallery with Melania.  Ashlee was aboard one of the first helicopters on the scene in Houston during Hurricane Harvey. Okay.  Through 18 hours of wind and rain, Ashlee braved live power lines and deep water, okay, to help save more than 40 lives.  Thank you, Ashlee.


Clarification comments welcome, I don't know what else I need to include.
string ... s="?,!;." ... var x=s.IndexOf(c);



s is used only once, so could be inlined.




var x=s.IndexOf(c); ... &amp;&amp;x&gt;-1&amp;&amp; ... c+" "+(x%2&gt;0?" "+w.ToLower():w):"")+c;



Change "?,!;." to ",;?!." and the test x%2&gt;0 can be simplified to x&lt;2.

Where in the spec does it require double-space after sentence-terminating punctuation? IMO you could lose the " "+ inside the parentheses.



Given the IMO ridiculous interpretation which they're allowing of "random", I don't see why you do such complicated stuff with z. Just use the character index modulo 3. A direct change to

string p(string i){int n=i.Length,j,x;string w="Um|Literally|You know|Er|Okay|Right|Ah|Like".Split('|')[n%8],o="";for(j=0;j&lt;n;j++){var c=i[j];x=",;.?!".IndexOf(c);o+=(x&gt;-1&amp;&amp;j%3&lt;1?c+" "+(x&lt;2?" "+w.ToLower():w):"")+c;}return o;}


gives a further nice saving. An alternative using Linq also saves a lot by exploiting fat arrow syntax, and would be cheaper if they give you free using statements:

using System.Linq;string p(string s)=&gt;string.Join("",s.Select((c,i)=&gt;{var w="Um|Literally|You know|Er|Okay|Right|Ah|Like".Split('|')[s.Length%8];var x=",;.?!".IndexOf(c);return(x&gt;=0&amp;&amp;i%3&lt;1?c+" "+(x&lt;2?" "+w.ToLower():w):"")+c;}));