Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# 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