Skip to content

Instantly share code, notes, and snippets.

@mayrop
Last active August 4, 2020 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayrop/04608bd01325119030898c85a13b37ca to your computer and use it in GitHub Desktop.
Save mayrop/04608bd01325119030898c85a13b37ca to your computer and use it in GitHub Desktop.

Given a number n, find the sum of all n-digit palindromes.

Given that an "even word" is a word in which each character appears an even number of times, write a function that takes in a string and returns the minimum number of letters to be removed to make that string an even word.

Given a decimal number as N, convert N into an equivalent irreducible fraction. An irreducible fraction is a fraction in which numerator and denominator are co-primes i.e., they have no other common divisor other than 1.

Sort an array of strings based on the number of distinct characters that occur in the word (followed by the length of the word)

# Interview Question of the Week: Jan 13, 2020
# 126 of rendezvous with cassidoo
# https://cassidoo.co/newsletter/
# Given a number n, find the sum of all n-digit palindromes.
# >> nPalindromes(2)
# >> 495 // 11 + 22 + 33 + 44 + 55 + 66 + 77 + 88 + 99
# Removing exponential notation
options(scipen=999)
nPalindrome <- function(n) {
# returning 0 by default
if (n == 0 | !is.numeric(n)) {
return(0)
}
if (n == 1) {
return(sum(1:9))
}
# this is how many inner loops we need to do
# to get the sum of inner numbers
times <- ceiling((n-2) / 2)
# this will be the base number that will the the numbers in the extremes
# i.e. for n = 3, 101, for n = 4, 1001, for n = 5, 10001
base <- 10 ^ (n-1) + 1
# outer sum will be...
outer_sum <- base * sum(1:9) * 10 ^ times
# no need to do any inner loop if we're giving n = 2
if (!times) {
return(outer_sum)
}
inner_sum <- 0
is_n_odd <- (n / 2) %% 1 != 0
for (i in 1:times) {
# how many combinations do we have...
# 9 (since when the first number is 0 it doesn't count)
n_comb <- 9 * 10 ^ (times-1)
# we're adding 10 exponentiated to the i
multiplier <- 10 ^ i
# but don't do that if if it's last number to add AND
# length of n is odd
if (i == times & is_n_odd) {
multiplier <- 0
}
inner_sum <- inner_sum + sum(1:9) * n_comb * (10 ^ (n - 1 - i) + multiplier)
}
return(outer_sum + inner_sum)
}
sapply(1:10, function(i) {
print(paste(i, "=>", nPalindrome(i)))
})
# [1] "1 => 45"
# [1] "2 => 495" (11 + 22 + 33 + 44 + 55 + 66 + 77 + 88 + 99)
# [1] "3 => 49500"
# [1] "4 => 495000"
# [1] "5 => 49500000"
# [1] "6 => 495000000"
# [1] "7 => 49500000000"
# [1] "8 => 495000000000"
# [1] "9 => 49500000000000"
# [1] "10 => 495000000000000"
# Interview Question of the Week: Feb 3, 2020
# 129 of rendezvous with cassidoo
# https://cassidoo.co/newsletter/
# Given that an "even word" is a word in which each
# character appears an even number of times, write a function
# that takes in a string and returns the minimum number of
# letters to be removed to make that string an even word.
# >> evenWord('aaaa')
# >> 0
# >> evenWord('potato')
# >> 2
evenWord <- function(string) {
# sorting alpha
string <- paste(sort(unlist(strsplit(string, ""))), collapse = "")
# removing double chars with regexp
string <- gsub("(.)\\1", "", string)
nchar(string)
}
evenWord("aaaa")
# [1] 0
evenWord("potato")
# [1] 2
evenWord("abcdabce")
# [1] 2
// Interview Question of the Week: Jul 20, 2020
// 153 of rendezvous with cassidoo
// https://cassidoo.co/newsletter/
// Given a decimal number as N, convert N into an
// equivalent irreducible fraction. An irreducible fraction
// is a fraction in which numerator and denominator are co-primes
// i.e., they have no other common divisor other than 1.
class Frac(n: Int, d: Int) {
def gcd: Int = greatestCommonDivisor(n, d)
def num: Int = n / gcd
def den: Int = d / gcd
// using euclid's alg https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclid's_algorithm
private def greatestCommonDivisor(a: Int, b: Int): Int = {
if (b == 0) a
else greatestCommonDivisor(b, a % b)
}
override def toString = num + "/" + den
}
def nToFrac(n: Double): Frac = {
require(n != 0, "number must be different to 0 to provide fraction")
def numberOfDecimals(n: Double): Int = (BigDecimal(n) - BigDecimal(n.toInt)).precision
// we obtain the denominator and numerator if we base the decimal to 10^x
val denominator: Int = scala.math.pow(10, numberOfDecimals(n)).toInt
val numerator: Int = (n * denominator).toInt
new Frac(numerator, denominator)
}
nToFrac(0.5) // 1/2
nToFrac(0.2) // 1/5
// Interview Question of the Week: Aug 3, 2020
// 155 of rendezvous with cassidoo
// https://cassidoo.co/newsletter/
// Sort an array of strings based on the number of distinct
// characters that occur in the word
// (followed by the length of the word).
def charNumSort(words: List[String]): String = {
words.zipWithIndex.map {
case (word, index) => List(
word,
word.toList.distinct.length, // first sort by number of distinct chars (asc)
word.length(), // apparently then length of the word (desc)
index // and we can finalize by the index (asc)
)
}.sortBy(
x => (x(1).toString.toInt, -x(2).toString.toInt, x(3).toString.toInt)
).map(x => x(0)).mkString(" ")
}
val words: List[String] = List("Bananas", "do", "not", "grow", "in", "Mississippi")
charNumSort(words)
// res40: String = do in not Mississippi Bananas grow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment