Skip to content

Instantly share code, notes, and snippets.

@kunigami
Created May 17, 2017 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunigami/33e6813fc3e0a9de7b59cc8dcd2a6d51 to your computer and use it in GitHub Desktop.
Save kunigami/33e6813fc3e0a9de7b59cc8dcd2a6d51 to your computer and use it in GitHub Desktop.
# Read the data
setwd('/')
words <- read.csv(
'/usr/share/dict/words',
colClasses="character",
header=FALSE
)
# Insert a header to the words
names(words) <- c("word")
require(dplyr)
# Add the words length column
words <- mutate(words, len=nchar(word))
# Compute the cost per word: w * (2^w)
words <- mutate(words, cost=(len * (2**(len))))
# Compute the total cost
total_cost = sum(words$cost)
# Total number of characters
n_characters = sum(words$len)
# Approach 3: cost = min(2^w, nchars)
words <- mutate(words, cost2=(pmin(2**len, n_characters)))
# Compute the total cost for approach 3
total_cost2 = sum(words$cost2)
# Diplay histogram
colors = c("#b2e7f5")
hist(
words$len,
col=colors,
main="Words by length",
xlab="Number of characters"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment