[output] | def DFS(root): s = Stack() s.push(root) while (s.size() > 0): node = s.pop() # Do something with the node if (node.left != NULL): s.push(node.left) if (node.right != NULL): s.push(node.right) [instruction] | Design the algorithm to perform a depth-first search on given binary tree [input] |
[output] | def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) [instruction] | Write the code to calculate a factorial recursively [input] |
[output] | def one_iteration_sort(arr): for i in range(len(arr)): min_index = i for j in range(i, len(arr)): if arr[j] < arr[min_index]: min_index = j arr[i], arr[min_index] = arr[min_index], arr[i] [instruction] | Create a sorting algorithm for an array that only needs 1 iteration [input] |
[output] | def detect_loop(head): slow_p = head fast_p = head while(slow_p and fast_p and fast_p.next): slow_p = slow_p.next fast_p = fast_p.next.next if slow_p == fast_p: return 1 return 0 [instruction] | Create a program to detect a loop in a linked list [input] |
[output] | def add_binary(b1, b2): # initalize result result = "" carry = 0 # iterate over the bits for i in range(max(len(b1), len(b2))): a = int(b1[-i-1]) if i < len(b1) else 0 b = int(b2[-i-1]) if i < len(b2) else 0 #calculate sum sum = carry ^ a ^ b [instruction] | Design an algorithm to add two given binary numbers [input] |
[output] | Data encapsulation is a software programming concept that allows the internal representation of an object to be hidden from the outside world. It is used to protect the data from being modified by parts of the program that should not have access to the data. It is also used to provide a way to exten [instruction] | Explain what data encapsulation is. [input] |
[output] | SELECT * from Table1, Table2 WHERE Table1.id = Table2.id; [instruction] | Write a SQL query to retrieve data from two tables with similar columns. [input] | Table1 | id | name | address | |----|------|---------| | 1 | John | NYC | | 2 | Mary | LA | Table2 | id | name | address | |----|------|---------| | 3 | Mark | Chicago| | 4 | Tom | Miami |
[output] | class Example { public: Example(); ~Example(); }; Example::Example() {} Example::~Example() {} [instruction] | Create a new C++ class with a constructor and destructor. [input] |
[output] | She - Noun Felt - Verb So - Adverb Excited - Adjective That - Conjunction Her - Determiner Application - Noun Got - Verb Approval - Noun [instruction] | Classify the given sentence using the Bag-of-Words approach. [input] | Sentence: She felt so excited that her application got approval.
[output] | The main difference between procedural and object-oriented programming is that object-oriented programming focuses on the objects and how they interact with each other, while procedural programming focuses on executing a sequence of steps. In object-oriented programming, data and functions are encap [instruction] | What is the difference between procedural and object-oriented programming? [input] |
[output] | input_string = "Hello World!" # Count the number of alphabets num_alphabets = sum(c.isalpha() for c in input_string) print("Number of alphabets in the string:", num_alphabets) [instruction] | Given a string, write a code that counts the number of alphabets in it. [input] | input_string = "Hello World!"
[output] | def find_median(a, b, c): # Calculate the maximum of the three numbers max_num = max(a, b, c) # Calculate the minimum of the three numbers min_num = min(a, b, c) # Calculate the median value as the sum of the three numbers minus the maximum and minimum median = a + b + c - ma [instruction] | Debug the following code for finding the median of three numbers. [input] | def find_median(a, b, c): if a > b: if b > c: return b elif a > c: return c else: return a elif b > c: return b elif a > c: return a else: return c
[output] | class Calculator: """Class for calculating the sum and product of two numbers.""" # Method for calculating sum of two numbers def get_sum(self, num1, num2): return num1 + num2 # Method for calculating product of two numbers def get_product(self, num1, num2): [instruction] | Create a class with two methods "get_sum" and "get_product" for calculating the sum and product of two numbers respectively. [input] |
[output] | import re # The given string my_string = 'Hi There! Welcome.@' # Remove special characters using regular expressions clean_string = re.sub('[^a-zA-Z0-9\s]', '', my_string) print(clean_string) # Hi There Welcome [instruction] | Use regular expression to remove special characters from any given string. [input] | my_string = 'Hi There! Welcome.@'
[output] | # Create an array of 10 random integers import random array = [] for i in range(10): array.append(random.randint(1, 50)) print(array) # [33, 22, 34, 18, 28, 22, 17, 11, 26, 45] [instruction] | Create an array of 10 random integers between 1 to 50 and print them. [input] |
[output] | 4 [instruction] | Given a string, return the length of the longest run of characters. [input] | string = "aaaabbccdd"
[output] | using System.Windows.Forms; public static class Program { static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } } public class Form1 : Form { public Form1() { MessageBox.Show("Welcome!"); } } [instruction] | Create a GUI application in C# that displays a welcome message when the program is launched. [input] |
[output] | def fibonacci(): a, b = 0, 1 while True: a, b = b, a + b yield a [instruction] | Change the following code to a yield statement so that it can be used as a generator. [input] | def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
[output] | a { color: blue; } [instruction] | Write a CSS rule that changes the color of all the links in a webpage to blue. [input] |
[output] | <table> <tr> <td>H</td> <td>e</td> <td>l</td> <td>l</td> </tr> <tr> <td>o</td> <td> </td> <td>W</td> <td>o</td> </tr> <tr> <td>r</td> <td>l</td> <td>d</td> <td> </td> </tr> </table> [instruction] | Given a string of length N, create an HTML table with N rows and 4 columns. [input] | string = "Hello World"
[output] | function countString(str) { let count = 0; for (let i = 0; i < str.length; i++) { if (str.substring(i, i + 5) == "Hello") { count++; } } return count; } [instruction] | Create a function in JavaScript that takes a string as an argument and returns the number of times that string is repeated in the input string. [input] | string = "HelloHello"
[output] | for i in list1: if i in list2: print(i) [instruction] | Write a Python statement to print out the common items between two lists. [input] | list1 = [1, 2, 3, 4] list2 = [2, 4, 6 , 8]
[output] | { "name": "John", "date": "22/02/2020" } [instruction] | Alter the following JSON object such that the date format of "date" field changes from "dd-mm-yyyy" to "dd/mm/yyyy". [input] | { "name": "John", "date": "22-02-2020" }
[output] | def find_max(nums): # initialise the max to the first element of the list max = nums[0] # loop over the list for i in range(1, len(nums)): # if the current element is greater than the max, # update the max if nums[i] > max: max = nums[i] # [instruction] | Using the comments as a guide, implement a function that takes as input a list of integers and returns the maximum value. [input] | nums = [2, 8, 9, 10]
[output] | def search_indices(array, search_term): # initialise an empty list indices_list = [] # loop over the array for index, s in enumerate(array): # if the search term is in the string if search_term in s.lower(): # append the index to the list [instruction] | Create a function that takes in an array of strings and a search term and returns an array of indices of strings that match the search term. [input] | array = ["I love dogs","I hate cats","Dogs are the best"] search_term = "dogs"
[output] | def insert_character(string, character): # initialise a new string new_string = "" # loop over the string for i in range(len(string)): # if the index is odd if i % 2 != 0: # add the character new_string += character # add the current [instruction] | Create a function to insert a given character at every odd index in a given string. [input] | string = "Hello" character = 'z'
[output] | def delete_even(arr, n): # initialise a list odd_list =[] # loop over the array for i in range(n): # if the number is odd if arr[i] % 2 != 0: # add it to the list odd_list.append(arr[i]) # return the list of odd numbers return odd_ [instruction] | Implement a function that takes in a number n and deletes all the even numbers in an array of integers first n numbers. [input] | arr = [2, 3, 4, 5, 6, 7] n = 6
[output] | let myStr = 'My string'; alert(myStr); [instruction] | Update the following JavaScript code so that it alerts a given string. [input] | let myStr = 'My string'
[output] | function sumNumbers(x, y) { let sum = 0 for(let i = x; i <= y; i++) { sum += i } return sum } [instruction] | Write a JavaScript function that takes two numbers and returns the sum of all numbers in between the two. [input] |
[output] | SELECT * FROM table WHERE Field = 'Name' AND Value = 'John'; [instruction] | Create an SQL query that returns rows where the column "Field" is "Value". [input] | Field = 'Name' Value = 'John'