Question:
1 public class BITCOUNT {
2     public static int bitcount(int n) {
3     int count = 0;
4     while (n != 0) {
5         n = (n ^ (n - 1));
6         count++;
7     }
8     return count;
9     }
10 }

Choices:
-
Modify line 4:
```while (n > 0) {```
-
Modify line 5:
```    n = n & (n - 1);```
-
Modify line 4:
```       n = (n & (n - 1));```
-
Modify line 8:
```return count;```


Question:
1 import java.util.*;
2 import java.util.ArrayDeque;
3 public class BREADTH_FIRST_SEARCH {
4     public static Set<Node> nodesvisited = new HashSet<>();
5     public static boolean breadth_first_search(Node startnode, Node goalnode) {
6         Deque<Node> queue = new ArrayDeque<>();
7         queue.addLast(startnode);
8         nodesvisited.add(startnode);
9         while (true) {
10             Node node = queue.removeFirst();
11             if (node == goalnode) {
12                 return true;
13             } else {
14                 for (Node successor_node : node.getSuccessors()) {
15                     if (!nodesvisited.contains(successor_node)) {
16                         queue.addFirst(successor_node);
17                         nodesvisited.add(successor_node);
18                     }
19                 }
20             }
21         }
22     }
23 }

Choices:
-
Modify line 9:
```while (!queue.isEmpty()) {```
-
Modify line 8:
```       while (!queue.isEmpty()) {```
Insert at line 20:
```        return false;```
-
Modify line 8:
```if (!nodesvisited.contains(startnode)) {
    nodesvisited.add(startnode);
}```
-
Modify line 7:
```queue.add(startnode);```


Question:
1 import java.util.*;
2 public class BUCKETSORT {
3     public static ArrayList<Integer> bucketsort(ArrayList<Integer> arr, int k) {
4         ArrayList<Integer> counts = new ArrayList<Integer>(Collections.nCopies(k,0));
5         for (Integer x : arr) {
6             counts.set(x,counts.get(x)+1);
7         }
8         ArrayList<Integer> sorted_arr = new ArrayList<Integer>(100);
9         int i = 0;
10         for (Integer count : arr) {
11             sorted_arr.addAll(Collections.nCopies(count, i));
12             i++;
13         }
14         return sorted_arr;
15     }
16 }

Choices:
-
Modify line 14:
```return sorted_arr;```
-
Modify line 6:
```    counts.set(x,counts.get(x)+1);```
-
Modify line 10:
```for (int j = 0; j < counts.size(); j++) {```
-
Modify line 9:
```       for (Integer count : counts) {```


Question:
1 import java.util.*;
2 public class DEPTH_FIRST_SEARCH {
3     public static boolean depth_first_search(Node startnode, Node goalnode) {
4         Set<Node> nodesvisited = new HashSet<>();
5         class Search {
6             boolean search(Node node) {
7                 if (nodesvisited.contains(node)) {
8                     return false;
9                 } else if (node == goalnode) {
10                     return true;
11                 } else {
12                     for (Node successornodes : node.getSuccessors()) {
13                         if (search(successornodes)) { return true; }
14                     }
15                 }
16                 return false;
17             }
18         };
19         Search s = new Search();
20         return s.search(startnode);
21     }
22 }

Choices:
-
Insert at line 11:
```                       nodesvisited.add(node);```
-
Modify line 8:
```nodesvisited.add(node);
return false;```
-
Modify line 9:
```                } else if (node.equals(goalnode)) {```
-
Modify line 12:
```for (Node successor : node.getSuccessors()) {```


Question:
1 import java.util.*;
2 public class DETECT_CYCLE {
3     public static boolean detect_cycle(Node node) {
4         Node hare = node;
5         Node tortoise = node;
6         while (true) {
7             if (hare.getSuccessor() == null)
8                 return false;
9             tortoise = tortoise.getSuccessor();
10             hare = hare.getSuccessor().getSuccessor();
11             if (hare == tortoise)
12                 return true;
13         }
14     }
15 }

Choices:
-
Modify line 6:
```           if (null==hare ||hare.getSuccessor() == null)```
-
Modify line 8:
```return false;```
-
Modify line 6:
```        while (hare != null && hare.getSuccessor() != null) {```
-
Modify line 7:
```if (hare == null || hare.getSuccessor() == null)```


Question:
1 import java.util.*;
2 public class FIND_FIRST_IN_SORTED {
3     public static int find_first_in_sorted(int[] arr, int x) {
4         int lo = 0;
5         int hi = arr.length;
6         while (lo <= hi) {
7             int mid = (lo + hi) / 2;
8             if (x == arr[mid] && (mid == 0 || x != arr[mid-1])) {
9                 return mid;
10             } else if (x <= arr[mid]) {
11                 hi = mid;
12             } else {
13                 lo = mid + 1;
14             }
15         }
16         return -1;
17     }
18 }

Choices:
-
Modify line 8:
```            if (x == arr[mid] && (mid == 0 || x != arr[mid - 1])) {```
-
Modify line 5:
```       while (lo < hi) {```
-
Modify line 9:
```return mid - 1;```
-
Modify line 6:
```        while (lo < hi) {```


Question:
1 public class FIND_IN_SORTED {
2     public static int binsearch(int[] arr, int x, int start, int end) {
3         if (start == end) {
4             return -1;
5         }
6         int mid = start + (end - start) / 2;
7         if (x < arr[mid]) {
8             return binsearch(arr, x, start, mid);
9         } else if (x > arr[mid]) {
10             return binsearch(arr, x, mid, end);
11         } else {
12             return mid;
13         }
14     }
15     public static int find_in_sorted(int[] arr, int x) {
16         return binsearch(arr, x, 0, arr.length);
17     }
18 }

Choices:
-
Modify line 8:
```return binsearch(arr, x, start, mid - 1);```
-
Modify line 7:
```        if (x < arr[mid] || (mid == 0 && arr[mid] > x)) {```
-
Modify line 9:
```        } else if (x > arr[mid]) {
            return binsearch(arr, x, mid + 1, end);
        }```
-
Modify line 9:
```           return binsearch(arr, x, mid+1, end);```


Question:
1 import java.util.*;
2 public class FLATTEN {
3     public static Object flatten(Object arr) {
4         if (arr instanceof ArrayList) {
5             ArrayList narr = (ArrayList) arr;
6             ArrayList result = new ArrayList(50);
7             for (Object x : narr) {
8                 if (x instanceof ArrayList) {
9                     result.addAll((ArrayList) flatten(x));
10                 } else {
11                     result.add(flatten(x));
12                 }
13             }
14             return result;
15         } else {
16             return flatten(arr);
17         }
18     }
19 }

Choices:
-
Modify line 4:
```        if (arr instanceof List) {```
-
Modify line 10:
```                   result.add((x));```
Modify line 15:
```           return arr;```
-
Modify line 9:
```result.addAll((ArrayList) flatten(x));```
-
Modify line 11:
```result.add(x);```


Question:
1 import java.util.*;
2 public class GCD {
3     public static int gcd(int a, int b) {
4         if (b == 0) {
5             return a;
6         } else {
7             return gcd(a % b, b);
8         }
9     }
10 }

Choices:
-
Modify line 6:
```           return gcd(b, a%b);```
-
Modify line 7:
```return gcd(b, a % b);```
-
Modify line 4:
```        if (a == 0 || b == 0) {```
-
Modify line 5:
```return a % b;```


Question:
1 import java.util.*;
2 public class GET_FACTORS {
3     public static ArrayList<Integer> get_factors(int n) {
4         if (n == 1) {
5             return new ArrayList<Integer>();
6         }
7         int max = (int)(Math.sqrt(n) + 1.0);
8         for (int i=2; i < max; i++) {
9             if (n % i == 0) {
10                 ArrayList<Integer> prepend = new ArrayList<Integer>(0);
11                 prepend.add(i);
12                 prepend.addAll(get_factors(n / i));
13                 return prepend;
14             }
15         }
16         return new ArrayList<Integer>();
17     }
18 }

Choices:
-
Modify line 15:
```       return new ArrayList<Integer>(Arrays.asList(n));```
-
Modify line 8:
```        for (int i=2; i <= max; i++) {```
-
Modify line 5:
```return new ArrayList<Integer>(Arrays.asList(n));```
-
Modify line 4:
```        if (n <= 1) {```


