| Author |
Message |
GustavoYz

Joined: 07 Jul 2010 Posts: 865 Location: .ar
|
Posted: 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.
Im wondering if there is a bash solution (one line, not a script) shorter than this mess I just did :
| 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).
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
|
|
 |
jamesbond
Joined: 26 Feb 2007 Posts: 1534 Location: The Blue Marble
|
Posted: 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 :
| 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
_________________ Fatdog64, Slacko and Puppeee user. Puppy user since 2.13
|
|
Back to top
|
|
 |
GustavoYz

Joined: 07 Jul 2010 Posts: 865 Location: .ar
|
Posted: 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 :
| 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...
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...
| Description |
|
| Filesize |
24.06 KB |
| Viewed |
542 Time(s) |

|
| Description |
|

Download |
| Filename |
FizzBuzz.jpg |
| Filesize |
57.46 KB |
| Downloaded |
157 Time(s) |
_________________

|
|
Back to top
|
|
 |
rcrsn51

Joined: 05 Sep 2006 Posts: 7744 Location: Stratford, Ontario
|
Posted: 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
|
|
 |
GustavoYz

Joined: 07 Jul 2010 Posts: 865 Location: .ar
|
Posted: 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
|
|
 |
jamesbond
Joined: 26 Feb 2007 Posts: 1534 Location: The Blue Marble
|
Posted: Sun 18 Nov 2012, 18:51 Post subject:
Re: FizzBuzz Subject description: Seeking a shoter Bash solution |
|
| GustavoYz wrote: |
And that's incorrect...
|
That was an optimisation went wrong. My original version looked like this before I tried to over-optimise and remove the "continue" with || ...
| 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
|
|
Back to top
|
|
 |
GustavoYz

Joined: 07 Jul 2010 Posts: 865 Location: .ar
|
Posted: 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!
_________________

|
|
Back to top
|
|
 |
L18L
Joined: 19 Jun 2010 Posts: 1700 Location: Burghaslach, Germany
|
Posted: 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
|
|
 |
GustavoYz

Joined: 07 Jul 2010 Posts: 865 Location: .ar
|
Posted: 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.
Buy Python is great, no discussions on that (I've to deal with it everyday )
I am (still) curious about how much less code is possible to solve the task in the shell (Bash or Sh).
_________________

|
|
Back to top
|
|
 |
L18L
Joined: 19 Jun 2010 Posts: 1700 Location: Burghaslach, Germany
|
Posted: 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
|
|
 |
GustavoYz

Joined: 07 Jul 2010 Posts: 865 Location: .ar
|
Posted: Wed 21 Nov 2012, 21:54 Post subject:
|
|
Thanks.
_________________

|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: 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" |
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
linuph

Joined: 03 Jun 2012 Posts: 127 Location: Philippines
|
Posted: 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
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3843
|
Posted: 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);
}
}
} |
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
rcrsn51

Joined: 05 Sep 2006 Posts: 7744 Location: Stratford, Ontario
|
Posted: Mon 26 Nov 2012, 12:22 Post subject:
|
|
Very nice. Doing three divisions is clearly overkill. Doing one division is better.
|
|
Back to top
|
|
 |
|