Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Last active December 17, 2024 12:55
Show Gist options
  • Save mdsumner/f812dc0583be14c1056d13d9233e23e5 to your computer and use it in GitHub Desktop.
Save mdsumner/f812dc0583be14c1056d13d9233e23e5 to your computer and use it in GitHub Desktop.

code golf for https://fosstodon.org/@coolbutuseless/113668013712145841

x <- c("apple", "app", "aptitude")
mx <- max(nchar(x))

pool <- c(letters, LETTERS)
flat <- function(x) {
  c(x, rep(0L, mx - length(x)))
}
## match letter to number and flatten to mx, then process for uniqueness by column (possibly could use rowSums to avoid apply loop)
which(apply(do.call(rbind, lapply(strsplit(x, ""), \(.x) flat(match(.x, pool)))), 2, \(.x) length(unique(.x))) > 1)[1L] + 1

[1] 4
@mdsumner
Copy link
Author

is it faster if we avoid apply()?

(none of this is really tested, fwiw)

## to use rowSums we need the longest string as index
index <- match(unlist(strsplit(x[which.max(nchar(x))], "")), pool)

which(!rowSums(do.call(cbind, lapply(strsplit(x, ""), \(.x) flat(match(.x, pool))))) == (index * length(x)))[1] + 1

@mdsumner
Copy link
Author

mdsumner commented Dec 17, 2024

ok, no need for pool or strsplit or match

x <- c("apple", "app", "aptitude")
mx <- max(nchar(x))

## a bad name for a function to pad zeros to the end of a vector shorter than 'mx'
flat <- function(x) {
  c(x, rep(0L, mx - length(x)))
}

## minimum number of first letters needed to determine non-uniqueness
minnl <- function(x) {
  ## to use rowSums we need the longest string as index
  index <- as.integer(charToRaw(x[which.max(nchar(x))]))
  which(!rowSums(do.call(cbind, lapply(x, \(.x) flat(as.integer(charToRaw(.x)))))) == (index * length(x)))[1] + 1
}

minnl(x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment