This challenge was inspired by Yeah But No But Yeah.


maxb´s colleague recently sent him the following piece of JavaScript as a joke:

let butScript = (nrOfButs) => {
    for(var i = 0; i < nrOfButs; i++){
        if(i % 3 == 0){
            console.log("Yeah")
        }
        if(i % 2 == 0){
            console.log("But")
        }
        if(i % 3 == 1){
            console.log("No")
        }
    }
}

Since the code is not only overly long but also incorrect, I´ll suggest to maxb´s boss to fire that guy and hire one of you folks instead.

I can´t guarantee anything; but I reckon there´ll be a decent wage, health insurance and possibly a company car in it. All You have to do is A) correct the code, so the Number of Buts printed is the same as asked in the parameter B) correct the grammar. (see below) C) make it as short as possible.

He might as well block my calls after that because he doesn´t like pennypickers; but who knows.

Input

a non-negative integer, in any convenient way

Output

Yeah and No, mostly separated by Buts. The code above and the examples below should clarify the pattern.
Output should never end in a But. And a sentence should always end with a stop (.) or exclamation mark; i.e. put one after every No that is directly followed by a Yeah (without a But) and one at the end of the output.
A single trailing "Yeah." may be printed (if you want to think positive), but that´s not a must.

You may print the output or return a list of strings.
Trailing blanks are acceptable, as well as a single leading or trailing newline.

examples (input => possible output)

input 0:

Yeah.

input 1:

Yeah
But
No.

input 2:

Yeah
But
No
But
Yeah.

input 3:

Yeah
But
No
But
Yeah
But
No.
Yeah!       <optional>

...

input 5:

Yeah
But
No
But
Yeah
But
No.
Yeah
But
No
But
Yeah.
  • These programs should be able to be a bit shorter, though the period could add 2-3 bytes depending on the language. I'll have to create my own solution in MathGolf, but I don't think I'll be able to beat Jelly and 05AB1E for this one. – maxb Sep 21 at 6:55
  • 1
    So if I understand correctly: 1. A dot should be added between every No Yeah or Yeah No. 2. A dot should be added at the very end. 3. A single line only containing Yeah or No should be removed. 4. When a sentence ends with a But the But should be removed? (not sure about this fourth one) – Kevin Cruijssen Sep 21 at 6:58
  • 5
    Hmm, shouldn't the output for n=0 be empty?.. It won't go into the for(var i = 0; i < nrOfButs; ... loop, so won't print anything there. And there is also the rule "A single trailing "Yeah." should be avoided." to consider. There are a bit too many unanswered questions, so I've voted to close as unclear for now.. Might change after some clarification and better (bullet-point) specified rules or step-by-step examples of the given test cases. – Kevin Cruijssen Sep 21 at 7:50
  • 1
    Typo: output for 3 ends with "Yeah!" shouldn't it be "Yeah." ? – JayCe Sep 21 at 13:44
  • 1
    @recursive I left another comment stating that I'd misunderstood the challenge. From my understanding, the order of the words should be the same as for my original challenge. However, the input dictates how many times "but" is present in the output. The output never ends in "but", and there needs to be a period between "yeah" and "no" if there's no "but" in-between – maxb Oct 4 at 4:32

JavaScript (Node.js), 80 bytes

a=>{s='';for(i=0;i<a+1;i++)s+=(i%2==0?'yeah':'no')+(i<a?'\nbut\n':'.');return s}

Try it online!

  • 2
    please give explanation why you downvote my answer so i can improve in another chance – Vico Sep 21 at 9:05
  • 2
    Hi, welcome to PPCG! I'm not the one who downvoted, but I completely agree with your comment above that people should leave an explanation of why they downvote!.. Anyway, I think one reason is because the i%2==0 check isn't really correct in comparison to the code in the challenge description. I admit that the challenge description is a bit vague tbh (as I mentioned in the comments below it). I can only guess here, but the downvote is most likely due to a different than expected output. (PS: And the yeah, no, but should be Titlecase instead of lowercase, but that's an easy fix. ;) ). – Kevin Cruijssen Sep 21 at 9:16
  • Some more general tips not related to this challenge: You might want to add a Try-it-online link with test code for your answers. In this case this could be a possible TIO-link. In addition, Tips for golfing in JavaScript and Tips for golfing in <all langauges> might be interesting to read through; I see a few parts you can golf in your current answer. :) Don't let this downvote discourage you, and enjoy your stay! – Kevin Cruijssen Sep 21 at 9:24
  • 2
    RE: the downvote: The post automatically went onto the low quality post queue due to it's length and content (a post from a new user with only a code-block will trigger this). Then when it is edited to actually fix that a downvote is automatically applied - the community here views this "feature" as a bug (it's not a very friendly welcome!). Please ignore it; check the format of the edited post too (I've also added a language tag for the code-block to add syntax highlighting). – Jonathan Allan Sep 21 at 9:28
  • thank you for your reply guys, i learn from my mistake – Vico Sep 21 at 9:34

Python 2, 105 81 bytes

f=lambda n,s=1:(n|s)*' 'and'\nBut\n'.join((['Yeah','No']*2)[:n+1])+'.\n'+f(n-3,0)

Try it online!

- 24 bytes, thanks to ovs

  • @ovs, thanks a lot – TFeld Sep 22 at 8:18

Retina, 48 47 bytes

.+
*
L$`__?_?|^
Yeah$&.
__
_¶But¶Yeah
_
¶But¶No

Try it online! Explanation:

.+
*

Convert to unary.

L$`__?_?|^
Yeah$&.

Match up to three _s at a time. Also handle the zero edge case as a match. Each match results in at least a Yeah with a trailing ..

__
_¶But¶Yeah

A match of 2 or 3 _s results in a But Yeah in the second position.

_
¶But¶No

The other _s turn into But No.

52 bytes in Retina 0.8.2:

.+
$*
M!`11?1?
%`^.*
Yeah$&.
11
1¶But¶Yeah
1
¶But¶No

Try it online! Explanation:

.+
$*

Convert to unary.

M!`11?1?

Match up to three 1s at a time.

%`^.*
Yeah$&.

Add a leading Yeah and trailing . for each match. The ^.* allows it to work for the zero case too.

11
1¶But¶Yeah
1
¶But¶No

Add in the But Yeah and/or No as above.

05AB1E,  44  42 34 bytes

Crossed out &nbsp;44&nbsp; is no longer regular 44 :)