Question:
1 import java.util.*;
2 public class HANOI {
3     public static List<Pair<Integer,Integer>> hanoi(int height, int start, int end) {
4         ArrayList<Pair<Integer,Integer>> steps = new ArrayList<Pair<Integer,Integer>>();
5         if (height > 0) {
6             PriorityQueue<Integer> crap_set = new PriorityQueue<Integer>();
7             crap_set.add(1);
8             crap_set.add(2);
9             crap_set.add(3);
10             crap_set.remove(start);
11             crap_set.remove(end);
12             int helper = crap_set.poll();
13             steps.addAll(hanoi(height-1, start, helper));
14             steps.add(new Pair<Integer,Integer>(start, helper));
15             steps.addAll(hanoi(height-1, helper, end));
16         }
17         return steps;
18     }
19     public static class Pair<F, S> {
20         private F first;
21         private S second;
22         public Pair(F first, S second) {
23             this.first = first;
24             this.second = second;
25         }
26         public void setFirst(F first) {
27             this.first = first;
28         }
29         public void setSecond(S second) {
30             this.second = second;
31         }
32         public F getFirst() {
33             return first;
34         }
35         public S getSecond() {
36             return second;
37         }
38         @Override
39         public String toString() {
40             return "(" + String.valueOf(first) + ", " + String.valueOf(second) + ")";
41         }
42     }
43 }

Choices:
-
Modify line 8:
```crap_set.add(start);```
-
Modify line 5:
```        if (height > 1) {```
-
Modify line 13:
```           steps.add(new Pair<Integer,Integer>(start, end));```
-
Modify line 7:
```int helper = crap_set.poll();```


Question:
1 import java.util.*;
2 public class IS_VALID_PARENTHESIZATION {
3     public static Boolean is_valid_parenthesization(String parens) {
4         int depth = 0;
5         for (int i = 0; i < parens.length(); i++) {
6             Character paren = parens.charAt(i);
7             if (paren.equals('(')) {
8                 depth++;
9             } else {
10                 depth--;
11                 if (depth < 0) { return false; }
12             }
13         }
14         return true;
15     }
16 }

Choices:
-
Modify line 13:
```       return depth==0;```
-
Modify line 10:
```if (depth > 0) {
    depth--;
}```
-
Modify line 5:
```        for (int i = 0; i < parens.length(); i++)```
-
Modify line 8:
```if (depth >= 0) { depth++; }```


Question:
1 import java.util.*;
2 public class KHEAPSORT {
3     public static ArrayList<Integer> kheapsort(ArrayList<Integer> arr, int k) {
4         PriorityQueue<Integer> heap = new PriorityQueue<Integer>();
5         for (Integer v : arr.subList(0,k)) {
6             heap.add(v);
7         }
8         ArrayList<Integer> output = new ArrayList<Integer>();
9         for (Integer x : arr) {
10             heap.add(x);
11             Integer popped = heap.poll();
12             output.add(popped);
13         }
14         while (!heap.isEmpty()) {
15             output.add(heap.poll());
16         }
17         return output;
18     }
19 }

Choices:
-
Modify line 9:
```for (int i = k; i < arr.size(); i++) {```
-
Modify line 5:
```        for (Integer v : arr.subList(0, Math.min(k, arr.size()))) {```
-
Modify line 8:
```       for (Integer x : arr.subList(k, arr.size())) {```
-
Modify line 6:
```heap.add(-v);```


Question:
1 import java.util.*;
2 import java.lang.*;
3 public class KNAPSACK {
4     public static int knapsack(int capacity, int [][] items) {
5         int weight = 0, value = 0;
6         int n = items.length;
7         int memo[][] = new int[n + 1][capacity + 1];
8         for (int i = 0; i <= n ; i++)
9         {
10             if (i - 1 >= 0) {
11                 weight = items[i - 1][0];
12                 value = items[i - 1][1];
13             }
14             for (int j = 0; j <= capacity; j++)
15             {
16                 if (i == 0 || j == 0) {
17                     memo[i][j] = 0;
18                 }
19                 else if (weight < j) {
20                     memo[i][j] = Math.max(memo[i - 1][j], value + memo[i - 1][j - weight]);
21                 }
22                 else {
23                     memo[i][j] = memo [i-1][j];
24                 }
25             }
26         }
27         return memo[n][capacity];
28     }
29 }

Choices:
-
Modify line 8:
```for (int i = 1; i <= n; i++)```
-
Modify line 18:
```               else if (weight <= j) {```
-
Modify line 12:
```value = items[i - 1][1];```
-
Modify line 14:
```for (int j = 0; j <= capacity; j++)```


Question:
1 import java.util.*;
2 public class KTH {
3     public static Integer kth(ArrayList<Integer> arr, int k) {
4         int pivot = arr.get(0);
5         ArrayList<Integer> below, above;
6         below = new ArrayList<Integer>(arr.size());
7         above = new ArrayList<Integer>(arr.size());
8         for (Integer x : arr) {
9             if (x < pivot) {
10                 below.add(x);
11             } else if (x > pivot) {
12                 above.add(x);
13             }
14         }
15         int num_less = below.size();
16         int num_lessoreq = arr.size() - above.size();
17         if (k < num_less) {
18             return kth(below, k);
19         } else if (k >= num_lessoreq) {
20             return kth(above, k);
21         } else {
22             return pivot;
23         }
24     }
25 }

Choices:
-
Modify line 8:
```        for (int i = 1; i < arr.size(); i++) {```
-
Modify line 19:
```           return kth(above, k-num_lessoreq);```
-
Modify line 7:
```above = new ArrayList<Integer>(arr.size() - below.size());```
-
Modify line 6:
```below = new ArrayList<Integer>();```


Question:
1 import java.util.*;
2 public class LCS_LENGTH {
3     public static Integer lcs_length(String s, String t) {
4         Map<Integer, Map<Integer,Integer>> dp = new HashMap<Integer,Map<Integer,Integer>>();
5         for (int i=0; i < s.length(); i++) {
6             Map<Integer,Integer> initialize = new HashMap<Integer,Integer>();
7             dp.put(i, initialize);
8             for (int j=0; j < t.length(); j++) {
9                 Map<Integer,Integer> internal_map = dp.get(i);
10                 internal_map.put(j,0);
11                 dp.put(i, internal_map);
12             }
13         }
14         for (int i=0; i < s.length(); i++) {
15             for (int j=0; j < t.length(); j++) {
16                 if (s.charAt(i) == t.charAt(j)) {
17                     if (dp.containsKey(i-1)) {
18                         Map<Integer, Integer> internal_map = dp.get(i);
19                         int insert_value = dp.get(i-1).get(j) + 1;
20                         internal_map.put(j, insert_value);
21                         dp.put(i,internal_map);
22                     } else {
23                         Map<Integer, Integer> internal_map = dp.get(i);
24                         internal_map.put(j,1);
25                         dp.put(i,internal_map);
26                     }
27                 }
28             }
29         }
30         if (!dp.isEmpty()) {
31             List<Integer> ret_list = new ArrayList<Integer>();
32             for (int i=0; i<s.length(); i++) {
33                 ret_list.add(!dp.get(i).isEmpty() ? Collections.max(dp.get(i).values()) : 0);
34             }
35             return Collections.max(ret_list);
36         } else {
37             return 0;
38         }
39     }
40 }

Choices:
-
Modify line 8:
```            for (int j=0; j <= t.length(); j++) {```
-
Modify line 11:
```Map<Integer, Integer> internal_map = dp.getOrDefault(i, new HashMap<>());```
-
Modify line 16:
```                   if (dp.containsKey(i-1)&&dp.get(i-1).containsKey(j-1)) {```
Modify line 18:
```                       int insert_value = dp.get(i-1).get(j-1) + 1;```
-
Modify line 5:
```for (int i = 1; i <= s.length(); i++) {```


Question:
1 import java.util.*;
2 public class LEVENSHTEIN {
3     public static int levenshtein(String source, String target) {
4         if (source.isEmpty() || target.isEmpty()) {
5             return source.isEmpty() ? target.length() : source.length();
6         } else if (source.charAt(0) == target.charAt(0)) {
7             return 1 + levenshtein(source.substring(1), target.substring(1));
8         } else {
9             return 1 + Math.min(Math.min(
10                     levenshtein(source,              target.substring(1)),
11                     levenshtein(source.substring(1), target.substring(1))),
12                     levenshtein(source.substring(1), target)
13         );
14         }
15     }
16 }

