Skip to content

Instantly share code, notes, and snippets.

View preston-56's full-sized avatar

Preston Osoro preston-56

View GitHub Profile
# Given a string s, return the number of palindromic substrings in it.
# A string is a palindrome when it reads the same backward as forward.
# A substring is a contiguous sequence of characters within the string.
def countSubsring(s):
"""
:type s: str
:rtype: int
# Given an array of strings `words`, return the first palindromic string in the array.
# If there is no such string, return an empty string `""`.
def firstPalindrome(words):
"""
:type words: List[str]
:rtype: str
"""
def is_palindrome(s):
"""
:type s: str
#Given a string `s`, find the first non-repeating character in it and return its index. If it does not exist, return `-1`.
def firstUniqChar(s):
"""
:type s: str
:rtype: int
"""
char_index = {}
for i, char in enumerate(s):
if char in char_index:
char_index[char] = float('inf')
// ([1,2,3,4,5,6,7],3) === [[1,2,3],[3,4,5],[6,7]]
// ([1,2,3,5,6,7],1) == [[1],[2],[3],[5],[6],[7]]
function splitArray(lst, n) {
let result = [];
for (let i = 0; i < lst.length; i += n) {
result.push(lst.slice(i, i + n));
}
return result;
}
# ([1,2,3,4,5,6,7],3) === [[1,2,3],[3,4,5],[6,7]]
# ([1,2,3,5,6,7],1) == [[1],[2],[3],[5],[6],[7]]
def split_list(lst,n):
result = []
for i in range(0, len(lst),n):
result.append(lst[i:i+n])
return result
# Test cases
print(split_list([1,2,3,4,5,6,7],3))
def containsDuplicate(nums):
num_set = set()
for num in nums:
if num in num_set:
return True
num_set.add(num)
return False
nums1 = [1,2,3,1]
print(containsDuplicate(nums1)); # True
numbers = [1, 1, 2, 3]
def has_repeated_numbers():
return len(set(numbers)) != len(numbers)
print(has_repeated_numbers())
@preston-56
preston-56 / fruits.rb
Last active February 15, 2023 10:54
test
# define a method `solution(a, b, c)` that calculates the total cost of buying all the fruits in a basket.
# The method takes in three parameters:
# a: an array of strings representing the names of the fruits in the basket.
# b: an array of integers representing the prices of each fruit, in the same order as the a array.
# c: a hash that maps the names of the fruits to their corresponding discount percentages.
# The method should return a single integer - the total cost of all the fruits in the basket after applying the discounts.
# Sample input
# a = ["apple", "orange", "banana", "apple", "banana"]
# b = [50, 40, 30]
# c = { "apple" => 10, "orange" => 20, "banana" => 5 }