real language

Bookmarking Digg Diigo DZone Earthlink Google
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language R

(Dataframe approach)

Author:David Dailey
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 ..>

# 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

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


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!




Security Code: