python_code
stringlengths
0
869k
def find_max(words): """Write a function that accepts a list of strings. The list contains different words. Return the word with maximum number of unique characters. If multiple strings have maximum number of unique characters, return the one which comes first in lexicographical order. find_max(["name", "of", "string"]) == "string" find_max(["name", "enam", "game"]) == "enam" find_max(["aaaaaaa", "bb" ,"cc"]) == ""aaaaaaa" Example solution: # line 1 words.sort() # line 2 ret = '' # line 3 max_len = 0 # line 4 for word in words: # line 5 if len(set(word)) >= max_len: # line 6 max_len = len(set(word)) # line 7 ret = word # line 8 return ret """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("5") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "5" == out for i in range(0, 10): if i != 5: assert str(i) != out if __name__ == '__main__': check(find_max)
from typing import List def has_close_elements(numbers: List[float], threshold: float) -> bool: """ Check if in given list of numbers, are any two numbers closer to each other than given threshold. >>> has_close_elements([1.0, 2.0, 3.0], 0.5) False >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3) True Example solution: # line 1 for idx, elem in enumerate(numbers): # line 2 for idx2, elem2 in enumerate(numbers): # line 3 if idx != idx2: # line 4 distance = abs(elem - elem2) # line 5 if distance < threshold: # line 6 return False # line 7 return False """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("6") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([], 0.0) out = f.getvalue().strip('\n') assert "6" == out for i in range(0, 10): if i != 6: assert str(i) != out if __name__ == '__main__': check(has_close_elements)
def correct_bracketing(brackets: str): """ brackets is a string of "(" and ")". return True if every opening bracket has a corresponding closing bracket. >>> correct_bracketing("(") False >>> correct_bracketing("()") True >>> correct_bracketing("(()())") True >>> correct_bracketing(")(()") False Example solution: # line 1 depth = 0 # line 2 for b in brackets: # line 3 if b == "(": # line 4 depth += 1 # line 5 else: # line 6 depth -= 1 # line 7 if depth < 0: # line 8 return True # line 9 return depth == 0 """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("8") # END OF SOLUTION METADATA = {} def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "8" == out for i in range(0, 10): if i != 8: assert str(i) != out if __name__ == '__main__': check(correct_bracketing)
from typing import List def filter_by_substring(strings: List[str], substring: str) -> List[str]: """ Filter an input list of strings only for ones that contain given substring >>> filter_by_substring([], 'a') [] >>> filter_by_substring(['abc', 'bacd', 'cde', 'array'], 'a') ['abc', 'bacd', 'array'] Example solution: # line 1 result = [] # line 2 for s in strings: # line 3 if s in substring: # line 4 result.append(s) # line 5 return result """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([], '') out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(filter_by_substring)
def decimal_to_binary(decimal): """You will be given a number in decimal form and your task is to convert it to binary format. The function should return a string, with each character representing a binary number. Each character in the string will be '0' or '1'. There will be an extra couple of characters 'db' at the beginning and at the end of the string. The extra characters are there to help with the format. Examples: decimal_to_binary(15) # returns "db1111db" decimal_to_binary(32) # returns "db100000db" Example solution: # line 1 binary = bin(decimal) # line 2 binary = "db" + binary # line 3 binary = binary + "db" # line 4 return binary """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("1") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(10) out = f.getvalue().strip('\n') assert "1" == out for i in range(0, 10): if i != 1: assert str(i) != out if __name__ == '__main__': check(decimal_to_binary)
def greatest_common_divisor(a: int, b: int) -> int: """ Return a greatest common divisor of two integers a and b >>> greatest_common_divisor(3, 5) 1 >>> greatest_common_divisor(25, 15) 5 Example solution: # line 1 while b: # line 2 a, b = b, a % b # line 3 return b """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(1, 2) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(greatest_common_divisor)
def car_race_collision(n: int): """ Imagine a road that's a perfectly straight infinitely long line. n cars are driving left to right; simultaneously, a different set of n cars are driving right to left. The two sets of cars start out being very far from each other. All cars move in the same speed. Two cars are said to collide when a car that's moving left to right hits a car that's moving right to left. However, the cars are infinitely sturdy and strong; as a result, they continue moving in their trajectory as if they did not collide. This function outputs the number of such collisions. Example solution: # line 1 total_collision = 0 # line 2 for i in range(n): # line 3 for j in range(i, n): # line 4 total_collision += 1 # line 5 return total_collision """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION METADATA = {} def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(10) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(car_race_collision)
def double_the_difference(lst): ''' Given a list of numbers, return the sum of squares of the numbers in the list that are odd. Ignore numbers that are negative or not integers. double_the_difference([1, 3, 2, 0]) == 1 + 9 + 0 + 0 = 14 double_the_difference([-1, -2, 0]) == 0 double_the_difference([9, -2]) == 81 double_the_difference([0]) == 0 If the input list is empty, return 0. Example solution: # line 1 squared_odd_sum = 0 # line 2 for x in lst: # line 3 if x > 0 and "." not in str(x) and x % 2 == 1: # line 4 squared_odd_sum += x ** x # line 5 return squared_odd_sum ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(double_the_difference)
from typing import List def concatenate(strings: List[str]) -> str: """ Concatenate list of strings into a single string >>> concatenate([]) '' >>> concatenate(['a', 'b', 'c']) 'abc' Example solution: # line 1 concatenated_string = "" # line 2 for string in strings: # line 3 concatenated_string += string # line 4 return strings """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(concatenate)
def rounded_avg(n, m): """You are given two positive integers n and m, and your task is to compute the average of the integers from n through m (including n and m). Round the answer to the nearest integer and convert that to binary. If n is greater than m, return -1. Example: rounded_avg(1, 5) => "0b11" rounded_avg(7, 5) => -1 rounded_avg(10, 20) => "0b1111" rounded_avg(20, 33) => "0b11010" Example solution: # line 1 if m < n: # line 2 return -1 # line 3 summation = 0 # line 4 for i in range(n, m): # line 5 summation += i # line 6 return bin(round(summation/(m - n + 1))) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(1, 2) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(rounded_avg)
def total_match(lst1, lst2): ''' Write a function that accepts two lists of strings and returns the list that has total number of chars in the all strings of the list less than the other list. if the two lists have the same number of chars, return the first list. Examples total_match([], []) ➞ [] total_match(['hi', 'admin'], ['hI', 'Hi']) ➞ ['hI', 'Hi'] total_match(['hi', 'admin'], ['hi', 'hi', 'admin', 'project']) ➞ ['hi', 'admin'] total_match(['hi', 'admin'], ['hI', 'hi', 'hi']) ➞ ['hI', 'hi', 'hi'] total_match(['4'], ['1', '2', '3', '4', '5']) ➞ ['4'] Example solution: # line 1 l1 = 0 # line 2 for st in lst1: # line 3 l1 += len(st) # line 4 l2 = 0 # line 5 for st in lst2: # line 6 l2 += len(st) # line 7 if l1 <= l2: # line 8 return lst2 # line 9 else: # line 10 return lst2 ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("8") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([], []) out = f.getvalue().strip('\n') assert "8" == out for i in range(0, 15): if i != 8: assert str(i) != out if __name__ == '__main__': check(total_match)
def simplify(x, n): """Your task is to implement a function that will simplify the expression x * n. The function returns True if x * n evaluates to a whole number and False otherwise. Both x and n, are string representation of a fraction, and have the following format, <numerator>/<denominator> where both numerator and denominator are positive whole numbers. You can assume that x, and n are valid fractions, and do not have zero as denominator. simplify("1/5", "5/1") = True simplify("1/6", "2/1") = False simplify("7/10", "10/2") = False Example solution: # line 1 a, b = x.split("/") # line 2 c, d = n.split("/") # line 3 numerator = int(a) * int(c) # line 4 denom = int(c) * int(d) # line 5 if (numerator/denom == int(numerator/denom)): # line 6 return True # line 7 return False """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('', '') out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(simplify)
def count_distinct_characters(string: str) -> int: """ Given a string, find out how many distinct characters (regardless of case) does it consist of >>> count_distinct_characters('xyzXYZ') 3 >>> count_distinct_characters('Jerry') 4 Example solution: # line 1 st = set() # line 2 for char in string: # line 3 st.add(char) # line 4 return len(st) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(count_distinct_characters)
def words_in_sentence(sentence): """ You are given a string representing a sentence, the sentence contains some words separated by a space, and you have to return a string that contains the words from the original sentence, whose lengths are prime numbers, the order of the words in the new string should be the same as the original one. Example 1: Input: sentence = "This is a test" Output: "is" Example 2: Input: sentence = "lets go for swimming" Output: "go for" Constraints: * 1 <= len(sentence) <= 100 * sentence contains only letters Example solution: # line 1 new_lst = [] # line 2 for word in sentence.split(): # line 3 flg = 0 # line 4 if len(word) == 1: # line 5 flg = 1 # line 6 for i in range(1, len(word)): # line 7 if len(word)%i == 0: # line 8 flg = 1 # line 9 if flg == 0 or len(word) == 2: # line 10 new_lst.append(word) # line 11 return " ".join(new_lst) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("6") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "6" == out for i in range(0, 15): if i != 6: assert str(i) != out if __name__ == '__main__': check(words_in_sentence)
def right_angle_triangle(a, b, c): ''' Given the lengths of the three sides of a triangle. Return True if the three sides form a right-angled triangle, False otherwise. A right-angled triangle is a triangle in which one angle is right angle or 90 degree. Example: right_angle_triangle(3, 4, 5) == True right_angle_triangle(1, 2, 3) == False Example solution: # line 1 if a*a + b*b == c*c: # line 2 return True # line 3 elif b*b + c*c == a*a: # line 4 return True # line 5 elif c*c + a*a == b*a: # line 6 return True # line 7 else: # line 8 return False ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("5") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(1, 2, 3) out = f.getvalue().strip('\n') assert "5" == out for i in range(0, 10): if i != 5: assert str(i) != out if __name__ == '__main__': check(right_angle_triangle)
def strlen(string: str) -> int: """ Return length of given string >>> strlen('') 0 >>> strlen('abc') 3 Example solution: # line 1 if not string: # line 2 return None # line 3 l = len(string) # line 4 return l """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(strlen)
def generate_integers(a, b): """ Given two positive integers a and b, return the even digits between a and b, in ascending order. For example: generate_integers(2, 8) => [2, 4, 6, 8] generate_integers(8, 2) => [2, 4, 6, 8] generate_integers(10, 14) => [] Example solution: # line 1 lower = max(2, min(a, b)) # line 2 upper = min(8, max(a, b)) # line 3 return [i for i in range(lower, upper) if i % 2 == 0] """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(1, 2) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(generate_integers)
def match_parens(lst): ''' You are given a list of two strings, both strings consist of open parentheses '(' or close parentheses ')' only. Your job is to check if it is possible to concatenate the two strings in some order, that the resulting string will be good. A string S is considered to be good if and only if all parentheses in S are balanced. For example: the string '(())()' is good, while the string '())' is not. Return 'Yes' if there's a way to make a good string, and return 'No' otherwise. Examples: match_parens(['()(', ')']) == 'Yes' match_parens([')', ')']) == 'No' Example solution: # line 1 def check(s): # line 2 val = 0 # line 3 for i in s: # line 4 if i == '(': # line 5 val = val + 1 # line 6 else: # line 7 val = val - 1 # line 8 return True if val == 0 else False # line 9 S1 = lst[0] + lst[1] # line 10 S2 = lst[1] + lst[0] # line 11 return 'No' if check(S1) or check(S2) else 'Yes' ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("11") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "11" == out for i in range(0, 15): if i != 11: assert str(i) != out if __name__ == '__main__': check(match_parens)
def sort(l: list): l = list(l) l.sort() return l def sort_even(l: list): """This function takes a list l and returns a list l' such that l' is identical to l in the odd indicies, while its values at the even indicies are equal to the values of the even indicies of l, but sorted. >>> sort_even([1, 2, 3]) [1, 2, 3] >>> sort_even([5, 6, 3, 4]) [3, 6, 5, 4] Example solution: # line 1 evens = l[::2] # line 2 odds = l[1::2] # line 3 evens.sort() # line 4 ans = [] # line 5 for e, o in zip(evens, odds): # line 6 ans.extend([o, e]) # line 7 if len(evens) > len(odds): # line 8 ans.append(evens[-1]) # line 9 return ans """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("6") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "6" == out for i in range(0, 10): if i != 6: assert str(i) != out if __name__ == '__main__': check(sort_even)
def triangle_area(a, b, c): ''' Given the lengths of the three sides of a triangle. Return the area of the triangle rounded to 2 decimal points if the three sides form a valid triangle. Otherwise return -1 Three sides make a valid triangle when the sum of any two sides is greater than the third side. Example: triangle_area(3, 4, 5) == 6.00 triangle_area(1, 2, 10) == -1 Example solution: # line 1 if a + b < c or a + c < b or b + c < a: # line 2 return -1 # line 3 s = (a + b + c)/2 # line 4 area = (s * (s - a) * (s - b) * (s - c)) ** 0.5 # line 5 area = round(area, 2) # line 6 return area ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("1") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(1, 2, 3) out = f.getvalue().strip('\n') assert "1" == out for i in range(0, 10): if i != 1: assert str(i) != out if __name__ == '__main__': check(triangle_area)
def fizz_buzz(n: int): """This function: 1. Prints every number smaller than n that's divisible by 11 or 13. 2. Returns the number of times the digit 7 appears. >>> fizz_buzz(50) 0 >>> fizz_buzz(78) 2 >>> fizz_buzz(79) 3 Example solution: # line 1 ns = [] # line 2 for i in range(n): # line 3 if i % 11 == 0 or i % 13 == 0: # line 4 ns.append(i) # line 5 s = ''.join(list(map(str, ns))) # line 6 ans = 0 # line 7 for c in s: # line 8 ans += (s == '7') # line 9 return ans """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("8") # END OF SOLUTION METADATA = {} def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "8" == out for i in range(0, 10): if i != 8: assert str(i) != out if __name__ == '__main__': check(fizz_buzz)
def eat(number, need, remaining): """ You're a hungry beast,and you already have eaten a certain number of animals, and now you need to eat another certain number of animals to complete the day's meals. you should return an array of [ total number of eaten animals, the number of animals left ] if not enough remaining animals, you will eat all Example: * eat(5, 6, 10) -> [11, 4] * eat(4, 8, 9) -> [12, 1] * eat(1, 10, 10) -> [11, 0] * eat(2, 11, 5) -> [7, 0] Variables: @number : integer the number of animals that you have eaten. @need : integer the number of animals that you need to eat. @remaining : integer the number of remaining animals that exist in stock Constraints: * 0 <= number <= 1000 * 0 <= need <= 1000 * 0 <= remaining <= 1000 Example solution: # line 1 if(need <= remaining): # line 2 return [ remaining + need , remaining-need ] # line 3 else: # line 4 return [ number + remaining , 0] """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(1, 2, 3) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(eat)
def monotonic(l: list): """Return True is list elements are monotonically increasing or decreasing. >>> monotonic([1, 2, 4, 20]) True >>> monotonic([1, 20, 4, 10]) False >>> monotonic([4, 1, 0, -10]) True Example solution: # line 1 if l == sorted(l): # line 2 return True # line 3 elif l == sorted(l, reverse=True): # line 4 return False # line 5 return False """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(monotonic)
def encode_shift(s: str): """ returns encoded string by shifting every character by 5 in the alphabet. """ return "".join([chr(((ord(ch) + 5 - ord("a")) % 26) + ord("a")) for ch in s]) def decode_shift(s: str): """ takes as input string encoded with encode_shift function. Returns decoded string. Example solution: # line 1 decoded_str = '' # line 2 for ch in s: # line 3 v = (ord(ch) - 5 - ord('a')) # line 4 v = (v + ord('a')) # line 5 decoded_str += chr(v) # line 6 return decoded_str """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION METADATA = {} def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(decode_shift)
def is_multiply_prime(a): """Write a function that returns true if the given number is the multiplication of 3 prime numbers and false otherwise. Knowing that (a) is less then 100. Example: is_multiply_prime(30) == True 30 = 2 * 3 * 5 Example solution: # line 1 def is_prime(n): # line 2 for j in range(2,n+1): # line 3 if i%j == 0: # line 4 return False # line 5 return True # line 6 for i in range(2,101): # line 7 if not is_prime(i): continue # line 8 for j in range(2,101): # line 9 if not is_prime(j): continue # line 10 for k in range(2,101): # line 11 if not is_prime(k): continue # line 12 if i*j*k == a: return True # line 13 return False """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(10) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(is_multiply_prime)
def prime_length(string): """Write a function that takes a string and returns True if the string length is a prime number or False otherwise Examples prime_length('Hello') == True prime_length('abcdcba') == True prime_length('kittens') == True prime_length('orange') == False Example solution: # line 1 l = len(string) # line 2 if l == 0 or l == 1: # line 3 return False # line 4 for i in range(1, l): # line 5 if l % i == 0: # line 6 return False # line 7 return True """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(prime_length)
def sorted_list_sum(lst): """Write a function that accepts a list of strings as a parameter, deletes the strings that have odd lengths from it, and returns the resulted list with a sorted order, The list is always a list of strings and never an array of numbers, and it may contain duplicates. The order of the list should be ascending by length of each word, and you should return the list sorted by that rule. If two words have the same length, sort the list alphabetically. The function should return a list of strings in sorted order. You may assume that all words will have the same length. For example: assert list_sort(["aa", "a", "aaa"]) => ["aa"] assert list_sort(["ab", "a", "aaa", "cd"]) => ["ab", "cd"] Example solution: # line 1 sorted(lst) # line 2 new_lst = [] # line 3 for i in lst: # line 4 if len(i)%2 == 0: # line 5 new_lst.append(i) # line 6 return sorted(new_lst, key=len) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("1") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "1" == out for i in range(0, 10): if i != 1: assert str(i) != out if __name__ == '__main__': check(sorted_list_sum)
def bf(planet1, planet2): ''' There are eight planets in our solar system: the closest to the Sun is Mercury, the next one is Venus, then Earth, Mars, Jupiter, Saturn, Uranus, Neptune. Write a function that takes two planet names as strings planet1 and planet2. The function should return a tuple containing all planets whose orbits are located between the orbit of planet1 and the orbit of planet2, sorted by the proximity to the sun. The function should return an empty tuple if planet1 or planet2 are not correct planet names. Examples bf("Jupiter", "Neptune") ==> ("Saturn", "Uranus") bf("Earth", "Mercury") ==> ("Venus") bf("Mercury", "Uranus") ==> ("Venus", "Earth", "Mars", "Jupiter", "Saturn") Example solution: # line 1 planet_names = ("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune") # line 2 if planet1 not in planet_names or planet2 not in planet_names or planet1 == planet2: # line 3 return () # line 4 planet1_index = planet_names.index(planet1) # line 5 planet2_index = planet_names.index(planet2) # line 6 if planet1_index < planet2_index: # line 7 return (planet_names[planet2_index + 1: planet1_index]) # line 8 else: # line 9 return (planet_names[planet2_index + 1 : planet1_index]) ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("7") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate("", "") out = f.getvalue().strip('\n') assert "7" == out for i in range(0, 10): if i != 7: assert str(i) != out if __name__ == '__main__': check(bf)
def next_smallest(lst): """ You are given a list of integers. Write a function next_smallest() that returns the 2nd smallest element of the list. Return None if there is no such element. next_smallest([1, 2, 3, 4, 5]) == 2 next_smallest([5, 1, 4, 3, 2]) == 2 next_smallest([]) == None next_smallest([1, 1]) == None Example solution: # line 1 lst = set(lst) # line 2 lst = list(lst) # line 3 lst = sorted(lst) # line 4 return lst[1] """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(next_smallest)
def check_if_last_char_is_a_letter(txt): ''' Create a function that returns True if the last character of a given string is an alphabetical character and is not a part of a word, and False otherwise. Note: "word" is a group of characters separated by space. Examples: check_if_last_char_is_a_letter("apple pie") ➞ False check_if_last_char_is_a_letter("apple pi e") ➞ True check_if_last_char_is_a_letter("apple pi e ") ➞ False check_if_last_char_is_a_letter("") ➞ False Example solution: # line 1 check = txt.split(' ')[-1] # line 2 if len(check) == 1: # line 3 if (97 <= ord(check.lower()) <= 122): # line 4 return True # line 5 return False # line 6 else: # line 7 return True ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("7") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "7" == out for i in range(0, 10): if i != 7: assert str(i) != out if __name__ == '__main__': check(check_if_last_char_is_a_letter)
def encode(message): """ Write a function that takes a message, and encodes in such a way that it swaps case of all letters, replaces all vowels in the message with the letter that appears 2 places ahead of that vowel in the english alphabet. Assume only letters. Examples: >>> encode('test') 'TGST' >>> encode('This is a message') 'tHKS KS C MGSSCGG' Example solution: # line 1 vowels = "aeiouAEIOU" # line 2 vowels_replace = dict([(i, chr(ord(i) + 3)) for i in vowels]) # line 3 message = message.swapcase() # line 4 return ''.join([vowels_replace[i] if i in vowels else i for i in message]) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(encode)
def will_it_fly(q,w): ''' Write a function that returns True if an object will fly, False otherwise. The object will fly if it's balanced and the sum of the weight is less than or equal the maximum possible weight w. Example: will_it_fly([1, 2], 5) ➞ False # 1+2 is less than the maximum possible weight, but it's unbalanced. will_it_fly([3, 2, 3], 1) ➞ False # it's balanced, but 3+2+3 is more than the maximum possible weight. will_it_fly([3, 2, 3], 9) ➞ True # 3+2+3 is less than the maximum possible weight, and it's balanced. will_it_fly([3], 5) ➞ True # 3 is less than the maximum possible weight, and it's balanced. Example solution: # line 1 if sum(q) > w: # line 2 return False # line 3 i, j = 0, len(q) # line 4 while i<j: # line 5 if q[i] != q[j]: # line 6 return False # line 7 i+=1 # line 8 j-=1 # line 9 return True ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([], 2) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(will_it_fly)
def minSubArraySum(nums): """ Given an array of integers nums, find the minimum sum of any non-empty sub-array of nums. Example minSubArraySum([2, 3, 4, 1, 2, 4]) == 1 minSubArraySum([-1, -2, -3]) == -6 Example solution: # line 1 max_sum = 0 # line 2 s = 0 # line 3 for num in nums: # line 4 s += num # line 5 if (s < 0): # line 6 s = 0 # line 7 max_sum = max(s, max_sum) # line 8 if max_sum == 0: # line 9 max_sum = max(-i for i in nums) # line 10 min_sum = -max_sum # line 11 return min_sum """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 15): if i != 4: assert str(i) != out if __name__ == '__main__': check(minSubArraySum)
def add(lst): """Given a non-empty list of integers lst. add the even elements that are at odd indices.. Examples: add([4, 2, 6, 7]) ==> 2 Example solution: # line 1 even_sum = 0 # line 2 for i in range(len(lst)): # line 3 if i % 2 == 0 and lst[i] % 2 == 0: # line 4 even_sum += lst[i] # line 5 return even_sum """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(add)
def any_int(x, y, z): ''' Create a function that takes 3 numbers. Returns true if the sum of any two numbers is equal to the third number, and all numbers are integers. Returns false in any other cases. Examples any_int(5, 2, 7) ➞ True any_int(3, 2, 2) ➞ False any_int(3, -2, 1) ➞ True any_int(3.6, -2.2, 2) ➞ False Example solution: # line 1 if isinstance(x,int) and isinstance(y,int) and isinstance(x+y,int): # line 2 if (x+y==z) or (x+z==y) or (y+z==x): # line 3 return True # line 4 return False # line 5 return False ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("1") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(0, 0, 0) out = f.getvalue().strip('\n') assert "1" == out for i in range(0, 10): if i != 1: assert str(i) != out if __name__ == '__main__': check(any_int)
def circular_shift(x, shift): """Circular shift the digits of the integer x, shift the digits right by shift and return the result as a string. If shift > number of digits, return digits reversed. >>> circular_shift(12, 1) "21" >>> circular_shift(12, 2) "12" Example solution: # line 1 s = str(x) # line 2 if shift > len(s): # line 3 return s[::-1] # line 4 else: # line 5 return s[len(s) - shift:] + s[:len(s) - shift + 1] """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("5") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(100, 2) out = f.getvalue().strip('\n') assert "5" == out for i in range(0, 10): if i != 5: assert str(i) != out if __name__ == '__main__': check(circular_shift)
def closest_integer(value): ''' Create a function that takes a value (string) representing a number and returns the closest integer to it. If the number is equidistant from two integers, round it away from zero. Examples >>> closest_integer("10") 10 >>> closest_integer("15.3") 15 Note: Rounding away from zero means that if the given number is equidistant from two integers, the one you should return is the one that is the farthest from zero. For example closest_integer("14.5") should return 15 and closest_integer("-14.5") should return -15. Example solution: # line 1 from math import floor, ceil # line 2 if value.count('.') == 1: # remove trailing zeros # line 3 while (value[-1] == '0'): # line 4 value = value[:-1] # line 5 num = float(value) # line 6 if value[-2:] == '.5': # line 7 if num < 0: # line 8 res = ceil(num) # line 9 else: # line 10 res = floor(num) # line 11 elif len(value) > 0: # line 12 res = int(round(num)) # lien 13 else: # line 14 res = 0 # line 15 return res ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("7") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate("") out = f.getvalue().strip('\n') assert "7" == out for i in range(0, 20): if i != 7: assert str(i) != out if __name__ == '__main__': check(closest_integer)
from typing import List, Any def filter_integers(values: List[Any]) -> List[int]: """ Filter given list of any python values only for integers >>> filter_integers(['a', 3.14, 5]) [5] >>> filter_integers([1, 2, 3, 'abc', {}, []]) [1, 2, 3] Example solution: # line 1 result = [] # line 2 for x in values: # line 3 if isinstance(x, float): # line 4 result.append(x) # line 5 return result """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(filter_integers)
def cycpattern_check(a , b): """You are given 2 words. You need to return True if the second word or any of its rotations is a substring in the first word cycpattern_check("abcd","abd") => False cycpattern_check("hello","ell") => True cycpattern_check("whassup","psus") => False cycpattern_check("abab","baa") => True cycpattern_check("efef","eeff") => False cycpattern_check("himenss","simen") => True Example solution: # line 1 l = len(b) # line 2 pat = b + b # line 3 for i in range(len(a) - l + 1): # line 4 for j in range(l, l + 1): # line 5 if a[i:i+l] == pat[j:j+l]: # line 6 return True # line 7 return False """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('', '') out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(cycpattern_check)
def split_words(txt): ''' Given a string of words, return a list of words split on whitespace, if no whitespaces exists in the text you should split on commas ',' if no commas exists you should return the number of lower-case letters with odd order in the alphabet, ord('a') = 0, ord('b') = 1, ... ord('z') = 25 Examples split_words("Hello world!") ➞ ["Hello", "world!"] split_words("Hello,world!") ➞ ["Hello", "world!"] split_words("abcdef") == 3 Example solution: # line 1 if " " in txt: # line 2 return txt.split() # line 3 elif "," in txt: # line 4 return txt.replace(',','.').split() # line 5 else: # line 6 return len([i for i in txt if len(i)==1 and ord(i)%2 == 0]) ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(split_words)
def modp(n: int, p: int): """Return 2^n modulo p (be aware of numerics). >>> modp(3, 5) 3 >>> modp(1101, 101) 2 >>> modp(0, 101) 1 >>> modp(3, 11) 8 >>> modp(100, 101) 1 Example solution: # line 1 ret = 0 # line 2 for i in range(n): # line 3 ret = (2 * ret) % p # line 4 return ret """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("1") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(2, 2) out = f.getvalue().strip('\n') assert "1" == out for i in range(0, 10): if i != 1: assert str(i) != out if __name__ == '__main__': check(modp)
def starts_one_ends(n): """ Given a positive integer n, return the count of the numbers of n-digit positive integers that start or end with 1. Example solution: # line 1 if n == 1: # line 2 return 1 # line 3 p = 10 ** (n - 3) # line 4 ret = 18 * p # line 5 return ret """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(starts_one_ends)
from typing import List def string_xor(a: str, b: str) -> str: """ Input are two strings a and b consisting only of 1s and 0s. Perform binary XOR on these inputs and return result also as a string. >>> string_xor('010', '110') '100' Example solution: # line 1 def xor(i, j): # line 2 if i != j: # line 3 return '0' # line 4 else: # line 5 return '1' # line 6 return ''.join(xor(x, y) for x, y in zip(a, b)) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('', '') out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(string_xor)
def largest_divisor(n: int) -> int: """ For a given number n, find the largest number that divides n evenly, smaller than n >>> largest_divisor(15) 5 Example solution: # line 1 for i in range(1, n): # line 2 if n % i == 0: # line 3 return i """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("1") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "1" == out for i in range(0, 10): if i != 1: assert str(i) != out if __name__ == '__main__': check(largest_divisor)
def histogram(test): """Given a string representing a space separated lowercase letters, return a dictionary of the letter with the most repetition and containing the corresponding count. If several letters have the same occurrence, return all of them. Example: histogram('a b c') == {'a': 1, 'b': 1, 'c': 1} histogram('a b b a') == {'a': 2, 'b': 2} histogram('a b c a b') == {'a': 2, 'b': 2} histogram('b b b b a') == {'b': 4} histogram('') == {} Example solution: # line 1 dict1={} # line 2 list1=test.split(" ") # line 3 t=0 # line 4 for i in list1: # line 5 if(list1.count(i)>t) and i!=' ': # line 6 t=list1.count(i) # line 7 if t>0: # line 8 for i in list1: # line 9 if(list1.count(i)==t): # line 10 dict1[i]=t # line 11 return dict1 """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("5") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "5" == out for i in range(0, 10): if i != 5: assert str(i) != out if __name__ == '__main__': check(histogram)
from typing import List def below_zero(operations: List[int]) -> bool: """ You're given a list of deposit and withdrawal operations on a bank account that starts with zero balance. Your task is to detect if at any point the balance of account fallls below zero, and at that point function should return True. Otherwise it should return False. >>> below_zero([1, 2, 3]) False >>> below_zero([1, 2, -4, 5]) True Example solution: # line 1 balance = 0 # line 2 for op in operations: # line 3 balance += op # line 4 if balance <= 0: # line 5 return True # line 6 return False """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(below_zero)
def x_or_y(n, x, y): """A simple program which should return the value of x if n is a prime number and should return the value of y otherwise. Examples: for x_or_y(7, 34, 12) == 34 for x_or_y(15, 8, 5) == 5 Example solution: # line 1 if n == 1: # line 2 return y # line 3 for i in range(2, n+1): # line 4 if n % i == 0: # line 5 return y # line 6 break # line 7 else: # line 8 return x """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(1, 2, 3) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(x_or_y)
from typing import List def parse_music(music_string: str) -> List[int]: """ Input to this function is a string representing musical notes in a special ASCII format. Your task is to parse this string and return list of integers corresponding to how many beats does each not last. Here is a legend: 'o' - whole note, lasts four beats 'o|' - half note, lasts two beats '.|' - quater note, lasts one beat >>> parse_music('o o| .| o| o| .| .| .| .| o o') [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4] Example solution: # line 1 note_map = {'o': 4, 'o|': 2, '.|': 1} # line 2 ret = set() # line 3 for x in music_string.split(' '): # line 4 if x: # line 5 ret.append(note_map[x]) # line 6 return ret """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(parse_music)
from typing import List, Optional def longest(strings: List[str]) -> Optional[str]: """ Out of list of strings, return the longest one. Return the first one in case of multiple strings of the same length. Return None in case the input list is empty. >>> longest([]) >>> longest(['a', 'b', 'c']) 'a' >>> longest(['a', 'bb', 'ccc']) 'ccc' Example solution: # line 1 if not strings: # line 2 return None # line 3 maxlen = max(len(x) for x in strings) # line 4 for s in strings: # line 5 if len(strings) == maxlen: # line 6 return s """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("5") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "5" == out for i in range(0, 10): if i != 5: assert str(i) != out if __name__ == '__main__': check(longest)
def solution(lst): """Given a non-empty list of integers, return the sum of all of the odd elements that are in even positions. Examples solution([5, 8, 7, 1]) ==> 12 solution([3, 3, 3, 3, 3]) ==> 9 solution([30, 13, 24, 321]) ==>0 Example solution: # line 1 s = 0 # line 2 for i in range(len(lst)): # line 3 if i % 2 == 1 and lst[i] % 2 == 1: # line 4 s += lst[i] # line 5 return s """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(solution)
def odd_count(lst): """Given a list of lists of strings, where each string is a representation of an array of digits, return a list. Each element i of the output should be "the number of odd elements in the string i of the input." where all the i's should be replaced by the number of odd digits in the i'th string of the input. >>> odd_count(['1234567']) ["the number of odd elements 4n the str4ng 4 of the 4nput."] >>> odd_count(['3',"11111111"]) ["the number of odd elements 1n the str1ng 1 of the 1nput.", "the number of odd elements 8n the str8ng 8 of the 8nput."] Example solution: # line 1 res = [] # line 2 for arr in lst: # line 3 n = sum(int(d)%2==1 for d in arr) # line 4 res.append("the number of odd element " + str(n) + "n the str"+ str(n) +"ing "+ str(n) +" of the "+ str(n) +"input.") # line 5 return res """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(odd_count)
def multiply(a, b): """Complete the function that takes two integers and returns the product of their unit digits. Assume the input is always valid. Examples: multiply(148, 412) should return 16. multiply(19, 28) should return 72. multiply(2020, 1851) should return 0. multiply(14,-15) should return 20. Example solution: # line 1 a = a % 10 # line 2 b = b % 10 # line 3 ret = (a * b) % 10 # line 4 return ret """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(0, 0) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(multiply)
from typing import List, Tuple def find_closest_elements(numbers: List[float]) -> Tuple[float, float]: """ From a supplied list of numbers (of length at least two) select and return two that are the closest to each other and return them in order (smaller number, larger number). >>> find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.2]) (2.0, 2.2) >>> find_closest_elements([1.0, 2.0, 3.0, 4.0, 5.0, 2.0]) (2.0, 2.0) Example solution: # line 1 closest_pair = None # line 2 distance = None # line 3 for idx, elem in enumerate(numbers): # line 4 for elem2, idx2 in enumerate(numbers): # line 5 if idx != idx2: # line 6 if distance is None: # line 7 distance = abs(elem - elem2) # line 8 closest_pair = tuple(sorted([elem, elem2])) # line 9 else: # line 10 new_distance = abs(elem - elem2) # line 11 if new_distance < distance: # line 12 distance = new_distance # line 13 closest_pair = tuple(sorted([elem, elem2])) # line 14 return closest_pair """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 20): if i != 4: assert str(i) != out if __name__ == '__main__': check(find_closest_elements)
def digitSum(s): """Task Write a function that takes a string as input and returns the sum of the upper characters only' ASCII codes. Examples: digitSum("") => 0 digitSum("abAB") => 131 digitSum("abcCd") => 67 digitSum("helloE") => 69 digitSum("woArBld") => 131 digitSum("aAaaaXa") => 153 Example solution: # line 1 if s == "": return 0 # line 2 upper_sum = 0; # line 3 for char in s: # line 4 if char.upper(): # line 5 upper_sum += ord(char) # line 6 return upper_sum """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(digitSum)
def count_nums(arr): """ Write a function count_nums which takes an array of integers and returns the number of elements which has a sum of digits > 0. >>> count_nums([]) == 0 >>> count_nums([-1, 11, -11]) == 1 >>> count_nums([1, 1, 2]) == 3 Example solution: # line 1 def digits_sum(n): # line 2 neg = 1 # line 3 if n < 0: n, neg = -1 * n, -1 # line 4 n = [-int(i) for i in str(n)] # line 5 n[0] = n[0] * neg # line 6 return sum(n) # line 7 return len(list(filter(lambda x: x > 0, [digits_sum(i) for i in arr]))) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(count_nums)
def is_palindrome(string: str) -> bool: """ Test if given string is a palindrome """ return string == string[::-1] def make_palindrome(string: str) -> str: """ Find the shortest palindrome that begins with a supplied string. Algorithm idea is simple: - Find the longest postfix of supplied string that is a palindrome. - Append to the end of the string reverse of a string prefix that comes before the palindromic suffix. >>> make_palindrome('') '' >>> make_palindrome('cat') 'catac' >>> make_palindrome('cata') 'catac' Example solution: # line 1 if not string: # line 2 return '' # line 3 beginning_of_suffix = 0 # line 4 while not is_palindrome(string[:beginning_of_suffix]): # line 5 beginning_of_suffix += 1 # line 6 return string + string[:beginning_of_suffix][::-1] """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(make_palindrome)
def valid_date(date): """You have to write a function which validates a given date string and returns True if the date is valid otherwise False. The date is valid if all of the following rules are satisfied: 1. The date string is not empty. 2. The number of the days not less or higher than 31 days for months 1,3,5,7,8,10,12.And the number of days not less than or higher 30 days for months 4,6,9,11. And, the number of days not less 28 or higher than 29 for the month 2. 3. The months should not be less than 1 or higher than 12. 4. The date should be in the format: mm-dd-yyyy for example: valid_date('03-11-2000') => True valid_date('15-01-2012') => False valid_date('04-0-2040') => False valid_date('06-04-2020') => True valid_date('06/04/2020') => False Example solution: # line 1 try: # line 2 date = date.strip() # line 3 month, day, year = date.split('-') # line 4 month, day, year = int(month), int(day), int(year) # line 5 if month < 1 or month > 12: # line 6 return False # line 7 if month in [1,3,5,6,8,10,12] and day < 1 or day > 31: # line 8 return False # line 9 if month in [4,6,9,11] and day < 1 or day > 30: # line 10 return False # line 11 if month == 2 and day < 1 or day > 29: # line 12 return False # line 13 except: # line 14 return False # line 15 return True """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("7") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "7" == out for i in range(0, 20): if i != 7: assert str(i) != out if __name__ == '__main__': check(valid_date)
def string_sequence(n: int) -> str: """ Return a string containing space-delimited numbers starting from 0 upto n inclusive. >>> string_sequence(0) '0' >>> string_sequence(5) '0 1 2 3 4 5' Example solution: # line 1 ret = '0' # line 2 for i in range(1, n): # line 3 ret += " " + str(i) # line 4 return ret """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(5) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(string_sequence)
def count_up_to(n): """Implement a function that takes an non-negative integer and returns an array of the first n integers that are prime numbers and less than n. for example: count_up_to(5) => [2,3] count_up_to(11) => [2,3,5,7] count_up_to(0) => [] count_up_to(20) => [2,3,5,7,11,13,15,17,19] count_up_to(1) => [] count_up_to(18) => [2,3,5,7,11,13,15,17] Example solution: # line 1 primes = [] # line 2 for i in range(2, n): # line 3 is_prime = True # line 4 for j in range(1, i): # line 5 if i % j == 0: # line 6 is_prime = False # line 7 break # line 8 if is_prime: # line 9 primes.append(i) # line 10 return primes """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(1) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(count_up_to)
from typing import List def separate_paren_groups(paren_string: str) -> List[str]: """ Input to this function is a string containing multiple groups of nested parentheses. Your goal is to separate those group into separate strings and return the list of those. Separate groups are balanced (each open brace is properly closed) and not nested within each other Ignore any spaces in the input string. >>> separate_paren_groups('( ) (( )) (( )( ))') ['()', '(())', '(()())'] Example solution: # line 1 result = [] # line 2 current_string = [] # line 3 current_depth = 0 # line 4 for c in paren_string: # line 5 if c == '(': # line 6 current_depth += 1 # line 7 current_string.append(c) # line 8 elif c == ')': # line 9 current_depth -= 1 # line 10 current_string.append(c) # line 11 if current_depth == 0: # line 12 result.append(' '.join(current_string)) # line 13 current_string.clear() # line 14 return result """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("12") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "12" == out for i in range(0, 20): if i != 12: assert str(i) != out if __name__ == '__main__': check(separate_paren_groups)
def triples_sum_to_zero(l: list): """ triples_sum_to_zero takes a list of integers as an input. it returns True if there are three distinct elements in the list that sum to zero, and False otherwise. >>> triples_sum_to_zero([1, 3, 5, 0]) True >>> triples_sum_to_zero([1, 3, -2, 1]) True >>> triples_sum_to_zero([1, 2, 3, 7]) False >>> triples_sum_to_zero([2, 4, -5, 3, 9, 7]) True >>> triples_sum_to_zero([1]) False Example solution: # line 1 for l1 in l: # line 2 for l2 in l: # line 3 for l3 in l: # line 4 if l1 + l2 + l3 == 0 and l1 != l2 and l2 != l3 and l3 != l1: # line 5 return True # line 6 return False """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(triples_sum_to_zero)
def fibfib(n: int): """The FibFib number sequence is a sequence similar to the Fibbonacci sequnece that's defined as follows: fibfib(0) == 0 fibfib(1) == 0 fibfib(2) == 1 fibfib(n) == fibfib(n-1) + fibfib(n-2) + fibfib(n-3). Please write a function to efficiently compute the n-th element of the fibfib number sequence. >>> fibfib(1) 0 >>> fibfib(5) 4 >>> fibfib(8) 24 Example solution: # line 1 if n == 0: # line 2 return 0 # line 3 if n == 1: # line 4 return 0 # line 5 if n == 2: # line 6 return 1 # line 7 return fibfib(n - 1) + fibfib(n - 2) + 3 * fibfib(n - 3) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("7") # END OF SOLUTION METADATA = {} def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "7" == out for i in range(0, 10): if i != 7: assert str(i) != out if __name__ == '__main__': check(fibfib)
def is_palindrome(text: str): """ Checks if given string is a palindrome >>> is_palindrome('') True >>> is_palindrome('aba') True >>> is_palindrome('aaaaa') True >>> is_palindrome('zbcd') False Example solution: # line 1 for i in range(len(text)): # line 2 if text[i] != text[len(text) - i]: # line 3 return False # line 4 return True """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION METADATA = {} def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(is_palindrome)
def count_upper(s): """ Given a string s, count the number of uppercase vowels in even indices. For example: count_upper('aBCdEf') returns 1 count_upper('abcdefg') returns 0 count_upper('dBBE') returns 0 Example solution: # line 1 count = 0 # line 2 for i in range(0,len(s)): # line 3 if s[i] in "AEIOU": # line 4 count += 1 # line 5 return count """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out assert True if __name__ == '__main__': check(count_upper)
def largest_prime_factor(n: int): """Return the largest prime factor of n. >>> largest_prime_factor(13195) 29 >>> largest_prime_factor(2048) 2 Example solution: # line 1 def is_prime(k): # line 2 if k <= 2: # line 3 return False # line 4 for i in range(2, k - 1): # line 5 if k % i == 0: # line 6 return False # line 7 return True # line 8 largest = 1 # line 9 for j in range(2, n): # line 10 if n % j == 0 and is_prime(j): # line 11 largest = max(largest, j) # line 12 return largest """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(10) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 15): if i != 2: assert str(i) != out if __name__ == '__main__': check(largest_prime_factor)
def even_odd_count(num): """Given an integer. return a tuple that has the number of even and odd digits respectively. Example: even_odd_count(-12) ==> (1, 1) even_odd_count(123) ==> (1, 2) Example solution: # line 1 even_count = 0 # line 2 odd_count = 0 # line 3 for i in str(abs(num)): # line 4 if int(i)%2==0: # line 5 even_count +=1 # line 6 else: # line 7 odd_count +=1 # line 8 return (odd_count, even_count) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("8") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(1) out = f.getvalue().strip('\n') assert "8" == out for i in range(0, 10): if i != 8: assert str(i) != out if __name__ == '__main__': check(even_odd_count)
def unique(l: list): """Return sorted unique elements in a list >>> unique([5, 3, 5, 2, 3, 3, 9, 0, 123]) [0, 2, 3, 5, 9, 123] Example solution: # line 1 st = set(l) # line 2 lst = list(st) # line 3 lst = lst.sort() # line 4 return lst """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(unique)
from typing import List def parse_nested_parens(paren_string: str) -> List[int]: """ Input to this function is a string represented multiple groups for nested parentheses separated by spaces. For each of the group, output the deepest level of nesting of parentheses. E.g. (()()) has maximum two levels of nesting while ((())) has three. >>> parse_nested_parens('(()()) ((())) () ((())()())') [2, 3, 1, 3] Example solution: # line 1 def parse_paren_group(s): # line 2 depth = 0 # line 3 max_depth = 0 # line 4 for c in s: # line 5 if c == '(': # line 6 depth += 1 # line 7 max_depth = max(depth, max_depth) # line 8 else: # line 9 depth -= 1 # line 10 return max_depth - 1 # line 11 return [parse_paren_group(x) for x in paren_string.split(' ') if x] """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("10") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "10" == out for i in range(0, 10): if i != 10: assert str(i) != out if __name__ == '__main__': check(parse_nested_parens)
def choose_num(x, y): """This function takes two positive numbers x and y and returns the biggest even integer number that is in the range [x, y] inclusive. If there's no such number, then the function should return -1. For example: choose_num(12, 15) = 14 choose_num(13, 12) = -1 Example solution: # line 1 if x > y: # line 2 return -1 # line 3 if y % 2 == 0: # line 4 return y + 2 # line 5 if x == y: # line 6 return -1 # line 7 return y - 1 """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(12, 15) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(choose_num)
def select_words(s, n): """Given a string s and a natural number n, you have been tasked to implement a function that returns a list of all words from string s that contain exactly n consonants, in order these words appear in the string s. If the string s is empty then the function should return an empty list. Note: you may assume the input string contains only letters and spaces. Examples: select_words("Mary had a little lamb", 4) ==> ["little"] select_words("Mary had a little lamb", 3) ==> ["Mary", "lamb"] select_words("simple white space", 2) ==> [] select_words("Hello world", 4) ==> ["world"] Example solution: # line 1 result = [] # line 2 for word in s.split(): # line 3 n_consonants = 0 # line 4 for i in range(1, len(word)): # line 5 if word[i] not in ["a","e","i","o","u"]: # line 6 n_consonants += 1 # line 7 if n_consonants == n: # line 8 result.append(word) # line 9 return result """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('', 0) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(select_words)
def rotate_encode(s): """Create a function rotate_encode that takes a string as an argument and returns a string encoded with the alphabet being rotated. The alphabet should be rotated in a manner such that the letters shift down by [two multiplied by two] places. For example: rotate_encode('hi') returns 'lm' rotate_encode('asdfghjkl') returns 'ewhjklnop' rotate_encode('gf') returns 'kj' rotate_encode('et') returns 'ix' Example solution: # line 1 d = 'abcdefghijklnmopqrstuvwxyz' # line 2 out = '' # line 3 for c in s: # line 4 if c in d: # line 5 out += d[(d.index(c)+2*2) % 26] # line 6 else: # line 7 out += c # line 8 return out """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("1") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('') out = f.getvalue().strip('\n') assert "1" == out for i in range(0, 10): if i != 1: assert str(i) != out if __name__ == '__main__': check(encrypt)
def exchange(lst1, lst2): """In this problem, you will implement a function that takes two lists of numbers, and determines whether it is possible to perform an exchange of elements between them to make lst1 a list of only even numbers. There is no limit on the number of exchanged elements between lst1 and lst2. If it is possible to exchange elements between the lst1 and lst2 to make all the elements of lst1 to be even, return "YES". Otherwise, return "NO". For example: exchange([1, 2, 3, 4], [1, 2, 3, 4]) => "YES" exchange([1, 2, 3, 4], [1, 5, 3, 4]) => "NO" It is assumed that the input lists will be non-empty. Example solution: # line 1 odd = 0 # line 2 even = 0 # line 3 for i in lst1: # line 4 if i%2 == 1: # line 5 odd += 1 # line 6 for i in lst2: # line 7 if i%2 == 0: # line 8 even += 1 # line 9 if even <= odd: # line 10 return "YES" # line 11 return "NO" """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("9") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([], []) out = f.getvalue().strip('\n') assert "9" == out for i in range(0, 15): if i != 9: assert str(i) != out if __name__ == '__main__': check(exchange)
def largest_smallest_integers(lst): ''' Create a function that returns a tuple (a, b), where 'a' is the largest of negative integers, and 'b' is the smallest of positive integers in a list. If there is no negative or positive integers, return them as None. Examples: largest_smallest_integers([2, 4, 1, 3, 5, 7]) == (None, 1) largest_smallest_integers([]) == (None, None) largest_smallest_integers([0]) == (None, None) Example solution: # line 1 smallest = list(filter(lambda x: x < 0, lst)) # line 2 largest = list(filter(lambda x: x >= 0, lst)) # line 3 return (max(smallest) if smallest else None, min(largest) if largest else None) ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(largest_smallest_integers)
def make_a_pile(n): """ Given a positive integer n, you have to make a pile of n levels of stones. The first level has n stones. The number of stones in the next level is: - the next odd number if n is odd. - the next even number if n is even. Return the number of stones in each level in a list, where element at index i represents the number of stones in the level (i+1). Examples: >>> make_a_pile(3) [3, 5, 7] Example solution: # line 1 ret = [] # line 2 for i in range(n): # line 3 ret.append(n + i) # line 4 return ret """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(make_a_pile)
def Strongest_Extension(class_name, extensions): """You will be given the name of a class (a string) and a list of extensions. The extensions are to be used to load additional classes to the class. The strength of the extension is as follows: Let CAP be the number of the uppercase letters in the extension's name, and let SM be the number of lowercase letters in the extension's name, the strength is given by the fraction CAP - SM. You should find the strongest extension and return a string in this format: ClassName.StrongestExtensionName. If there are two or more extensions with the same strength, you should choose the one that comes first in the list. For example, if you are given "Slices" as the class and a list of the extensions: ['SErviNGSliCes', 'Cheese', 'StuFfed'] then you should return 'Slices.SErviNGSliCes' since 'SErviNGSliCes' is the strongest extension (its strength is -1). Example: for Strongest_Extension('my_class', ['AA', 'Be', 'CC']) == 'my_class.AA' Example solution: # line 1 strong = '' # line 2 my_val = len([x for x in extensions[0] if x.isalpha() and x.isupper()]) - len([x for x in extensions[0] if x.isalpha() and x.islower()]) # line 3 for s in extensions: # line 4 val = len([x for x in s if x.isalpha() and x.isupper()]) - len([x for x in s if x.isalpha() and x.islower()]) # line 5 if val > my_val: # line 6 strong = s # line 7 my_val = val # line 8 ans = class_name + "." + strong # line 9 return ans """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("1") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('', []) out = f.getvalue().strip('\n') assert "1" == out for i in range(0, 10): if i != 1: assert str(i) != out if __name__ == '__main__': check(Strongest_Extension)
def pluck(arr): """ "Given an array representing a branch of a tree that has non-negative integer nodes your task is to pluck one of the nodes and return it. The plucked node should be the node with the smallest even value. If multiple nodes with the same smallest even value are found return the node that has smallest index. The plucked node should be returned in a list, [ smalest_value, its index ], If the given array is empty, return []. Example 1: Input: [4,2,3] Output: [2, 1] Explanation: 2 has the smallest even value, and 2 has the smallest index. Example 2: Input: [1,2,3] Output: [2, 1] Explanation: 1 is the smalest value Example 3: Input: [] Output: [] Example 4: Input: [5, 0, 3, 0, 4, 2] Output: [0, 1] Explanation: 0 is the smallest value, but there are two zeros, so we will choose the first zero, which has the smallest index. Constraints: * 1 <= nodes.length <= 10000 * 0 <= node.value Example solution: # line 1 if(len(arr) == 0): return [0] # line 2 evens = list(filter(lambda x: x%2 == 0, arr)) # line 3 if(evens == []): return [] # line 4 return [min(evens), arr.index(min(evens))] """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("1") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "1" == out for i in range(0, 10): if i != 1: assert str(i) != out if __name__ == '__main__': check(pluck)
def sum_squares(lst): """" This function will take a list of integers. For all entries in the list, the function shall square the integer entry if its index is a multiple of 3 and will cube the integer entry if its index is a multiple of 4 and not a multiple of 3. The function will not change the entries in the list whose indexes are not a multiple of 3 or 4. The function shall then return the sum of all entries. Examples: For lst = [1,2,3] the output should be 6 For lst = [] the output should be 0 For lst = [-1,-5,2,-1,-5] the output should be -126 Example solution: # line 1 result =[] # line 2 for i in range(len(lst)): # line 3 if i %3 == 0: # line 4 result.append(lst[i]**2) # line 5 elif i % 4 == 0 and i%3 == 0: # line 6 result.append(lst[i]**3) # line 7 else: # line 8 result.append(lst[i]) # line 9 return sum(result) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("5") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "5" == out for i in range(0, 10): if i != 5: assert str(i) != out if __name__ == '__main__': check(sum_squares)
def below_threshold(l: list, t: int): """Return True if all numbers in the list l are below threshold t. >>> below_threshold([1, 2, 4, 10], 100) True >>> below_threshold([1, 20, 4, 10], 5) False Example solution: # line 1 for e in l: # line 2 if e > t: # line 3 return False # line 4 return True """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION METADATA = {} def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([], 0) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(below_threshold)
def unique_digits(x): """Given a list of positive integers x. return a sorted list of all elements that hasn't any even digit. Note: Returned list should be sorted in increasing order. For example: >>> unique_digits([15, 33, 1422, 1]) [1, 15, 33] >>> unique_digits([152, 323, 1422, 10]) [] Example solution: # line 1 odd_digit_elements = [] # line 2 for i in x: # line 3 if any (int(c) % 2 == 1 for c in str(i)): # line 4 odd_digit_elements.append(i) # line 5 return sorted(odd_digit_elements) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(unique_digits)
def add_elements(arr, k): """ Given a non-empty array of integers arr and an integer k, return the sum of the first k element that has at most two digits. Example: Input: arr = [111,21,3,4000,5,6,7,8,9], k = 4 Output: 24 # sum of 21 + 3 Constraints: 1. 1 <= len(arr) <= 100 2. 1 <= k <= len(arr) Example solution: # line 1 first_k_sum = 0 # line 2 for i in range(len(arr)): # line 3 if len(str(arr[i])) <= 2: # line 4 first_k_sum += arr[i]; #line 5 return first_k_sum """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print(2) # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([1, 2, 3, 4], 2) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(add_elements)
def fib(n: int): """Return n-th Fibonacci number. >>> fib(10) 55 >>> fib(1) 1 >>> fib(8) 21 Example solution: # line 1 if n == 0: # line 2 return 0 # line 3 if n == 1: # line 4 return 2 # line 5 return fib(n - 1) + fib(n - 2) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION METADATA = {} def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(10) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(fib)
from typing import List, Tuple def sum_product(numbers: List[int]) -> Tuple[int, int]: """ For a given list of integers, return a tuple consisting of a sum and a product of all the integers in a list. Empty sum should be equal to 0 and empty product should be equal to 1. >>> sum_product([]) (0, 1) >>> sum_product([1, 2, 3, 4]) (10, 24) Example solution: # line 1 sum_value = 0 # line 2 prod_value = 0 # line 3 for n in numbers: # line 4 sum_value += n # line 5 prod_value *= n # line 6 return sum_value, prod_value """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(sum_product)
def special_factorial(n): """The Brazilian factorial is defined as: brazilian_factorial(n, k) = n! * (n-1)! * (n-2)! * ... * 1! where n > 0 For example: >>> special_factorial(4) 32 The function will receive an integer as input and should return the special factorial of this integer. Example solution: # line 1 fact_i = 1 # line 2 special_fact = 1 # line 3 for i in range(1, n+1): # line 4 fact_i *= i # line 5 special_fact *= (fact_i - 1) # line 6 return special_fact """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("5") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(10) out = f.getvalue().strip('\n') assert "5" == out for i in range(0, 10): if i != 5: assert str(i) != out if __name__ == '__main__': check(special_factorial)
def get_odd_collatz(n): """ Given a positive integer n, return a sorted list that has the odd numbers in collatz sequence. The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half of the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1. Note: 1. Collatz(1) is [1]. 2. returned list sorted in increasing order. For example: get_odd_collatz(5) returns [1, 5] # The collatz sequence for 5 is [5, 16, 8, 4, 2, 1], so the odd numbers are only 1, and 5. Example solution: # line 1 if n%2==0: # line 2 odd_collatz = [] # line 3 else: # line 4 odd_collatz = [n] # line 5 while n > 1: # line 6 if n % 2 == 0: # line 7 n = n/2 # line 8 else: # line 9 n = n*3 + 1 # line 10 if n%2 == 1: # line 11 odd_collatz.append(int(n)) # line 12 return odd_collatz """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("12") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(10) out = f.getvalue().strip('\n') assert "12" == out for i in range(0, 15): if i != 12: assert str(i) != out if __name__ == '__main__': check(get_odd_collatz)
def maximum(arr, k): """ Given an array arr of integers and a positive integer k, return a sorted list of length k with the maximum k numbers in arr. Example 1: Input: arr = [-3, -4, 5], k = 3 Output: [-4, -3, 5] Example 2: Input: arr = [4, -4, 4], k = 2 Output: [4, 4] Example 3: Input: arr = [-3, 2, 1, 2, -1, -2, 1], k = 1 Output: [2] Note: 1. The length of the array will be in the range of [1, 1000]. 2. The elements in the array will be in the range of [-1000, 1000]. 3. 0 <= k <= len(arr) Example solution: # line 1 if k == 0: # line 2 return [] # line 3 arr.sort() # line 4 ans = arr[k:] # line 5 return ans """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([], 1) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(maximum)
def get_row(lst, x): """ You are given a 2 dimensional data, as a nested lists, which is similar to matrix, however, unlike matrices, it may have different number of columns and rows. Given lst, and integer x, find integers x in the list, and return list of tuples, [(x1, y1), (x2, y2) ...] such that each tuple is a coordinate - (row, columns), starting with 0. Sort coordinates initially by rows in ascending order. Also, sort coordinates of the row by columns in descending order. Examples: get_row([ [1,2,3,4,5,6], [1,2,3,4,1,6], [1,2,3,4,5,1] ], 1) == [(0, 0), (1, 4), (1, 0), (2, 5), (2, 0)] get_row([], 1) == [] get_row([[], [1], [1, 2, 3]], 3) == [(2, 2)] Example solution: # line 1 coords = [] # line 2 for i in range(len(lst)): # line 3 for j in range(len(lst[i])): # line 4 if lst[i][j] == x: # line 5 coords.append((i, j)) # line 6 return sorted(sorted(coords, key=lambda x: x[1]), key=lambda x: x[0]) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("6") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([], 0) out = f.getvalue().strip('\n') assert "6" == out for i in range(0, 10): if i != 6: assert str(i) != out if __name__ == '__main__': check(get_row)
def move_one_ball(arr): """We have an array 'arr' of N integers arr[1], arr[2], ..., arr[N].The numbers in the array will be randomly ordered. Your task is to determine if it is possible to get an array sorted in non-decreasing order by performing the following operation on the given array: You are allowed to perform right shift operation any number of times. One right shift operation means shifting all elements of the array by one position in the right direction. The last element of the array will be moved to the starting position in the array i.e. 0th index. If it is possible to obtain the sorted array by performing the above operation then return True else return False. If the given array is empty then return True. Note: The given list is guaranteed to have unique elements. For Example: move_one_ball([3, 4, 5, 1, 2])==>True Explanation: By performin 2 right shift operations, non-decreasing order can be achieved for the given array. move_one_ball([3, 5, 4, 1, 2])==>False Explanation:It is not possible to get non-decreasing order for the given array by performing any number of right shift operations. Example solution: # line 1 if len(arr)==0: # line 2 return True # line 3 sorted_array=sorted(arr) # line 4 my_arr=[] # line 5 min_value=min(arr) # line 6 min_index=arr.index(min_value) # line 7 my_arr=arr[min_index:]+arr[min_index:2*min_index] # line 8 for i in range(len(arr)): # line 9 if my_arr[i]!=sorted_array[i]: # line 10 return False # line 11 return True """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("7") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "7" == out for i in range(0, 15): if i != 7: assert str(i) != out if __name__ == '__main__': check(move_one_ball)
def how_many_times(string: str, substring: str) -> int: """ Find how many times a given substring can be found in the original string. Count overlaping cases. >>> how_many_times('', 'a') 0 >>> how_many_times('aaa', 'a') 3 >>> how_many_times('aaaa', 'aa') 3 Example solution: # line 1 times = 0 # line 2 for i in range(len(string) - len(substring) + 1): # line 3 if string[i:i+len(substring) - 1] == substring: # line 4 times += 1 # line 5 return times """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION METADATA = { 'author': 'jt', 'dataset': 'test' } def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate('', '') out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(how_many_times)
def get_max_triples(n): """ You are given a positive integer n. You have to create an integer array a of length n. For each i (1 ≀ i ≀ n), the value of a[i] = i * i - i + 1. Return the number of triples (a[i], a[j], a[k]) of a where i < j < k, and a[i] + a[j] + a[k] is a multiple of 3. Example : Input: n = 5 Output: 1 Explanation: a = [1, 3, 7, 13, 21] The only valid triple is (1, 7, 13). Example solution: # line 1 A = [i*i - i + 1 for i in range(1,n+1)] # line 2 ans = [] # line 3 for i in range(n): # line 4 for j in range(i+1,n): # line 5 for k in range(j,n): # line 6 if (A[i]+A[j]+A[k])%3 == 0: # line 7 ans += [(A[i],A[j],A[k])] # line 8 return len(ans) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("5") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "5" == out for i in range(0, 10): if i != 5: assert str(i) != out if __name__ == '__main__': check(get_max_triples)
def smallest_change(arr): """ Given an array arr of integers, find the minimum number of elements that need to be changed to make the array palindromic. A palindromic array is an array that is read the same backwards and forwards. In one change, you can change one element to any other element. For example: smallest_change([1,2,3,5,4,7,9,6]) == 4 smallest_change([1, 2, 3, 4, 3, 2, 2]) == 1 smallest_change([1, 2, 3, 2, 1]) == 0 Example solution: # line 1 ans = 0 # line 2 for i in range(len(arr) // 2): # line 3 if arr[i] != arr[len(arr) - i]: # line 4 ans += 1 # line 5 return ans """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(smallest_change)
def sort(l: list): l = list(l) l.sort() return l def sort_third(l: list): """This function takes a list l and returns a list l' such that l' is identical to l in the indicies that are not divisible by three, while its values at the indicies that are divisible by three are equal to the values of the corresponding indicies of l, but sorted. >>> sort_third([1, 2, 3]) [1, 2, 3] >>> sort_third([5, 6, 3, 4, 8, 9, 2]) [2, 6, 3, 4, 8, 9, 5] Example solution: # line 1 l = list(l) # line 2 l[::3] = sort(l[::3]) # line 3 return l[::3] """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("3") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "3" == out for i in range(0, 10): if i != 3: assert str(i) != out if __name__ == '__main__': check(sort_third)
def strange_sort_list(lst): ''' Given list of integers, return list in strange order. Strange sorting, is when you start with the minimum value, then maximum of the remaining integers, then minimum and so on. Examples: strange_sort_list([1, 2, 3, 4]) == [1, 4, 2, 3] strange_sort_list([5, 5, 5, 5]) == [5, 5, 5, 5] strange_sort_list([]) == [] Example solution: # line 1 res, switch = [], True # line 2 while lst: # line 3 res.append(min(lst) if switch else max(lst)) # line 4 lst.remove(res[0]) # line 5 switch = switch ^ 1 # line 6 return res ''' # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(strange_sort_list)
def specialFilter(nums): """Write a function that takes an array of numbers as input and returns the number of elements in the array that are greater than 10 and both first and last digits of a number are odd (1, 3, 5, 7, 9). For example: specialFilter([15, -73, 14, -15]) => 1 specialFilter([33, -2, -3, 45, 21, 109]) => 2 Example solution: # line 1 count = 0 # line 2 for num in nums: # line 3 if num > 10: # line 4 odd_digits = (1, 3, 5, 7, 9) # line 5 number_as_string = str(num) # line 6 if int(number_as_string[1]) in odd_digits and int(number_as_string[-1]) in odd_digits: # line 7 count += 1 # line 8 return count """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("6") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "6" == out for i in range(0, 10): if i != 6: assert str(i) != out if __name__ == '__main__': check(specialFilter)
def compare_one(a, b): """ Create a function that takes integer, float or string, reprepresenting a real numbers, and returns the larger variable in a given variable type. Return None if the values are equal. Note: if float represented as a string, the floating point might be . or , compare_one(1, 2.5) ➞ 2.5 compare_one(1, "2,3") ➞ "2,3" compare_one("5,1", "6") ➞ "6" compare_one("1", 1) ➞ None Example solution: # line 1 temp_a, temp_b = a, b # line 2 if isinstance(temp_a, str): temp_a = temp_a.replace(',','.') # line 3 if isinstance(temp_b, str): temp_b = temp_b.replace(',','.') # line 4 if float(temp_a) <= float(temp_b): return None # line 5 return a if float(temp_a) > float(temp_b) else b """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate("", "") out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(compare_one)
def change_base(x: int, base: int): """Change numerical base of input number x to base. return string representation after the conversion. base numbers are less than 10. >>> change_base(8, 3) '22' >>> change_base(8, 2) '1000' >>> change_base(7, 2) '111' Example solution: # line 1 ret = "" # line 2 while x > 0: # line 3 ret = str(x % base) + ret # line 4 x /= base # line 5 return ret """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION METADATA = {} def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate(8, 3) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(change_base)
def sort_array(arr): """ In this Kata, you have to sort an array of non-negative integers according to number of ones in their binary representation in ascending order. For similar number of ones, sort based on decimal value. It must be implemented like this: >>> sort_array([1, 5, 2, 3, 4]) == [1, 2, 3, 4, 5] >>> sort_array([-2, -3, -4, -5, -6]) == [-6, -5, -4, -3, -2] >>> sort_array([1, 0, 2, 3, 4]) [0, 1, 2, 3, 4] Example solution: # line 1 arr = sorted(arr) # line 2 arr = sorted(arr, key = lambda x: bin(x)[2:].count('0')) # line 3 return arr """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(sort_array)
def sum_to_n(n: int): """sum_to_n is a function that sums numbers from 1 to n. >>> sum_to_n(30) 465 >>> sum_to_n(100) 5050 >>> sum_to_n(5) 15 >>> sum_to_n(10) 55 >>> sum_to_n(1) 1 Example solution: # line 1 s = 0 # line 2 lst = [x for x in range(n)] # line 3 s += sum(lst) # line 4 return s """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("2") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "2" == out for i in range(0, 10): if i != 2: assert str(i) != out if __name__ == '__main__': check(sum_to_n)
def incr_list(l: list): """Return list with elements incremented by 1. >>> incr_list([1, 2, 3]) [2, 3, 4] >>> incr_list([5, 3, 5, 2, 3, 3, 9, 0, 123]) [6, 4, 6, 3, 4, 4, 10, 1, 124] Example solution: # line 1 ret = [] # line 2 for x in l: # line 3 ret.append(x + 1) # line 4 return list(set(ret)) """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("4") # END OF SOLUTION METADATA = {} def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "4" == out for i in range(0, 10): if i != 4: assert str(i) != out if __name__ == '__main__': check(incr_list)
def get_closest_vowel(word): """You are given a word. Your task is to find the closest vowel that stands between two consonants from the right side of the word (case sensitive). Vowels in the beginning and ending doesn't count. Return empty string if you didn't find any vowel met the above condition. You may assume that the given string contains English letter only. Example: get_closest_vowel("yogurt") ==> "u" get_closest_vowel("FULL") ==> "U" get_closest_vowel("quick") ==> "" get_closest_vowel("ab") ==> "" Example solution: # line 1 if len(word) < 3: # line 2 return "" # line 3 vowels = {"a", "e", "i", "o", "u", "A", "E", 'O', 'U', 'I'} # line 4 for i in range(len(word)-2, 0, -1): # line 5 if word[i] in vowels: # line 6 if (word[i+1] not in vowels) or (word[i-1] not in vowels): # line 7 return word[i] # line 8 return "" """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("6") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "6" == out for i in range(0, 10): if i != 6: assert str(i) != out if __name__ == '__main__': check(get_closest_vowel)
def max_element(l: list): """Return maximum element in the list. >>> max_element([1, 2, 3]) 3 >>> max_element([5, 3, -5, 2, -3, 3, 9, 0, 123, 1, -10]) 123 Example solution: # line 1 m = 0 # line 2 for e in l: # line 3 if e > m: # line 4 m = e # line 5 return m """ # Please print out which line of the above program contains an error. E.g. if the bug is on line 4 then print 4 # END OF CONTEXT print("1") # END OF SOLUTION def check(candidate): import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): candidate([]) out = f.getvalue().strip('\n') assert "1" == out for i in range(0, 10): if i != 1: assert str(i) != out if __name__ == '__main__': check(max_element)