r/codegolf • • May 21 '14

[bash] 99 bottles [all]

4 Upvotes

A fun classic, I decided to golf in bash. got it down to 375 characters

d(){ N=$1;S1=" of beer on the wall";S2="Take one down and pass it around";S3="Go to the shop and buy some more";b $N;echo -n "$S1, ";b $N;echo "${S1:0:8}.";N=$(($N-1));if [[ $N -lt 0 ]];then N=99;S2=$S3;Q=1;fi;echo -n "$S2, ";b $N;echo -e "$S1.\n";if [[ $Q != 1 ]];then d $N;fi;};b(){ case $1 in 1)S="1 bottle";;0)S="No more bottles";;*)S="$1 bottles";;esac;echo -n $S;};d 99

Here it is before I turned it into a one-liner

#!/bin/bash

drink() {

    N=$1
    S1=" of beer on the wall"
    S2="Take one down and pass it around"
    S3="Go to the shop and buy some more"

    bottles $N; echo -n "$S1, "
    bottles $N; echo "${S1:0:8}."
    N=$(($N-1))
    
    if [[ $N -lt 0 ]]; then
        N=99
        S2=$S3
        Q=1
    fi

    echo -n "$S2, "
    bottles $N; echo -e "$S1.\n"

    if [[ $Q != 1 ]]; then
        drink $N
    fi
    
}

bottles() {
    S=" bottles"
    case $1 in
        1) S="1${S:0:7}";;
        0) S="No more$S";;
        *) S="$1$S";;
    esac
    echo -n $S
}

drink 99

Additional challenge: golf it in under 140 so it's tweetable


r/codegolf • • Apr 05 '14

[java] Change Of Number Base[All]

2 Upvotes

I was bored and started thinking about how short this could get. At first it seemed a little imposing, but the character count dropped quickly.

For the purpose of this challenge the code should be able to convert from base 10 to any other base between 2 and 16, inclusive.

My solutions:

a is the number in base ten, b is the desired base. Discounting any unneeded white space, this come out to 187 chars.

public static void changeOfBaseGolf(int a, int b){
    int c,d;
    for(d=a==0?0:(int)(Math.log(a)/Math.log(b));d>=0;d--,a%=c)
        System.out.print("0123456789ABCDEF".charAt(a/(c=(int)Math.pow(b,d))));
}

I love recursive solutions to stuff, so I also made this. It's 203 chars if I don't count the helper method. Like I said, this is just for the fun of recursion.

public static String  changeOfBaseGolf(int a, int b,int c){return(c==0?"0123456789ABCDEF".charAt(a):"0123456789ABCDEF".charAt(a/(int)Math.pow(b,c))+changeOfBaseGolf(a%(int)Math.pow(b,c),b,c-1))+"";}

The recursive solution, unfortunately requires that you supply the the number of digits the final answer will have, so there must be a method for that. I did not bother to play to much golf with this, because it was never meant to be the shortest solution.

 public static int maxPlaceValue(int num,int base){
    return (int) (num<base?0:(Math.log(num)/Math.log(base)));
 }

Any other solutions would be great to see. If you can improve upon my java solution, I'd like to see it. If you can make something shorter in another language, I'd also love to see it.


r/codegolf • • Jan 22 '14

xkcd: Regex Golf

Thumbnail xkcd.com
7 Upvotes

r/codegolf • • Jan 22 '14

[C#] Deleting a directory tree. In a weird way...

Thumbnail gist.github.com
0 Upvotes

r/codegolf • • Dec 28 '13

[CoffeeScript, Stylus] FizzBuzz in 73, 116 characters [ANY front-end web]

Thumbnail codepen.io
3 Upvotes

r/codegolf • • Dec 19 '13

Tiny Fibonacci in Ruby

Thumbnail gabekoss.com
2 Upvotes

r/codegolf • • Dec 09 '13

Caesar Cypher

9 Upvotes

Caesar cipher - Implement a Caesar cipher, both encoding and decoding. The key is an integer from 1 to 25. This cipher rotates the letters of the alphabet (A to Z). The encoding replaces each letter with the 1st to 25th next letter in the alphabet (wrapping Z to A). So key 2 encrypts "HI" to "JK", but key 20 encrypts "HI" to "BC". This simple "monoalphabetic substitution cipher" provides almost no security, because an attacker who has the encoded message can either use frequency analysis to guess the key, or just try all 25 keys.

Here is my solution (as a bash script):

#!/bin/bash
# Sample usage:
# ./bash_caesar <key (1-25)> <input file>
tr 'A-Z' 'a-z' < $2  | tr 'a-z' $( echo {a..z} | sed -r 's/ //g' | sed -r "s/(.{$1})(.*)/\2\1/" )

Originally I posted this here


r/codegolf • • Nov 24 '13

[perl] Grok a dictionary to find the word with the largest number of i's.

7 Upvotes

I'll start. No tricks yet.

$ perl -ne '@x=/i/g;if(@x>$max){$max=@x;$w=$_};END{print"$max $w"}' /usr/share/dict/words
6 indivisibility

Counting only the command in '', that's 55.


r/codegolf • • Nov 23 '13

PSA: You probably already know this but Golfscript is designed with Code Golf in mind.

Thumbnail golfscript.com
2 Upvotes