[output] | colors = 'red #FF0000, blue #0000FF, green #00FF00' dict_colors = {i.split()[0]:i.split()[1] for i in colors.split(', ')} # returns {'red': '#FF0000', 'blue': '#0000FF', 'green': '#00FF00'} [instruction] | Transform the following string in Python into a dictionary whose keys are the names of the colors and the values are the hex codes for the colors. [input] | "red #FF0000, blue #0000FF, green #00FF00"
[output] | public static int searchArray(int[] arr, int x) { for(int i = 0; i < arr.length; i++) { if (arr[i] == x) return i; } return -1; } [instruction] | Write a function in Java that can be used to search an array and return the index of the element. [input] |
[output] | <button onclick="alert('You have clicked the button!')">Click Me!</button> [instruction] | Edit the following HTML code to include a JavaScript alert that appears when a user clicks the button. [input] | <button>Click Me!</button>
[output] | SELECT * FROM customers WHERE state = 'New York' AND purchase_date >= DATE_SUB(NOW(), INTERVAL 6 MONTH) [instruction] | Create an SQL query that returns all customers in New York state who have purchased a product in the last 6 months. [input] |
[output] | def gcm(x, y): if(x > y): gcm = x else: gcm = y while(True): if((gcm % x == 0) and (gcm % y == 0)): return gcm gcm = gcm + 1 # Driver Program x = 12 y = 18 print(gcm(x, y)) [instruction] | Design an algorithm that determines the greatest common multiple of two numbers. [input] |
[output] | function calculate(x, operator, y) { switch (operator) { case '+': return x + y; case '-': return x - y; case '*': return x * y; case '/': return x / y; case '%': return x % y; default: return NaN; } } let result = calculate(6, '*', 3); console.log(result); // 18 [instruction] | Write a JavaScript function that takes two numbers and a mathematical operator as parameters and returns the result of the specified operation. [input] | x = 6, y = 3, operator = '*'
[output] | import sys # Set a variable variableName = sys.argv[1] # Get a value from command line argument value = sys.argv[2] # Set the value of the variable exec(variableName + "=" + value) print(variableName) [instruction] | Create a Python script that sets the value of a variable from the command-line. [input] |
[output] | class Program { static void Main(string[] args) { // create an object of type Hello Hello helloObject = new Hello(); // call the SayHello method of the helloObject helloObject.SayHello(); } } // Create a class named Hello public class H [instruction] | Create an object-oriented C# program that prints "Hello, World!". [input] |
[output] | def array_sum(arr): total = 0 # Iterate through arr and add elements to total for i in range (0, len(arr)): total += arr[i] return total # Driver code arr = [2, 4, 7, 10] sum = array_sum(arr) print (sum) # -> 23 [instruction] | Design an algorithm that calculates the sum of all numbers in a given array. [input] | [2, 4, 7, 10]
[output] | string = "Hello" for letter in string: print(letter) # Outputs: # H # e # l # l # o [instruction] | Create a for loop that iterates over a string and prints out every letter. [input] | string = "Hello"
[output] | """ This python script contains a function which takes in two arguments and returns the sum of the two arguments. """ def add(x, y): return x + y [instruction] | Create a header comment for the following python script. [input] | def add(x, y): return x + y
[output] | try: num = int(input('Enter a number')) print(num * 5) except Exception as e: print("Error: {}".format(e)) [instruction] | Modify the given code to handle errors in the following Python script. [input] | num = int(input('Enter a number')) print(num * 5)
[output] | def replace_char(string, char_old, char_new): new_string = '' for char in string: if char == char_old: new_string = new_string + char_new else: new_string = new_string + char return new_string [instruction] | Suggest an enhancement to the following code to support unicode strings. [input] | def replace_char(string, char_old, char_new): for i in range(len(string)): if string[i] == char_old: string[i] = char_new return string
[output] | def compare(a, b): if a < b: return True else: return False [instruction] | Write a logic to obtain boolean output from a given comparison. [input] |
[output] | SELECT DISTINCT location FROM office WHERE (CURRENT_DATE - date_established) > (5 YEARS) [instruction] | Write a SQL query to list the unique office locations which are older than 5 years. [input] |
[output] | <html> <head> <title>Navigation Bar Example</title> </head> <body> <ul> <li>Home</li> <li>About</li> <li>Contact</li> </ul> <div> <h1>Home</h1> <p>This is the home page.</p> </div> <div> <h1>About</h1> <p>This is the about page. [instruction] | Construct an HTML page with a navigation bar and two sections. [input] |
[output] | def are_char_sets_equal(string1, string2): set1 = set(string1.lower()) set2 = set(string2.lower()) return set1 == set2 [instruction] | Write a function that takes two strings and compare to see if they have the same set of characters. [input] | string1 = "Listen" string2 = "Silent"
[output] | def list_primes(n): primes = [] for num in range(2, n+1): prime=True for i in range(2, int(num/2)+1): if num%i == 0: prime=False if prime: primes.append(num) return primes [instruction] | Find all prime numbers up to a given number. [input] |
[output] | def get_max_depth(nums): max_depth = 0 curr_depth = 0 for n in nums: if n is None: max_depth = max(max_depth, curr_depth) curr_depth -= 1 else: curr_depth += 1 return max_depth [instruction] | Write a function to compute the maximum depth of binary tree from a given list of numbers. [input] | nums = [3,9,20,None,None,15,7]
[output] | def get_first_element(tuples): return [t[0] for t in tuples] [instruction] | For each tuple in a given list, write a function to get the first element. [input] | [(1,2), (3,4), (5,6)]
[output] | def calculate_duration_in_seconds(time1, time2): start = datetime.strptime(time1, "%H:%M:%S") end = datetime.strptime(time2, "%H:%M:%S") duration = end-start return int(duration.total_seconds()) [instruction] | Given a list of valid times, return the duration in seconds between two times. [input] | time1 = "23:45:17" time2 = "01:30:12"
[output] | def get_nth_fibonacci_number(n): if n==0: return 0 elif n==1: return 1 else: return get_nth_fibonacci_number(n-1)+get_nth_fibonacci_number(n-2) [instruction] | Find the nth Fibonacci number in a given range. [input] |
[output] | class FilterList: def __init__(self, nums, condition): self.nums = nums self.condition = condition def __iter__(self): for num in self.nums: if self.condition(num): yield num [instruction] | Create a class that can be used to filter a given list of numbers using a given condition. [input] | nums = [3,6,7,2,1,12,14] condition = lambda x: x>5
[output] | def is_arithmetic_sequence(sequence): is_arithmetic = True diff = 0 for i in range(1, len(sequence)): if i ==1: diff = sequence[i]-sequence[i-1] else: new_diff = sequence[i]-sequence[i-1] if new_diff != diff: is_arithmet [instruction] | Construct an algorithm that checks if the given sequence is an arithmetic sequence. [input] |
[output] | def sort_dict_of_dicts(d, key): sorted_dict = dict(sorted(d.items(), key = lambda kv:(kv[1][key], kv[0]),reverse=True)) return sorted_dict [instruction] | Write a function to sort a dictionary of dictionaries in descending order based on a given key. [input] | d = {'dict1': {'name':'tom','age':20}, 'dict2': {'name':'jerry','age':25}, 'dict3': {'name':'bill','age':24}} key = 'age'
[output] | def json_to_dict(json_str): return json.loads(json_str) [instruction] | Create a function that takes a string representation of a JSON object and converts it into a Python dictionary. [input] |
[output] | def create_new_list(input_list, size): return input_list[:size] [instruction] | Write a function to create an new array of given size by copying the elements of given array. [input] | input_list = [1, 2, 3, 4, 5] size = 3
[output] | def swap_first_last(my_list): my_list[0], my_list[-1] = my_list[-1], my_list[0] return my_list [instruction] | Swap the first and last items in a given list. [input] | my_list = [3, 4, 8, 11, 5]
[output] | int highestNum = 0; foreach (int num in array) { if (num > highestNum) { highestNum = num; } } Console.WriteLine("The highest number in the array is " + highestNum); [instruction] | Develop a program to identify the highest number in a given array. [input] | Array: [2, 5, 15, 4, 7, 10]
[output] | class MilesToKilometers { public static void main(String[] args) { // Convert miles to kilometers double miles = 10; double kilometers = miles * 1.60934; System.out.println("Miles: " + miles); System.out.println("Kilometers: " + kilometers); } } [instruction] | Produce a Java program that converts miles to kilometers. [input] | Miles: 10