[output] | #include <stdio.h> //Computes the factorial of a given number int factorial(int num) { if(num == 0 || num ==1) return 1; return num * factorial(num-1); } int main() { int num; scanf("%d", &num); printf("Factorial = %d", factorial(num)); return 0; } [instruction] | Edit the following C program so that it prints the factorial of a number. [input] | #include <stdio.h> int main() { int i, num; scanf("%d", &num);
[output] | final_price = total_price - (total_price * 0.10) [instruction] | Using only the existing variables, edit the code to subtract the `total_price` by 10%. [input] | total_price = 100 final_price = total_price
[output] | def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) [instruction] | Refactor the code to use a recursive algorithm instead of a loop to find the factorial of a given number. [input] |
[output] | import statistics mean = statistics.mean(array) median = statistics.median(array) std_dev = statistics.stdev(array) [instruction] | Write code to calculate statistical measures such as Standard Deviation, Median and Mean from a given set of numbers. [input] | array = [2, 4, 8, 7, 5]
[output] | def sum_odd_numbers(num): result = 0 for i in range(1, num+1): if i % 2 != 0: result += i return result [instruction] | Using an existing loop, create code to calculate the sum of all odd numbers up to a given number. [input] |
[output] | list3 = [] for i in range(len(list1)): list3.append(list1[i]) list3.append(list2[i]) [instruction] | Write a code in Python that takes two lists of equal lengths and generates a new list with the elements of both lists combined. [input] | list1 = [1, 2, 3] list2 = [4, 5, 6]
[output] | class CalendarDate: def __init__(self, day, month, year): self.day = day self.month = month self.year = year def get_date(self): return self.day, self.month, self.year [instruction] | Design a class to hold calendar date-related information. [input] |
[output] | def is_prime(n): if n <= 1: return False for i in range(2,n): if n % i == 0: return False return True [instruction] | Write a function that returns True if a given number is prime and False if it is not. [input] |
[output] | def classify_string(string): if string == string[::-1]: return "Palindrome" elif " " in string: return "Phrase" else: return "Word" input_string = "racecar" print(classify_string(input_string)) # Palindrome [instruction] | Classify a given string as either a palindrome, a word or a phrase. [input] | racecar
[output] | def bits_in_byte(): return 8 total_bits = bits_in_byte() print(total_bits) # 8 [instruction] | Calculate the total number of bits in a single byte. [input] |
[output] | def dict_value(data, key): return data[key] data = {'name': 'John', 'age': 30, 'occupation': 'teacher'} value = dict_value(data, 'name') print(value) # John [instruction] | Retrieve the value of a specific key from a given dictionary. [input] | {'name': 'John', 'age': 30, 'occupation': 'teacher'}
[output] | def slice_string(string, n): return string[n:] input_string = 'Python' print(slice_string(input_string, 3)) # hon [instruction] | Slice a string after the nth character. [input] | Python
[output] | def percent_format(number): return '{:.2%}'.format(number) number = 0.23 print(percent_format(number)) # 23.00% [instruction] | Format a given number as a percent value. [input] | 0.23
[output] | def index_in_array(data, value): return data.index(value) data = [2, 8, 7, 5, 4] value = 4 print(index_in_array(data, value)) # 4 [instruction] | Find the index in an array of a given value. [input] | [2, 8, 7, 5, 4], 4
[output] | for item in [1,2,3,4,5]: print(item) [instruction] | Create a for loop in Python that iterates through the list [1,2,3,4,5]. [input] |
[output] | function multiply($x, $y) { return $x * $y; } [instruction] | Using PHP, define a function that multiplies two numbers. [input] |
[output] | x = 7 y = 5 print(x * y) [instruction] | Edit the following program so that it prints out the result of x multiplied by y. [input] | x = 7 y = 5 print(x + y)
[output] | public class Number { int num1; int num2; public Number(int num1, int num2) { this.num1 = num1; this.num2 = num2; } } [instruction] | Use Java to create a constructor to set the values of instance variables num1 and num2. [input] |
[output] | class Greeting extends React.Component { constructor(props) { super(props); this.state = { time: new Date().toLocaleTimeString(), }; } render() { return <h3>Good {this.props.timeOfDay}, the current time is {this.state.time}.</h3>; } } [instruction] | Using React, create a component that displays a greeting with the current time. [input] |
[output] | <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </ul> </nav> [instruction] | Using HTML, code a navigation bar. [input] |
[output] | p { background-color: white; } [instruction] | Write a CSS rule to set the background color of all <p> elements to white. [input] |
[output] | for (let i = 0; i <= 50; i++) { if (i % 2 == 0) { console.log(i); } } [instruction] | Write a JavaScript code that prints out even numbers between 0 and 50. [input] |
[output] | public class Person { private String firstName; private String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } } [instruction] | Create a Java class Person with instance variables firstName and lastName. [input] |
[output] | <html> <head> <title>My webpage</title> </head> <body> <h1>My name is John Smith</h1> </body> </html> [instruction] | Write an HTML page with a header that displays your name. [input] |
[output] | A recurrence relation is an equation that defines a sequence of terms in terms of the values of the preceding terms. It is typically used to define a sequence in which each term depends on the values of previous terms. Recurrence relations are used in algorithms and computer programs to recursively [instruction] | Explain the concept of recurrence relations in algorithms [input] |
[output] | total_distance = 36 m [instruction] | Calculate the total distance traveled by an object with a given initial velocity and acceleration [input] | Initial velocity = 12 m/s Acceleration = 2 m/s^2
[output] | def isAnagram(str1, str2): # convert both strings into lowercase str1 = str1.lower() str2 = str2.lower() # sort both strings sortedStr1 = ''.join(sorted(str1)) sortedStr2 = ''.join(sorted(str2)) # check if sorted strings are equal if sortedStr1 == sortedStr2: return True [instruction] | Create a program in Python that takes two strings as input and determines if they are anagrams of each other [input] | string1="listen"; string2="silent";
[output] | Big-O notation is a method of expressing the time complexity of an algorithm. It quotes the upper bound of an algorithm’s running time in terms of a function of the size of the input data. This is useful for determining the efficacy of an algorithm as different inputs of different sizes require diff [instruction] | What is the Big-O notation of a given algorithm [input] |
[output] | class Calculator { constructor(num1, num2) { this.num1 = num1; this.num2 = num2; } add() { return this.num1 + this.num2; } subtract() { return this.num1 - this.num2; } multiply() { return this.num1 * this.num2; } divide() { return this.num1 / this.num2; } [instruction] | Update the following JavaScript code to use object-oriented programming. [input] | const calculator = { number1: 0, number2: 0, add(num1, num2) { return num1 + num2; }, subtract(num1, num2) { return num1 - num2; }, multiply(num1, num2) { return num1 * num2; }, divide(num1, num2) { return num1 / num2; } }
[output] | SELECT * FROM Users [instruction] | Generate an SQL query to get all the columns of the table "Users". [input] |