text stringlengths 154 7.5k |
|---|
<s>[INST] Write a function to find the shortest distance between two words in a list of words. The two words could be the same and might appear multiple times in the list. [/INST] Here is the code to solve this problem:
```python
import collections
from typing import List
def shortest_word_distance(wordsDict: List[s... |
<s>[INST] Write a Python function that finds and returns the maximum area of islands in a given 2D grid. An island is represented by 1s and the area of the island is the number of connected 1s. Connections are considered only in four directions (up, down, left, right). [/INST] Here is the code to solve this problem:
`... |
<s>[INST] Given the head of a linked list, remove the nth node from the end of the list and return its head. [/INST] Here is the code to solve this problem:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def removeNthFromEnd(head: ListNode, n: int) ... |
<s>[INST] Write a python function to find the smallest repeating substring in a given string. [/INST] Here is the code to solve this problem:
```python
def find_smallest_repeating(s):
for i in range(1, len(s)//2 + 1):
if len(s) % i == 0:
if s[:i] * (len(s) // i) == s:
return s[:... |
<s>[INST] Write a python function to find the number of positive integers that are less than or equal to a given number and are co-prime with the given number.
**Note:** Two numbers are co-prime if their greatest common divisor (GCD) is 1. [/INST] Here is the code to solve this problem:
```python
from math import gcd... |
<s>[INST] Write a python function to find the total number of ways to reach the last cell of a grid from the first cell using only right and down moves. The grid is represented by a rectangular matrix. [/INST] Here is the code to solve this problem:
```python
def grid_Paths(m, n):
if m == 1 or n == 1:
retu... |
<s>[INST] Write a function that takes a string and returns the number of valid parentheses combinations that can be formed using the characters of the string.
A valid parentheses combination is defined as a string where every opening parenthesis "(" has a corresponding closing parenthesis ")" and they are properly nes... |
<s>[INST] Write a function to find the second lowest grade from a list of student grades. [/INST] Here is the code to solve this problem:
```python
def second_lowest_grade(grades):
unique_grades = list(set(grades))
unique_grades.sort()
return unique_grades[1] if len(unique_grades) >= 2 else None
``` </s> |
<s>[INST] Write a python function to calculate the Euclidean distance between two points in a 3D space. [/INST] Here is the code to solve this problem:
```python
import math
def distance_3d(point1, point2):
x1, y1, z1 = point1
x2, y2, z2 = point2
return math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2... |
<s>[INST] Write a python function to find the maximum number of consecutive 1's in the array if you can change at most one 0. [/INST] Here is the code to solve this problem:
```python
def find_max_one(nums):
left = 0
count_zero = 0
max_length = 0
for right in range(len(nums)):
if nums[right] ==... |
<s>[INST] Write a python function that takes a string and returns minimum number of deletions required, so that no two adjacent characters are same. [/INST] Here is the code to solve this problem:
```python
def minDeletions(s: str) -> int:
count = 0
for i in range(1, len(s)):
if s[i] == s[i-1]:
... |
<s>[INST] Write a Python function to find the first missing positive integer in an unsorted array of integers. [/INST] Here is the code to solve this problem:
```python
def find_first_missing(arr):
i = 0
n = len(arr)
while i < n:
correct_pos = arr[i] - 1
if 0 < arr[i] <= n and arr[i] != arr... |
<s>[INST] Write a python function that takes a list of strings and returns a dictionary where the keys are the strings that appear more than once in the list and the values are the counts of those strings. [/INST] Here is the code to solve this problem:
```python
from collections import Counter
def string_counts(stri... |
<s>[INST] Write a python function to check whether a given number is a power of 2 or not. [/INST] Here is the code to solve this problem:
```python
def is_power_of_2(n):
if n <= 0:
return False
while n != 1:
if n % 2 != 0:
return False
n //= 2
return True
``` </s> |
<s>[INST] Write a Python function that takes a list of integers as an input and returns a new list with all duplicate elements removed, while maintaining the original order of the remaining elements. [/INST] Here is the code to solve this problem:
```python
def deduplicate_list(input_list):
seen = set()
result... |
<s>[INST] Write a python function to find the number of times a given number appears in a list of sorted numbers using binary search. [/INST] Here is the code to solve this problem:
```python
def find_occurrence(arr, num):
def find_left(arr, num):
left, right = 0, len(arr)
while left < right:
... |
<s>[INST] Write a Python function to find the convex hull of a set of points using Graham's scan algorithm. [/INST] Here is the code to solve this problem:
```python
import math
def polar_angle(p0, p1):
return math.atan2(p1[1]-p0[1], p1[0]-p0[0])
def distance(p, anchor):
return (p[1]-anchor[1])**2 + (p[0]-an... |
<s>[INST] Write a function to find the maximum length of consecutive characters in a given string. [/INST] Here is the code to solve this problem:
```python
def max_consecutive(s):
max_len = 1
current_len = 1
for i in range(1, len(s)):
if s[i] == s[i-1]:
current_len += 1
max... |
<s>[INST] Write a function to find the number of days in a given month. The function should take the month number as an argument and return the number of days in that month. Assume that the year is not a leap year. [/INST] Here is the code to solve this problem:
```python
def get_month_days(month):
if month in [1,... |
<s>[INST] Write a python function to calculate the number of ways of placing n indistinguishable balls into m distinguishable boxes. [/INST] Here is the code to solve this problem:
```python
import math
def ways_to_place_balls_into_boxes(n, m):
# We are using the stars and bars theorem
# The formula is (n+m-1... |
<s>[INST] Write a python function to find the length of the longest increasing subsequence (LIS) in a given list of integers. [/INST] Here is the code to solve this problem:
```python
def lis(nums):
if not nums:
return 0
n = len(nums)
dp = [1] * n
for i in range(1, n):
for j in range(i)... |
<s>[INST] Write a function to find the maximum length of a substring in a given string where the characters are in consecutive alphabetical order. [/INST] Here is the code to solve this problem:
```python
def find_max_consecutive_substring(s):
max_length = 1
current_length = 1
for i in range(1, len(s)):
... |
<s>[INST] Write a python function to find the shortest path length in a directed graph using breadth-first search (BFS). [/INST] Here is the code to solve this problem:
```python
from typing import List, Tuple
from collections import deque
def shortestPathLength(graph: List[List[int]]) -> int:
if len(graph) == 1:... |
<s>[INST] Write a function to find the k-th largest element in an unsorted list using a min heap. [/INST] Here is the code to solve this problem:
```python
import heapq
def heap_k_largest(nums, k):
min_heap = []
for num in nums:
if len(min_heap) < k:
heapq.heappush(min_heap, num)
elif num > min_heap... |
<s>[INST] Write a python function to find the most common words in a given list of strings. The function should return a list of tuples, where each tuple contains a word and its frequency, sorted by frequency in descending order. [/INST] Here is the code to solve this problem:
```python
from collections import Counter... |
<s>[INST] Write a function to find the largest permutation of a given number.
The function should take an integer as input and return the largest permutation of that number. If the number is a single digit, it should return the number itself. If there are multiple permutations that are the largest, it should return th... |
<s>[INST] Write a python function to find the number of even elements in a given list. [/INST] Here is the code to solve this problem:
```python
def count_even(lst):
count = 0
for i in lst:
if i % 2 == 0:
count += 1
return count
``` </s> |
<s>[INST] Write a function to check if a binary tree is a complete binary tree. A complete binary tree is a binary tree where every level, except possibly the last, is completely filled, and all nodes are as far left as possible. [/INST] Here is the code to solve this problem:
```python
class Node:
def __init__(se... |
<s>[INST] Write a function to check if two given sentences are anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. [/INST] Here is the code to solve this problem:
```python
def are_anagrams(str1, ... |
<s>[INST] Write a function that takes a list of integers as input and returns the maximum amount of water that can be trapped after raining. The function should implement the two-pointer approach to solve the problem efficiently. [/INST] Here is the code to solve this problem:
```python
def trap_water(height):
lef... |
<s>[INST] Write a function to find the next greater element for every element in the given array. The next greater element for an element x is the first greater element on the right side of x in array. Elements for which no greater element exist, consider next greater element as -1. [/INST] Here is the code to solve th... |
<s>[INST] Implement a function that takes a list of integers and returns the maximum sum that can be obtained by robbing houses under the condition that no two adjacent houses can be robbed. [/INST] Here is the code to solve this problem:
```python
def max_rob(nums):
lead = 0
tail = 0
for i, n in enumerate... |
<s>[INST] Write a function to find the number of distinct sub-sequences of a string that are equal to a given target string.
For example, the string "rabbbit" has 3 distinct sub-sequences that are equal to the target "rabbit". [/INST] Here is the code to solve this problem:
```python
def find_num_of_subsequences(s, t... |
<s>[INST] Write a python function to find the median of three numbers. The median is the middle number in a sorted, ascending or descending, list of numbers and can be more descriptive of that data set than the average. If there is an even number of observations, the median will be the average of the two middle numbers... |
<s>[INST] Write a function to concatenate two lists index-wise in Python. [/INST] Here is the code to solve this problem:
```python
def concat_lists(list1, list2):
return [(list1[i], list2[i]) for i in range(len(list1))]
``` </s> |
<s>[INST] Write a python function to find the most frequent character in a string. If there are multiple characters with the same highest frequency, return all of them in a list. The characters in the output list should be in the order of their appearance in the string. [/INST] Here is the code to solve this problem:
... |
<s>[INST] Write a function to find the string that appears most frequently in a list of strings. If there are multiple strings with the same highest frequency, return all of them in a list. [/INST] Here is the code to solve this problem:
```python
from collections import Counter
def most_frequent_strings(strings):
... |
<s>[INST] Write a function to return the largest number that is comprised of some permutation of the digits of the given number. The digits should be arranged in descending order. [/INST] Here is the code to solve this problem:
```python
def largest_permutation(num):
if num == 0:
return 0
num_str = str... |
<s>[INST] Write a python function that checks if a given string is a rotation of another string. [/INST] Here is the code to solve this problem:
```python
def is_rotation(str1, str2):
if len(str1) != len(str2):
return False
temp = str1 + str1
return str2 in temp
``` </s> |
<s>[INST] Write a python function to find the nth Catalan number. The nth Catalan number is defined as C(n) = (2n)! / ((n+1)! * n!) and is used in various counting problems, such as the number of correct bracket sequences of length 2n. [/INST] Here is the code to solve this problem:
```python
def is_catalan(n):
c... |
<s>[INST] Write a function to find the first repeated character in a string. [/INST] Here is the code to solve this problem:
```python
from collections import defaultdict
def first_rep_char(s):
dic = defaultdict(int)
for i in s:
dic[i] += 1
if dic[i] > 1:
return i
return -1
```... |
<s>[INST] Write a python function to find the minimum number of operations to convert a given number to 1. The operations are as follows:
1. If the number is even, you can divide it by 2.
2. If the number is odd, you can either add or subtract 1 from it. [/INST] Here is the code to solve this problem:
```python
def mi... |
<s>[INST] Write a python function to find the common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". [/INST] Here is the code to solve this problem:
```python
def findCommonPrefix(strs):
if not strs:
return ""
shortest_str = min(strs, key=len)
for... |
<s>[INST] Write a function to check if a given string can be split into consecutive descending numbers. Consecutive descending numbers mean that after splitting the string into substrings, each substring can be converted to an integer which is one less than the previous integer. For example, "123122121" can be split in... |
<s>[INST] Write a function that converts a given 24-hour format time to a 12-hour format time. [/INST] Here is the code to solve this problem:
```python
def convert_time(time):
hours, minutes = map(int, time.split(":"))
if hours >= 12:
suffix = "PM"
else:
suffix = "AM"
if hours == 0... |
<s>[INST] Write a function that takes a list of integers as input and returns a list of tuples. Each tuple should contain the integer from the input list and its square. However, if the square of the number is an even number, multiply it by 2 before adding it to the tuple. [/INST] Here is the code to solve this problem... |
<s>[INST] Write a python function that takes a list of integers as input and returns the list sorted in ascending order. However, there is a twist. The function should sort the list using a bubble sort algorithm but with a twist. Instead of swapping elements directly, you should use a temporary variable for the swap. A... |
<s>[INST] Write a function to calculate the length of the hypotenuse of a right-angled triangle using the Pythagorean theorem. [/INST] Here is the code to solve this problem:
```python
import math
def hypotenuse(a, b):
return math.sqrt(a**2 + b**2)
``` </s> |
<s>[INST] Write a function to find the longest substring that contains only unique characters in a given string. [/INST] Here is the code to solve this problem:
```python
def findLongestUniqueSubstr(s):
char_map = {}
left = 0
result = 0
for right in range(len(s)):
if s[right] in char_map:
... |
<s>[INST] Write a python function that finds the maximum value in an array of numbers using divide and conquer technique. [/INST] Here is the code to solve this problem:
```python
def find_max_divide_conquer(arr):
if len(arr) == 1:
return arr[0]
else:
mid = len(arr) // 2
left_max = find... |
<s>[INST] Write a python function to find the maximum sum of a subarray with the length of exactly k. [/INST] Here is the code to solve this problem:
```python
def max_subarray_sum_of_length_k(arr, k):
if k > len(arr):
return None
max_sum = sum(arr[:k])
temp_sum = max_sum
for i in range(k, len(... |
<s>[INST] Write a function that accepts a list of words and returns the length of the longest one. [/INST] Here is the code to solve this problem:
```python
def max_length_word(words):
return len(max(words, key=len))
``` </s> |
<s>[INST] Write a function that takes in a list of integers and returns the length of the longest consecutive elements sequence. [/INST] Here is the code to solve this problem:
```python
def get_longest_consecutive_sequence(nums):
if not nums:
return 0
nums = set(nums)
longest_streak = 0
for nu... |
<s>[INST] Write a python function to check if the given array can be divided into two parts with equal sum. [/INST] Here is the code to solve this problem:
```python
def canTwoPartsEqualSum(A):
total_sum = sum(A)
if total_sum % 2 != 0:
return False
target = total_sum // 2
dp = [False] * (target... |
<s>[INST] Write a function that takes in a list of integers and returns a new list with every even-indexed element doubled and every odd-indexed element halved. [/INST] Here is the code to solve this problem:
```python
def double_even_half_odd(arr):
result = []
for i in range(len(arr)):
if i % 2 == 0:
... |
<s>[INST] Implement a function `find_min_window_substring` that takes two strings as inputs: a source string `s` and a pattern string `t`. The function should return the minimum window in `s` which will contain all the characters in `t`. If there is no such window in `s` that covers all characters in `t`, return the em... |
<s>[INST] Write a function that calculates the minimum number of skips required to reach a meeting on time. Given a list of distances between n meeting rooms, a speed, and the number of hours before the meeting starts, determine the minimum number of rest periods you need to skip in order to arrive at the meeting on ti... |
<s>[INST] Write a python function to check if a given array of integers can be rearranged to form an arithmetic progression. [/INST] Here is the code to solve this problem:
```python
def is_arithmetic_progression(arr):
if len(arr) < 2:
return False
arr.sort()
diff = arr[1] - arr[0]
for i in ran... |
<s>[INST] Write a function called `find_rectangle` which takes a list of tuples (`dots`) representing the coordinates of four points on a 2D plane. The function should return `1` if these four points can form a rectangle, and `0` otherwise. [/INST] Here is the code to solve this problem:
```python
def find_rectangle(d... |
<s>[INST] Write a Python function that finds a specific string in a list of strings, but with a twist. The function should only search through non-empty strings in the list. This function is similar to a search in a sparse array where many elements are empty. If the string is found, return its index; otherwise, return ... |
<s>[INST] Write a function to find the number of different integers in a given string. [/INST] Here is the code to solve this problem:
```python
def numdiffints(word):
intList = []
ci = 0
while ci < len(word):
if word[ci].isdigit():
newint = ""
while word[ci].isdigit():
... |
<s>[INST] Write a function to find the length of the longest consecutive sequence of non-zero elements in a list. Your function should return the length of this sequence. [/INST] Here is the code to solve this problem:
```python
def longest_nonzero_sequence(nums):
max_len = 0
current_len = 0
for num in num... |
<s>[INST] Create a python function that finds the minimum number of operations required to convert one string to another. The operations are: insert a character, delete a character, or substitute one character for another. [/INST] Here is the code to solve this problem:
```python
def min_ops(s1, s2):
m, n = len(s1... |
<s>[INST] Write a function to calculate the sum of all numbers in a given list using recursion. [/INST] Here is the code to solve this problem:
```python
def recursive_sum(lst):
if not lst:
return 0
else:
return lst[0] + recursive_sum(lst[1:])
``` </s> |
<s>[INST] Create a function to find the rolling maximum in a list of integers. The function should return a list of the maximum values up to that point in the list. [/INST] Here is the code to solve this problem:
```python
def rolling_max(numbers):
max_list = []
current_max = numbers[0]
for num in numbers:... |
<s>[INST] Write a python function to calculate the prefix sum of a given array. [/INST] Here is the code to solve this problem:
```python
def calculate_Prefix_Sum(nums):
output = []
previous_number = 0
for i in nums:
new_index = i + previous_number
output.append(new_index)
previous_... |
<s>[INST] Write a function to find the smallest positive integer that is missing from the given array.
For example, the array [1,2,3,4,5] should return 6, as it is the first positive integer that is missing. The array [3,4,-1,1] should return 2. [/INST] Here is the code to solve this problem:
```python
def missing_in... |
<s>[INST] Write a function to check if a given number is a perfect number or not. A perfect number is a positive integer that is equal to the sum of its proper divisors. For example, 28 is a perfect number because its divisors are 1, 2, 4, 7, 14 and their sum is 28. [/INST] Here is the code to solve this problem:
```p... |
<s>[INST] Write a function to find a valid phone number in a given string. A valid phone number is a ten-digit number, which might be prefixed with a '(' and ')' and/or spaces. The function should return the phone number without these extra characters. [/INST] Here is the code to solve this problem:
```python
import r... |
<s>[INST] Write a function to find the maximum element in a list of tuples based on the second element of each tuple. [/INST] Here is the code to solve this problem:
```python
def max_second_element(tuples_list):
if not tuples_list:
return None
return max(tuples_list, key=lambda x: x[1])[1]
``` </s> |
<s>[INST] Write a python function to find the minimum number of insertions required to make a string palindrome. [/INST] Here is the code to solve this problem:
```python
def min_Insertions(str) :
n = len(str)
dp = [[0 for x in range(n+1)] for y in range(n+1)]
t = str[::-1]
for i in range(1, n + 1):
... |
<s>[INST] Write a function to convert binary number in the form of a string to its decimal representation. [/INST] Here is the code to solve this problem:
```python
def bin_to_decimal(binary_string):
decimal = 0
base = 1
binary_string = binary_string[::-1]
for digit in binary_string:
if digit.... |
<s>[INST] Write a python function to find the maximum length of a contiguous subarray with an equal number of 0 and 1. [/INST] Here is the code to solve this problem:
```python
def max_length_of_subarray(arr):
max_length = 0
count = 0
hash_map = {0: -1}
for i in range(len(arr)):
if arr[i] == 0:... |
<s>[INST] Write a python function to check if a given matrix is a valid magic square. A magic square is a square matrix in which the sums of every row, every column, and both main diagonals are all equal. Also, all entries in the matrix should be unique integers from 1 to n^2, where n is the size of the matrix. [/INST]... |
<s>[INST] Write a python function that takes a positive integer n as input and returns the length of the Collatz sequence starting with n. The Collatz sequence is defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next t... |
<s>[INST] Write a function to find the largest number in a list of lists. [/INST] Here is the code to solve this problem:
```python
def largest_in_lists(lists):
return max([max(lst) for lst in lists])
``` </s> |
<s>[INST] Write a function to find the maximum sum of a subarray such that the subarray contains at least one positive integer and the length of the subarray is at least 2. [/INST] Here is the code to solve this problem:
```python
def max_positive_subarray_sum(arr):
max_sum = 0
current_sum = 0
has_positive... |
<s>[INST] Write a function to find the number of unique substrings of a given string. [/INST] Here is the code to solve this problem:
```python
def count_unique_substrings(s):
substrings = set()
for i in range(len(s)):
for j in range(i+1, len(s)+1):
substrings.add(s[i:j])
return len(sub... |
<s>[INST] Write a python function to calculate the sum of the series 1^2 + 2^2 + 3^2 + ... + n^2. [/INST] Here is the code to solve this problem:
```python
def square_series_sum(n):
sum = 0
for i in range(1, n+1):
sum += i**2
return sum
``` </s> |
<s>[INST] Write a function to calculate the running sum of an array of integers. [/INST] Here is the code to solve this problem:
```python
def running_sum(nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
result = []
total = 0
for num in nums:
total += num
result.append... |
<s>[INST] Write a function to find all keys in the dictionary that have the given value. [/INST] Here is the code to solve this problem:
```python
def find_keys_by_value(mydict, value):
keys = [key for key, val in mydict.items() if val == value]
return keys
``` </s> |
<s>[INST] Write a python function to convert a given string to uppercase without using the inbuilt `upper()` function. [/INST] Here is the code to solve this problem:
```python
def convert_to_upper(string):
upper_string = ""
for char in string:
if 'a' <= char <= 'z':
# Convert lowercase to ... |
<s>[INST] Write a function to find the largest rectangular area that can be formed within a given histogram. [/INST] Here is the code to solve this problem:
```python
def maxRectangleArea(height):
stack = [-1]
max_area = 0
for i in range(len(height)):
while stack[-1] != -1 and height[stack[-1]] >= height[i]:... |
<s>[INST] Write a function to calculate the area of a triangle given the coordinates of its three vertices.
The function should take six parameters, representing the x and y coordinates of the three vertices: `(x1, y1)`, `(x2, y2)`, and `(x3, y3)`. It should return the area of the triangle formed by these points.
**F... |
<s>[INST] Write a function to find the maximum possible value that can be formed by performing at most one swap operation on any two digits of a given number. [/INST] Here is the code to solve this problem:
```python
def max_possible_value(num):
num_str = str(num)
max_num = num
for i in range(len(num_str))... |
<s>[INST] Write a python function that takes a list of integers as an argument and returns a list of tuples where each tuple contains two elements: the integer and its square root (rounded to two decimal places). If the integer is negative, the square root should be represented as None. [/INST] Here is the code to solv... |
<s>[INST] Write a function that finds the longest zigzag subsequence in a given list of integers. A zigzag sequence is defined as a sequence where each element is either greater than both neighbors or smaller than both neighbors. [/INST] Here is the code to solve this problem:
```python
def longest_zigzag_subsequence(... |
<s>[INST] Write a python function to find the largest power of 2 less than or equal to a given number. [/INST] Here is the code to solve this problem:
```python
def largest_power_of_2(n):
power = 1
while power * 2 <= n:
power *= 2
return power
``` </s> |
<s>[INST] Write a python function to find the largest sum of non-adjacent elements in a list of numbers. [/INST] Here is the code to solve this problem:
```python
def largest_non_adjacent_sum(nums):
incl = 0
excl = 0
for i in nums:
new_excl = excl if excl > incl else incl
incl = excl + i
... |
<s>[INST] Write a function that takes a string as input and returns the minimum number of adjacent swaps required to make the string a palindrome. If it's not possible to make the string a palindrome, return -1. A palindrome is a string that reads the same backward as forward. [/INST] Here is the code to solve this pro... |
<s>[INST] Write a python function to find the number of times a given value appears in an array. The function should return the count of occurrences of the value in the array. [/INST] Here is the code to solve this problem:
```python
def count_value_occurrences(arr, value):
count = 0
for num in arr:
if... |
<s>[INST] Write a python function that accepts three integers as an input and returns the smallest number among them. [/INST] Here is the code to solve this problem:
```python
def minimum_of_three(a, b, c):
return min(a, b, c)
``` </s> |
<s>[INST] Write a function to find the third word in the text that immediately follows the first and second words. [/INST] Here is the code to solve this problem:
```python
def find_third_word(text: str, first: str, second: str) -> str:
words = text.split()
for i in range(2, len(words)):
if words[i - 2... |
<s>[INST] Write a function called `fibonacci_seq` that generates the Fibonacci sequence up to the nth number. The Fibonacci sequence is a series of numbers where a number is the addition of the last two numbers, starting with 0 and 1. [/INST] Here is the code to solve this problem:
```python
def fibonacci_seq(n):
... |
<s>[INST] Write a python function to find the degree of an array. The degree of an array is defined as the maximum frequency of any one of its elements. Your function should return the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums. [/INST] Here is the code to solve this p... |
<s>[INST] Write a function to find the kth smallest element in an unsorted array using QuickSelect algorithm. [/INST] Here is the code to solve this problem:
```python
import random
def quickSelect_kthSmallest(arr, k):
if k > 0 and k <= len(arr):
pos = random_partition(arr, 0, len(arr) - 1)
if pos... |
<s>[INST] Create a python function that finds the maximum and minimum elements in an unsorted list. Then, calculate and return the difference between them. This function should be robust against empty lists by returning None in such cases. [/INST] Here is the code to solve this problem:
```python
def find_max_min_diff... |
<s>[INST] Write a python function to check if a given Tic-Tac-Toe board configuration has a winner or not. The function should return 'X' if player X wins, 'O' if player O wins, 'Draw' if the game is a draw, and 'Pending' if the game is still in progress. The board is represented as a 3x3 list of lists, where each cell... |
<s>[INST] Write a Python function that takes a list of integers as input and returns the length of the longest subarray with a product greater than zero. [/INST] Here is the code to solve this problem:
```python
def longest_positive_product_subarray(nums):
res, n = 0, len(nums)
section = [[0]]
for i i... |
<s>[INST] Write a python function to find the total number of ways to reach the last cell of a grid from the first cell using only right and down moves. [/INST] Here is the code to solve this problem:
```python
def total_ways(m, n):
dp = [[0 for _ in range(n)] for _ in range(m)]
for i in range(m):
dp[i... |
End of preview. Expand in Data Studio
README.md exists but content is empty.
- Downloads last month
- 5