1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Question Fizzbuzz Coding Challage

Discussion in 'Chit Chat' started by Cecil, Aug 2, 2017.

Tags:
  1. Cecil

    Cecil 17+ years progress programming and still learning.

    I saw a YouTube video the other day and I thought it was quite interesting about hiring programmers and asking them the write a simple piece of code.


    So today I thought I actually write the FizzBuzz game in the ABL and I came up with 3 alternatives which did the same thing.

    What alternative code could you come up with in creating the FizzBuzz game?

    1st
    Code (progress):
    1. define variable i         as integer   no-undo.
    2. define variable result as character no-undo.
    3.  
    4. do i = 1 to 100:
    5.        
    6.     if i MOD 3 ne 0 and i MOD 5 ne 0 then
    7.     do:
    8.         result = result + string(i) + '~r~n'.
    9.         next.
    10.     end.  
    11.        
    12.     if i MOD 3 eq 0 then          
    13.         result = result + 'Fizz'.
    14.  
    15.     if i MOD 5 eq 0 then          
    16.         result = result + 'Buzz'.
    17.        
    18.     result = result + '~r~n'.    
    19.    
    20. end.
    21.  
    22. message result
    23.     view-as alert-box info.
    2nd:
    Code (progress):
    1. define variable i         as integer   no-undo.
    2. define variable result as character no-undo.
    3.  
    4. do i = 1 to 100:
    5.        
    6.     case true:      
    7.         when (i MOD 3 eq 0 and i MOD 5 eq 0) then
    8.             result = result + 'FizzBuzz~r~n'.
    9.         when (i MOD 3 eq 0) then          
    10.             result = result + 'Fizz~r~n'.
    11.         when (i MOD 5 eq 0) then          
    12.             result = result + 'Buzz~r~n'.
    13.         otherwise          
    14.             result = result + string(i) + '~r~n'.
    15.     end case.      
    16.    
    17. end.
    18.  
    19. message result
    20.     view-as alert-box info.
    3rd (I think this is my most elegant one I wrote):

    Code (progress):
    1. define variable i         as integer   no-undo.
    2. define variable result as character no-undo.
    3.  
    4. do i = 1 to 100:
    5.  
    6.     assign
    7.         result = result + string(i) when (i MOD 3 ne 0 and i MOD 5 ne 0)
    8.         result = result + 'Fizz'    when (i MOD 3 eq 0)
    9.         result = result + 'Buzz'    when (i MOD 5 eq 0)
    10.         result = result + '~r~n'.  
    11.  
    12. end.
    13.  
    14. message result
    15.     view-as alert-box info.
     
    Last edited: Aug 2, 2017
  2.  
  3. Cecil

    Cecil 17+ years progress programming and still learning.

    Same as #2, but a little bit more normalized:

    Code (progress):
    1. define variable i         as integer   no-undo.
    2. define variable result as character no-undo.
    3.  
    4. &SCOPED-DEFINE fizz 3
    5. &SCOPED-DEFINE buzz 5
    6. &SCOPED-DEFINE fizzTxt Fizz
    7. &SCOPED-DEFINE buzzTxt Buzz
    8. &SCOPED-DEFINE newline chr(13) + chr(10)
    9.  
    10.  
    11. do i = 1 to 100:
    12.        
    13.     case true:      
    14.         when (i MOD {&fizz} eq 0 and i MOD {&buzz} eq 0) then
    15.             result = result + '{&fizzTxt}{&buzzTxt}' + {&newline}.
    16.         when (i MOD {&fizz} eq 0) then          
    17.             result = result + '{&fizzTxt}' + {&newline}.
    18.         when (i MOD {&buzz} eq 0) then          
    19.             result = result + '{&buzzTxt}'  + {&newline}.
    20.         otherwise          
    21.             result = result + string(i) + {&newline}.
    22.     end case.      
    23.    
    24. end.
    25.  
    26. message result
    27.     view-as alert-box info.
     
    Last edited: Aug 2, 2017
  4. Cringer

    Cringer ProgressTalk.com Moderator Staff Member

    For a bit of fun, with recursion:
    Code (progress):
    1.  
    2. define variable result as character   no-undo.
    3.  
    4. run FizzBuzz(1).
    5.  
    6. message result
    7.     view-as alert-box info buttons ok.
    8.  
    9. procedure FizzBuzz:
    10.     define input  parameter MyInt as integer     no-undo.
    11.  
    12.     if MyInt gt 100 then
    13.         return.
    14.  
    15.     case true:      
    16.         when (MyInt MOD 3 eq 0 and MyInt MOD 5 eq 0) then
    17.             result = result + 'FizzBuzz~r~n'.
    18.         when (MyInt MOD 3 eq 0) then          
    19.             result = result + 'Fizz~r~n'.
    20.         when (MyInt MOD 5 eq 0) then          
    21.             result = result + 'Buzz~r~n'.
    22.         otherwise          
    23.             result = result + string(MyInt) + '~r~n'.
    24.     end case.
    25.  
    26.     run FizzBuzz(MyInt + 1).
    27. end.
    28.  
     
    Cecil likes this.
  5. Cringer

    Cringer ProgressTalk.com Moderator Staff Member

    A variation on your #3 because I like the fact you can do this in Progress.
    Code (progress):
    1.  
    2. define variable i         as integer   no-undo.
    3. define variable result as character no-undo.
    4.  
    5. do i = 1 to 100:
    6.  
    7.     assign
    8.         result = result + string(i) when (i MOD 3 ne 0 and i MOD 5 ne 0)
    9.         result = result + trim(string((i MOD 3 eq 0),'Fizz/'))
    10.         result = result + trim(string((i MOD 5 eq 0),'Buzz/'))
    11.         result = result + '~r~n'.  
    12.  
    13. end.
    14.  
    15. message result
    16.     view-as alert-box info.
    17.  
     
    Cecil likes this.

Share This Page