[Dd_#©3‚ßÝ”¥æ€¸”#s蔀³”¶.øý'.«,®3-

Also prints the optional Yeah..

Try it online or verify all test cases.

Explanation:

                   # Take the (implicit) input `n`
[                  # Start an infinite loop
 Dd_#              #  If `n` is negative: stop the infinite loop
 ©                 #  Temporary store `n` (without popping)
  3‚ß              #  Take the max `m` of `n` and 3
     Ý”¥æ€¸”#sè    #  Create an alternating list of "Yeah" and "No" of size `m`
  ”€³”¶.ø          #  Push "\nBut\n"
         ý         #  Join the alternating list above with it
  '.«              #  Append a trailing "."
  ,                #  Print everything to STDOUT with a trailing newline
 ®3-               #  Decrease `n` by 3

Additional explanation why ”¥æ.” is "Yeah.", ”¥æ€¸” is "Yeah No", and ”€³” is "But".


Could have been 33 bytes by changing Ý”¥æ€¸”#s蔀³”¶.øý'.« to Ý„¥æ€¸#sè'€³¶.øý'.«™, but unfortunately is currently bugged for multi-line words in the Elixir rewrite of 05AB1E. Try Ý„¥æ€¸#sè'€³¶.øý'.«™ in the Python legacy version as prove that it works. (NOTE: I cannot switch the entire answer to the legacy version even if I change the new functionality d_ to 0‹, because apparently ß is bugged in the legacy version giving an "'<' not supported between instances of 'str' and 'int'" error for [2,'3'].. >.>)

  • Your indents seem kinda broken :) Though maybe it's just me – seadoggie01 Sep 21 at 18:27
  • @seadoggie01 Hmm, do you have a screenshot perhaps? For me it looks like this. Although I do see messed up indentations with APL answers a lot, so I can imagine it happens with 05AB1E as well when using a font with different widths. – Kevin Cruijssen Sep 21 at 19:32
  • imgur.com/a/x5vSNql I think that's not how it's supposed to look? – seadoggie01 Sep 21 at 19:42
  • Oh... I just read your comment all the way through and saw the link, sorry. ("ADHD much?" "Yeah") – seadoggie01 Sep 21 at 19:49
  • @seadoggie01 The indentation is intended this time. It's too long to put all after one-another, and this seems kinda logical to me. The highlighting color is due to the ' (single quote) unfortunately. I could remove the highlighting completely if that's better, but then everything including the comments is just black. I wish there was a hightlighting specific for 05AB1E, but unfortunately I have to use the Python one, which doesn't work very well when I use single quotes, as you can see. ;) – Kevin Cruijssen Sep 21 at 20:06

JavaScript (Node.js), 82 bytes

_=>_>3?f(3)+f(_-3):[...Array(_+1)].map((a,i)=>i%2?'No':'Yeah').join`\nBut\n`+'.\n'

Try it online!

  • @Titus there is a new sentence. Check it out again and compare to the given test case – Luis felipe De jesus Munoz Sep 24 at 22:56
  • Maybe I got the TiOs mixed up. Approved. – Titus Sep 25 at 11:39

C (clang), 80 74 bytes

f(a,i){for(i=0;-i>~a;)printf("%s%s\n","YeahNo"+i%2*5,i++<a?"\nBut":".");}

-6 bytes thanks to @Jonathan Frech

Try it online!

  • 74 bytes. – Jonathan Frech Sep 23 at 2:55
  • Not working for n=5 case. – cleblanc Sep 24 at 14:35
  • Incorrect for N>3. There should be a new sentence after every "Yeah But No But Yeah But No." – Titus Sep 24 at 21:03

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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