This file contains hidden or 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
# frozen_string_literal: true | |
# Returns compressed string | |
# Remove a maximum length prefix of `str` made of a single character `char` repeating at most 9 times | |
# @param {String} str - String to compress | |
# @param {Integer} times - How many times a single character `char` will repeat | |
# @return {String} Compressed string | |
def compress_string(str, times = 9) | |
compressed = +"" | |
count = 1 |
This file contains hidden or 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
# frozen_string_literal: true | |
# A phrase is a `palindrome` if, after converting all uppercase letters into lowercase letters | |
# and removing all non-alphanumeric characters, it reads the same forward and backward. | |
# Alphanumeric characters include letters and numbers | |
# @param {String} s | |
# @return {Boolean} | |
def palindrome?(s) | |
str = (s.is_a?(String) ? s : s.to_s).delete("^0-9A-Za-z").downcase | |
i = 0 |
This file contains hidden or 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
# frozen_string_literal: true | |
# Given an array of integers `nums` and an integer `target`, | |
# return indices of the two numbers such that they add up to `target` | |
# @param {Integer[]} nums | |
# @param {Integer} target | |
# @return {Integer[]} Indices of the two numbers | |
def two_sum(nums, target) | |
i = 0 | |
options = {} |
This file contains hidden or 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
/** | |
* Calculate the number of days between two dates | |
* @param {Date} date1 | |
* @param {Date} date2 | |
* @returns {number} Number of days between two dates | |
*/ | |
const daysBetween = (date1, date2) => { | |
if (!isValidDate(date1) || !isValidDate(date2)) throw new Error("InvalidArgumentException - Invalid Date!"); | |
// Calculating the time difference in milliseconds of two dates |