Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- inputs <- c('dbbaCEDbdAacCEAadcB', 'EbAAdbBEaBaaBBdAccbeebaec')
- tally <- function(input) {
- letters <- unlist(strsplit(input, ''))
- hash <- new.env()
- # Tally scores.
- sapply(letters, function(letter) {
- # If the letter is not uppercase it's a score. Otherwise, it's a loss.
- score <- ifelse(gregexpr("[A-Z]", letter) < 1, 1, -1)
- letter <- tolower(letter)
- hash[[letter]] <- ifelse(is.null(hash[[letter]]), score, hash[[letter]] + score)
- })
- # Get score values.
- scores <- c()
- keys <- ls(hash)
- scores <- t(sapply(keys, function(key) {
- c(scores, c(key, hash[[key]]))
- }))
- colnames(scores) <- c('player', 'score')
- scores <- as.data.frame(scores)
- scores$score <- as.numeric(as.character(scores$score))
- # Sort the scores.
- scores[order(scores$score, decreasing=T),]
- }
- format <- function(scores) {
- str <- sapply(1:nrow(scores), function(i) {
- row <- scores[i,]
- paste0(row$player, ':', row$score)
- })
- str
- }
Add Comment
Please, Sign In to add comment