do you fail at FizzBuzz? simple prog test

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • globalrev

    do you fail at FizzBuzz? simple prog test



    claims people take a lot of time to write a simple program like this:


    "Write a program that prints the numbers from 1 to 100. But for
    multiples of three print "Fizz" instead of the number and for the
    multiples of five print "Buzz". For numbers which are multiples of
    both three and five print "FizzBuzz".

    for i in range(1,101):
    if i%3 == 0 and i%5 != 0:
    print "Fizz"
    elif i%5 == 0 and i%3 != 0:
    print "Buzz"
    elif i%5 == 0 and i%3 == 0:
    print "FizzBuzz"
    else:
    print i


    is there a better way than my solution? is mine ok?
  • John Machin

    #2
    Re: do you fail at FizzBuzz? simple prog test

    globalrev wrote:

    >
    claims people take a lot of time to write a simple program like this:
    >
    >
    "Write a program that prints the numbers from 1 to 100. But for
    multiples of three print "Fizz" instead of the number and for the
    multiples of five print "Buzz". For numbers which are multiples of
    both three and five print "FizzBuzz".
    >
    for i in range(1,101):
    if i%3 == 0 and i%5 != 0:
    print "Fizz"
    elif i%5 == 0 and i%3 != 0:
    print "Buzz"
    elif i%5 == 0 and i%3 == 0:
    print "FizzBuzz"
    else:
    print i
    >
    >
    is there a better way than my solution? is mine ok?
    Try doing it using %3 and %5 only once each.

    Comment

    • Kam-Hung Soh

      #3
      Re: do you fail at FizzBuzz? simple prog test

      On Sun, 11 May 2008 11:12:37 +1000, globalrev <skanemupp@yaho o.sewrote:

      >
      claims people take a lot of time to write a simple program like this:
      >
      >
      "Write a program that prints the numbers from 1 to 100. But for
      multiples of three print "Fizz" instead of the number and for the
      multiples of five print "Buzz". For numbers which are multiples of
      both three and five print "FizzBuzz".
      >
      for i in range(1,101):
      if i%3 == 0 and i%5 != 0:
      print "Fizz"
      elif i%5 == 0 and i%3 != 0:
      print "Buzz"
      elif i%5 == 0 and i%3 == 0:
      print "FizzBuzz"
      else:
      print i
      >
      >
      is there a better way than my solution? is mine ok?
      Looks OK to me.

      A different version, and I test for multiples of 3 and 5 first:

      map(lambda x: (not x%3 and not x%5 and "FizzBuzz") or (not x%3 and "Fizz")
      or (not x%5 and "Buzz") or x, xrange(1,101))

      --
      Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

      Comment

      • Mensanator

        #4
        Re: do you fail at FizzBuzz? simple prog test

        On May 10, 8:12�pm, globalrev <skanem...@yaho o.sewrote:

        >
        claims people take a lot of time to write a simple program like this:
        >
        "Write a program that prints the numbers from 1 to 100. But for
        multiples of three print "Fizz" instead of the number and for the
        multiples of five print "Buzz". For numbers which are multiples of
        both three and five print "FizzBuzz".
        >
        for i in range(1,101):
        � � if i%3 == 0 and i%5 != 0:
        � � � � print "Fizz"
        � � elif i%5 == 0 and i%3 != 0:
        � � � � print "Buzz"
        � � elif i%5 == 0 and i%3 == 0:
        � � � � print "FizzBuzz"
        � � else:
        � � � � print i
        >
        is there a better way than my solution? is mine ok?
        Define better.
        >>f = ['','','Fizz']*100
        >>b = ['','','','','Bu zz']*100
        >>for i in xrange(1,100):
        fb = f[i-1]+b[i-1]
        if fb=='':
        print i
        else:
        print fb

        Comment

        • Grant Edwards

          #5
          Re: do you fail at FizzBuzz? simple prog test

          On 2008-05-11, John Machin <sjmachin@lexic on.netwrote:
          >"Write a program that prints the numbers from 1 to 100. But for
          >multiples of three print "Fizz" instead of the number and for the
          >multiples of five print "Buzz". For numbers which are multiples of
          >both three and five print "FizzBuzz".
          >>
          >for i in range(1,101):
          > if i%3 == 0 and i%5 != 0:
          > print "Fizz"
          > elif i%5 == 0 and i%3 != 0:
          > print "Buzz"
          > elif i%5 == 0 and i%3 == 0:
          > print "FizzBuzz"
          > else:
          > print i
          >>
          >>
          >is there a better way than my solution? is mine ok?
          >
          Try doing it using %3 and %5 only once each.
          for i in xrange(101):
          print (("","Fizz")[i%3==0] + ("","Buzz")[i%5==0]) or str(i)

          His is better though, since it's more obvious what's intended.

          Here's one that's less opaque

          for i in xrange(101):
          s = ""
          if i%3 == 0: s += "Fizz"
          if i%5 == 0: s += "Buzz"
          if s:
          print s
          else:
          print i

          There are dozens of other ways to do it.
          --
          Grant Edwards grante Yow! ... or were you
          at driving the PONTIAC that
          visi.com HONKED at me in MIAMI last
          Tuesday?

          Comment

          • Ivan Illarionov

            #6
            Re: do you fail at FizzBuzz? simple prog test

            On Sat, 10 May 2008 18:12:37 -0700, globalrev wrote:

            >
            claims people take a lot of time to write a simple program like this:
            >
            >
            "Write a program that prints the numbers from 1 to 100. But for
            multiples of three print "Fizz" instead of the number and for the
            multiples of five print "Buzz". For numbers which are multiples of both
            three and five print "FizzBuzz".
            >
            for i in range(1,101):
            if i%3 == 0 and i%5 != 0:
            print "Fizz"
            elif i%5 == 0 and i%3 != 0:
            print "Buzz"
            elif i%5 == 0 and i%3 == 0:
            print "FizzBuzz"
            else:
            print i
            >
            >
            is there a better way than my solution? is mine ok?
            ['%s%s' % (not i%3 and 'Fizz' or '', not i%5 and 'Buzz' or '')
            or str(i) for i in xrange(1, 101)]

            -- Ivan

            Comment

            • Ivan Illarionov

              #7
              Re: do you fail at FizzBuzz? simple prog test

              On Sun, 11 May 2008 04:26:10 +0000, Ivan Illarionov wrote:
              On Sat, 10 May 2008 18:12:37 -0700, globalrev wrote:
              >
              >http://reddit.com/r/programming/info/18td4/comments
              >>
              >claims people take a lot of time to write a simple program like this:
              >>
              >>
              >"Write a program that prints the numbers from 1 to 100. But for
              >multiples of three print "Fizz" instead of the number and for the
              >multiples of five print "Buzz". For numbers which are multiples of both
              >three and five print "FizzBuzz".
              >>
              >for i in range(1,101):
              > if i%3 == 0 and i%5 != 0:
              > print "Fizz"
              > elif i%5 == 0 and i%3 != 0:
              > print "Buzz"
              > elif i%5 == 0 and i%3 == 0:
              > print "FizzBuzz"
              > else:
              > print i
              >>
              >>
              >is there a better way than my solution? is mine ok?
              >
              ['%s%s' % (not i%3 and 'Fizz' or '', not i%5 and 'Buzz' or '')
              or str(i) for i in xrange(1, 101)]
              >
              -- Ivan
              or, more correctly, if you actually need to "print":

              sys.stdout.writ e('\n'.join('%s %s' %
              (not i%3 and 'Fizz' or '', not i%5 aBuzz' or '')
              or str(i)
              for i in xrange(1, 101)))

              -- Ivan

              Comment

              • John Machin

                #8
                Re: do you fail at FizzBuzz? simple prog test

                On May 11, 1:24 pm, Mensanator <mensana...@aol .comwrote:
                On May 10, 8:12�pm, globalrev <skanem...@yaho o.sewrote:
                >
                >
                >>
                claims people take a lot of time to write a simple program like this:
                >
                "Write a program that prints the numbers from 1 to 100. But for
                multiples of three print "Fizz" instead of the number and for the
                multiples of five print "Buzz". For numbers which are multiples of
                both three and five print "FizzBuzz".
                >
                for i in range(1,101):
                � � if i%3 == 0 and i%5 != 0:
                � � � � print "Fizz"
                � � elif i%5 == 0 and i%3 != 0:
                � � � � print "Buzz"
                � � elif i%5 == 0 and i%3 == 0:
                � � � � print "FizzBuzz"
                � � else:
                � � � � print i
                >
                is there a better way than my solution? is mine ok?
                >
                Define better.
                >
                >f = ['','','Fizz']*100
                >b = ['','','','','Bu zz']*100
                >for i in xrange(1,100):
                >
                fb = f[i-1]+b[i-1]
                if fb=='':
                print i
                else:
                print fb
                You seem to have an unfortunate fixation on 100. Consider changing the
                above instances to 34, 20, and 101.

                Comment

                • Mensanator

                  #9
                  Re: do you fail at FizzBuzz? simple prog test

                  On May 11, 12:04 am, John Machin <sjmac...@lexic on.netwrote:
                  On May 11, 1:24 pm, Mensanator <mensana...@aol .comwrote:
                  >
                  >
                  >
                  >
                  >
                  On May 10, 8:12�pm, globalrev <skanem...@yaho o.sewrote:
                  >>
                  claims people take a lot of time to write a simple program like this:
                  >
                  "Write a program that prints the numbers from 1 to 100. But for
                  multiples of three print "Fizz" instead of the number and for the
                  multiples of five print "Buzz". For numbers which are multiples of
                  both three and five print "FizzBuzz".
                  >
                  for i in range(1,101):
                  � � if i%3 == 0 and i%5 != 0:
                  � � � � print "Fizz"
                  � � elif i%5 == 0 and i%3 != 0:
                  � � � � print "Buzz"
                  � � elif i%5 == 0 and i%3 == 0:
                  � � � � print "FizzBuzz"
                  � � else:
                  � � � � print i
                  >
                  is there a better way than my solution? is mine ok?
                  >
                  Define better.
                  >
                  >>f = ['','','Fizz']*100
                  >>b = ['','','','','Bu zz']*100
                  >>for i in xrange(1,100):
                  >
                          fb = f[i-1]+b[i-1]
                          if fb=='':
                                  print i
                          else:
                                  print fb
                  >
                  You seem to have an unfortunate fixation on 100. Consider changing the
                  above instances to 34, 20, and 101.
                  Ok, I agree with 101, but I wouldn't necessarily
                  say the others were unfortunate. You might be
                  surprised at how often such fixations discover
                  bugs, something that I have a gift for.

                  Comment

                  • Gabriel Genellina

                    #10
                    Re: do you fail at FizzBuzz? simple prog test

                    En Sat, 10 May 2008 22:12:37 -0300, globalrev <skanemupp@yaho o.seescribió:

                    >
                    claims people take a lot of time to write a simple program like this:
                    >
                    >
                    "Write a program that prints the numbers from 1 to 100. But for
                    multiples of three print "Fizz" instead of the number and for the
                    multiples of five print "Buzz". For numbers which are multiples of
                    both three and five print "FizzBuzz".
                    >
                    for i in range(1,101):
                    if i%3 == 0 and i%5 != 0:
                    print "Fizz"
                    elif i%5 == 0 and i%3 != 0:
                    print "Buzz"
                    elif i%5 == 0 and i%3 == 0:
                    print "FizzBuzz"
                    else:
                    print i
                    >
                    >
                    is there a better way than my solution? is mine ok?
                    Is it correct? Did you get at it in less than 15 minutes? If so, then it's OK.
                    The original test was not "write the most convoluted algorithm you can think of", nor "write the best program to solve this". It was a *practical* test: if you can't get anything remotely working for such a simple problem in 15 minutes, we're not interested in your services.

                    (We used this question last year - some people gave a sensible answer in less than 5 minutes, but others did not even know how to start)

                    --
                    Gabriel Genellina

                    Comment

                    • bockman@virgilio.it

                      #11
                      Re: do you fail at FizzBuzz? simple prog test

                      On 12 Mag, 09:00, "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote:
                      En Sat, 10 May 2008 22:12:37 -0300, globalrev <skanem...@yaho o.seescribió:
                      >
                      >
                      >
                      >
                      >>
                      claims people take a lot of time to write a simple program like this:
                      >
                      "Write a program that prints the numbers from 1 to 100. But for
                      multiples of three print "Fizz" instead of the number and for the
                      multiples of five print "Buzz". For numbers which are multiples of
                      both three and five print "FizzBuzz".
                      >
                      for i in range(1,101):
                          if i%3 == 0 and i%5 != 0:
                              print "Fizz"
                          elif i%5 == 0 and i%3 != 0:
                              print "Buzz"
                          elif i%5 == 0 and i%3 == 0:
                              print "FizzBuzz"
                          else:
                              print i
                      >
                      is there a better way than my solution? is mine ok?
                      >
                      Is it correct? Did you get at it in less than 15 minutes? If so, then it'sOK.
                      The original test was not "write the most convoluted algorithm you can think of", nor "write the best program to solve this". It was a *practical* test: if you can't get anything remotely working for such a simple problem in 15 minutes, we're not interested in your services.
                      >
                      (We used this question last year - some people gave a sensible answer in less than 5 minutes, but others did not even know how to start)
                      >
                      --
                      Gabriel Genellina- Nascondi testo tra virgolette -
                      >
                      - Mostra testo tra virgolette -
                      As a test, I would leave out the last sentence, and see how many
                      people (and how fast) figure out than a number can be multiple of
                      three _and_ five and that the requirement is somehow incomplete ...

                      Ciao
                      -----
                      FB

                      Comment

                      • Arnaud Delobelle

                        #12
                        Re: do you fail at FizzBuzz? simple prog test

                        On May 11, 4:36 am, Grant Edwards <gra...@visi.co mwrote:
                        On 2008-05-11, John Machin <sjmac...@lexic on.netwrote:
                        >
                        >
                        >
                        "Write a program that prints the numbers from 1 to 100. But for
                        multiples of three print "Fizz" instead of the number and for the
                        multiples of five print "Buzz". For numbers which are multiples of
                        both three and five print "FizzBuzz".
                        >
                        for i in range(1,101):
                            if i%3 == 0 and i%5 != 0:
                                print "Fizz"
                            elif i%5 == 0 and i%3 != 0:
                                print "Buzz"
                            elif i%5 == 0 and i%3 == 0:
                                print "FizzBuzz"
                            else:
                                print i
                        >
                        is there a better way than my solution? is mine ok?
                        >
                        Try doing it using %3 and %5 only once each.
                        >
                        for i in xrange(101):
                            print (("","Fizz")[i%3==0] + ("","Buzz")[i%5==0]) or str(i)
                        >
                        His is better though, since it's more obvious what's intended.
                        >
                        Here's one that's less opaque
                        >
                        for i in xrange(101):
                            s = ""
                            if i%3 == 0: s += "Fizz"
                            if i%5 == 0: s += "Buzz"
                            if s:
                                print s
                            else:
                                print i
                        >
                        Let's not forget to generalise the problem and code it OOP-style :)

                        class FizzBuzzer(obje ct):
                        def __init__(self, *fizzles):
                        self.fizzles = fizzles
                        def translate(self, n):
                        return ''.join(val for (p, val) in self.fizzles if not n%p) or
                        n
                        def range(self, start, stop=None, step=None):
                        if stop is None:
                        start, stop = 0, start
                        if step is None:
                        step = 1
                        for n in xrange(start, stop, step):
                        yield self.translate( n)
                        def __getitem__(sel f, obj):
                        if isinstance(obj, slice):
                        return self.range(obj. start, obj.stop, obj.step)
                        else:
                        return self.translate( obj)

                        # FizzBuzzer in action:
                        >>fizzbuzz = FizzBuzzer((3, 'Fizz'), (5, 'Buzz'))
                        >>for val in fizzbuzz[1:21]:
                        ... print val
                        ...
                        1 21 1
                        1
                        2
                        Fizz
                        4
                        Buzz
                        Fizz
                        7
                        8
                        Fizz
                        Buzz
                        11
                        Fizz
                        13
                        14
                        FizzBuzz
                        16
                        17
                        Fizz
                        19
                        Buzz
                        >>abc = FizzBuzzer((2, 'Ah'), (3, 'Bee'), (5, 'Cee'))
                        >>list(abc[25:35])
                        25 35 1
                        ['Cee', 'Ah', 'Bee', 'Ah', 29, 'AhBeeCee', 31, 'Ah', 'Bee', 'Ah']
                        >>>
                        --
                        Arnaud

                        Comment

                        • Arnaud Delobelle

                          #13
                          Re: do you fail at FizzBuzz? simple prog test

                          On May 12, 9:30 am, Arnaud Delobelle <arno...@google mail.comwrote:
                          [...]
                          # FizzBuzzer in action:
                          >
                          >fizzbuzz = FizzBuzzer((3, 'Fizz'), (5, 'Buzz'))
                          >for val in fizzbuzz[1:21]:
                          >
                          ...     print val
                          ...
                          1 21 1
                          ^^^^^^^^
                          Ignore this, it's debugging output
                          1
                          2
                          Fizz
                          4
                          Buzz
                          Fizz
                          7
                          8
                          Fizz
                          Buzz
                          11
                          Fizz
                          13
                          14
                          FizzBuzz
                          16
                          17
                          Fizz
                          19
                          Buzz>>abc = FizzBuzzer((2, 'Ah'), (3, 'Bee'), (5, 'Cee'))
                          >list(abc[25:35])
                          >
                          25 35 1
                          ^^^^^^^^^
                          Same
                          ['Cee', 'Ah', 'Bee', 'Ah', 29, 'AhBeeCee', 31, 'Ah', 'Bee', 'Ah']
                          --
                          Arnaud

                          Comment

                          • Chris

                            #14
                            Re: do you fail at FizzBuzz? simple prog test

                            On May 11, 3:12 am, globalrev <skanem...@yaho o.sewrote:

                            >
                            claims people take a lot of time to write a simple program like this:
                            >
                            "Write a program that prints the numbers from 1 to 100. But for
                            multiples of three print "Fizz" instead of the number and for the
                            multiples of five print "Buzz". For numbers which are multiples of
                            both three and five print "FizzBuzz".
                            >
                            for i in range(1,101):
                                if i%3 == 0 and i%5 != 0:
                                    print "Fizz"
                                elif i%5 == 0 and i%3 != 0:
                                    print "Buzz"
                                elif i%5 == 0 and i%3 == 0:
                                    print "FizzBuzz"
                                else:
                                    print i
                            >
                            is there a better way than my solution? is mine ok?
                            personally if you're just checking if a modulus result is 0 or not I
                            would rather do as it looks neat imho.

                            for i in xrange(1,101):
                            if not i % 3 and i % 5:
                            print 'Fizz'
                            elif i % 3 and not i % 5:
                            print 'Buzz'
                            elif not i % 3 and not i % 5:
                            print 'FizzBuzz'
                            else:
                            print i

                            Comment

                            • Duncan Booth

                              #15
                              Re: do you fail at FizzBuzz? simple prog test

                              Ivan Illarionov <ivan.illariono v@gmail.comwrot e:
                              >>is there a better way than my solution? is mine ok?
                              >>
                              >['%s%s' % (not i%3 and 'Fizz' or '', not i%5 and 'Buzz' or '')
                              > or str(i) for i in xrange(1, 101)]
                              >>
                              >-- Ivan
                              >
                              or, more correctly, if you actually need to "print":
                              >
                              sys.stdout.writ e('\n'.join('%s %s' %
                              (not i%3 and 'Fizz' or '', not i%5 aBuzz' or '')
                              or str(i)
                              for i in xrange(1, 101)))
                              I think the variant I came up with is a bit clearer:

                              for i in range(1,101):
                              print '%s%s' % ('' if i%3 else 'Fizz', '' if i%5 else 'Buzz') or i

                              Comment

                              Working...