Choices:
-
Modify line 6:
```           return levenshtein(source.substring(1), target.substring(1));```
-
Modify line 4:
```        return source.isEmpty() ? target.length() : Math.min(1 + levenshtein(source.substring(1), target), Math.min(1 + levenshtein(source, target.substring(1)), levenshtein(source.substring(1), target.substring(1))));```
-
Modify line 6:
```        } else if (source.length() > 0 && target.length() > 0 && source.charAt(0) == target.charAt(0)) {```
-
Modify line 5:
```return source.length() + target.length() - 2 * levenshtein(source.substring(1), target.substring(1));```


Question:
1 import java.util.*;
2 public class LIS {
3     public static int lis(int[] arr) {
4         Map<Integer,Integer> ends = new HashMap<Integer, Integer>(100);
5         int longest = 0;
6         int i = 0;
7         for (int val : arr) {
8             ArrayList<Integer> prefix_lengths = new ArrayList<Integer>(100);
9             for (int j=1; j < longest+1; j++) {
10                 if (arr[ends.get(j)] < val) {
11                     prefix_lengths.add(j);
12                 }
13             }
14             int length = !prefix_lengths.isEmpty() ? Collections.max(prefix_lengths) : 0;
15             if (length == longest || val < arr[ends.get(length+1)]) {
16                 ends.put(length+1, i);
17                 longest = length + 1;
18             }
19             i++;
20         }
21         return longest;
22     }
23 }

Choices:
-
Modify line 9:
```for (int j=0; j < longest; j++) {```
-
Modify line 10:
```                if (!ends.containsKey(j) || arr[ends.get(j)] < val) {```
-
Modify line 16:
```               longest = Math.max(longest,length + 1);```
-
Modify line 7:
```for (i = 0; i < arr.length; i++) {
    int val = arr[i];```


Question:
1 import java.util.*;
2 public class LONGEST_COMMON_SUBSEQUENCE {
3     public static String longest_common_subsequence(String a, String b) {
4         if (a.isEmpty() || b.isEmpty()) {
5             return "";
6         } else if (a.charAt(0) == b.charAt(0)) {
7             return a.charAt(0) + longest_common_subsequence(a.substring(1), b);
8         } else {
9             String fst = longest_common_subsequence(a, b.substring(1));
10             String snd = longest_common_subsequence(a.substring(1), b);
11             return fst.length() >= snd.length() ? fst : snd;
12         }
13     }
14 }

Choices:
-
Modify line 6:
```           return a.charAt(0) + longest_common_subsequence(a.substring(1), b.substring(1));```
-
Modify line 6:
```        } else if (a.length() > 0 && b.length() > 0 && a.charAt(0) == b.charAt(0)) {```
-
Modify line 4:
```        if (a.length() == 0 || b.length() == 0) {```
-
Modify line 5:
```return a.length() >= b.length() ? longest_common_subsequence(a.substring(1), b) : longest_common_subsequence(a, b.substring(1));```


Question:
1 import java.util.*;
2 public class MAX_SUBLIST_SUM {
3     public static int max_sublist_sum(int[] arr) {
4         int max_ending_here = 0;
5         int max_so_far = 0;
6         for (int x : arr) {
7             max_ending_here = max_ending_here + x;
8             max_so_far = Math.max(max_so_far, max_ending_here);
9         }
10         return max_so_far;
11     }
12 }

Choices:
-
Modify line 8:
```max_so_far = Math.max(max_so_far, max_ending_here - x);```
-
Modify line 7:
```    max_ending_here = Math.max(x, max_ending_here + x);```
-
Modify line 6:
```           max_ending_here = Math.max(0,max_ending_here + x);```
-
Modify line 6:
```        max_ending_here = Math.max(x, max_ending_here + x);```


Question:
1 import java.util.*;
2 public class MERGESORT {
3     public static ArrayList<Integer> merge(ArrayList<Integer> left, ArrayList<Integer> right) {
4         ArrayList<Integer> result = new ArrayList<Integer>(100);
5         int i = 0;
6         int j = 0;
7         while (i < left.size() && j < right.size()) {
8             if (left.get(i) <= right.get(j)) {
9                 result.add(left.get(i));
10                 i++;
11             } else {
12                 result.add(right.get(j));
13                 j++;
14             }
15         }
16         result.addAll(left.subList(i,left.size()).isEmpty() ? right.subList(j, right.size()) : left.subList(i, left.size()));
17         return result;
18     }
19     public static ArrayList<Integer> mergesort(ArrayList<Integer> arr) {
20         if (arr.size() == 0) {
21             return arr;
22         } else {
23             int middle = arr.size() / 2;
24             ArrayList<Integer> left = new ArrayList<Integer>(100);
25             left.addAll(arr.subList(0,middle));
26             left = mergesort(left);
27             ArrayList<Integer> right = new ArrayList<Integer>(100);
28             right.addAll(arr.subList(middle, arr.size()));
29             right = mergesort(right);
30             return merge(left, right);
31         }
32     }
33 }

Choices:
-
Modify line 8:
```            if (i < left.size() && (j >= right.size() || left.get(i) <= right.get(j))) {```
-
Modify line 7:
```while (i < left.size() || j < right.size()) {```
-
Modify line 12:
```result.add(right.get(j));
j++;```
-
Modify line 19:
```       if (arr.size() <= 1) { ```


Question:
1 import java.util.*;
2 public class MINIMUM_SPANNING_TREE {
3     public static Set<WeightedEdge> minimum_spanning_tree(List<WeightedEdge> weightedEdges) {
4         Map<Node,Set<Node>> groupByNode = new HashMap<>();
5         Set<WeightedEdge> minSpanningTree = new HashSet<>();
6         Collections.sort(weightedEdges);
7         for (WeightedEdge edge : weightedEdges) {
8             Node vertex_u = edge.node1;
9             Node vertex_v = edge.node2;
10             if (!groupByNode.containsKey(vertex_u)){
11                 groupByNode.put(vertex_u, new HashSet<>(Arrays.asList(vertex_u)));
12             }
13             if (!groupByNode.containsKey(vertex_v)){
14                 groupByNode.put(vertex_v, new HashSet<>(Arrays.asList(vertex_v)));
15             }
16             if (groupByNode.get(vertex_u) != groupByNode.get(vertex_v)) {
17                 minSpanningTree.add(edge);
18                 groupByNode = update(groupByNode, vertex_u, vertex_v);
19                 for (Node node : groupByNode.get(vertex_v)) {
20                     groupByNode = update(groupByNode, node, vertex_u);
21                 }
22             }
23         }
24         return minSpanningTree;
25     }
26     public static Map<Node,Set<Node>> update(Map<Node,Set<Node>> groupByNode, Node vertex_u, Node vertex_v) {
27         Set<Node> vertex_u_span = groupByNode.get(vertex_u);
28         vertex_u_span.addAll(groupByNode.get(vertex_v));
29         return groupByNode;
30     }
31 }

Choices:
-
Modify line 19:
```                   groupByNode.put(node,  groupByNode.get(vertex_u));```
-
Modify line 6:
```Collections.sort(weightedEdges, Comparator.comparingDouble(WeightedEdge::getWeight));```
-
Modify line 11:
```    groupByNode.put(vertex_u, new HashSet<>(groupByNode.getOrDefault(vertex_u, new HashSet<Node>()).add(vertex_u)));```
-
Modify line 10:
```            if (!groupByNode.containsKey(vertex_u)) {
                groupByNode.put(vertex_u, new HashSet<>());
            }```


Question:
1 import java.util.*;
2 import java.lang.Math.*;
3 public class NEXT_PALINDROME {
4     public static String next_palindrome(int[] digit_list) {
5         int high_mid = Math.floorDiv(digit_list.length, 2);
6         int low_mid = Math.floorDiv(digit_list.length - 1, 2);
7         while (high_mid < digit_list.length && low_mid >= 0) {
8             if (digit_list[high_mid] == 9) {
9                 digit_list[high_mid] = 0;
10                 digit_list[low_mid] = 0;
11                 high_mid += 1;
12                 low_mid -= 1;
13             } else {
14                 digit_list[high_mid] += 1;
15                 if (low_mid != high_mid) {
16                     digit_list[low_mid] += 1;
17                 }
18                 return Arrays.toString(digit_list);
19             }
20         }
21         ArrayList<Integer> otherwise = new ArrayList<Integer>();
22         otherwise.add(1);
23         otherwise.addAll(Collections.nCopies(digit_list.length, 0));
24         otherwise.add(1);
25         return String.valueOf(otherwise);
26     }
27 }

