Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 02 Sep 2014, 10:16
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
FizzBuzz
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 3 [32 Posts]   Goto page: 1, 2, 3 Next
Author Message
GustavoYz


Joined: 07 Jul 2010
Posts: 896
Location: .ar

PostPosted: Sun 18 Nov 2012, 01:32    Post subject:  FizzBuzz
Subject description: Seeking a shorter Bash solution
 

I surprised myself reading this.
Is likely a very easy challenge, but effective somehow. Shocked

Im wondering if there is a bash solution (one line, not a script) shorter than this mess I just did Laughing :
Code:
for nn in `seq 1 100`;  do { [ $(( $nn % 15 )) == 0 ] && echo "FizzBuzz"; } || { [ $(( $nn % 5 )) == 0 ] && echo "Buzz"; } || { [ $(( $nn % 3 )) == 0 ] && echo "Fizz"; } || ( echo "$nn") ; done  | column


Although is like "cheat" on this context, my Perl solution end up being (way) shorter:
Code:
perl -E 'say $_ % 15 ? $_ % 3 ? $_ % 5 ? "$_" : "Buzz" : "Fizz" : "Fizzbuzz" for 1 .. 100 ' | column

(note that both commands are piped to `column` for easy reading, not essential).

Question As I know that there is a lot of Bash wisdom surrounding, I'm wondering, are there better/shorter(/prettier) Bash ways?

_________________

Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 2134
Location: The Blue Marble

PostPosted: Sun 18 Nov 2012, 05:04    Post subject: Re: FizzBuzz
Subject description: Seeking a shoter Bash solution
 

GustavoYz wrote:
Im wondering if there is a bash solution (one line, not a script) shorter than this mess I just did Laughing :
Code:
for nn in `seq 1 100`;  do { [ $(( $nn % 15 )) == 0 ] && echo "FizzBuzz"; } || { [ $(( $nn % 5 )) == 0 ] && echo "Buzz"; } || { [ $(( $nn % 3 )) == 0 ] && echo "Fizz"; } || ( echo "$nn") ; done  | column



That's just "sh" not "bash". This is bash:
Code:
for ((a=1; a <= 100; a++)); do ! ((a%15)) && echo FizzBuzz || ! ((a%3)) && echo Fizz ||  ! ((a%5)) && echo Buzz ||  echo $a; done | column

I'm sure technosaurus can make it even shorter Cool

_________________
Fatdog64, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread
Back to top
View user's profile Send private message 
GustavoYz


Joined: 07 Jul 2010
Posts: 896
Location: .ar

PostPosted: Sun 18 Nov 2012, 15:00    Post subject: Re: FizzBuzz
Subject description: Seeking a shoter Bash solution
 

jamesbond wrote:
GustavoYz wrote:
Im wondering if there is a bash solution (one line, not a script) shorter than this mess I just did Laughing :
Code:
for nn in `seq 1 100`;  do { [ $(( $nn % 15 )) == 0 ] && echo "FizzBuzz"; } || { [ $(( $nn % 5 )) == 0 ] && echo "Buzz"; } || { [ $(( $nn % 3 )) == 0 ] && echo "Fizz"; } || ( echo "$nn") ; done  | column



That's just "sh" not "bash". This is bash:
Code:
for ((a=1; a <= 100; a++)); do ! ((a%15)) && echo FizzBuzz || ! ((a%3)) && echo Fizz ||  ! ((a%5)) && echo Buzz ||  echo $a; done | column



And that's incorrect...
Rolling Eyes

I appreciate the reply, but your "Bash" way is grouping the || and && in the wrong way and you have more than 100 items on that...
FizzBuzz_NO.jpg
 Description   
 Filesize   24.06 KB
 Viewed   714 Time(s)

FizzBuzz_NO.jpg

FizzBuzz.jpg
Description 
jpg

 Download 
Filename  FizzBuzz.jpg 
Filesize  57.46 KB 
Downloaded  361 Time(s) 

_________________

Back to top
View user's profile Send private message 
rcrsn51


Joined: 05 Sep 2006
Posts: 9066
Location: Stratford, Ontario

PostPosted: Sun 18 Nov 2012, 15:39    Post subject:  

Is the objective to have the least amount of code or the least amount of computation?
Back to top
View user's profile Send private message 
GustavoYz


Joined: 07 Jul 2010
Posts: 896
Location: .ar

PostPosted: Sun 18 Nov 2012, 16:04    Post subject:  

rcrsn51 wrote:
Is the objective to have the least amount of code or the least amount of computation?


I would like to learn a "better" way of achieve the same on the shell (trough Bash or sh) if is possible.
If two versions of the same command, working equally well and at comparable speed, i'd prefer the shorter and/or mnemonic.

_________________

Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 2134
Location: The Blue Marble

PostPosted: Sun 18 Nov 2012, 18:51    Post subject: Re: FizzBuzz
Subject description: Seeking a shoter Bash solution
 

GustavoYz wrote:

And that's incorrect...
Rolling Eyes

That was an optimisation went wrong. My original version looked like this before I tried to over-optimise and remove the "continue" with || ... Laughing
Code:
time for ((a=1; a <= 100; a++)); do ! ((a%15)) && echo FizzBuzz && continue; ! ((a%3)) && echo Fizz && continue;  ! ((a%5)) && echo Buzz && continue;  echo $a; done

_________________
Fatdog64, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread
Back to top
View user's profile Send private message 
GustavoYz


Joined: 07 Jul 2010
Posts: 896
Location: .ar

PostPosted: Mon 19 Nov 2012, 14:09    Post subject: Re: FizzBuzz
Subject description: Seeking a shoter Bash solution
 

