Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language R

(Dataframe approach)

Date:05/03/08
Author:David Dailey
URL:n/a
Comments:0
Info:http://www.r-project.org/
Score: (3.00 in 50 votes)
# 99 bottles of beer on the wall
# For R-2.6.1 and later (possibly some earlier as well)
# D. Dailey, 3 May 2008 <compass98155 .. at .. gmail.com>

# The strategy: Build a data frame with the lyrics, then 

# First: Individual phrases
bottles <- c( rep( 'bottles', 98), 'bottle', 'bottles' )
beerFrame <- data.frame(
  FirstPhrase = sprintf( '%s %s of beer on the wall', c( 99:1, "No more" ), bottles ),
  SecondPhrase = sprintf( '%s %s of beer', c( 99:1, "no more" ), bottles ),
  ThirdPhrase = sprintf( 'Take one down and pass it around'),
  LastPhrase = sprintf( '%s bottles of beer on the wall', c( 98:1, "no more", 99 ) ),
  stringsAsFactors=FALSE )

# Special cases for phrases
beerFrame[ nrow( beerFrame ) - 2, 'LastPhrase' ]  <- '1 bottle of beer on the wall'
beerFrame[ nrow( beerFrame ),     'ThirdPhrase' ] <- 'Go to the store and buy some more'

# Second: Combine phrases into lines
beerFrame[, 'TopLine']    <- with( beerFrame, sprintf( '%s, %s.', FirstPhrase, SecondPhrase ) )
beerFrame[, 'BottomLine'] <- with( beerFrame, sprintf( '%s, %s.', ThirdPhrase, LastPhrase ) )

# Third: Combine lines into stanzas
beerFrame[, 'Stanza' ] <- with( beerFrame, sprintf( '%s\n%s\n', TopLine, BottomLine ) )

# Now display it all (each stanza already ends with \n)
cat( beerFrame[, 'Stanza'], sep='\n' )

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Vectorized and makes correct lyricsGiovanni Millo10/05/051
Recursive ApproachRoland01/04/060

Comments

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: