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
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
Vectorized and makes correct lyrics | Giovanni Millo | 10/05/05 | 1 | |
Recursive Approach | Roland | 01/04/06 | 0 |
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!
Comments