function table=beer(plain, skull);

table = '';
t = 0;
l = length(plain);
wall = beer_translation(plain);
disp(sprintf('First Wall : %s \n',wall))

k = length(wall);
while (k > 0)
    
    % Left rotation
    wall = beer_left_rotation(wall);
    disp(sprintf('"%d bottles of beer"',k));
    disp(sprintf('Wall : %s ',wall));
    
    % Multiplication by 10*k+1
    multi = k*10+1;
    mult = num2str(multi);
    result = beer_multi(wall,mult);

    x = '';
    x(1,:) = result;
    i = 1; 
    while (x(i,1) == '0')
        for j = 1:length(x)-1
            x(i+1,j) = x(i,j+1);
        end;
        i = i+1;
    end
    result = deblank(x(i,:));
    if result == ''
        result = '0';
    end;
   
    wall = result(length(result)-k+1:length(result));
    disp(sprintf('New Wall : %s \n',wall));
    
    carry = '0';
    restmp = '';
    for i = length(skull):-1:1
        if length(wall) + i -1 < length(skull)
            restmp = num2str(str2num(carry)+str2num(skull(i)));
        else
            restmp = num2str(str2num(carry)+str2num(wall(i-length(skull)+length(wall)))+str2num(skull(i)));
        end;
        if length(restmp) == 2
            sum(i+1) = restmp(2);
            carry = restmp(1);
        else
            sum(i+1) = restmp(1);
            carry = '0';
        end;
    end;
    sum(1) = carry;
    wall = sum(length(sum)-k+1:length(sum));
    disp('"on the wall"')
    disp(sprintf('New Wall : %s \n',wall));
    
    disp('"take one down"')
    bone = wall(length(wall));
    disp(sprintf('Bone : %s ',bone));
    wall(length(wall)) = '';
    wall = deblank(wall);
    if wall == ''
        disp('New Wall : []');
    else
        disp(sprintf('New Wall : %s \n',wall));
    end;
    
    t = t+1;   
    bigbone(1:t) = bone;
    disp('"pass it around"')
    disp(sprintf('Big Bone : %s ',bigbone));
    tentable = strcat(table,'0');
    disp(sprintf('10 x table : %s ',tentable));
    
    if length(tentable) < length(bigbone) 
        max = length(bigbone);
        min = length(tentable);
        a = bigbone;
        b = tentable;
    else
        max = length(tentable);
        min = length(bigbone);
        a = tentable;
        b = bigbone;
    end;
    
    carry = '0';
    restmp = '';
    sum = '';
    for i = max:-1:1,
        if min + i -1 < max
            restmp = num2str(str2num(carry)+str2num(a(i)));
        else
            restmp = num2str(str2num(carry)+str2num(b(i-max+min))+str2num(a(i)));
        end;

        if length(restmp) == 2
            sum(i+1) = restmp(2);
            carry = restmp(1);
        else
            sum(i+1) = restmp(1);
            carry = '0';
        end;
    end;
    tmp = '';
    if carry == '0'
        tmp(1:length(sum)-1) = sum(2:length(sum));
        sum = tmp;
    else
        sum(1)=carry;
    end;
    
    table = (sum(i,:));
    disp(sprintf('New Table : %s \n',table));
    k = length(wall);
end;


function w = beer_translation(p)
l = length(p);
for i = 1:l,
    if (double(p(i)) < 91 & double(p(i)) > 64)
        tmp = num2str(double(p(i))-64);
        if length(tmp) == 1
            w(2*i-1) = '0';
            w(2*i) = tmp;
        else
            w(2*i-1) = tmp(1);
            w(2*i) = tmp(2);
        end;
    elseif (double(p(i)) == 32)
        w(2*i-1) = '0';
        w(2*i) = '0';
    end;   
end;

function tmp = beer_left_rotation(w)
k = length(w);
tmp(k) = w(1);
for i = 1:k-1,
    tmp(i) = w(i+1);
end
    
function result = beer_multi(w,m)
k = length(w);
res = '';
for i = 1:length(m)
    mul = m(length(m)+1-i);
    carry = '0';
    ind = k+length(m)+2-i;
    for j = 1:k,
        restmp = num2str(str2num(mul)*str2num(w(k-j+1))+str2num(carry));
        if length(restmp) == 2
            res(i,ind) = restmp(2);
            ind = ind - 1;
            carry = restmp(1);
        else
            res(i,ind) = restmp(1);
            ind = ind - 1;
            carry = '0';
        end;
    end;
    if carry == '0'
    else res(i,ind) = carry;
    end;
end;
s = size(res);
result = '';
carry = '0';
for i = s(2):-1:1,
    rest = str2num(res(1,i));
    if rest == []
        rest = 0;
    end;
    for j = 2:s(1),
        if (double(res(j,i)) == 0 | double(res(j,i)) == [])
            rest = rest;
        else
            rest = rest+str2num(res(j,i));
        end;
    end;
    rest = rest+ str2num(carry);
    if length(num2str(rest)) == 2
        a = num2str(rest);
        result(i) = a(2);
        carry = a(1);
    else
        result(i) = num2str(rest);
        carry = '0';
    end;
end;
