[output] | my_list = [1, 2, 3, 4, 5] [instruction] | Create an array of five elements using the language of your choice. [input] |
[output] | The main difference between Lists and Sets are that Lists are ordered and can contain duplicate elements, while Sets are unordered and cannot contain duplicate elements. Additionally, Lists support indexing and iteration, while Sets do not. [instruction] | What is the difference between the List and the Set data structures? [input] |
[output] | def countK(arr, k): if len(arr) == 0: return 0 if arr[0] % k == 0: return 1 + countK(arr[1:], k) else: return 0 + countK(arr[1:], k) [instruction] | Implement a recursive algorithm to count the number of elements that are divisible by k in an array. [input] | [2, 4, 8, 10, 11], k = 3
[output] | Shallow copying is the process of creating a copy of the original object which only contains references to the values contained in the original instance. This means that any shallow copy only contains references to the same objects as the original instance, while any changes made to the copy will be [instruction] | What is the difference between shallow copying and deep copying? [input] |
[output] | function factorial(x, y) { let result = 1; for (let i = x; i <= y; i++) { result *= i; } return result; } [instruction] | Construct a factorial function in JavaScript that takes in two inputs x and y as parameters. [input] |
[output] | def median(data): data.sort() length = len(data) if length == 0: return 0 if length % 2 == 0: return (data[length // 2] + data[length // 2 - 1]) / 2 elif length % 2 == 1: return data[length // 2] [instruction] | Modify the following code so that it takes a list of numbers and calculates their median. [input] | def median(data):
[output] | import random # Generate 25 random numbers random_list = [] for i in range(25): random_list.append(random.randint(0, 8)) print(random_list) [instruction] | Create a program that uses a loop to generate 25 random numbers from 0 to 8. [input] |
[output] | The given line of code creates a list from 0 to 4 (inclusive) using a list comprehension. It is equivalent to the following code: a = [] for i in range(5): a.append(i) Therefore, it creates a list 'a' with elements "0", "1", "2", "3", and "4". [instruction] | Given the following line of code, explain what it does. [input] | a = [i for i in range(5)]
[output] | numbers = range(10) evens = list(filter(lambda x : x % 2 == 0, numbers)) [instruction] | Re-write the following code using only the filter() function. [input] | numbers = range(10) evens = [] for x in numbers: if x % 2 == 0: evens.append(x)
[output] | words = ["Stack", "Overflow", "Python", "Coding"] words = list(filter(lambda x : len(x)<=5, words)) print(words) [instruction] | Given an array of strings "words", write a code to remove all the words which are greater than 5 characters in length. [input] | words = ["Stack", "Overflow", "Python", "Coding"]
[output] | import random random_number = random.randint(1, 10) print("A random number between 1 and 10 is: " + str(random_number)) [instruction] | Create a program that generates a random integer from 1 to 10. [input] |
[output] | alphabet = ["a", "b", "c"] new_alphabet = [] for letter in alphabet: new_alphabet += [letter]*2 print(new_alphabet) [instruction] | Given an array of strings "alphabet", write a code to repeat each element in the array. [input] | alphabet = ["a", "b", "c"]
[output] | myList = [1, 2, 3, 4, 5, 6, 7, 8, 9] divisible_by_3 = list(filter(lambda x: x % 3 == 0, myList)) print(divisible_by_3) [instruction] | Given a list of numbers, write a program that finds all the numbers that are divisible by 3. [input] | myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
[output] | SELECT table1.name, table1.city, table2.email FROM table1 INNER JOIN table2 ON table1.name=table2.name; [instruction] | Write a SQL statement to join table1 and table 2 on the "name" column. [input] |
[output] | #include <iostream> using namespace std; int main() { int start, end; cout<<"Enter start of range: "; cin>>start; cout<<"Enter end of range: "; cin>>end; cout<<"Printing even numbers between "<< start <<" and "<< end <<": "; for(int i = start; i <= end; i++) { if(i [instruction] | Create a C++ program to print all the even numbers of a given range. [input] | 1 10
[output] | javac filename.java [instruction] | Compile a Java program using the command line. [input] |
[output] | function factorial(num) { // If the number is less than 0, reject it. if (num < 0) { return -1; } // If the number is 0, its factorial is 1. else if (num == 0) { return 1; } // Otherwise, call the recursive procedure. else { return (num * f [instruction] | Design a JavaScript program to find the factorial of an integer. [input] | 5
[output] | def sum_of_squares(n): if n == 1: return 1 else: return n*n + sum_of_squares(n-1) number = 5 result = sum_of_squares(number) print('The sum of squares of numbers smaller than {} is {}.'.format(number, result)) [instruction] | Write a method in Python which takes a positive integer and returns the sum of squares of all numbers smaller than the given number. [input] | 5
[output] | SELECT * FROM table WHERE id > 10 LIMIT 5; [instruction] | Generate a MySQL query to fetch a specified number of records starting from a given ID. [input] | 5 10
[output] | In JavaScript, the "==" operator compares two values by performing type-conversion if necessary, while the "===" operator does not perform type-conversion and compares the values directly. For example, the statement "3 == '3'" will return true for the "==" operator, but false for the "===" operator, [instruction] | Describe the differences between "==" and "===" operators in JavaScript. [input] |
[output] | def hi_it(name): print("Hi," + str(name)) hi_it("John") [instruction] | Debug the code to fix the error. [input] | def hi_it(name): print("Hi," + name) hi_it("John")
[output] | list_words = ["apple", "banana", "orange", "mango"] target_word = "pineapple" if target_word in list_words: print ("Target word is in the list") else: print ("Target word is not in the list") [instruction] | Given a list of words and a target word, determine if the target word is in the list [input] | list_words = ["apple", "banana", "orange", "mango"] target_word = "pineapple"
[output] | emails = ["test@example.com", "example@example.org", "test@test.co.uk"] from collections import defaultdict by_domain = defaultdict(list) for email in emails: domain = email.split("@")[-1] by_domain[domain].append(email) print(dict(by_domain)) [instruction] | Given a collection of email addresses, group them by the domain [input] | emails = ["test@example.com", "example@example.org", "test@test.co.uk"]
[output] | string = "Hello World " stripped_string = string.strip() print (stripped_string) [instruction] | Strip the spaces in the given string [input] | string = "Hello World "
[output] | arr = [[1,2,3], [4,5,6], [7,8,9]] sums = [] for i in range(len(arr[0])): total = 0 for row in arr: total += row[i] sums.append(total) print(sums) [instruction] | Given a 2D array, return the sums of all columns [input] | arr = [[1,2,3], [4,5,6], [7,8,9]]
[output] | strings = ["a", "abc", "defg"] strings.sort(key=len) print(strings) [instruction] | Given a collection of strings, sort the strings by their length [input] | strings = ["a", "abc", "defg"]
[output] | arr = [1,2,3,4,5] for index in range(len(arr)-1, -1, -1): print(arr[index]) [instruction] | Make a program that prints the elements of a given array in reverse order [input] | arr = [1,2,3,4,5]
[output] | list_of_strings = ["cat", "dog", "rat", "bat"] target_string = "at" result = [string for string in list_of_strings if target_string in string] print(result) [instruction] | Given a list of strings and a target string, return a list of strings which contain the target string [input] | list_of_strings = ["cat", "dog", "rat", "bat"] target_string = "at"
[output] | arr = [1,2,3,4] arr.reverse() [instruction] | Insert code to reverse a list in place. [input] | arr = [1,2,3,4]
[output] | def sum_second_elements(arr): sum_num = 0 for i in range(1, len(arr), 2): sum_num += arr[i] return sum_num [instruction] | Sum every second element in an array. [input] | arr = [1, 5, 4, 8, 10, 12]