Last active
February 14, 2017 14:40
[2017-02-06] Challenge #302 [Easy] Spelling with Chemistry. See https://www.reddit.com/r/dailyprogrammer/comments/5seexn/20170206_challenge_302_easy_spelling_with/
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
# | |
# [2017-02-06] Challenge #302 [Easy] Spelling with Chemistry | |
# Using the periodic table of elements, convert text into elementized words. | |
# https://www.reddit.com/r/dailyprogrammer/comments/5seexn/20170206_challenge_302_easy_spelling_with/ | |
# | |
# by Kory Becker | |
# http://primaryobjects.com | |
# | |
data <- read.csv('https://gist.githubusercontent.com/primaryobjects/46edcc29e7b6dac3ea84a17995707678/raw/b361d2bd9952016a3481c0220f8d9fef8963299e/elements.tsv', sep='\t') | |
elementize <- function(text) { | |
elements <- data.frame() | |
count <- 0 | |
symbolIndex <- 1 | |
# Get applicable elements by matching symbol in the text. | |
symbols <- which(sapply(tolower(data$Symbol), function(symbol) { grep(symbol, text) > 0 }) == TRUE) | |
# Sort the symbols by length, to get the biggest replacements first. | |
symbols <- symbols[order(sapply(names(symbols), nchar), decreasing=T)] | |
while (count < nchar(text)) { | |
index <- symbols[symbolIndex] | |
name <- names(symbols[symbolIndex]) | |
symbol <- data[index,]$Symbol | |
# Find indices within the string where the symbol matches. | |
indices <- unlist(gregexpr(name, text)) | |
if (indices[1] > -1) { | |
# This symbol was found in the text, replace it in. | |
text <- gsub(name, toupper(symbol), text) | |
count <- count + (nchar(name) * length(indices)) | |
for (i in indices) { | |
elements <- rbind(elements, data.frame(name=as.character(data[index,]$Element), symbol=data[index,]$Symbol, index=i)) | |
} | |
} | |
symbolIndex <- symbolIndex + 1 | |
} | |
# Finally, go through the text and replace in the proper symbol casing. | |
symbolsUsed <- elements$symbol[order(sapply(as.character(elements$symbol), nchar), decreasing=T)] | |
sapply(symbolsUsed, function(symbol) { | |
text <<- gsub(toupper(symbol), symbol, text) | |
}) | |
list(text=text, elements=elements[order(elements$index),]) | |
} | |
tests <- c('genius', 'functions', 'bacon', 'poison', 'sickness', 'ticklish') | |
result <- sapply(tests, function(test) { | |
result <- elementize(test) | |
output <- paste0(result$text, ' (') | |
sapply(seq(nrow(result$elements)), function(i) { | |
name <- result$elements[i,]$name | |
output <<- paste0(output, tolower(name), ', ') | |
}) | |
# Remove trailing comma. | |
output <- gsub('[, ]+$', '', output) | |
output <- paste0(output, ')') | |
output | |
}) | |
print(result) |
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
genius | |
"GeNiUS (germanium, nickel, uranium, sulfur)" | |
functions | |
"FUNCTiONS (fluorine, uranium, nitrogen, carbon, titanium, oxygen, nitrogen, sulfur)" | |
bacon | |
"BAcON (boron, actinium, oxygen, nitrogen)" | |
poison | |
"PoISON (polonium, iodine, sulfur, oxygen, nitrogen)" | |
sickness | |
"SiCKNEsS (silicon, carbon, potassium, nitrogen, einsteinium, sulfur)" | |
ticklish | |
"TiCKLiSH (titanium, carbon, potassium, lithium, sulfur, hydrogen)" |
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
Source: | |
https://www.reddit.com/r/dailyprogrammer/comments/5seexn/20170206_challenge_302_easy_spelling_with/ | |
The IUPAC Periodic Table of the Elements is one of the most recognizable features of modern chemistry - the organization of all known chemical elements along with some of their most fundamental properties, together with their names and symbols. Today we're going to use that as we spell some words. | |
Here's the list of the elements alphabetized by the element's name. We can also see the symbol (1 or 2 letters - we're omitting the emerging elements that often contain three letters), atomic number and weight, and Pauling Electronegativities (c), which are unused in this challenge. | |
You'll be given a list of words, one per line. Example: | |
genius | |
Output Description | |
Your program should emit the word as a series of elements by name with proper capitalization from the above table. Example: | |
GeNiUS (germanium nickel uranium sulfur) | |
Challenge Input | |
functions | |
bacon | |
poison | |
sickness | |
ticklish | |
Challenge Output | |
FUNCTiONS (flourine, uranium, nitrogen, carbon, titanium, oxygen, nitrogen, sulfur) | |
BaCoN (barium, cobalt, nitrogen) | |
POISON (phosphorus, oxygen, iodine, sulfur, oxygen, nitrogen) | |
SiCKNeSS (silicon, carbon, potassium, neon, sulfur, sulfur) | |
TiCKLiSH (titanium, carbon, potassium, lithium, sulfur, hydrogen) | |
Bonus | |
Note that bacon has a few different possibilities. Which is the heaviest by atomic weight? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment