messages
listlengths
2
2
[ { "role": "user", "content": "Given a 2-dimensional array, calculate the sum of each row and store the sums in a new array. Then, print out the new array.\n\narr = [\n [1, 1, 1],\n [2, 2, 2],\n [3, 3, 3]\n]\n\nExample output:\n[3, 6, 9]" }, { "role": "system", "content": "Here's one way to so...
[ { "role": "user", "content": "Write a function for removing duplicate values from an array, without using any built-in methods or data structures (such as sets or dictionaries) to store unique values." }, { "role": "system", "content": "Here's a possible implementation of a function that removes...
[ { "role": "user", "content": "Using conditional statements, print out whether a given number is a prime number or not.\nnumber = 17" }, { "role": "system", "content": "To determine if a given number is a prime number, we can check if it is divisible by any number other than 1 and itself. Here's ...
[ { "role": "user", "content": "Write a program that takes in a list of integers and returns the smallest positive integer that is not present in the list.\n\nConstraints:\n- The input list can contain up to 100,000 integers.\n- The input list can contain positive and negative integers.\n- The program should ...
[ { "role": "user", "content": "Create a 2D array with n columns and m rows and fill it with random numbers. Additionally, write a function that calculates the average of each row in the array and returns the row with the highest average value. Print out the row number and its corresponding average value. \n\...
[ { "role": "user", "content": "Design an algorithm to help a user navigate through a maze. The maze is represented as a two-dimensional grid where each cell can be either empty or blocked. The user starts at the top-left cell and must reach the bottom-right cell.\n\nYour algorithm should find a path from the...
[ { "role": "user", "content": "Create a stack that supports the following operations:\n1. Push: Add an element to the top of the stack.\n2. Pop: Remove and return the element from the top of the stack.\n3. Peek: Return the element at the top of the stack without removing it.\n4. isEmpty: Check if the stack i...
[ { "role": "user", "content": "Create a function that takes two strings as parameters, compares them, and returns 0 if they are equal, 1 if the first string is greater (based on lexicographic order), and -1 if the second string is greater. \n\nIn addition, you need to handle the following edge cases:\n\n1. I...
[ { "role": "user", "content": "Create a function that takes in an array of integers and returns a new array with only the prime numbers from the original array. The prime numbers should be sorted in ascending order.\n\nExample:\nInput: [3, 2, 4, 5, 8]\nOutput: [2, 3, 5]" }, { "role": "system", "c...
[ { "role": "user", "content": "Design a memory efficient algorithm for a given problem.\nGiven a list of integers, find the average of all the elements.\nThe algorithm must have a time complexity of O(n) and use only a constant amount of extra space." }, { "role": "system", "content": "To solve t...
[ { "role": "user", "content": "Sort a given array of integers using Quick Sort and make sure that the algorithm has a time complexity of O(nlogn) in all cases." }, { "role": "system", "content": "Quick Sort is a divide-and-conquer algorithm that sorts an array by selecting a pivot element and par...
[ { "role": "user", "content": "Find the angle between two given vectors in a three-dimensional space. The time complexity of your solution should be O(1) and you should not use any trigonometric functions." }, { "role": "system", "content": "To find the angle between two vectors in three-dimensio...
[ { "role": "user", "content": "Implement a function that takes in an array of integers and prints out each element in the array. However, the function should only print out the element if it is a prime number.\n\nProposed Test:\nlet nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];" }, { "role": "system", ...
[ { "role": "user", "content": "Implement a search engine using recursion that returns all the words from a given list that contain a specific prefix.\nquery = \"comp\"\ndata = [\"mobile\",\"computer\",\"laptop\",\"tablet\"]" }, { "role": "system", "content": "Here is a Python implementation of a ...
[ { "role": "user", "content": "Write a code snippet to find all the Pythagorean triplets whose sum of the squares of the three numbers is equal to the given number. The code should have a time complexity of O(n^2) and a space complexity of O(1)." }, { "role": "system", "content": "To find all the...
[ { "role": "user", "content": "Design a program to find all prime numbers up to a given limit n. Additionally, for each prime number found, the program should determine and display the sum of its digits." }, { "role": "system", "content": "Here is a Python program that finds all prime numbers up ...
[ { "role": "user", "content": "Given a list of integers and a target value, calculate two DISTINCT integers in the list that add up to the target value. The output should be a list containing the two integers in ascending order.\ntarget = 6\nlist = [1, 2, 2, 3, 4, 5]" }, { "role": "system", "cont...
[ { "role": "user", "content": "Write a code snippet to determine the number of prime numbers in a given array." }, { "role": "system", "content": "Here's a code snippet in Python to determine the number of prime numbers in a given array:\n\n```python\ndef is_prime(num):\n if num <= 1:\n ...
[ { "role": "user", "content": "Convert the following array of strings to a set of tuples in Python. Each tuple should contain the string from the original array along with its length and the number of vowels it contains." }, { "role": "system", "content": "Here's how you can convert the array of ...
[ { "role": "user", "content": "Write a regex pattern to check if the given string is a US phone number. Additionally, validate that the phone number is in the format (XXX) XXX-XXXX, where X represents a digit from 0 to 9." }, { "role": "system", "content": "The regex pattern to validate a US phon...
[ { "role": "user", "content": "Modify the code below to create a new function called \"addSquares()\" that adds the squares of two numbers and returns the result. The function should only accept positive integers as input and throw an error message if any other data type or negative integers are passed as ar...
[ { "role": "user", "content": "Given an array of integers, find the second largest number and its frequency. If there are multiple numbers with the same frequency, return the smallest number among them.\n\nExample:\nInput: [13, 54, 78, 54, 3, 87]\nOutput: Second largest number: 78, Frequency: 1" }, { ...
[ { "role": "user", "content": "Create a function that groups a given set of numbers into continuous ranges. A continuous range is defined as a sequence of numbers where each number is exactly one greater than the previous number.\n\nExample:\n\nInput: [1, 3, 4, 6, 7, 8, 10]\nOutput: [[1], [3, 4], [6, 7, 8], ...
[ { "role": "user", "content": "Create a function to register a new user using a given username, email and password. The username must be unique and contain only alphanumeric characters. The email must be a valid email address and should not already be registered in the system. The password must be at least 8...
[ { "role": "user", "content": "Implement a function that takes in a list of integers and returns a new list with each integer multiplied by 2.\n\nRequirements:\n- The function should use asynchronous programming techniques.\n- The time complexity should be O(n), where n is the length of the input list.\n- Th...
[ { "role": "user", "content": "Create a function that converts an arbitrary number from any base between 2 and 16 to its decimal representation, without using any built-in conversion functions or libraries. The function should be able to handle numbers with up to 100 digits." }, { "role": "system", ...
[ { "role": "user", "content": "Create an array of numbers and check if a given number exists in the array. Output true if it exists, output false if it does not. Additionally, if the number exists in the array, also output its index position in the array. \nFor example, given the array [1, 2, 3, 4, 5] and th...
[ { "role": "user", "content": "You receive an array of numbers as input and you need to calculate the median number. However, you are not allowed to use any built-in functions or libraries to sort the array. The input array will always contain an odd number of elements." }, { "role": "system", "c...
[ { "role": "user", "content": "Create a data structure to store records of students' attendance with the following constraints: each record should include the student's name, ID, date and time of attendance, and attendance status (present, absent, late). The data structure should also be able to handle multi...
[ { "role": "user", "content": "You need to write a function that validates a given password. It should only be valid if it contains at least one lowercase letter, one uppercase letter, one number, and one special character. Additionally, the password should be at least 8 characters long and should not contai...
[ { "role": "user", "content": "Devise a data structure to store an inventory of items in a store. Additionally, implement a method that allows the user to add an item to the inventory, considering that each item has a unique identifier. The method should handle the scenario where the user tries to add an ite...
[ { "role": "user", "content": "Create a code to print the items from a given array in reverse without using any built-in functions or methods for reversing an array. The code should only use logical steps and operations to achieve the desired result." }, { "role": "system", "content": "Here's a c...
[ { "role": "user", "content": "Reverse a given array without using a loop. The solution must have a time complexity of O(1) and must not use any built-in array manipulation functions such as reverse(). Additionally, you are not allowed to create a new array to store the reversed elements. The original array ...
[ { "role": "user", "content": "Create a code to raise a number to a given power p, using only basic arithmetic operations (addition, subtraction, multiplication, and division) and without using any built-in exponentiation functions or operators." }, { "role": "system", "content": "Here's an examp...
[ { "role": "user", "content": "Given a two-dimensional array, write a code to print the elements column-wise. The array may contain duplicate elements.\n[[1, 2, 2], [3, 4, 5], [5, 6, 7, 7]]" }, { "role": "system", "content": "Here is a code snippet in Python to print the elements column-wise from...
[ { "role": "user", "content": "Create a function to find the total number of vowels in a series of strings. The strings can contain both lowercase and uppercase letters. Ignore any non-alphabetic characters in the strings.\n\nExample:\nInput:\nstring1: \"Hello\"\nstring2: \"WorlD\"\nstring3: \"12345\"\n\nOut...
[ { "role": "user", "content": "Write a function that takes in a list of strings and returns True if any of the strings contains the letter `a`. Additionally, the function should ignore any strings that start with a lowercase letter." }, { "role": "system", "content": "Here's an implementation of ...
[ { "role": "user", "content": "Filter the array based on the following condition: only include elements that are prime numbers and greater than 2. Return a new array of elements that satisfy the condition.\narr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" }, { "role": "system", "content": "Here is the filt...
[ { "role": "user", "content": "Design an algorithm to check if a given string can be converted to a palindrome, considering only alphanumeric characters and ignoring the case. \n\n#Erroneous Code#\n```python\ndef is_palindrome(string):\n string = string.lower()\n return string == string[::-1]\n```\n\n#...
[ { "role": "user", "content": "Design a function in Python to sort a list of tuples based on the first element in descending order, while ignoring the cases of the first element. The function should handle both integer and string values in the tuples." }, { "role": "system", "content": "Here's a ...
[ { "role": "user", "content": "Design an algorithm to check if two given binary trees are identical, where each node contains an integer value and an additional character value. The algorithm should also compare the character values of the corresponding nodes and return true only if both the integer values a...
[ { "role": "user", "content": "Check if a given positive integer is a prime number and a power of two." }, { "role": "system", "content": "To check if a positive integer is a prime number, we need to iterate from 2 to the square root of the given number and check if any of these numbers divide th...
[ { "role": "user", "content": "Create a Python program to extract the URLs from a given list of strings, where the URLs must be in a specific format: \n- They must start with \"http://\" or \"https://\"\n- They must contain at least one dot (.) after the domain name\n- They must not contain any special chara...
[ { "role": "user", "content": "Write a code to find the most frequently occurring item in a list of integers, where the length of the list is n and each integer is in the range of 1 to m. Additionally, the code should have a time complexity of O(n) and a space complexity of O(m)." }, { "role": "syste...
[ { "role": "user", "content": "Explode a string with a given delimiter, while also removing any whitespace characters before and after each element in the resulting list.\ngiven_str = \"apple, banana, orange\"" }, { "role": "system", "content": "Here is one way to explode the string with a given ...
[ { "role": "user", "content": "Construct a Python class for a camera object that includes the following methods:\n1. `capture_photo()` - captures a photo and returns the image data.\n2. `record_video(duration: int)` - records a video for the specified duration (in seconds).\n3. `zoom_in(level: int)` - zooms ...
[ { "role": "user", "content": "Modify the program to work with any dataset and also handle missing values.\ndef sum_values(data):\n \"\"\"Returns the sum of values in the given dataset.\"\"\"\n sum = 0\n for value in data:\n if value is not None: # Check for missing values\n sum +...
[ { "role": "user", "content": "Implement a modified version of the selection sort algorithm to sort the given array in descending order. The array may contain duplicate elements." }, { "role": "system", "content": "To modify the selection sort algorithm to sort the given array in descending order...
[ { "role": "user", "content": "Create two lists, one in Python and one in JavaScript, with 10 numbers each. The numbers in the Python list must be generated randomly within the range of 1 to 100, inclusive. The JavaScript list must contain only prime numbers generated randomly within the range of 1 to 100, i...
[ { "role": "user", "content": "Write a function that takes in 2 lists, reverses the elements of each list, combines the elements to output a single list, and then sorts the elements in ascending order." }, { "role": "system", "content": "Here is the Python code for the function you described:\n\n...
[ { "role": "user", "content": "Take a string as an input and determine the number of times the letter 'a' appears in the string. However, you should ignore any occurrences of the letter 'a' that are followed by the letter 'b'. \n\nString: \"The quick brown fox jumps over the lazy dog\"" }, { "role": ...
[ { "role": "user", "content": "Implement the Caesar Cipher algorithm that shifts the characters in a string by a specified number of spaces. The input string can contain uppercase and lowercase letters, digits, and special characters. The shift value can be any integer between -100 and 100. The output should...
[ { "role": "user", "content": "Compute the probability of each string from the below list. Additionally, implement a function that calculates the probability of a given string occurring in a larger text corpus. \n\n['apple', 'banana', 'watermelon', 'mango']\n\nText corpus example:\n\"This is an example text ...
[ { "role": "user", "content": "Change the given string to a list of characters, where each character is represented as a string." }, { "role": "system", "content": "To convert a given string to a list of characters, where each character is represented as a string, you can use the list() function ...
[ { "role": "user", "content": "Write a function to encrypt the text with a modified Caesar Cipher.\n\nModify the original text by:\n- Reversing the order of the letters\n- Shifting each letter by the ASCII value of the previous letter in the alphabet (e.g. 'a' shifted by 1 becomes 'b', 'b' shifted by 2 becom...
[ { "role": "user", "content": "Write a function that takes a date and time in the format \"YYYY-MM-DD HH:MM:SS\" and converts it to the format \"DD-MM-YYYY HH:MM\". Additionally, handle any potential errors that may occur during the conversion process." }, { "role": "system", "content": "To conve...
[ { "role": "user", "content": "Use the given string to find the number of occurrences of each letter, disregarding case sensitivity and excluding any non-alphabetic characters.\nHello World" }, { "role": "system", "content": "The given string is \"Hello World\". \n\nTo find the number of occurren...
[ { "role": "user", "content": "Implement a function that takes in three string parameters and prints out the longest one. However, the function should ignore leading and trailing whitespace in the strings when determining the longest one. Additionally, if two or more strings have the same length, the functio...
[ { "role": "user", "content": "Write a Python function that takes in a string as input and returns the number of jokes that can be written using the characters in the input string. The function should consider the following rules for a string to be considered a valid joke:\n\n1. The string must contain at le...
[ { "role": "user", "content": "Explain the difference between a shallow copy and a deep copy. Additionally, provide a code example in Python to demonstrate how to create a deep copy of a nested list." }, { "role": "system", "content": "In Python, a shallow copy and a deep copy are two different m...
[ { "role": "user", "content": "Determine whether a given string is a palindrome or not, considering alphanumeric characters only and ignoring case sensitivity.\nInput: \"A man, a plan, a canal: Panama!\"" }, { "role": "system", "content": "To determine whether a given string is a palindrome or no...
[ { "role": "user", "content": "Increase the difficulty of the given programming test by requiring the following additional specifications:\n\n1. Modify the code to remove the use of classes completely. Instead, implement the functionality using pure functions and higher-order functions.\n\n2. The `print_deta...
[ { "role": "user", "content": "Given a list of lists, generate a single flattened list. However, you must solve this problem without using any built-in functions or methods that directly flatten a list.\n\nlst = [[1, 2], [3, 4], [5, 6]]" }, { "role": "system", "content": "Here is one way to solve...
[ { "role": "user", "content": "Create a function that takes two parameters, a string and an integer, and prints the string the given number of times. The string must contain at least 5 characters and the integer must be within the range of 1 to 10." }, { "role": "system", "content": "Here is an e...
[ { "role": "user", "content": "Implement an algorithm to delete a node in a singly linked list, given only access to the node. The node to be deleted will always be in the middle of the linked list.\nhead = [4, 5, 1, 9, 2, 6, 8]\ndel = 2" }, { "role": "system", "content": "To delete a node in a s...
[ { "role": "user", "content": "Write a Python function that takes in a list of integers and returns a new list containing only the prime numbers from the original list." }, { "role": "system", "content": "Here is a Python function that takes in a list of integers and returns a new list containing...
[ { "role": "user", "content": "Write a function to reverse a doubly linked list in O(n) time complexity." }, { "role": "system", "content": "Here is an example implementation of a function to reverse a doubly linked list in O(n) time complexity:\n\n```python\nclass Node:\n def __init__(self, d...
[ { "role": "user", "content": "Create a condition statement to check if all elements in a list are unique and are in ascending order. The time complexity should be less than O(n^2)." }, { "role": "system", "content": "One way to solve this problem with a time complexity of less than O(n^2) is to ...
[ { "role": "user", "content": "Create a function in python to print all prime numbers between two given values. Do not include the two given values in the output." }, { "role": "system", "content": "Here's a Python function that takes two numbers as input and prints all the prime numbers between ...
[ { "role": "user", "content": "Write a function that takes in an array of positive integers and returns a new array where the elements are reversed, but only for odd integers. If an even integer is encountered, it should remain in its original position. Additionally, the function should sort the resulting ar...
[ { "role": "user", "content": "Insert 1000 random numbers between 0 and 1000 in a list." }, { "role": "system", "content": "Here is a Python code snippet that generates a list of 1000 random numbers between 0 and 1000:\n\n```python\nimport random\n\nrandom_numbers = [random.randint(0, 1000) for _...
[ { "role": "user", "content": "Write a Python program to parse a text string given a regular expression and return the matches. However, you must implement the regular expression parsing algorithm from scratch without using any built-in regular expression libraries or functions." }, { "role": "system...
[ { "role": "user", "content": "Create a class that represents a student that contains the following information: name, email, age, type of degree, and a list of courses the student is enrolled in." }, { "role": "system", "content": "Here's an example of how you can create a class in Python that r...
[ { "role": "user", "content": "Create a function that takes in a positive integer greater than 1 and returns true if the number is a prime number and false if it is not. The function should also check if the input is within the range of 2 to 10^9, inclusive." }, { "role": "system", "content": "He...
[ { "role": "user", "content": "Rewrite the following code to use a loop instead of recursion. Your solution should have a time complexity of O(n), where n is the length of the input string. Additionally, your solution should use only constant extra space and not create any new strings or data structures." ...
[ { "role": "user", "content": "Print out the Fibonacci series up to a given number, but only if the number is prime.\nn = 10" }, { "role": "system", "content": "A number is considered prime if it is only divisible by 1 and itself. Here's a Python code to print the Fibonacci series up to a given n...
[ { "role": "user", "content": "Create a function that takes an array of arrays of numbers, calculates the sum of the numbers across all the arrays, and returns the average of those sums. Each array within the main array can have a different length.\n\narr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12, 13]]...
[ { "role": "user", "content": "Write a class that implements an arithmetic operator and returns the result of the operation. The class should only support addition and subtraction operations. The input numbers must be positive integers and the result should be a positive integer as well. Your implementation ...
[ { "role": "user", "content": "Design a program to count the total number of even numbers in a given array, but now the program should also output the sum of all the even numbers in the array. Additionally, the program should handle arrays with a size of up to 10^6 elements." }, { "role": "system", ...
[ { "role": "user", "content": "Create an array with 4 elements, where the first element is the sum of the first and second element of the previous array, the second element is the difference between the first and second element of the previous array, the third element is the product of the first and second e...
[ { "role": "user", "content": "Using the same array, write a function that returns the largest number without using any built-in sorting or max functions.\n[3, 5, 22, 11, 16, 9]" }, { "role": "system", "content": "One way to find the largest number in an array without using any built-in sorting o...
[ { "role": "user", "content": "Write a Python function to create a dictionary with the given list as its keys and values, where each value is a list of integers. The length of the list should be determined by the index of the key in the given list." }, { "role": "system", "content": "Here is a Py...
[ { "role": "user", "content": "Write a program that implements Simpson's rule for numerical integration." }, { "role": "system", "content": "Here is an example Python program that implements Simpson's rule for numerical integration:\n\n```python\ndef f(x):\n # Define the function to be integra...
[ { "role": "user", "content": "How would you create a function in Python to add two lists of numbers element-wise, where each list can have varying lengths?" }, { "role": "system", "content": "To add two lists of numbers element-wise, even if the lists have varying lengths, you can use the `zip_l...
[ { "role": "user", "content": "Implement a class hierarchy for a simple banking system. The system should have a base class Account, with subclasses CheckingAccount and SavingsAccount. The Account class should have the following attributes: account_number (string), account_balance (float), and interest_rate ...
[ { "role": "user", "content": "Design a class for a user profile with the following additional requirements:\n- The profile must have a unique username, which cannot be changed once set.\n- The profile must store the user's date of birth, which should be validated to ensure it is in a valid format (e.g., dd/...
[ { "role": "user", "content": "Using the given array, create a function that takes in two arguments, the array and a number, and returns the position of the number in the array. The function should have a time complexity of O(log n) or better, where n is the length of the array. Additionally, the function sh...
[ { "role": "user", "content": "Create an algorithm which is able to sort a given array of integers in descending order. The algorithm should have a time complexity of O(n log n)." }, { "role": "system", "content": "To sort a given array of integers in descending order with a time complexity of O(...
[ { "role": "user", "content": "Create an input validation function in Python which checks that the input is an integer between 2-6 and is divisible by 3." }, { "role": "system", "content": "Here's a function in Python that validates if an input is an integer between 2-6 and is divisible by 3:\n\n...
[ { "role": "user", "content": "Implement merge sort in Python without using recursion." }, { "role": "system", "content": "Here's an implementation of merge sort in Python without using recursion:\n\n```python\ndef merge_sort(arr):\n current_size = 1\n while current_size < len(arr) - 1:\n ...
[ { "role": "user", "content": "Make a function in Python that sorts a list of integers in increasing order. The function should not use any built-in sorting functions or methods. Additionally, the function should have a time complexity of O(nlogn) and should not use any additional data structures. The input ...
[ { "role": "user", "content": "Create a function to encrypt a string using the XOR operator and a randomly generated key of length 10." }, { "role": "system", "content": "Here's a Python function that encrypts a string using the XOR operator and a randomly generated key of length 10:\n\n```python...
[ { "role": "user", "content": "Given a list of numbers, write a program to determine the sum of all the even numbers in the list." }, { "role": "system", "content": "Here is a program in Python that takes a list of numbers as input and calculates the sum of all the even numbers in the list:\n\n``...
[ { "role": "user", "content": "Create a class with two methods to generate a random prime number and a random palindrome string." }, { "role": "system", "content": "Here is an example of a class in Python with two methods to generate a random prime number and a random palindrome string:\n\n```pyt...
[ { "role": "user", "content": "Given a string, remove any duplicate characters and sort the remaining characters in ascending order.\n\ninput_string = \"aaabbbccc\"" }, { "role": "system", "content": "To remove any duplicate characters from the input string, we can convert it into a set, which wi...
[ { "role": "user", "content": "Write a function to sort a stack in ascending order using only a constant amount of extra space.\nstack = [4, 7, 2, 1]" }, { "role": "system", "content": "To sort a stack in ascending order using only a constant amount of extra space, we can use the following steps:...
[ { "role": "user", "content": "From the array of integers given below, write a Python code snippet to select all prime numbers.\narray = [2, 3, 5, 7, 9, 11, 13, 17];" }, { "role": "system", "content": "Here is a Python code snippet to select all prime numbers from the given array:\n\n```python\na...
[ { "role": "user", "content": "Sort the following array in descending order:\narr = [4, 2, 3, 5, 1]\n\nThe original problem required sorting the array in ascending order, which can be done using a simple sorting algorithm such as bubble sort or selection sort. To increase the difficulty, we will now sort the...
[ { "role": "user", "content": "Write a Python script that prints out the Fibonacci sequence up to the nth number, where n is a prime number." }, { "role": "system", "content": "To write a Python script that prints out the Fibonacci sequence up to the nth number, where n is a prime number, you can...
[ { "role": "user", "content": "Suggest a way to solve the following problem in Python: extracting the last element from a given list. The solution should not use any built-in Python functions or methods that directly extract the last element." }, { "role": "system", "content": "One way to solve t...