Skip to content

Instantly share code, notes, and snippets.

View AdeelK93's full-sized avatar

Adeel Khan AdeelK93

View GitHub Profile
rainbowText <- function(text, colorFun = grDevices::rainbow, ...) {
text <- trimws(text)
# colorspace has some good fucntions that you can use here
colors <- colorFun(nchar(text), ...)
# wrap each letter in a differently colored span
html <- paste0(
Map(function(letter, color)
paste0("<span style='color:", color, "'>", letter, "</span>"),
strsplit(text, "")[[1]], colors
), collapse = ""
@AdeelK93
AdeelK93 / Lcd.hs
Created December 12, 2017 06:34
Lowest common denominator within a list of numbers
module Lcd where
import Data.List
import Control.Arrow
-- Integer square root
isqrt :: Integer -> Integer
isqrt = floor . sqrt . fromIntegral
-- Either a list of factors or the number itself (if prime)
primefactors :: Integer -> [Integer]
@AdeelK93
AdeelK93 / as_monotonic.R
Created June 2, 2017 05:57
Jagged to monotonic time series
as.monotonic <- function(x) {
while(sum(diff(x)<0)) {
lastMonotonic <- which(diff(x)<0)
firstNonMonotonic <- lastMonotonic[1] + 1
# which chunk of the time series needs to be adjusted up
lastNonMonotonic <- ifelse(length(lastMonotonic)>1, lastMonotonic[2], length(x))
monotonicRange <- firstNonMonotonic:lastNonMonotonic
# add last monotonic value to nonmonotonic chunk
x[monotonicRange] <- x[monotonicRange] + x[lastMonotonic[1]]
}