[output] | arr = [1, -2, 2, 4, 3] sum = 3 # create an empty list to store the longest subarray longest = [] # loop through the array for i in range(len(arr)): # set the sum of current subarray to 0 curr_sum = 0 # another loop to check if any subarray of the array # has a sum less than the gi [instruction] | Given an array of integers, find the longest subarray with total sum less than the given sum. [input] | arr = [1, -2, 2, 4, 3], sum = 3
[output] | arr = [(3, 5), (4, 1), (1, 4), (2, 2)] # sort the array based on the second element in each pair arr.sort(key=lambda x: x[1]) print("The array sorted in ascending order based on the second element of each array element pair is:", arr) [instruction] | Sort a given array in ascending order based on the second element of each array element pair. [input] | arr = [(3, 5), (4, 1), (1, 4), (2, 2)]
[output] | public static String removeCharAt(String s, int pos, int n) { return s.substring(0, pos) + s.substring(pos + n); } // Driver Code public static void main(String[] args) { String str = "This is a sample string"; int n = 5; int pos = 8; [instruction] | Write a Java program to remove n characters from a given string, starting from a specified position. [input] | str = "This is a sample string" n = 5 pos = 8
[output] | SELECT surname, LENGTH(surname) FROM table ORDER BY LENGTH(surname) DESC LIMIT 10; [instruction] | Write a SQL query to find the 10 longest surnames from a given table of surnames. [input] |
[output] | #!/bin/bash # This script shows the internet speed # Get deivce interface interface=$(ip route list | grep default | cut -d' ' -f5) # Get download speed download=$(curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python - | grep Download | cut -d':' -f2 | cu [instruction] | Write a shell script to detect the internet speed and display it in the terminal. [input] |
[output] | for x in myList: print(x) [instruction] | Write a snippet of code for looping over a list of numbers. [input] | myList = [1, 2, 3, 4]
[output] | highScoringStudents = [student for student in studentList if student['score'] > 80] [instruction] | Given a list of student records, write a Python code snippet to find all the students who scored above 80. [input] | studentList = [ {'name': 'John', 'score': 80}, {'name': 'Jane', 'score': 90}, {'name': 'Tom', 'score': 75}, {'name': 'Eric', 'score': 85} ]
[output] | public boolean canBeEqual(String s1, String s2) { if (s1.length() != s2.length()) return false; int len = s1.length(); for (int i = 0; i < len; i++) { if (s1.charAt(i) != s2.charAt(i) && s2.charAt(i) != '*') return false; } return true; } [instruction] | Create a function to check if the two strings can be made equal by replacing the letters with asterisks in the same order. [input] | String s1 = "aaabbcc"; String s2 = "a*a*b*c*";
[output] | function evaluateExpression(str) { let expression = str.split(' ').map(el => { if (el === '+' || el === '-' || el === '*' || el === '/') { return el; } return parseInt(el); }); let stack = []; let result; for (let i = 0; i < expression.length; [instruction] | Write a JavaScript program to parse and evaluate a simple mathematical expression entered as a string. [input] | String str = "2 + 3 * 4 - 5 / 3";
[output] | SELECT c.first_name, c.last_name, COUNT(o.customer_id) as num_orders FROM customers c JOIN orders o WHERE c.id = o.customer_id GROUP BY c.id ORDER BY num_orders DESC; [instruction] | Create a SQL query to find the first name, last name, and the total number of orders placed for all customers, sorted by Number of orders in descending order. [input] |
[output] | SELECT c.first_name, c.last_name FROM customers c JOIN orders o WHERE c.id = o.customer_id GROUP BY c.id HAVING COUNT(o.customer_id) > 3 ORDER BY c.first_name ASC; [instruction] | Create a SQL query to get the names of the customers who have placed orders more than 3 times. [input] |
[output] | total_items = len(array) unique_items = len(set(sum(array, []))) total_unique_items = total_items - unique_items print(total_unique_items) # Output: 4 [instruction] | Calculate the total number of unique elements in this array. [input] | array = [[1, 1], [2, 2], [3, 3], [4, 4]]
[output] | from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser') h1_text = soup.find("h1").text print(h1_text) # Output: Hello World! [instruction] | Parse this HTML document and print out the text inside the first <h1> tag. [input] | html_doc = "<html><head><title>Sample Website</title></head><body><h1>Hello World!</h1><p>This is a sample website.</p><ul><li>Item 1</li><li>Item 2</li><ul></body></html>"
[output] | for i in range(0, 11): print(i) [instruction] | Using the comment as a guide, insert a for loop in the following code to print out all numbers from 0 to 10. [input] | # TODO: Insert a for loop here
[output] | def longest_palindrome_substring(string): n = len(string) t = [[False for _ in range(n)] for _ in range(n)] max_length = 1 i = 0 while (i < n): t[i][i] = True i = i + 1 start = 0 i = 0 while i < n - 1: if (string[i] == string[i + 1]): [instruction] | Find the longest palindromic substring of this string. [input] | string = "carrace"
[output] | i = 0 while i < len(arr): pair = (arr[i], arr[i+1]) print(pair) i += 2 # Output: (1, 2) (3, 4) (5, None) [instruction] | Group the elements in this array into pairs and print them. [input] | arr = [1, 2, 3, 4, 5]
[output] | def sort_dict_by_values(d): sorted_dict = dict(sorted(d.items(), key=lambda x: x[1])) return sorted_dict print(sort_dict_by_values(my_dict)) # Output: {'Marcus': 3, 'Hanna': 5, 'Jerry': 10} [instruction] | Write a function in Python to sort this dictionary by values. [input] | my_dict = {"Jerry": 10, "Marcus": 3, "Hanna": 5}
[output] | import itertools flattened_list = list(itertools.chain.from_iterable(nested_list)) print(flattened_list) # Output: [1, 2, 3, 4, 5, 6] [instruction] | Flatten this nested list into a single list. [input] | nested_list = [[1,2], [3,4], [5,6]]
[output] | my_set = set(my_list) print(my_set) # Output: {1, 2, 3, 4} [instruction] | Create a set from this list where no duplicates will be present. [input] | my_list = [1, 2, 1, 2, 3, 4]
[output] | from itertools import permutations permutation_list = list(permutations(my_list)) print(permutation_list) # Output: [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] [instruction] | Generate all possible permutations of this list. [input] | my_list = [1, 2, 3]
[output] | var words = 'Some of my favorite words are pencil, dinosaur, and coffee.'; var result = words.match(/[a-zA-Z]+/g); console.log(result); // Result: ["Some", "of", "my", "favorite", "words", "are", "pencil", "dinosaur", "and", "coffee"] [instruction] | Use regex to extract words from the sentence 'Some of my favorite words are pencil, dinosaur, and coffee.' [input] |
[output] | def extract_emails(html_page): emails = [] soup = BeautifulSoup(html_page) for a in soup.find_all('a'): if a.get('href') and (a.get('href').startswith('mailto:')): emails.append(a.get('href')[7:]) return emails # Usage page = '<html> <body> Janice's email is janice@example.com, and Susi [instruction] | Design a function in Python to automate the extraction of emails from a single HTML page. [input] | <html> <body> Janice's email is janice@example.com, and Susie's is susie@example.com. </body> </html>
[output] | let array = [2,4,5,7,1]; let sumOfArrayElements = 0; for(let i = 0; i < array.length; i++) { sumOfArrayElements += array[i]; } console.log(sumOfArrayElements); // Output: 19 [instruction] | Given an array of integers, write a program in JavaScript to find the sum of the elements of the given array? [input] |
[output] | def is_palindrome(input_string): input_string = input_string.lower() if len(input_string) == 0: return False if len(input_string) == 1: return True if input_string[0] == input_string[-1]: return is_palindrome(input_string[1:-1]) return False # Usage st [instruction] | Write a Python script which checks if an input string is a palindrome. [input] |
[output] | def toggle_class(html_string, tag, tag_class): soup = BeautifulSoup(html_string, 'html.parser') tag_html = soup.find(tag) if tag_html: classes = tag_html.get('class') if classes and tag_class in classes: classes.remove(tag_class) else: cla [instruction] | Design a function in Python to toggle whether a given HTML tag has a given class. [input] | <div class="myClass">Hello world</div>
[output] | def format_as_slug(string): slug = string.lower() slug = slug.strip() slug = slug.replace(' ', '-') slug = re.sub('[^a-zA-Z0-9_\-]', '', slug) return slug # Usage string = "This is a sample string to be converted" result = format_as_slug(string) print(result) # Output: this-is- [instruction] | Design a function in Python to format a given string as a URL slug. [input] | string = "This is a sample string to be converted"
[output] | let array = [1, 9, 4, 2, 5, 7]; let sortedArray = array.sort(function(a, b){return a-b}); console.log(sortedArray); // Output: [1, 2, 4, 5, 7, 9] [instruction] | Create a script in JavaScript to sort the elements of an array in ascending order. [input] |
[output] | class MyClass: def __init__(self): # initialization code self.data = [] self.count = 0 def add_data(self, data_element): # add data to a list self.data.append(data_element) self.count += 1 def get_data_count(self): # return number of data elements return self.coun [instruction] | Build a class in Python with three methods. [input] |
[output] | g++ file.cpp -o file [instruction] | Compile the given C++ code. [input] | #include <iostream> using namespace std; int main() { cout << "Hello World!"; return 0; }
[output] | def divide(num1, num2): try: return num1 / num2 except: print('Error: Unable to perform division') [instruction] | Add a try-catch block to the given code snippet. [input] | def divide(num1, num2): return num1 / num2