6
\$\begingroup\$

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.
\$\endgroup\$
19
  • \$\begingroup\$ 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. \$\endgroup\$
    – maxb
    Sep 21, 2018 at 6:55
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ Sep 21, 2018 at 6:58
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ Sep 21, 2018 at 7:50
  • 1
    \$\begingroup\$ Typo: output for 3 ends with "Yeah!" shouldn't it be "Yeah." ? \$\endgroup\$
    – JayCe
    Sep 21, 2018 at 13:44
  • 1
    \$\begingroup\$ @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 \$\endgroup\$
    – maxb
    Oct 4, 2018 at 4:32

6 Answers 6

5
\$\begingroup\$

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!

\$\endgroup\$
10
  • 2
    \$\begingroup\$ please give explanation why you downvote my answer so i can improve in another chance \$\endgroup\$
    – Vico
    Sep 21, 2018 at 9:05
  • 2
    \$\begingroup\$ 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. ;) ). \$\endgroup\$ Sep 21, 2018 at 9:16
  • \$\begingroup\$ 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! \$\endgroup\$ Sep 21, 2018 at 9:24
  • 2
    \$\begingroup\$ 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). \$\endgroup\$ Sep 21, 2018 at 9:28
  • \$\begingroup\$ thank you for your reply guys, i learn from my mistake \$\endgroup\$
    – Vico
    Sep 21, 2018 at 9:34
4
\$\begingroup\$

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

\$\endgroup\$
0
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

05AB1E,  44  42 34 31 bytes

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

[Ðd_#3‚ßÝ„¥æ€¸#sè'€³¶.øý'.«™,3-

Also prints the optional Yeah..

Try it online or verify all test cases.

Explanation:

[               # Loop indefinitely:
 Ð              #  Triplicate the top value
                #  (which is the implicit input `n` in the first iteration)
  d_            #  Pop one copy, and if it's negative:
    #           #   Stop the infinite loop
  3‚ß           #  Take the max `m` of another copy of `n` and 3
     Ý„¥æ€¸#sè  #  Create an alternating list of "yeah" and "no" of size `m`
  '€³¶.ø       '#  Push "\nbut\n"
        ý       #  Join the alternating list with this as delimiter
  '.«          '#  Append a trailing "."
     ™          #  Titlecase each word in this string
      ,         #  Pop and print it with trailing newline
  3-            #  Decrease `n` by 3 for the next iteration

See this 05AB1E tips of mine (section How to use the dictionary?) to understand why „¥æ€¸ is "yeah no" and '€³ is "but".

\$\endgroup\$
5
  • \$\begingroup\$ Your indents seem kinda broken :) Though maybe it's just me \$\endgroup\$ Sep 21, 2018 at 18:27
  • \$\begingroup\$ @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. \$\endgroup\$ Sep 21, 2018 at 19:32
  • \$\begingroup\$ imgur.com/a/x5vSNql I think that's not how it's supposed to look? \$\endgroup\$ Sep 21, 2018 at 19:42
  • \$\begingroup\$ Oh... I just read your comment all the way through and saw the link, sorry. ("ADHD much?" "Yeah") \$\endgroup\$ Sep 21, 2018 at 19:49
  • \$\begingroup\$ @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. ;) \$\endgroup\$ Sep 21, 2018 at 20:06
1
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ @Titus there is a new sentence. Check it out again and compare to the given test case \$\endgroup\$ Sep 24, 2018 at 22:56
  • \$\begingroup\$ Maybe I got the TiOs mixed up. Approved. \$\endgroup\$
    – Titus
    Sep 25, 2018 at 11:39
0
\$\begingroup\$

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!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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