conversations
listlengths
2
2
[ { "from": "human", "value": "Write a function that will return the number of distinct Isograms up to a given length. An Isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that meets this criteria." }, { "from": "gpt", "value": "Here is the code ...
[ { "from": "human", "value": "Write a python function to find the kth smallest number in the Cartesian product of two given lists. The Cartesian product of two lists is the set of all possible pairs (a, b) where a is an element of the first list and b is an element of the second list. The function should tak...
[ { "from": "human", "value": "Create a python function that takes a string of numbers in word format and converts it to an integer. The function should recognize the following words: 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', and 'nine'." }, { "from": "gpt", "value": ...
[ { "from": "human", "value": "Write a python function to find the number of distinct words in a given string. A word is defined as a sequence of non-space characters." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n```python\ndef distinct_words(s):\n words = s.split()\n...
[ { "from": "human", "value": "Write a python function to find the number of distinct subsequences in a given string.\n\nA subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. For e...
[ { "from": "human", "value": "Write a python function to check if the given string can be converted to a palindrome by removing at most one character.\n\nA palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward." }, { "from": "gpt", "value":...
[ { "from": "human", "value": "Write a function that checks if a given string is a permutation of a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward. A permutation is a rearrangement of letters. The palindrome does not need to be limit...
[ { "from": "human", "value": "Write a Python function to split a string into a list of strings, each containing a single character. For an empty string, return an empty list." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n```python\ndef char_split(text: str) -> list:\n ...
[ { "from": "human", "value": "Write a Python function that takes an integer as input and returns the minimum non-negative integer x such that n + x is a beautiful number. A number is considered beautiful if the sum of its digits is less than or equal to a given target.\n\nThe function should be named `get_be...
[ { "from": "human", "value": "Write a function to count the number of words in a list that can be formed by characters of a given string.\n\nThe function should take two parameters: a string of allowed characters and a list of words. It should return the number of words in the list that can be formed using o...
[ { "from": "human", "value": "Write a function to find the number of steps required to transform a given string into a palindrome by flipping characters from the start to the end. The function should return the minimum number of flips required.\n\nA flip operation is defined as changing any character at inde...
[ { "from": "human", "value": "Write a function to find the number of balanced strings in a given string. A balanced string is defined as a string where the number of 'L' characters is equal to the number of 'R' characters." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n``...
[ { "from": "human", "value": "Write a python function to find the longest possible word chain from a given list of words. A word chain is a sequence of words [word_1, word_2, ..., word_k] where k >= 1, and word_1 is the predecessor of word_2, word_2 is the predecessor of word_3, and so on. A word is consider...
[ { "from": "human", "value": "Write a function to find the maximum width of a binary tree. The width of a tree is the number of nodes on the level with the maximum number of nodes. The tree is represented as a list where the index of any element e at position i has its left child at position 2*i+1 and right ...
[ { "from": "human", "value": "Write a python function to check if a list is a palindrome.\n\nA palindrome is a sequence that reads the same backward as forward. For example, [1, 2, 3, 2, 1] is a palindrome list." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n```python\nde...
[ { "from": "human", "value": "Write a Python function to find the number of distinct prime factors of a number.\n\nA prime factor is a factor that is a prime number. A distinct prime factor is a prime number that divides the number exactly, without leaving a remainder, and appears only once in the factorizat...
[ { "from": "human", "value": "Write a python function to find the number of steps required to reduce a given number to 1 by the following rules:\n- If the number is even, divide it by 2.\n- If the number is odd, either increment or decrement it by 1." }, { "from": "gpt", "value": "Here is the cod...
[ { "from": "human", "value": "Write a function to find the number of arithmetic triplets in a list of integers. An arithmetic triplet is a set of three numbers that are consecutive terms of an arithmetic sequence. For example, [2, 4, 6] and [10, 5, 0] are arithmetic triplets with differences of 2 and -5, res...
[ { "from": "human", "value": "Write a Python function that takes a string as input and returns the shortest possible encoding of the string. The encoding rules are as follows:\n- If a substring of length n can be repeated k times to form the original string, then the substring can be encoded as k[n] where k ...
[ { "from": "human", "value": "Write a python function that takes a string as an input and returns the greatest letter that appears in both lowercase and uppercase in the string. If there is no such letter, return an empty string." }, { "from": "gpt", "value": "Here is the code to solve this probl...
[ { "from": "human", "value": "Write a function that takes in a string and returns a list of instructions based on stack operations to build the string. You can only use two operations: 'Push' which adds a character to the end of the string, and 'Pop' which removes the last character of the string. The functi...
[ { "from": "human", "value": "Write a python function that checks if a given list of words can form a meaningful sentence in English considering the basic grammatical rules. The function should only return True if:\n1. The list starts with a capital letter (a noun or proper noun).\n2. The list ends with a pu...
[ { "from": "human", "value": "Write a python function to find the minimum number of operations required to convert a string into palindrome. An operation is defined as inserting any character at any position in the string." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n``...
[ { "from": "human", "value": "Write a python function to calculate the number of steps to reduce a number to zero by performing the following operations:\n- If the current number is even, divide it by 2.\n- If the current number is odd, subtract 1 from it." }, { "from": "gpt", "value": "Here is t...
[ { "from": "human", "value": "Write a python function that checks if a given list of words is a valid word ladder. A word ladder is a sequence of words where each adjacent pair of words differ by exactly one letter. For instance, \"cold\" -> \"cord\" -> \"card\" -> \"ward\" is a valid word ladder because the...
[ { "from": "human", "value": "Write a Python function that takes a string as input and checks if it's a valid IPv4 address. An IP address in string \"x.x.x.x\" is considered valid if the following conditions are met:\n- It consists of four parts separated by dots.\n- Each part is between 0 and 255.\n- No par...
[ { "from": "human", "value": "Write a Python function to find the nth element of the Cantor sequence.\nThe Cantor sequence is a sequence of fractions that lists all positive rational numbers in such a way that each number appears exactly once. The sequence is constructed by starting with the fraction 1/1 and...
[ { "from": "human", "value": "Write a function that takes a string as an input and returns the number of vowels in the string." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n```python\ndef vowel_counter(string):\n vowels = ['a', 'e', 'i', 'o', 'u']\n count = 0\n ...
[ { "from": "human", "value": "Write a function to find the index of the smallest repeating substring in a given string.\n\nGiven a string s, find the index of the smallest repeating substring. If there are multiple repeating substrings at the same index, return the index of the lexicographically smallest one...
[ { "from": "human", "value": "Write a function that counts the number of nodes in a binary tree. The binary tree is represented as a series of nested lists, where each list represents a node and contains two more lists representing the left and right child, or None if the child is not present. Each node also...
[ { "from": "human", "value": "Write a function that takes a list of strings representing a series of arithmetic operations and returns the result of evaluating the operations. The operations can be addition (+), subtraction (-), multiplication (*), or division (/). The function should handle parentheses to i...
[ { "from": "human", "value": "Write a python function to find the number of balanced parentheses in a string.\n\nBalanced parentheses are defined as either an empty string or a string that consists of two parts: a left parenthesis, a balanced string, a right parenthesis, and then another balanced string.\n\n...
[ { "from": "human", "value": "Write a function to calculate the score of a simplified version of the game \"Parens\". The game works as follows: you are given a string of parentheses. For every pair of parentheses, if the parentheses are balanced and form a valid pair, you earn a point. The score is doubled ...
[ { "from": "human", "value": "Write a python function to find the number of ways to express a given number as a sum of consecutive positive integers.\n\nFor example, given the number 9, there are three ways to express it as a sum of consecutive positive integers: 2+3+4, 4+5, and 8+1." }, { "from": "g...
[ { "from": "human", "value": "Write a python function to count the number of ways to decode a message encoded in a given format. A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1, 'B' -> 2, ..., 'Z' -> 26. Given a non-empty string containing only digits, ...
[ { "from": "human", "value": "Write a function that takes an integer n as input and returns the nth number in the Lucas sequence. The Lucas sequence is defined as follows: L(0) = 2, L(1) = 1, and L(n) = L(n-1) + L(n-2) for n >= 2. For example, the first few numbers in the Lucas sequence are 2, 1, 3, 4, 7, 11...
[ { "from": "human", "value": "Write a function to find the Levenshtein distance between two strings using the iterative algorithm.\n\nThe Levenshtein distance is a string metric for measuring the difference between two sequences. It is calculated as the minimum number of single-character edits (insertions, d...
[ { "from": "human", "value": "Write a python function to determine whether a given string is a palindrome or not. A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward." }, { "from": "gpt", "value": "Here is the code to solve this problem:...
[ { "from": "human", "value": "Write a python function to find the number of valid parentheses strings of given length n. A parentheses string is valid if:\n- Any left parenthesis '(' must have a corresponding right parenthesis ')'.\n- Any right parenthesis ')' must have a corresponding left parenthesis '('.\...
[ { "from": "human", "value": "Write a python function to check if a given string is an interleaving of two other given strings. A string is an interleaving of two strings if it can be formed by merging two strings by adding the characters of the two strings in any order.\n\nFor example, \"dbca\" is an interl...
[ { "from": "human", "value": "Write a function that determines the minimum number of edges to cut in a brick wall to create a passage. The wall is represented as a list of lists, where each sublist represents a row of bricks in the wall. Each brick's length is represented by an integer in the sublist. The go...
[ { "from": "human", "value": "Write a function that generates the smallest lexicographical string by performing operations on a given string. The operations include adding a value 'a' to all odd-indexed characters in the string (considering 0-based indexing) and rotating the string by 'b' positions to the ri...
[ { "from": "human", "value": "Write a python function to find the minimum number of operations required to convert a string into a palindrome. An operation is defined as inserting any character at any position in the string." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n...
[ { "from": "human", "value": "Write a function to find the number of occurrences of each character in a given string. The function should return a dictionary where the keys are the characters and the values are the number of occurrences." }, { "from": "gpt", "value": "Here is the code to solve th...
[ { "from": "human", "value": "Write a function that takes two strings and returns the number of common characters between them.\n\nGiven two strings `str1` and `str2`, the function `count_common_characters` should return the total number of characters that appear in both strings. The count for each character...
[ { "from": "human", "value": "Write a python function to find the index of the first occurrence of a word in a sentence that is a prefix of a given search word.\n\nFor Example:\n- Given the sentence \"the quick brown fox jumps over the lazy dog\" and the search word \"quickly\", the function should return 1 ...
[ { "from": "human", "value": "Create a function to find the maximum number of fruit that can be collected by two baskets according to the following rules:\n- Each basket can collect only one type of fruit.\n- The function should take a list of fruits represented by strings and return the maximum number of fr...
[ { "from": "human", "value": "Write a function to evaluate the value of a postfix expression, also known as Reverse Polish Notation (RPN).\n\nA postfix expression is a mathematical notation in which every operator follows all of its operands. For example, the infix expression (a + b) * c would be written as ...
[ { "from": "human", "value": "Write a function to find the number of distinct binary strings that can be formed by rearranging the bits in a given binary string. Two binary strings are considered distinct if they are not identical." }, { "from": "gpt", "value": "Here is the code to solve this pro...
[ { "from": "human", "value": "Write a python function to find the minimum number of operations to convert a given number into 1 by any of the following operations:\n1. Subtracting 1 from it.\n2. If the number is divisible by a divisor greater than 1, you may choose to divide the number by this divisor." },...
[ { "from": "human", "value": "Write a function to find the number of distinct islands in a given 2D binary matrix. An island is a group of 1s (representing land) connected 4-directionally (horizontal or vertical). Two islands are considered the same if and only if one island can be translated (without rotati...
[ { "from": "human", "value": "Write a python function that takes a binary tree and returns the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root." }, { "from": "gpt", "value": "Here...
[ { "from": "human", "value": "Write a function to generate the first n Catalan numbers. Catalan numbers are a sequence of natural numbers that have many applications in combinatorial mathematics. The nth Catalan number can be directly calculated using the formula:\n\nC(n) = (2n)! / ((n+1)! * n!)\n\nFor examp...
[ { "from": "human", "value": "Write a python function to find the nth triangular number. A triangular number or triangle number counts objects arranged in an equilateral triangle. The nth triangular number is the number of dots in the triangular arrangement with n dots on a side, and is equal to the sum of t...
[ { "from": "human", "value": "Write a function to find the smallest leaf in a binary tree represented as a string where '0' represents a leaf node and '1' represents a node with two children. The binary tree is represented as a string of '0's and '1's where each character represents a node and the position r...
[ { "from": "human", "value": "Write a python function to find the n’th heptagonal number. A heptagonal number is a figurate number that represents a heptagon. The nth heptagonal number is given by the formula: n(5n - 3)/2." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n``...
[ { "from": "human", "value": "Write a python function to find the number of distinct islands in a grid. An island is a group of connected 1s (representing land) not connected to the border. Each cell in the grid can either be 0 (water) or 1 (land). Two cells are considered connected if they are adjacent hori...
[ { "from": "human", "value": "Write a python function to find the number of distinct subsequences of a given string. A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining ch...
[ { "from": "human", "value": "Write a python function to find the number of ways to write a given number as a sum of consecutive positive integers.\n\nFor example, the number 9 can be expressed as the sum of consecutive numbers in three ways: 2+3+4, 4+5, and 9. Therefore, the function should return 3 for an ...
[ { "from": "human", "value": "Write a python function to check if a sentence is a pangram. A pangram is a sentence that contains every single letter of the alphabet at least once. For example, the sentence \"The quick brown fox jumps over the lazy dog\" is a pangram, because it uses the letters A-Z at least ...
[ { "from": "human", "value": "Write a python function to find the number of distinct permutations of a string with duplicate characters. The input string will consist of lowercase English letters only." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n```python\nfrom math im...
[ { "from": "human", "value": "Write a function to find the number of islands in a given grid. An island is formed by connected 1s (representing land) and the grid's four edges are considered to be surrounded by water. You should return the count of islands that exist in the grid." }, { "from": "gpt",...
[ { "from": "human", "value": "Write a python function to find the number of unique permutations that can be generated from a given string.\n\nThe function should take a string as input and return the number of unique permutations that can be made from the characters of the string. The string can contain dupl...
[ { "from": "human", "value": "Write a python function to find the minimum number of operations to convert a number X to Y. An operation is defined as either multiplying X by 2 or adding 1 to X." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n```python\ndef convert_number(x...
[ { "from": "human", "value": "Write a function to count the number of times a pattern appears in a given text. The function should return the number of matches found. Use regular expressions to find the matches." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n```python\nim...
[ { "from": "human", "value": "Write a function that takes a string as an input, which is supposed to represent a set of sets (nested sets) of integers. The outer sets are denoted by curly braces, and the inner sets are also denoted by curly braces, separated by commas. The goal is to extract the unique integ...
[ { "from": "human", "value": "Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. You can i...
[ { "from": "human", "value": "Write a function that returns the nth number in the Fibonacci sequence.\n\nThe Fibonacci sequence is a series of numbers in which each number (after the first two) is the sum of the two preceding ones. The sequence starts with 0 and 1.\n\nExample of the Fibonacci sequence: 0, 1,...
[ { "from": "human", "value": "You are given a string and you have to find its first word. The first word is defined as the first sequence of non-space characters in the string. Write a function to accomplish this task." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n```pyt...
[ { "from": "human", "value": "Write a function that takes a list of strings as input and returns a list of integers. Each string in the input list represents a range of numbers in the format \"start-end\". The function should return a list of integers that includes all the integers in the given ranges.\n\nFo...
[ { "from": "human", "value": "Write a function to calculate the number of 1 bits in the binary representation of a number. This is often referred to as the population count or popcount." }, { "from": "gpt", "value": "Here is the code to solve this problem:\n```python\ndef popcount(n):\n count ...
[ { "from": "human", "value": "Write a python function to find the number of distinct binary strings that can be formed by deleting a single character from a given binary string.\n\nFor example, if the input string is \"101\", then the output should be 3, as there are 3 distinct strings that can be formed by ...
[ { "from": "human", "value": "Write a python function to find the number of islands in a given 2D grid map. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water." }, { "from": "gpt...
[ { "from": "human", "value": "Write a python function to find the number of distinct subsequences in a given string.\n\nA subsequence of a string is obtained by deleting zero or more characters from the string, without changing the order of the remaining characters.\n\nFor example, the string \"abc\" has the...
[ { "from": "human", "value": "Write a function to find the diagonal elements of a given square matrix.\n\nA square matrix is a matrix with the same number of rows and columns. \nThe diagonal elements of a matrix are the elements where the row index is equal to the column index. \n\nFor example, in the matrix...
[ { "from": "human", "value": "Write a python function to calculate the score of a given string of parentheses. The score is calculated based on the following rules:\n- An empty pair of parentheses \"()\" has a score of 1.\n- If S is a concatenation of A and B, then the score of S is the sum of the scores of ...
[ { "from": "human", "value": "Write a function to find the number of distinct islands in a grid. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Each island is labeled with a distinc...
[ { "from": "human", "value": "Write a python function to find the number of special equivalent groups in a list of strings. Two strings are special equivalent if the number of occurrences of each letter in one string is equal to the number of occurrences of the corresponding letter in the other string, and t...
[ { "from": "human", "value": "Write a function to convert a decimal number to a binary string and vice versa. Your function should take two parameters: a number (either integer for decimal to binary conversion or a binary string for binary to decimal conversion) and a conversion type (either 'to_binary' or '...
[ { "from": "human", "value": "Write a Python function that takes two strings and returns the length of the longest common subsequence (LCS) between them.\n\nA subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elemen...
[ { "from": "human", "value": "Write a function to simulate a game of \"Stones on a Table\". There are N stones on a table, labeled from 1 to N. The goal of the game is to remove all stones from the table. On each turn, you can remove a stone and its adjacent stones (if any) from the table. The game ends when...
[ { "from": "human", "value": "Write a python function to check if the given sequence of moves for a robot is circular or not. A move is considered circular if the robot ends up at the same position it started from after executing all the moves. The robot starts at position (0, 0) and moves according to the f...
[ { "from": "human", "value": "Write a function to check if a given binary tree is a valid binary search tree (BST).\n\nA valid BST is defined as follows:\n- The left subtree of a node contains only nodes with keys less than the node's key.\n- The right subtree of a node contains only nodes with keys greater ...
[ { "from": "human", "value": "Write a function that takes a list of strings (words) and returns the number of unique anagrams that can be made from those words. 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 onc...
[ { "from": "human", "value": "Write a python function that takes a list of integers and returns a list of intervals. Each interval is represented as a tuple, where the first element is the start of the interval and the second element is the end of the interval. An interval is created by consecutive integers ...
[ { "from": "human", "value": "Write a function that checks if a given string is a palindrome using a stack data structure. A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward." }, { "from": "gpt", "value": "Here is the code to solve this...
[ { "from": "human", "value": "Write a function that calculates the number of substrings containing only one distinct letter. Given a string `s`, return the total number of such substrings.\n\nFor example, given the string \"aaaba\", the function should return 8, as there are 3 substrings of \"a\" with length...
[ { "from": "human", "value": "Write a python function to find the minimum number of steps required to make two strings equal, where you can perform three operations: insert a character, delete a character, or replace a character." }, { "from": "gpt", "value": "Here is the code to solve this probl...
[ { "from": "human", "value": "Write a python function to check whether a given number is a perfect number or not. A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For example, 28 is a perfect number because its divisors are 1, 2, 4, 7, 14,...
[ { "from": "human", "value": "Write a python function to find the number of islands in a given 2D grid of 1's (representing land) and 0's (representing water). An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are...
[ { "from": "human", "value": "Write a function to calculate the length of the longest common subsequence (LCS) between two strings.\n\nA subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. The longest common...
[ { "from": "human", "value": "Write a python function to find the number of unique words in a sentence. The function should return a dictionary where keys are the unique words and values are their frequency in the sentence. Note that the function should be case-sensitive." }, { "from": "gpt", "va...
[ { "from": "human", "value": "Write a python function that checks if a given tree is a binary search tree (BST).\n\nA binary search tree (BST) is a binary tree where every node follows the following rules:\n- The left subtree of a node contains only nodes with keys less than the node's key.\n- The right subt...
[ { "from": "human", "value": "Write a function to find the character that is added to a string to make it a palindrome.\n\nA palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).\n\nFor example, \"rac...
[ { "from": "human", "value": "Write a function that takes a list of words and returns the length of the longest word chain. A word chain is a sequence of words where each word is a predecessor of the next word. A word is considered a predecessor of another if it can be transformed into the next word by addin...
[ { "from": "human", "value": "Write a function that takes an integer as input and returns its hexadecimal representation as a string. The function should handle both positive and negative numbers. For negative numbers, use the two's complement method. The output should not contain any leading zeros, except f...
[ { "from": "human", "value": "Write a python function to find the nth Catalan number using dynamic programming.\n\nCatalan numbers are a sequence of natural numbers that have many applications in combinatorial mathematics. The nth Catalan number is given by the following direct formula:\nC(n) = (2n)! / ((n+1...
[ { "from": "human", "value": "Write a function to reverse the digits of a given non-negative integer and check if it is a palindrome.\n\nFor this function, you should:\n- Reverse the digits of the given non-negative integer.\n- Check if the reversed integer is a palindrome.\n- Return True if the reversed int...
[ { "from": "human", "value": "Write a python function to find the smallest common multiple of two numbers.\n\nThe smallest common multiple (SCM) of two numbers is the smallest number that is a multiple of both numbers. For example, the SCM of 4 and 6 is 12, because 12 is the smallest number that is both a mu...
[ { "from": "human", "value": "Write a python function to find the minimum number of operations required to convert a given number of pages into a booklet. An operation consists of folding a page in half. A page can be folded in half only if it is even. If the number of pages is odd, one page is discarded bef...