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
| # 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 } |
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
| numbers = [1, 1, 2, 3] | |
| def has_repeated_numbers(): | |
| return len(set(numbers)) != len(numbers) | |
| print(has_repeated_numbers()) |
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
| 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 |
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
| # ([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)) |
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
| // ([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; | |
| } |
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
| #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') |
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
| # 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 |
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
| # 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 |