Skip to content

Instantly share code, notes, and snippets.

@omokehinde
omokehinde / rotated.py
Last active November 7, 2022 15:29
function that takes 2 lists as arguments and returns True if the list are rotated version of each other or False otherwise.
# A function that takes 2 lists as arguments and returns True if the list are rotated version of each other or False otherwise.
def rotated(list1, list2):
if len(list1) != len(list2) or list1[0] == list2[0]:
return False
first_elem_list1 = list1[0]
if first_elem_list1 in list2:
index_first_elem_list2 = list2.index(first_elem_list1)
else: return False
@omokehinde
omokehinde / isValid.js
Created June 2, 2022 00:19
A function that checks if a string containing brackets is valid.
let isValid = (s) => {
let bracket_obj = {
"[":"]",
"(":")",
"{":"}"
};
let open_brackets = [];
for (let i = 0; i < s.length; i++) {
if (s[i] in bracket_obj) {
open_brackets.push(s[i]);
@omokehinde
omokehinde / isValidString.py
Last active June 2, 2022 00:11
Write a function that returns True or False if the string contains opening and closing brackets in the right order
def isValid(s):
bracket_dic = {
"[":"]",
"{":"}",
"(":")"
}
open_bracket = []
for i in s:
if i not in bracket_dic and len(open_bracket) == 0: return False
const pointlessFunction = (input: string): string => {
let oneToThreeObj = {
1: "one", 2: "two", 3: "three"
};
let output = "";
[...input].forEach((char)=>{
// ts-ignore
if (char in oneToThreeObj) output+=oneToThreeObj[char];
});
def WordSplit(strArr)
# code goes here
dict = strArr[1].split(",")
for i in dict
for j in dict
if i+j == strArr[0]
return "#{i}, #{j}"
end
end
def StringReduction(str)
# code goes here
hash = {"ab":"c", "ac":"b", "bc":"a",
"ba":"c", "ca":"b", "cb":"a"}
count = 0
while str != str[0]*str.length
if hash.has_key? :"#{str[count..count+1]}"
str = str.sub(str[count..count+1], hash[:"#{str[count..count+1]}"])
count = 0
@omokehinde
omokehinde / absolutePermutation.py
Created February 10, 2022 00:44
HackerRank Absolute Permutation challenge
# We define to be a permutation of the first natural numbers in the range . Let denote the value at position in permutation using -based indexing.
# is considered to be an absolute permutation if holds true for every .
# Given and , print the lexicographically smallest absolute permutation . If no absolute permutation exists, print -1.
# Example
# Create an array of elements from to , . Using based indexing, create a permutation where every . It can be rearranged to so that all of the absolute differences equal :
@omokehinde
omokehinde / kaprekarNumbers.py
Last active February 10, 2022 23:46
HackerRank Modified Kaprekar Numbers challenge
# A modified Kaprekar number is a positive whole number with a special property. If you square it, then split the number into two integers and sum those integers, you have the same value you started with.
# Consider a positive whole number with digits. We square to arrive at a number that is either digits long or digits long. Split the string representation of the square into two parts, and . The right hand part, must be digits long. The left is the remaining substring. Convert those two substrings back to integers, add them and see if you get .
# Example
# First calculate that . Split that into two strings and convert them back to integers and . Test , so this is not a modified Kaprekar number. If , still , and . This gives us , the original .
@omokehinde
omokehinde / laptopRental.py
Last active November 7, 2022 17:25
Find minimum laptop a school needs to rent out to students
# you're given a list of time intervals during which students at a school need a laptop.
# These time intervals are represented by pairs of integers [start, end], where 0 <= start < end.
# However, start and end don't represent real times; therefore, they may be greater than 24.
# No two students can use a laptop at the same time, but immediately after a student is done
# using a laptop, another student can use that same laptop. For example, if one student rents a
# laptop during the time interval [0, 2], another student can rent the same laptop during any
# time interval starting with 2. Write a function that returns the minimum number of laptops
# that the school needs to rent such that all students will always have access to a laptop when
# they need one. laptopRentals(times)
# Parameters times: Array (of Array (of Integers)) - A 2D array containing the times the
@omokehinde
omokehinde / beautifulTriplets.py
Last active February 1, 2022 23:28
HackerRank Beautiful Triplets challenge
# Given a sequence of integers , a triplet is beautiful if:
# Given an increasing sequenc of integers and the value of , count the number of beautiful triplets in the sequence.
# Example
# There are three beautiful triplets, by index: . To test the first triplet, and .
# Function Description