K-nearest Points to Origin
Function Name: findKNearestPoints
Description
Write a function that finds the 'k' nearest points to the origin (0, 0) in a 2D plane. The points are provided as a list of tuples (x, y), representing the cartesian coordinates of the points. You should return a list of 'k' tuples that are closest to the origin, sorted by their Euclidean distance from the origin. In case of a tie in distances, sort the points by their x-coordinates, and if those are the same, by their y-coordinates.
Requirements
- The function should take two parameters: a list of tuples representing the points, and an integer 'k'.
- Euclidean distance should be used to calculate the distance between a point and the origin.
- Do not use any built-in sort functions from the language's standard library for sorting by distance. However, you may use built-in functions for sorting the result in the case of distance ties.
- The function should handle edge cases gracefully, such as 'k' being larger than the number of points provided.
Examples
Given the points [(1,2), (3,4), (0,-1)], and k = 2, the function should return [(1,2), (0,-1)].Given the points [(1,3), (3,3), (2,-1), (2,1)], and k = 3, the function should return [(2,-1), (2,1), (1,3)].
String Permutations
Fri Oct 11 2024
Write a function that takes a single string parameter and returns all possible permutations of the characters in the string.The string will contain only unique characters and will not be empty.The permutations can be returned in any order.If the string is a single character, the function should return an array containing just that character.
Substring with Concatenation of All Words
Sun Oct 13 2024
Write a function `findSubstring(s, words)` that takes a string `s` and an array of strings `words` as parameters.All strings in `words` are of the same length.The function should return all starting indices in `s` where a concatenation of each word in `words` exactly once forms a substring.The indices should be returned in ascending order.The function should handle overlapping words and words can appear in any order.