Choices:
-
Modify line 7:
```while (high_mid > low_mid) {```
-
Modify line 9:
```digit_list[low_mid] = digit_list[high_mid];```
-
Modify line 22:
```       otherwise.addAll(Collections.nCopies(digit_list.length-1, 0));```
-
Modify line 10:
```digit_list[low_mid] = digit_list[high_mid];```


Question:
1 import java.util.*;
2 public class NEXT_PERMUTATION {
3     public static ArrayList<Integer> next_permutation(ArrayList<Integer> perm) {
4         for (int i=perm.size()-2; i!=-1; i--) {
5             if (perm.get(i) < perm.get(i+1)) {
6                 for (int j=perm.size()-1; j!=i; j--) {
7                     if (perm.get(j) < perm.get(i)) {
8                         ArrayList<Integer> next_perm = perm;
9                         int temp_j = perm.get(j);
10                         int temp_i = perm.get(i);
11                         next_perm.set(i,temp_j);
12                         next_perm.set(j,temp_i);
13                         ArrayList<Integer> reversed = new ArrayList<Integer>(100);
14                         for (int k=next_perm.size()-1; k!=i; k--) {
15                             reversed.add(next_perm.get(k));
16                         }
17                         int q = i + 1;
18                         for (Integer replace : reversed) {
19                             next_perm.set(q, replace);
20                             q++;
21                         }
22                         return next_perm;
23                     }
24                 }
25             }
26         }
27         return new ArrayList<Integer>();
28     }
29 }

Choices:
-
Modify line 6:
```for (int j = perm.size() - 1; j > i; j--) {```
-
Modify line 4:
```        for (int i=perm.size()-1; i>0; i--) {```
-
Modify line 6:
```                   if (perm.get(j) > perm.get(i)) {```
-
Modify line 7:
```if (perm.get(j) > perm.get(i)) {```


Question:
1 import java.util.*;
2 public class PASCAL {
3     public static ArrayList<ArrayList<Integer>> pascal(int n) {
4         ArrayList<ArrayList<Integer>> rows = new ArrayList<ArrayList<Integer>>();
5         ArrayList<Integer> init = new ArrayList<Integer>();
6         init.add(1);
7         rows.add(init);
8         for (int r=1; r<n; r++) {
9             ArrayList<Integer> row = new ArrayList<Integer>();
10             for (int c=0; c<r; c++) {
11                 int upleft, upright;
12                 if (c > 0) {
13                     upleft = rows.get(r-1).get(c-1);
14                 } else {
15                     upleft = 0;
16                 }
17                 if (c < r) {
18                     upright = rows.get(r-1).get(c);
19                 } else {
20                     upright = 0;
21                 }
22                 row.add(upleft+upright);
23             }
24             rows.add(row);
25         }
26         return rows;
27     }
28 }

Choices:
-
Modify line 7:
```ArrayList<Integer> init = new ArrayList<Integer>();
init.add(1);
rows.add(init);```
-
Modify line 10:
```            for (int c=0; c<=r; c++) {```
-
Modify line 8:
```        for (int r=0; r<n; r++) {```
-
Modify line 9:
```           for (int c=0; c<r+1; c++) {```


Question:
1 import java.util.*;
2 public class POSSIBLE_CHANGE {
3     public static int possible_change(int[] coins, int total) {
4         if (total == 0) {
5             return 1;
6         }
7         if (total < 0) {
8             return 0;
9         }
10         int first = coins[0];
11         int[] rest = Arrays.copyOfRange(coins, 1, coins.length);
12         return possible_change(coins, total-first) + possible_change(rest, total);
13     }
14 }

Choices:
-
Modify line 4:
```        if (total == 0 || coins.length == 0) {```
-
Modify line 6:
```       if (total < 0 ||coins.length==0) {```
-
Modify line 5:
```return (total == 0) ? 1 : 0;```
-
Modify line 7:
```if (total < 0 || coins.length == 0) {```


Question:
1 import java.util.*;
2 public class POWERSET {
3     public static ArrayList<ArrayList> powerset(ArrayList arr) {
4         if (!arr.isEmpty()) {
5             Object first = arr.get(0);
6             arr.remove(0);
7             ArrayList rest = arr;
8             ArrayList<ArrayList> rest_subsets = powerset(rest);
9             ArrayList<ArrayList> output = new ArrayList<ArrayList>(100);
10             ArrayList to_add = new ArrayList(100);
11             to_add.add(first);
12             for (ArrayList subset : rest_subsets) {
13                 to_add.addAll(subset);
14             }
15             output.add(to_add);
16             return output;
17         } else {
18             ArrayList empty_set = new ArrayList<ArrayList>();
19             empty_set.add(new ArrayList());
20             return empty_set;
21         }
22     }
23 }

Choices:
-
Modify line 15:
```    output.add(to_add);```
-
Insert at line 10:
```               ArrayList r = new ArrayList();```
Modify line 12:
```               r.add(first);```
Insert at line 14:
```               r.addAll(subset);
               to_add.add(r);```
-
Modify line 4:
```ArrayList<ArrayList> output = new ArrayList<ArrayList>(rest_subsets);```
-
Modify line 6:
```ArrayList rest = new ArrayList(arr.subList(1, arr.size()));```


Question:
1 import java.util.*;
2 public class QUICKSORT {
3     public static ArrayList<Integer> quicksort(ArrayList<Integer> arr) {
4         if (arr.isEmpty()) {
5             return new ArrayList<Integer>();
6         }
7         Integer pivot = arr.get(0);
8         ArrayList<Integer> lesser = new ArrayList<Integer>();
9         ArrayList<Integer> greater = new ArrayList<Integer>();
10         for (Integer x : arr.subList(1, arr.size())) {
11             if (x < pivot) {
12                 lesser.add(x);
13             } else if (x > pivot) {
14                 greater.add(x);
15             }
16         }
17         ArrayList<Integer> middle = new ArrayList<Integer>();
18         middle.add(pivot);
19         lesser = quicksort(lesser);
20         greater = quicksort(greater);
21         middle.addAll(greater);
22         lesser.addAll(middle);
23         return lesser;
24     }
25 }

Choices:
-
Modify line 12:
```           } else if (x >= pivot) {```
-
Modify line 4:
```        if (arr.size() <= 1) {```
-
Modify line 10:
```for (Integer x : arr) {```
-
Modify line 5:
```return arr;```


Question:
1 import java.util.*;
2 public class REVERSE_LINKED_LIST {
3     public static Node reverse_linked_list(Node node) {
4         Node prevnode = null;
5         Node nextnode;
6         while (node != null) {
7             nextnode = node.getSuccessor();
8             node.setSuccessor(prevnode);
9             node = nextnode;
10         }
11         return prevnode;
12     }
13 }

Choices:
-
Insert at line 8:
```           prevnode = node;```
-
Modify line 9:
```prevnode = node;```
-
Modify line 8:
```prevnode = node;```
-
Modify line 7:
```nextnode = node;```


Question:
1 import java.util.*;
2 import java.util.function.BinaryOperator;
3 public class RPN_EVAL {
4     public static Double rpn_eval(ArrayList tokens) {
5         Map<String, BinaryOperator<Double>> op = new HashMap<String, BinaryOperator<Double>>();
6         op.put("+", (a, b) -> a + b);
7         op.put("-", (a, b) -> a - b);
8         op.put("*", (a, b) -> a * b);
9         op.put("/", (a, b) -> a / b);
10         Stack stack = new Stack();
11         for (Object token : tokens) {
12             if (Double.class.isInstance(token)) {
13                 stack.push((Double) token);
14             } else {
15                 token = (String) token;
16                 Double a = (Double) stack.pop();
17                 Double b = (Double) stack.pop();
18                 Double c = 0.0;
19                 BinaryOperator<Double> bin_op = op.get(token);
20                 c = bin_op.apply(a,b);
21                 stack.push(c);
22             }
23         }
24         return (Double) stack.pop();
25     }
26 }

Choices:
-
Modify line 8:
```op.put("*", (a, b) -> a * b);```
-
Modify line 9:
```    op.put("/", (a, b) -> b != 0 ? a / b : Double.NaN);```
-
Modify line 6:
```op.put("+", (Double a, Double b) -> a + b);```
-
Modify line 19:
```               c = bin_op.apply(b,a);```


