Exercises | math | | Search

python codekatas solutions

Cell 0

The palindrome(string) function checks if the input string, with all lower-case characters, is the same when reversed. It does this by comparing characters from the start and end of the string, working its way towards the center, and returns True if no mismatches are found.

Cell 1

The merge(a, b) function merges two sorted lists a and b in descending order into a single list, with a time complexity of O(n log n). It does this using a list comprehension that pops the largest element from either list at each iteration, as implemented by return [max(a, b).pop(0) for _ in a+b].

Cell 2

The last_word_length function takes a string as input and returns the length of its last word. It does this by counting non-space characters from the end of the string until it encounters a space.

Cell 3

The find_in_sorted function performs a binary search on a sorted list of numbers to find a target value, returning its index if found, and -1 otherwise. It uses a binary search algorithm to efficiently search through the list by repeatedly dividing the search interval in half.

Cell 4

The simplify_path function takes a Unix-style file path as input, splits it into tokens, and uses a stack to remove unnecessary directory references, returning the simplified path. The function handles various edge cases, including parent directories (..), multiple parent directories (...), and trailing slashes.

Cell 5

The create_max function takes an input number num as a string and an integer k as parameters, returning the maximum number that can be formed by removing digits from num and keeping at most k digits. The function uses a stack to iteratively select the largest digits from num until a maximum number of k digits have been chosen.

Cell 6

The code implements a singly linked list node class (ListNode) and a function (reverse_list) to reversely iterate through the linked list, swapping the next node reference, and returning the new head of the reversed list.

Alternatively, you could also condense it into two sentences:

The ListNode class represents a node in a singly linked list, with methods for initialization, string representation, and equality comparison. The reverse_list function takes the head of a linked list as input and returns the new head of the reversed list, achieved through iterative traversal and node reference swapping.

Cell 7

The ListNode class represents a node in a singly linked list with attributes and methods for initialization, string representation, and equality comparison. The reverse_list function is a recursive function that reverses a singly linked list by updating the next attribute of each node to point to the previous node in the reversed list.

Cell 8

The max_profit function calculates the maximum possible profit from a list of stock prices by iterating through the list and adding up the differences between each pair of consecutive prices. The function returns 0 as the total profit if the input list is empty, contains only one price, or has prices in descending order.

Cell 9

The subsets function generates all possible subsets of a given list by recursively building upon itself, removing and adding elements to create new subsets. It returns a list containing all possible subsets of the input list, including the empty set and the original list itself.

Cell 10

The string_permutations(s) function generates all possible permutations of a given string s and returns them in a sorted list. If the input string is empty, it returns a list containing an empty string, otherwise it recursively generates all permutations of the string.

Cell 11

The QuickSort algorithm is a divide-and-conquer sorting technique that uses the partition function to reorder an array such that all elements less than or equal to the pivot are on its left. The quicksort function recursively sorts the left and right subarrays generated by partitioning the input array until the base case is reached, where the subarray contains only one element or is empty.

Cell 12

The mergesort algorithm is implemented through two main functions: merge which combines two sorted lists and mergesort which recursively sorts a list using the mergesort algorithm.

Cell 13

The shortest_path function finds the shortest path between two nodes (u and v) in a graph g by performing a breadth-first search (BFS) traversal. It returns the path as a list of nodes if found, otherwise returns -1.