jamesbond wrote:
Code:
time for ((a=1; a <= 100; a++)); do ! ((a%15)) && echo FizzBuzz && continue; ! ((a%3)) && echo Fizz && continue;  ! ((a%5)) && echo Buzz && continue;  echo $a; done

Thanks!
Razz

_________________

Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 2505
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Wed 21 Nov 2012, 12:03    Post subject: FizzBuzz
Subject description: python
 

Before this thread is SOLVED another FizzBuzz
not fast
not short
but python
Code:
for a in range(1,101):
    if  a % 15 == 0:
        print 'FizzBuzz'
        continue
    if a %  5 == 0:
        print 'Fizz'
        continue
    if a %  3 == 0:
        print 'Buzz'
        continue
    print a
or 2 lines less but slower:
Code:
for a in range(1,101):
    if  a % 15 == 0:
        print 'FizzBuzz'
    elif a %  5 == 0:
        print 'Fizz'
    elif a %  3 == 0:
        print 'Buzz'
    else:
        print a
or:
Code:
for a in range(1,101):
    p = ''
    if not a % 5 : p += 'Fizz'
    if not a % 3 : p += 'Buzz'
    if not p : p  = a         
    print p

Why learn bash if....
Back to top
View user's profile Send private message 
GustavoYz


Joined: 07 Jul 2010
Posts: 896
Location: .ar

PostPosted: Wed 21 Nov 2012, 14:16    Post subject:  

Well, I wasn't after a python script or solution and my question was about 'one-liners', but thanks a lot anyway.
I solved it on python time ago (using the interpreter originally) with this code:
Code:
python -c 'print ["Fizz"[i%3*4:]+"Buzz"[i%5*4:]or str(i)for i in range(1,101)]'

but found plenty python versions that taught me better ways...

Quote:
Why learn bash if....

Lots of reasons... Quick one? There is not such thing as a python shell yet, to replace actual Bash/sh/zsh or even tclsh. Rolling Eyes
Buy Python is great, no discussions on that (I've to deal with it everyday Smile )

I am (still) curious about how much less code is possible to solve the task in the shell (Bash or Sh).

_________________

Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 2505
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Wed 21 Nov 2012, 15:09    Post subject: FizzBuzz
Subject description: sh
 

GustavoYz wrote:
I am (still) curious about how much less code is possible to solve the task in the shell (Bash or Sh).

not less but fast:
a one liner in my console wrote:
# time i=0;while [ $i -lt 100 ];do i=$(($i+1));p='';[[ $((i%3)) -eq 0 ]] && p='Fizz';[[ $((i%5)) -eq 0 ]] && p="${p}Buzz";[ "$p" ]|| p=$i;echo -n "$p ";done

real 0m0.000s
user 0m0.000s
sys 0m0.000s
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz #
Back to top
View user's profile Send private message 
GustavoYz


Joined: 07 Jul 2010
Posts: 896
Location: .ar

PostPosted: Wed 21 Nov 2012, 21:54    Post subject:  

Thanks. Very Happy
_________________

Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4348

PostPosted: Mon 26 Nov 2012, 03:18    Post subject:  

that was about what I had too

Code:
n=0;s="";b="";while [ $((n)) != 100 ]; do b="";n=$(($n+1));[ $(($n%3)) == 0 ] && b=Fizz;[ $(($n%5)) == 0 ] && b=${b}Buzz"
";[ "$b" ] && s=$s"   "$b || s=$s"   "$n;done;echo "$s"

prints out 5 tab separated columns using only basic shell

Note it is normally much faster to echo $s (without formatting) than "$s" (with formatting)

or some variation of
Code:
n=0;s="";while [ $(($n)) != 100 ];do n=$(($n+1));case $(($n%15)) in 3|6|9|12)s=$s"   "Fizz;;5|10)s=$s"   "Buzz"
";;0)s=$s"   "FizzBuzz"
";;*)s=$s"   "$n;;esac;done;echo "$s"

creating the string only takes ~ 0.01s on my box while outputting it to the console takes ~0.05 ... output to a file reduces this to ~0.002s though

with bashisms it could be simplified to
Code:
n=0;s="";while [ $((n++)) != 100 ];do case $(($n%15)) in 3|6|9|12)s=$s"   "Fizz;;5|10)s=$s"   "Buzz"
";;0)s=$s"   "FizzBuzz"
";;*)s=$s"   "$n;;esac;done;echo "$s"

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
linuph


Joined: 03 Jun 2012
Posts: 128
Location: Philippines

PostPosted: Mon 26 Nov 2012, 07:49    Post subject:  

technosaurus:
that's 0.03 user seconds on my P3 1GHz with echo to console, 0.02 with echo to file
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4348

PostPosted: Mon 26 Nov 2012, 09:37    Post subject:  

here is the C for comparison
Code:
#include <stdio>

int void main(){
int n=0;

while (n++ < 100){
switch (n%15){
case 3 :
case 6 :
case 9 :
case 12 :
printf("Fizz\t");
break;
case 5 :
case 10 :
printf("Buzz\n");
break;
case 0 :
printf("FizzBuzz\n");
break;
default :
printf("%d\t",n);
}
}

}

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
rcrsn51


Joined: 05 Sep 2006
Posts: 9066
Location: Stratford, Ontario

PostPosted: Mon 26 Nov 2012, 12:22    Post subject:  

technosaurus wrote:
Code:
switch (n%15){


Very nice. Doing three divisions is clearly overkill. Doing one division is better.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 3 [32 Posts]   Goto page: 1, 2, 3 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1032s ][ Queries: 13 (0.0079s) ][ GZIP on ]