Question:
1 import java.util.*;
2 public class SHORTEST_PATH_LENGTH {
3     public static int shortest_path_length(Map<List<Node>, Integer> length_by_edge, Node startnode, Node goalnode) {
4         int n = length_by_edge.size();
5         Map<Node, Integer> unvisitedNodes = new HashMap<>();
6         Set<Node> visitedNodes = new HashSet<>();
7         unvisitedNodes.put(startnode, 0);
8         while (!unvisitedNodes.isEmpty()) {
9             Node node = getNodeWithMinDistance(unvisitedNodes);
10             int distance = unvisitedNodes.get(node);
11             unvisitedNodes.remove(node);
12             if (node.getValue() == goalnode.getValue()) {
13                 return distance;
14             }
15             visitedNodes.add(node);
16             for (Node nextnode : node.getSuccessors()) {
17                 if (visitedNodes.contains(nextnode)) {
18                     continue;
19                 }
20                 if (unvisitedNodes.get(nextnode) == null) {
21                     unvisitedNodes.put(nextnode, Integer.MAX_VALUE);
22                 }
23                 unvisitedNodes.put(nextnode, Math.min(unvisitedNodes.get(nextnode),
24                         unvisitedNodes.get(nextnode) + length_by_edge.get(Arrays.asList(node, nextnode))));
25             }
26         }
27         return Integer.MAX_VALUE;
28     }
29     public static Node getNodeWithMinDistance(Map<Node,Integer> list) {
30         Node minNode = null;
31         int minDistance = Integer.MAX_VALUE;
32         for (Node node : list.keySet()) {
33             int distance = list.get(node);
34             if (distance < minDistance) {
35                 minDistance = distance;
36                 minNode = node;
37             }
38         }
39         return minNode;
40     }
41 }

Choices:
-
Modify line 23:
```                       distance + length_by_edge.get(Arrays.asList(node, nextnode))));```
-
Modify line 12:
```if (node.equals(goalnode)) {```
-
Modify line 7:
```unvisitedNodes.put(startnode, 0);```
-
Modify line 13:
```return visitedNodes.size() == unvisitedNodes.size() ? Integer.MAX_VALUE : distance;```


Question:
1 import java.util.*;
2 import java.lang.Math.*;
3 public class SHORTEST_PATH_LENGTHS {
4     final static int INF = 99999;
5     public static Map<List<Integer>,Integer> shortest_path_lengths(int numNodes, Map<List<Integer>,Integer> length_by_edge) {
6         Map<List<Integer>,Integer> length_by_path = new HashMap<>();
7         for (int i = 0; i < numNodes; i++) {
8             for (int j =0; j < numNodes; j++) {
9                 List<Integer> edge = new ArrayList<>(Arrays.asList(i,j));
10                 if (i == j) {
11                     length_by_path.put(edge, 0);
12                 }
13                 else if (length_by_edge.containsKey(edge) ) {
14                     length_by_path.put(edge, length_by_edge.get(edge));
15                 } else {
16                     length_by_path.put(edge, INF);
17                 }
18             }
19         }
20         for (int k = 0; k < numNodes; k++) {
21             for (int i = 0; i < numNodes; i++) {
22                 for (int j = 0; j < numNodes; j++) {
23                     int update_length = Math.min(length_by_path.get(Arrays.asList(i,j)),
24                                                  sumLengths(length_by_path.get(Arrays.asList(i,k)),
25                                                             length_by_path.get(Arrays.asList(j,k))));
26                     length_by_path.put(Arrays.asList(i,j), update_length);
27                 }
28             }
29         }
30         return length_by_path;
31     }
32     static private int sumLengths(int a, int b) {
33         if(a == INF || b == INF) {
34             return INF;
35         }
36         return a + b;
37     }
38 }

Choices:
-
Modify line 11:
```length_by_path.put(edge, i == j ? 0 : INF);```
-
Modify line 8:
```for (int j = 0; j < numNodes; j++) {```
-
Modify line 24:
```                                                           length_by_path.get(Arrays.asList(k,j))));```
-
Modify line 10:
```if (i != j && length_by_edge.containsKey(edge)) {```


Question:
1 import java.util.*;
2 public class SHUNTING_YARD {
3     public static List shunting_yard(ArrayList tokens) {
4         Map<String, Integer> precedence = new HashMap<String, Integer>();
5         precedence.put("+", 1);
6         precedence.put("-", 1);
7         precedence.put("*", 2);
8         precedence.put("/", 2);
9         ArrayList rpntokens = new ArrayList(100);
10         ArrayDeque opstack = new ArrayDeque();
11         for (Object token: tokens) {
12             if (Integer.class.isInstance(token)) {
13                 rpntokens.add((Integer) token);
14             } else {
15                 String operator = (String) token;
16                 while (!opstack.isEmpty() && precedence.get(operator) <= precedence.get(opstack.getLast())) {
17                     rpntokens.add(opstack.pop());
18                 }
19             }
20         }
21         while (!opstack.isEmpty()) {
22             rpntokens.add(opstack.pop());
23         }
24         return rpntokens;
25     }
26 }

Choices:
-
Modify line 6:
```precedence.put("-", 2);```
-
Modify line 16:
```while (!opstack.isEmpty() && (precedence.get(operator) < precedence.get((String) opstack.getLast()) || (precedence.get(operator) == precedence.get((String) opstack.getLast()) && !operator.equals("(")))) {```
-
Modify line 10:
```       for (Object token : tokens) {```
Insert at line 17:
```               opstack.push(token);```
-
Modify line 5:
```precedence.put("+", 2);```


Question:
1 import java.util.*;
2 public class SIEVE {
3     public static boolean all(ArrayList<Boolean> arr) {
4         for (boolean value : arr) {
5             if (!value) { return false; }
6         }
7         return true;
8     }
9     public static boolean any(ArrayList<Boolean> arr) {
10         for (boolean value: arr) {
11             if (value) { return true; }
12         }
13         return false;
14     }
15     public static ArrayList<Boolean> list_comp(int n, ArrayList<Integer> primes) {
16         ArrayList<Boolean> built_comprehension = new ArrayList<Boolean>();
17         for (Integer p : primes) {
18             built_comprehension.add(n % p > 0);
19         }
20         return built_comprehension;
21     }
22     public static ArrayList<Integer> sieve(Integer max) {
23         ArrayList<Integer> primes = new ArrayList<Integer>();
24         for (int n=2; n<max+1; n++) {
25             if (any(list_comp(n, primes))) {
26                 primes.add(n);
27             }
28         }
29         return primes;
30     }
31 }

Choices:
-
Modify line 5:
```return true;```
-
Modify line 4:
```for (boolean value : arr) {```
-
Modify line 24:
```           if (all(list_comp(n, primes))) {```
-
Modify line 5:
```if (value) { return true; }```


Question:
1 import java.util.*;
2 public class SQRT {
3     public static double sqrt(double x, double epsilon) {
4         double approx = x / 2d;
5         while (Math.abs(x-approx) > epsilon) {
6             approx = 0.5d * (approx + x / approx);
7         }
8         return approx;
9     }
10 }

Choices:
-
Modify line 6:
```approx = 0.5 * (approx + x / approx);```
-
Modify line 8:
```return approx * (approx % 1.0);```
-
Modify line 5:
```        while (Math.abs(approx*approx - x) > epsilon) {```
-
Modify line 4:
```       while (Math.abs(x-approx*approx) > epsilon) {```


Question:
1 import java.util.*;
2 public class SUBSEQUENCES {
3     public static ArrayList<ArrayList> subsequences(int a, int b, int k) {
4         if (k == 0) {
5             return new ArrayList();
6         }
7         ArrayList ret = new ArrayList(50);
8         for (int i=a; i<b+1-k; i++) {
9             ArrayList base = new ArrayList(50);
10             for (ArrayList rest : subsequences(i+1, b, k-1)) {
11                 rest.add(0,i);
12                 base.add(rest);
13             }
14             ret.addAll(base);
15         }
16         return ret;
17     }
18 }

Choices:
-
Modify line 5:
```return new ArrayList<ArrayList>();```
-
Modify line 4:
```        if (k == 0 || a > b) {```
-
Modify line 11:
```ArrayList<Integer> newRest = new ArrayList<>(rest);
newRest.add(0, i);
base.add(newRest);```
-
Modify line 4:
```           ArrayList empty_set = new ArrayList<ArrayList>();
           empty_set.add(new ArrayList());
           return empty_set;```


