Skip to content

Instantly share code, notes, and snippets.

View binos30's full-sized avatar

Venus Lumanglas binos30

View GitHub Profile
@binos30
binos30 / compress_string.rb
Created August 18, 2024 08:08
String Compression
# 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
@binos30
binos30 / palindrome.rb
Created August 18, 2024 07:19
Valid Palindrome
# 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
@binos30
binos30 / two_sum.rb
Last active October 15, 2024 00:22
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target
# 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 = {}
@binos30
binos30 / daysBetween.js
Last active March 18, 2025 07:24
Calculate the number of days between two dates in JavaScript
/**
* 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