Created
May 17, 2017 17:38
-
-
Save kunigami/33e6813fc3e0a9de7b59cc8dcd2a6d51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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