Question:
1 import java.util.*;
2 public class TOPOLOGICAL_ORDERING {
3     public static ArrayList<Node> topological_ordering (List<Node> directedGraph) {
4         ArrayList<Node> orderedNodes = new ArrayList<Node>();
5         for (Node node : directedGraph) {
6             if (node.getPredecessors().isEmpty()) {
7                 orderedNodes.add(node);
8             }
9         }
10         int listSize = orderedNodes.size();
11         for (int i = 0; i < listSize; i++) {
12             Node node = orderedNodes.get(i);
13             for (Node nextNode : node.getSuccessors()) {
14                 if (orderedNodes.containsAll(nextNode.getSuccessors()) && !orderedNodes.contains(nextNode)) {
15                     orderedNodes.add(nextNode);
16                     listSize++;
17                 }
18             }
19         }
20         return orderedNodes;
21     }
22 }

Choices:
-
Modify line 7:
```if (!orderedNodes.contains(node)) {
    orderedNodes.add(node);
}```
-
Modify line 13:
```               if (orderedNodes.containsAll(nextNode.getPredecessors()) && !orderedNodes.contains(nextNode)) {```
-
Modify line 6:
```    if (node.getPredecessors().isEmpty() || !orderedNodes.contains(node)) {```
-
Modify line 11:
```while (orderedNodes.size() < directedGraph.size()) {```


Question:
1 import java.util.*;
2 public class TO_BASE {
3     public static String to_base(int num, int b) {
4         String result = "";
5         String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
6         int i;
7         while (num > 0) {
8             i = num % b;
9             num = num / b;
10             result = result + String.valueOf(alphabet.charAt(i));
11         }
12         return result;
13     }
14 }

Choices:
-
Modify line 9:
```           result = String.valueOf(alphabet.charAt(i))+result;```
-
Modify line 8:
```i = (num % b) % alphabet.length();```
-
Modify line 10:
```result = alphabet.charAt(i) + result;```
-
Modify line 7:
```        while (num >= b) {```


Question:
1 import java.util.*;
2 public class WRAP {
3     public static void main(String[] args) {
4         System.out.println("abc".lastIndexOf("c",30));
5     }
6     public static ArrayList<String> wrap(String text, int cols) {
7         ArrayList<String> lines = new ArrayList<String>();
8         String line;
9         while (text.length() > cols) {
10             int end = text.lastIndexOf(" ", cols);
11             if (end == -1) {
12                 end = cols;
13             }
14             line = text.substring(0,end);
15             text = text.substring(end);
16             lines.add(line);
17         }
18         return lines;
19     }
20 }

Choices:
-
Insert at line 17:
```       lines.add(text);```
-
Modify line 9:
```while (text.length() > 0 && text.length() >= cols) {```
-
Modify line 4:
```System.out.println("abc".lastIndexOf("c"));```
-
Modify line 18:
```return lines.size() > 0 ? lines : null;```


Question:
1 def bitcount(n):
2     count = 0
3     while n:
4         n ^= n - 1
5         count += 1
6     return count

Choices:
-
Modify line 3:
```n &= n - 1```
-
Modify line 2:
```while n & (n - 1):```
-
Modify line 4:
```count += 1 if n & 1 else 0```
-
Modify line 3:
```       n &= n - 1```


Question:
1 from collections import deque as Queue
2 def breadth_first_search(startnode, goalnode):
3     queue = Queue()
4     queue.append(startnode)
5     nodesseen = set()
6     nodesseen.add(startnode)
7     while True:
8         node = queue.popleft()
9         if node is goalnode:
10             return True
11         else:
12             queue.extend(node for node in node.successors if node not in nodesseen)
13             nodesseen.update(node.successors)
14     return False

Choices:
-
Modify line 5:
```nodesseen.add(node)```
-
Modify line 6:
```while queue:```
-
Modify line 4:
```nodesseen.add(startnode)```
-
Modify line 6:
```   while queue:```


Question:
1 def bucketsort(arr, k):
2     counts = [0] * k
3     for x in arr:
4         counts[x] += 1
5     sorted_arr = []
6     for i, count in enumerate(arr):
7         sorted_arr.extend([i] * count)
8     return sorted_arr

Choices:
-
Modify line 5:
```   for i, count in enumerate(counts):```
-
Modify line 4:
```sorted_arr = [None] * k```
-
Modify line 3:
```counts[x] += 1```
-
Modify line 2:
```for x in range(k):```


Question:
1 def depth_first_search(startnode, goalnode):
2     nodesvisited = set()
3     def search_from(node):
4         if node in nodesvisited:
5             return False
6         elif node is goalnode:
7             return True
8         else:
9             return any(
10                 search_from(nextnode) for nextnode in node.successors
11             )
12     return search_from(startnode)

Choices:
-
Modify line 1:
```nodesvisited.add(node)```
-
Modify line 4:
```nodesvisited.add(node)
return False```
-
Insert at line 8:
```           nodesvisited.add(node)```
-
Modify line 3:
```if node in nodesvisited or nodesvisited.add(node):```


Question:
1 def detect_cycle(node):
2     hare = tortoise = node
3     while True:
4         if hare.successor is None:
5             return False
6         tortoise = tortoise.successor
7         hare = hare.successor.successor
8         if hare is tortoise:
9             return True

Choices:
-
Modify line 1:
```hare = node
tortoise = node```
-
Modify line 3:
```        if hare is None or hare.successor is None:```
-
Modify line 3:
```       if hare is None or hare.successor is None:```
-
Modify line 2:
```while hare and hare.successor:```


Question:
1 def find_first_in_sorted(arr, x):
2     lo = 0
3     hi = len(arr)
4     while lo <= hi:
5         mid = (lo + hi) // 2
6         if x == arr[mid] and (mid == 0 or x != arr[mid - 1]):
7             return mid
8         elif x <= arr[mid]:
9             hi = mid
10         else:
11             lo = mid + 1
12     return -1

Choices:
-
Modify line 1:
```lo = -1```
-
Modify line 23:
```    mid = lo + (hi - lo) // 2```
-
Modify line 3:
```    while lo < hi:```
-
Modify line 3:
```   while lo < hi:```


Question:
1 def find_in_sorted(arr, x):
2     def binsearch(start, end):
3         if start == end:
4             return -1
5         mid = start + (end - start) // 2
6         if x < arr[mid]:
7             return binsearch(start, mid)
8         elif x > arr[mid]:
9             return binsearch(mid, end)
10         else:
11             return mid
12     return binsearch(0, len(arr))

Choices:
-
Modify line 8:
```           return binsearch(mid + 1, end)```
-
Modify line 2:
```if start >= end:```
-
Modify line 3:
```return start```
-
Modify line 22:
```mid = start + (end - start) // 2 if start < end else start```


Question:
1 def flatten(arr):
2     for x in arr:
3         if isinstance(x, list):
4             for y in flatten(x):
5                 yield y
6         else:
7             yield flatten(x)

Choices:
-
Modify line 2:
```if isinstance(x, collections.Iterable) and not isinstance(x, (str, bytes)):```
-
Modify line 6:
```           yield x```
-
Modify line 6:
```yield x```
-
Modify line 4:
```yield x```


Question:
1 def gcd(a, b):
2     if b == 0:
3         return a
4     else:
5         return gcd(a % b, b)

Choices:
-
Modify line 4:
```return gcd(b, a % b)```
-
Modify line 2:
```return gcd(b, a % b)```
-
Modify line 4:
```       return gcd(b, a % b)```


