+ Reply to Thread
Results 1 to 24 of 24

Brain Teaser for a Friday Afternoon

  1. #1
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Brain Teaser for a Friday Afternoon

    I was reading something last night that reminded me of the below puzzle which is more about problem solving that writing code.

    How long does it take you to do the following?

    Write some code to write the whole numbers from 1 to 100
    for the multiples of 3 instead of the number write "Duck"
    for the multiples of 5 instead of the number write "Sauce"
    for the multiples of 15 instead of the number write "DuckSauce"

    Writing to the immediate window is fine
    It's got to be less than 50 lines of code (you can't just write all the numbers to 100 )
    It isn't a trick question, but the crux of it is that you've got to try doing it on paper, rather than in the VBA editor - so you can't do loads of trial and error! Stretch your brain on a Friday!

    Pseudo code is fine too if you fancy having a go but your VBA isn't the strongest

    You may know this exercise by another name and have a solution, if so wait a while before posting your answer - give people a chance to have a go.

    Once people have had a go, we'll see if anyone has a really nifty solution

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Brain Teaser for a Friday Afternoon

    answer: not long ;-)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Brain Teaser for a Friday Afternoon

    under 5 minutes using 108 characters
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Brain Teaser for a Friday Afternoon



    I'm at 113 characters including spaces, time for a fiddle. Had either of you heard of this test before under its usual name? The article I was reading was by a recruiter who reckoned that 60% of programming candidates he interviewed failed the test given a ten minute allowance and a language of their choice - which I thought was pretty suprising

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Brain Teaser for a Friday Afternoon

    Excluding sub/end sub, 3 lines, 110 characters, about 10 minutes.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Brain Teaser for a Friday Afternoon

    No not heard of it before.

    I agree that does sound very high.
    If I was doing it with pen/paper under interview conditions I would have gone with the safe loop and if test approach.
    Being able to test in real time I used a loop and switch function.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Brain Teaser for a Friday Afternoon

    no never heard it before. haven't checked number of characters but it's 8 lines including sub and end sub lines and 2 variable declaration lines. pretty sure it's over 200 characters

    edit: make that 3 lines including sub and end sub if I output to a sheet instead of the immediate window
    Last edited by JosephP; 11-16-2012 at 10:33 AM.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Brain Teaser for a Friday Afternoon

    For i = 1 To 100
    Debug.Print Choose(1 - 2 * (i Mod 3 = 0) - (i Mod 5 = 0), i, "Sauce", "Duck", "DuckSauce")
    Next

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Brain Teaser for a Friday Afternoon

    That's genius

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Brain Teaser for a Friday Afternoon

    in the immediate window,

    Please Login or Register  to view this content.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Brain Teaser for a Friday Afternoon

    Mrs. Pope has a clever boy ...

    And he even squeezed in the control variable for the Next.
    Last edited by shg; 11-16-2012 at 10:57 AM.

  12. #12
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Brain Teaser for a Friday Afternoon

    For x = 1 To 100
    Debug.Print IIf(x Mod 3, "", "Duck") & IIf(x Mod 5, IIf(x Mod 3, x, ""), "Sauce")
    Next


    Which is 105, but I can't get any lower

    Must admit, If I was in an interview scenario, I'd go for a safer If test though

    Great solution Andy
    Last edited by Kyle123; 11-16-2012 at 10:47 AM.

  13. #13
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Brain Teaser for a Friday Afternoon

    Are you all purposely making your code hard to see?
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Brain Teaser for a Friday Afternoon

    Shows up fine in IE9 ...

  15. #15
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Brain Teaser for a Friday Afternoon

    Taking advantage of Andy's Immediate window trick

    For i=1 To 100:Debug.Print Choose(1-2*(i Mod 3=0)-(i Mod 5=0),i,"Sauce","Duck","DuckSauce"):Next i

  16. #16
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Brain Teaser for a Friday Afternoon

    being an excelian
    Please Login or Register  to view this content.

  17. #17
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Brain Teaser for a Friday Afternoon

    Nifty, is a cheeky language switch cheating?

    for(i=1;i<101;i++){console.log((i%3?'':'Duck')+(i%5?i%3?i:'':'Sauce'))}

    @joseph I reckon you should get a couple of bonus points for not using a loop
    Last edited by Kyle123; 11-16-2012 at 11:11 AM.

  18. #18
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Brain Teaser for a Friday Afternoon

    The test is actually called FizzBuzz on account that Fizz and Buzz are used in place of Duck and Sauce. It's apparently a pretty common interview technique for interviewers recruiting for programmers - I find it surprising that apparently such a high proportion of candidates fail the test. The blog I was reading stated 60%, other comments I've read state that it's not as high as that, but still a substantial amount fail.

  19. #19
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Brain Teaser for a Friday Afternoon


  20. #20
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Brain Teaser for a Friday Afternoon

    Great link

    Without a shadow of a doubt, this one is my favourite http://rosettacode.org/wiki/FizzBuzz#Chef

    Not to mention possibly one of the oddest looking languages I've ever seen!

  21. #21
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Brain Teaser for a Friday Afternoon

    That's pretty whimsical ...

  22. #22
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Brain Teaser for a Friday Afternoon

    I gave it a shot, here are the results:

    Time: 2min 28sec
    Lines (including sub/end sub): 19
    Characters (including sub/end sub and spaces): 421

    Code used (semi-hidden to allow others to try before viewing mine):
    Please Login or Register  to view this content.


    After I finished I took a look at the other solutions. Some really clever stuff, nice job guys!
    I just created a code I felt was safe and and accurate as quickly as I could.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  23. #23
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Brain Teaser for a Friday Afternoon

    OK I cheated and used a formula - cell A1 copied down

    =IFERROR(LOOKUP(2,1/(MOD(ROWS(A$1:A1),{3,5,15})=0),{"Duck","Sauce","DuckSauce"}),ROWS(A$1:A1))

    (94 characters)
    Audere est facere

  24. #24
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Brain Teaser for a Friday Afternoon

    you can cheet better than that:

    Formula: copy to clipboard
    Please Login or Register  to view this content.



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1