Question:
1 def get_factors(n):
2     if n == 1:
3         return []
4     for i in range(2, int(n ** 0.5) + 1):
5         if n % i == 0:
6             return [i] + get_factors(n // i)
7     return []

Choices:
-
Modify line 4:
```if n % i == 0:```
-
Modify line 3:
```for i in range(2, int(n ** 0.5) + 1) if n % i == 0:```
-
Modify line 6:
```   return [n]```
-
Modify line 1:
```if n <= 1:```


Question:
1 def hanoi(height, start=1, end=3):
2     steps = []
3     if height > 0:
4         helper = ({1, 2, 3} - {start} - {end}).pop()
5         steps.extend(hanoi(height - 1, start, helper))
6         steps.append((start, helper))
7         steps.extend(hanoi(height - 1, helper, end))
8     return steps

Choices:
-
Modify line 5:
```       steps.append((start, end))```
-
Modify line 3:
```helper = ({1, 2, 3} - {start, end}).pop()```
-
Modify line 5:
```steps.append((start, end))```
-
Modify line 2:
```if height >= 1:```


Question:
1 def is_valid_parenthesization(parens):
2     depth = 0
3     for paren in parens:
4         if paren == '(':
5             depth += 1
6         else:
7             depth -= 1
8             if depth < 0:
9                 return False
10     return True

Choices:
-
Modify line 1:
```    depth = 1```
-
Modify line 2:
```        if paren not in '()':
            return False```
-
Modify line 9:
```   return depth == 0```
-
Modify line 3:
```        if paren == '(' or paren == ')':```


Question:
1 def kheapsort(arr, k):
2     import heapq
3     heap = arr[:k]
4     heapq.heapify(heap)
5     for x in arr:
6         yield heapq.heappushpop(heap, x)
7     while heap:
8         yield heapq.heappop(heap)

Choices:
-
Modify line 5:
```heapq.heappushpop(heap, x)```
-
Modify line 2:
```heap = arr[:k+1]```
-
Modify line 4:
```   for x in arr[k:]:```
-
Modify line 7:
```yield heapq.heappop(heap) if heap else None```


Question:
1 def knapsack(capacity, items):
2     from collections import defaultdict
3     memo = defaultdict(int)
4     for i in range(1, len(items) + 1):
5         weight, value = items[i - 1]
6         for j in range(1, capacity + 1):
7             memo[i, j] = memo[i - 1, j]
8             if weight < j:
9                 memo[i, j] = max(
10                     memo[i, j],
11                     value + memo[i - 1, j - weight]
12                 )
13     return memo[len(items), capacity]

Choices:
-
Modify line 7:
```           if weight <= j:```
-
Modify line 5:
```for j in range(weight + 1)```
-
Modify line 7:
```if j >= weight:```
-
Modify line 2:
```memo = defaultdict(lambda: defaultdict(int))```


Question:
1 def kth(arr, k):
2     pivot = arr[0]
3     below = [x for x in arr if x < pivot]
4     above = [x for x in arr if x > pivot]
5     num_less = len(below)
6     num_lessoreq = len(arr) - len(above)
7     if k < num_less:
8         return kth(below, k)
9     elif k >= num_lessoreq:
10         return kth(above, k)
11     else:
12         return pivot

Choices:
-
Modify line 1:
```pivot = arr[len(arr) // 2]```
-
Modify line 9:
```       return kth(above, k - num_lessoreq)```
-
Modify line 3:
```above = [x for x in arr[1:] if x >= pivot]```
-
Modify line 2:
```below = [x for x in arr[1:] if x < pivot]```


Question:
1 def lcs_length(s, t):
2     from collections import Counter
3     dp = Counter()
4     for i in range(len(s)):
5         for j in range(len(t)):
6             if s[i] == t[j]:
7                 dp[i, j] = dp[i - 1, j] + 1
8     return max(dp.values()) if dp else 0

Choices:
-
Modify line 2:
```dp[0, 0] = 0```
-
Modify line 3:
```for i in range(len(s) + 1):```
-
Modify line 4:
```for j in range(len(t) + 1)```
-
Modify line 6:
```               dp[i, j] = dp[i - 1, j - 1] + 1```


Question:
1 def levenshtein(source, target):
2     if source == '' or target == '':
3         return len(source) or len(target)
4     elif source[0] == target[0]:
5         return 1 + levenshtein(source[1:], target[1:])
6     else:
7         return 1 + min(
8             levenshtein(source,     target[1:]),
9             levenshtein(source[1:], target[1:]),
10             levenshtein(source[1:], target)
11         )

Choices:
-
Modify line 4:
```       return levenshtein(source[1:], target[1:])```
-
Modify line 1:
```    if len(source) == 0 or len(target) == 0:```
-
Modify line 2:
```return max(len(source), len(target))```


Question:
1 def lis(arr):
2     ends = {}
3     longest = 0
4     for i, val in enumerate(arr):
5         prefix_lengths = [j for j in range(1, longest + 1) if arr[ends[j]] < val]
6         length = max(prefix_lengths) if prefix_lengths else 0
7         if length == longest or val < arr[ends[length + 1]]:
8             ends[length + 1] = i
9             longest = length + 1
10     return longest

Choices:
-
Modify line 4:
```prefix_lengths = [j for j in range(length + 1) if j == 0 or arr[ends[j]] < val]```
-
Modify line 3:
```for i, val in enumerate(arr, start=1):```
-
Modify line 8:
```           longest = max(longest, length + 1)```
-
Modify line 2:
```longest = longest + 1```


Question:
1 def longest_common_subsequence(a, b):
2     if not a or not b:
3         return ''
4     elif a[0] == b[0]:
5         return a[0] + longest_common_subsequence(a[1:], b)
6     else:
7         return max(
8             longest_common_subsequence(a, b[1:]),
9             longest_common_subsequence(a[1:], b),
10             key=len
11         )

Choices:
-
Modify line 2:
```return a[0] + longest_common_subsequence(a[1:], b[1:])```
-
Modify line 4:
```return a[0] + longest_common_subsequence(a[1:], b[1:])```
-
Modify line 1:
```if not a or not b:
    return 0```
-
Modify line 4:
```       return a[0] + longest_common_subsequence(a[1:], b[1:])```


Question:
1 def max_sublist_sum(arr):
2     max_ending_here = 0
3     max_so_far = 0
4     for x in arr:
5         max_ending_here = max_ending_here + x
6         max_so_far = max(max_so_far, max_ending_here)
7     return max_so_far

Choices:
-
Modify line 1:
```max_ending_here = max(x, max_ending_here + x)```
-
Modify line 2:
```max_so_far = float('-inf')```
-
Modify line 4:
```    max_ending_here = max(x, max_ending_here + x)```
-
Modify line 4:
```       max_ending_here = max(0, max_ending_here + x)```


Question:
1 def mergesort(arr):
2     def merge(left, right):
3         result = []
4         i = 0
5         j = 0
6         while i < len(left) and j < len(right):
7             if left[i] <= right[j]:
8                 result.append(left[i])
9                 i += 1
10             else:
11                 result.append(right[j])
12                 j += 1
13         result.extend(left[i:] or right[j:])
14         return result
15     if len(arr) == 0:
16         return arr
17     else:
18         middle = len(arr) // 2
19         left = mergesort(arr[:middle])
20         right = mergesort(arr[middle:])
21         return merge(left, right)

Choices:
-
Modify line 14:
```   if len(arr) <= 1:```
-
Modify line 6:
```            if left[i] < right[j]:```
-
Modify line 11:
```j += 1```
-
Modify line 10:
```result.append(right[j])```


Question:
1 def minimum_spanning_tree(weight_by_edge):
2     group_by_node = {}
3     mst_edges = set()
4     for edge in sorted(weight_by_edge, key=weight_by_edge.__getitem__):
5         u, v = edge
6         if group_by_node.setdefault(u, {u}) != group_by_node.setdefault(v, {v}):
7             mst_edges.add(edge)
8             group_by_node[u].update(group_by_node[v])
9             for node in group_by_node[v]:
10                 group_by_node[node].update(group_by_node[u])
11     return mst_edges

Choices:
-
Modify line 5:
```if group_by_node.get(u, None) is not group_by_node.get(v, None):```
-
Modify line 9:
```               group_by_node[node] = group_by_node[u]```
-
Modify line 4:
```u, v = edge[0], edge[1]```
-
Modify line 1:
```group_by_node = {node: {node} for node in weight_by_edge.keys()}```


Question:
1 def next_palindrome(digit_list):
2     high_mid = len(digit_list) // 2
3     low_mid = (len(digit_list) - 1) // 2
4     while high_mid < len(digit_list) and low_mid >= 0:
5         if digit_list[high_mid] == 9:
6             digit_list[high_mid] = 0
7             digit_list[low_mid] = 0
8             high_mid += 1
9             low_mid -= 1
10         else:
11             digit_list[high_mid] += 1
12             if low_mid != high_mid:
13                 digit_list[low_mid] += 1
14             return digit_list
15     return [1] + (len(digit_list)) * [0] + [1]

Choices:
-
Modify line 14:
```   return [1] + (len(digit_list) - 1) * [0] + [1]```
-
Modify line 3:
```while high_mid > low_mid:```
-
Modify line 27:
```low_mid = (len(digit_list) - 1) // 2 if len(digit_list) % 2 == 0 else (len(digit_list) + 1) // 2```
-
Modify line 27:
```high_mid = (len(digit_list) - 1) // 2```


Question:
1 def next_permutation(perm):
2     for i in range(len(perm) - 2, -1, -1):
3         if perm[i] < perm[i + 1]:
4             for j in range(len(perm) - 1, i, -1):
5                 if perm[j] < perm[i]:
6                     next_perm = list(perm)
7                     next_perm[i], next_perm[j] = perm[j], perm[i]
8                     next_perm[i + 1:] = reversed(next_perm[i + 1:])
9                     return next_perm

Choices:
-
Modify line 6:
```next_perm[i], next_perm[j] = next_perm[j], next_perm[i]```
-
Modify line 3:
```for j in range(len(perm) - 1, i, -1)```
-
Modify line 4:
```               if perm[i] < perm[j]:```
-
Modify line 1:
```    for i in range(len(perm) - 1, -1, -1):```


Question:
1 def pascal(n):
2     rows = [[1]]
3     for r in range(1, n):
4         row = []
5         for c in range(0, r):
6             upleft = rows[r - 1][c - 1] if c > 0 else 0
7             upright = rows[r - 1][c] if c < r else 0
8             row.append(upleft + upright)
9         rows.append(row)
10     return rows

Choices:
-
Modify line 3:
```row = [1]```
-
Modify line 1:
```rows = [[1], [1, 1]]```
-
Modify line 5:
```upleft = rows[r - 1][c - 1] if c > 0 else 0```
-
Modify line 4:
```       for c in range(0, r + 1):```


Question:
1 def possible_change(coins, total):
2     if total == 0:
3         return 1
4     if total < 0:
5         return 0
6     first, *rest = coins
7     return possible_change(coins, total - first) + possible_change(rest, total)

Choices:
-
Modify line 3:
```   if total < 0 or not coins:```
-
Modify line 4:
```if total < 0 or len(coins) == 0:```
-
Modify line 3:
```return 0 if coins and total < 0 else 1 if total == 0 else possible_change(rest, total) + possible_change(coins, total - first)```
-
Modify line 5:
```if total < 0 or coins == []:
    return 0```


Question:
1 def powerset(arr):
2     if arr:
3         first, *rest = arr
4         rest_subsets = powerset(rest)
5         return [[first] + subset for subset in rest_subsets]
6     else:
7         return [[]]

Choices:
-
Modify line 4:
```       return rest_subsets + [[first] + subset for subset in rest_subsets]```
-
Modify line 1:
```    result = powerset(rest)
    return [[first] + subset for subset in result] + result```
-
Modify line 2:
```first, rest = arr[0], arr[1:]```
-
Modify line 3:
```    return [first] + subset for subset in rest_subsets] or []```


Question:
1 def quicksort(arr):
2     if not arr:
3         return []
4     pivot = arr[0]
5     lesser = quicksort([x for x in arr[1:] if x < pivot])
6     greater = quicksort([x for x in arr[1:] if x > pivot])
7     return lesser + [pivot] + greater

Choices:
-
Modify line 4:
```    lesser = [x for x in arr[1:] if x < pivot]```
-
Modify line 5:
```   greater = quicksort([x for x in arr[1:] if x >= pivot])```
-
Modify line 2:
```return arr if len(arr) <= 1 else []```
-
Modify line 3:
```pivot = arr[len(arr) // 2]```


Question:
1 def reverse_linked_list(node):
2     prevnode = None
3     while node:
4         nextnode = node.successor
5         node.successor = prevnode
6         node = nextnode
7     return prevnode

Choices:
-
Insert at line 5:
```       prevnode = node```
-
Modify line 2:
```while node is not None:```
-
Modify line 3:
```nextnode = node.next```
-
Modify line 1:
```prevnode = node```


Question:
1 def rpn_eval(tokens):
2     def op(symbol, a, b):
3         return {
4             '+': lambda a, b: a + b,
5             '-': lambda a, b: a - b,
6             '*': lambda a, b: a * b,
7             '/': lambda a, b: a / b
8         }[symbol](a, b)
9     stack = []
10     for token in tokens:
11         if isinstance(token, float):
12             stack.append(token)
13         else:
14             a = stack.pop()
15             b = stack.pop()
16             stack.append(
17                 op(token, a, b)
18             )
19     return stack.pop()

Choices:
-
Modify line 10:
```        if isinstance(token, (int, float)):```
-
Modify line 8:
```stack = list()```
-
Modify line 16:
```               op(token, b, a)```
-
Modify line 11:
```stack.append(float(token))```


Question:
1 def shunting_yard(tokens):
2     precedence = {
3         '+': 1,
4         '-': 1,
5         '*': 2,
6         '/': 2
7     }
8     rpntokens = []
9     opstack = []
10     for token in tokens:
11         if isinstance(token, int):
12             rpntokens.append(token)
13         else:
14             while opstack and precedence[token] <= precedence[opstack[-1]]:
15                 rpntokens.append(opstack.pop())
16     while opstack:
17         rpntokens.append(opstack.pop())
18     return rpntokens

Choices:
-
Insert at line 15:
```           opstack.append(token)```
-
Modify line 14:
```rpntokens.append(opstack.pop())```
-
Modify line 11:
```rpntokens.append(token)```
-
Modify line 8:
```opstack = [')']```


Question:
1 def sieve(max):
2     primes = []
3     for n in range(2, max + 1):
4         if any(n % p > 0 for p in primes):
5             primes.append(n)
6     return primes

Choices:
-
Modify line 1:
```primes = [2]```
-
Modify line 2:
```for n in range(2, max + 1):```
-
Modify line 3:
```       if all(n % p > 0 for p in primes):```
-
Modify line 3:
```        if all(n % p != 0 for p in primes):```


Question:
1 def sqrt(x, epsilon):
2     approx = x / 2
3     while abs(x - approx) > epsilon:
4         approx = 0.5 * (approx + x / approx)
5     return approx

Choices:
-
Modify line 3:
```approx = (approx + x / approx) / 2```
-
Modify line 2:
```    while abs(x - approx * approx) > epsilon:```
-
Modify line 2:
```   while abs(x - approx ** 2) > epsilon:```
-
Modify line 1:
```approx = x if x < 1 else 1```


Question:
1 def subsequences(a, b, k):
2     if k == 0:
3         return []
4     ret = []
5     for i in range(a, b + 1 - k):
6         ret.extend(
7             [i] + rest for rest in subsequences(i + 1, b, k - 1)
8         )
9     return ret

Choices:
-
Modify line 2:
```return [[]]```
-
Modify line 3:
```ret = [[]] if k == 1 else []```
-
Modify line 2:
```       return [[]]```
-
Modify line 1:
```if k == 0 or b < a:```


Question:
1 import string
2 def to_base(num, b):
3     result = ''
4     alphabet = string.digits + string.ascii_uppercase
5     while num > 0:
6         i = num % b
7         num = num // b
8         result = result + alphabet[i]
9     return result

Choices:
-
Modify line 5:
```i = num % b if b <= len(alphabet) else num % len(alphabet)```
-
Modify line 7:
```       result = alphabet[i] + result```
-
Modify line 22:
```    num = int(num // b)```
-
Modify line 7:
```result = alphabet[i] + result```


Question:
1 def topological_ordering(nodes):
2     ordered_nodes = [node for node in nodes if not node.incoming_nodes]
3     for node in ordered_nodes:
4         for nextnode in node.outgoing_nodes:
5             if set(ordered_nodes).issuperset(nextnode.outgoing_nodes) and nextnode not in ordered_nodes:
6                 ordered_nodes.append(nextnode)
7     return ordered_nodes

Choices:
-
Modify line 4:
```if all(out_node in ordered_nodes for out_node in nextnode.outgoing_nodes) and nextnode not in ordered_nodes:```
-
Modify line 3:
```for nextnode in node.outgoing_nodes:
    if nextnode not in ordered_nodes and all(child in ordered_nodes for child in nextnode.incoming_nodes):
        ordered_nodes.append(nextnode)```
-
Modify line 4:
```           if set(ordered_nodes).issuperset(nextnode.incoming_nodes) and nextnode not in ordered_nodes:```
-
Modify line 6:
```return sorted(ordered_nodes, key=lambda x: nodes.index(x))```


Question:
1 def wrap(text, cols):
2     lines = []
3     while len(text) > cols:
4         end = text.rfind(' ', 0, cols + 1)
5         if end == -1:
6             end = cols
7         line, text = text[:end], text[end:]
8         lines.append(line)
9     return lines

Choices:
-
Insert at line 8:
```   lines.append(text)```
-
Modify line 3:
```end = text.rfind(' ', 0, cols) if cols < len(text) else cols```
-
Modify line 2:
```while len(text) > 0:```
-
Modify line 6:
```line, text = text[:end], text[end + 1:] if text else ''```