id
stringlengths 20
21
| content
stringlengths 337
2.14k
| max_stars_repo_path
stringlengths 40
71
|
---|---|---|
quixbugs-java_data_1 | package java_programs;
import java.util.*;
import java.lang.Math.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class NEXT_PALINDROME {
public static String next_palindrome(int[] digit_list) {
int high_mid = Math.floorDiv(digit_list.length, 2);
int low_mid = Math.floorDiv(digit_list.length - 1, 2);
while (high_mid < digit_list.length && low_mid >= 0) {
if (digit_list[high_mid] == 9) {
digit_list[high_mid] = 0;
digit_list[low_mid] = 0;
high_mid += 1;
low_mid -= 1;
} else {
digit_list[high_mid] += 1;
if (low_mid != high_mid) {
digit_list[low_mid] += 1;
}
return Arrays.toString(digit_list);
}
}
ArrayList<Integer> otherwise = new ArrayList<Integer>();
otherwise.add(1);
otherwise.addAll(Collections.nCopies(digit_list.length, 0));
otherwise.add(1);
return String.valueOf(otherwise);
}
}
| QuixBugs/QuixBugs/java_programs/NEXT_PALINDROME.java |
quixbugs-java_data_2 | package java_programs;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class FIND_IN_SORTED {
public static int binsearch(int[] arr, int x, int start, int end) {
if (start == end) {
return -1;
}
int mid = start + (end - start) / 2; // check this is floor division
if (x < arr[mid]) {
return binsearch(arr, x, start, mid);
} else if (x > arr[mid]) {
return binsearch(arr, x, mid, end);
} else {
return mid;
}
}
public static int find_in_sorted(int[] arr, int x) {
return binsearch(arr, x, 0, arr.length);
}
}
| QuixBugs/QuixBugs/java_programs/FIND_IN_SORTED.java |
quixbugs-java_data_3 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class LEVENSHTEIN {
public static int levenshtein(String source, String target) {
if (source.isEmpty() || target.isEmpty()) {
return source.isEmpty() ? target.length() : source.length();
} else if (source.charAt(0) == target.charAt(0)) {
return 1 + levenshtein(source.substring(1), target.substring(1));
} else {
return 1 + Math.min(Math.min(
levenshtein(source, target.substring(1)),
levenshtein(source.substring(1), target.substring(1))),
levenshtein(source.substring(1), target)
);
}
}
}
| QuixBugs/QuixBugs/java_programs/LEVENSHTEIN.java |
quixbugs-java_data_4 | package java_programs;
import java.util.*;
import java.util.function.BinaryOperator;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class RPN_EVAL {
public static Double rpn_eval(ArrayList tokens) {
Map<String, BinaryOperator<Double>> op = new HashMap<String, BinaryOperator<Double>>();
op.put("+", (a, b) -> a + b);
op.put("-", (a, b) -> a - b);
op.put("*", (a, b) -> a * b);
op.put("/", (a, b) -> a / b);
Stack stack = new Stack();
for (Object token : tokens) {
if (Double.class.isInstance(token)) {
stack.push((Double) token);
} else {
token = (String) token;
Double a = (Double) stack.pop();
Double b = (Double) stack.pop();
Double c = 0.0;
BinaryOperator<Double> bin_op = op.get(token);
c = bin_op.apply(a,b);
stack.push(c);
}
}
return (Double) stack.pop();
}
}
| QuixBugs/QuixBugs/java_programs/RPN_EVAL.java |
quixbugs-java_data_5 | package java_programs;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class BITCOUNT {
public static int bitcount(int n) {
int count = 0;
while (n != 0) {
n = (n ^ (n - 1));
count++;
}
return count;
}
}
| QuixBugs/QuixBugs/java_programs/BITCOUNT.java |
quixbugs-java_data_6 | package java_programs;
import java.util.*;
import java.lang.Math.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Angela Chen
*/
public class SHORTEST_PATH_LENGTHS {
// Define Infinite as a large enough value. This value will be used
// for vertices not connected to each other
final static int INF = 99999;
public static Map<List<Integer>,Integer> shortest_path_lengths(int numNodes, Map<List<Integer>,Integer> length_by_edge) {
Map<List<Integer>,Integer> length_by_path = new HashMap<>();
for (int i = 0; i < numNodes; i++) {
for (int j =0; j < numNodes; j++) {
List<Integer> edge = new ArrayList<>(Arrays.asList(i,j));
if (i == j) {
length_by_path.put(edge, 0);
}
else if (length_by_edge.containsKey(edge) ) {
length_by_path.put(edge, length_by_edge.get(edge));
} else {
length_by_path.put(edge, INF);
}
}
}
for (int k = 0; k < numNodes; k++) {
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
int update_length = Math.min(length_by_path.get(Arrays.asList(i,j)),
sumLengths(length_by_path.get(Arrays.asList(i,k)),
length_by_path.get(Arrays.asList(j,k))));
length_by_path.put(Arrays.asList(i,j), update_length);
}
}
}
return length_by_path;
}
static private int sumLengths(int a, int b) {
if(a == INF || b == INF) {
return INF;
}
return a + b;
}
}
| QuixBugs/QuixBugs/java_programs/SHORTEST_PATH_LENGTHS.java |
quixbugs-java_data_7 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class LONGEST_COMMON_SUBSEQUENCE {
public static String longest_common_subsequence(String a, String b) {
if (a.isEmpty() || b.isEmpty()) {
return "";
} else if (a.charAt(0) == b.charAt(0)) {
return a.charAt(0) + longest_common_subsequence(a.substring(1), b);
} else {
String fst = longest_common_subsequence(a, b.substring(1));
String snd = longest_common_subsequence(a.substring(1), b);
return fst.length() >= snd.length() ? fst : snd;
}
}
}
| QuixBugs/QuixBugs/java_programs/LONGEST_COMMON_SUBSEQUENCE.java |
quixbugs-java_data_8 | package java_programs;
import java.util.*;
/**
*
* @author Angela Chen
*/
public class SHORTEST_PATH_LENGTH {
public static int shortest_path_length(Map<List<Node>, Integer> length_by_edge, Node startnode, Node goalnode) {
int n = length_by_edge.size();
// the shortest distance from source to each node
Map<Node, Integer> unvisitedNodes = new HashMap<>();
Set<Node> visitedNodes = new HashSet<>();
unvisitedNodes.put(startnode, 0);
while (!unvisitedNodes.isEmpty()) {
Node node = getNodeWithMinDistance(unvisitedNodes);
int distance = unvisitedNodes.get(node);
unvisitedNodes.remove(node);
if (node.getValue() == goalnode.getValue()) {
return distance;
}
visitedNodes.add(node);
for (Node nextnode : node.getSuccessors()) {
if (visitedNodes.contains(nextnode)) {
continue;
}
if (unvisitedNodes.get(nextnode) == null) {
unvisitedNodes.put(nextnode, Integer.MAX_VALUE);
}
unvisitedNodes.put(nextnode, Math.min(unvisitedNodes.get(nextnode),
unvisitedNodes.get(nextnode) + length_by_edge.get(Arrays.asList(node, nextnode))));
}
}
return Integer.MAX_VALUE;
}
public static Node getNodeWithMinDistance(Map<Node,Integer> list) {
Node minNode = null;
int minDistance = Integer.MAX_VALUE;
for (Node node : list.keySet()) {
int distance = list.get(node);
if (distance < minDistance) {
minDistance = distance;
minNode = node;
}
}
return minNode;
}
} | QuixBugs/QuixBugs/java_programs/SHORTEST_PATH_LENGTH.java |
quixbugs-java_data_9 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class REVERSE_LINKED_LIST {
public static Node reverse_linked_list(Node node) {
Node prevnode = null;
Node nextnode;
while (node != null) {
nextnode = node.getSuccessor();
node.setSuccessor(prevnode);
node = nextnode;
}
return prevnode;
}
}
| QuixBugs/QuixBugs/java_programs/REVERSE_LINKED_LIST.java |
quixbugs-java_data_10 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class LCS_LENGTH {
public static Integer lcs_length(String s, String t) {
// make a Counter
// pair? no! just hashtable to a hashtable.. woo.. currying
Map<Integer, Map<Integer,Integer>> dp = new HashMap<Integer,Map<Integer,Integer>>();
// just set all the internal maps to 0
for (int i=0; i < s.length(); i++) {
Map<Integer,Integer> initialize = new HashMap<Integer,Integer>();
dp.put(i, initialize);
for (int j=0; j < t.length(); j++) {
Map<Integer,Integer> internal_map = dp.get(i);
internal_map.put(j,0);
dp.put(i, internal_map);
}
}
// now the actual code
for (int i=0; i < s.length(); i++) {
for (int j=0; j < t.length(); j++) {
if (s.charAt(i) == t.charAt(j)) {
if (dp.containsKey(i-1)) {
Map<Integer, Integer> internal_map = dp.get(i);
int insert_value = dp.get(i-1).get(j) + 1;
internal_map.put(j, insert_value);
dp.put(i,internal_map);
} else {
Map<Integer, Integer> internal_map = dp.get(i);
internal_map.put(j,1);
dp.put(i,internal_map);
}
}
}
}
if (!dp.isEmpty()) {
List<Integer> ret_list = new ArrayList<Integer>();
for (int i=0; i<s.length(); i++) {
ret_list.add(!dp.get(i).isEmpty() ? Collections.max(dp.get(i).values()) : 0);
}
return Collections.max(ret_list);
} else {
return 0;
}
}
}
| QuixBugs/QuixBugs/java_programs/LCS_LENGTH.java |
quixbugs-java_data_11 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class PASCAL {
public static ArrayList<ArrayList<Integer>> pascal(int n) {
ArrayList<ArrayList<Integer>> rows = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> init = new ArrayList<Integer>();
init.add(1);
rows.add(init);
for (int r=1; r<n; r++) {
ArrayList<Integer> row = new ArrayList<Integer>();
for (int c=0; c<r; c++) {
int upleft, upright;
if (c > 0) {
upleft = rows.get(r-1).get(c-1);
} else {
upleft = 0;
}
if (c < r) {
upright = rows.get(r-1).get(c);
} else {
upright = 0;
}
row.add(upleft+upright);
}
rows.add(row);
}
return rows;
}
}
| QuixBugs/QuixBugs/java_programs/PASCAL.java |
quixbugs-java_data_12 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class IS_VALID_PARENTHESIZATION {
public static Boolean is_valid_parenthesization(String parens) {
int depth = 0;
for (int i = 0; i < parens.length(); i++) {
Character paren = parens.charAt(i);
if (paren.equals('(')) {
depth++;
} else {
depth--;
if (depth < 0) { return false; }
}
}
return true;
}
}
| QuixBugs/QuixBugs/java_programs/IS_VALID_PARENTHESIZATION.java |
quixbugs-java_data_13 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class MAX_SUBLIST_SUM {
public static int max_sublist_sum(int[] arr) {
int max_ending_here = 0;
int max_so_far = 0;
for (int x : arr) {
max_ending_here = max_ending_here + x;
max_so_far = Math.max(max_so_far, max_ending_here);
}
return max_so_far;
}
}
| QuixBugs/QuixBugs/java_programs/MAX_SUBLIST_SUM.java |
quixbugs-java_data_14 | package java_programs;
import java.util.*;
import java.lang.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class KNAPSACK {
public static int knapsack(int capacity, int [][] items) {
int weight = 0, value = 0;
int n = items.length;
int memo[][] = new int[n + 1][capacity + 1];
for (int i = 0; i <= n ; i++)
{
if (i - 1 >= 0) {
weight = items[i - 1][0];
value = items[i - 1][1];
}
for (int j = 0; j <= capacity; j++)
{
if (i == 0 || j == 0) {
memo[i][j] = 0;
}
else if (weight < j) {
memo[i][j] = Math.max(memo[i - 1][j], value + memo[i - 1][j - weight]);
}
else {
memo[i][j] = memo [i-1][j];
}
}
}
return memo[n][capacity];
}
}
| QuixBugs/QuixBugs/java_programs/KNAPSACK.java |
quixbugs-java_data_15 | package java_programs;
import java.util.*;
public class TOPOLOGICAL_ORDERING {
public static ArrayList<Node> topological_ordering (List<Node> directedGraph) {
ArrayList<Node> orderedNodes = new ArrayList<Node>();
for (Node node : directedGraph) {
if (node.getPredecessors().isEmpty()) {
orderedNodes.add(node);
}
}
int listSize = orderedNodes.size();
for (int i = 0; i < listSize; i++) {
Node node = orderedNodes.get(i);
for (Node nextNode : node.getSuccessors()) {
if (orderedNodes.containsAll(nextNode.getSuccessors()) && !orderedNodes.contains(nextNode)) {
orderedNodes.add(nextNode);
listSize++;
}
}
}
return orderedNodes;
}
}
| QuixBugs/QuixBugs/java_programs/TOPOLOGICAL_ORDERING.java |
quixbugs-java_data_16 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class POSSIBLE_CHANGE {
public static int possible_change(int[] coins, int total) {
if (total == 0) {
return 1;
}
if (total < 0) {
return 0;
}
int first = coins[0];
int[] rest = Arrays.copyOfRange(coins, 1, coins.length);
return possible_change(coins, total-first) + possible_change(rest, total);
}
}
| QuixBugs/QuixBugs/java_programs/POSSIBLE_CHANGE.java |
quixbugs-java_data_17 | package java_programs;
import java.util.*;
import java.util.ArrayDeque;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class BREADTH_FIRST_SEARCH {
public static Set<Node> nodesvisited = new HashSet<>();
public static boolean breadth_first_search(Node startnode, Node goalnode) {
Deque<Node> queue = new ArrayDeque<>();
queue.addLast(startnode);
nodesvisited.add(startnode);
while (true) {
Node node = queue.removeFirst();
if (node == goalnode) {
return true;
} else {
for (Node successor_node : node.getSuccessors()) {
if (!nodesvisited.contains(successor_node)) {
queue.addFirst(successor_node);
nodesvisited.add(successor_node);
}
}
}
}
/**
* The buggy program always drops into while(true) loop and will not return false
* Removed below line to fix compilation error
*/
// return false;
}
} | QuixBugs/QuixBugs/java_programs/BREADTH_FIRST_SEARCH.java |
quixbugs-java_data_18 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class HANOI {
// default start=1, end=3
public static List<Pair<Integer,Integer>> hanoi(int height, int start, int end) {
ArrayList<Pair<Integer,Integer>> steps = new ArrayList<Pair<Integer,Integer>>();
if (height > 0) {
PriorityQueue<Integer> crap_set = new PriorityQueue<Integer>();
crap_set.add(1);
crap_set.add(2);
crap_set.add(3);
crap_set.remove(start);
crap_set.remove(end);
int helper = crap_set.poll();
steps.addAll(hanoi(height-1, start, helper));
steps.add(new Pair<Integer,Integer>(start, helper));
steps.addAll(hanoi(height-1, helper, end));
}
return steps;
}
public static class Pair<F, S> {
private F first; //first member of pair
private S second; //second member of pair
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public void setFirst(F first) {
this.first = first;
}
public void setSecond(S second) {
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "(" + String.valueOf(first) + ", " + String.valueOf(second) + ")";
}
}
}
| QuixBugs/QuixBugs/java_programs/HANOI.java |
quixbugs-java_data_19 | package java_programs;
import java.util.*;
import java.lang.Math.*;
/**
*
* @author Angela Chen
*/
public class SHORTEST_PATHS {
// Define Infinite as a large enough value. This value will be used
// for vertices not connected to each other
final static int INF = 99999;
public static Map<String, Integer> shortest_paths(String source, Map<List<String>,Integer> weight_by_edge) {
Map<String,Integer> weight_by_node = new HashMap<String,Integer>();
for (List<String> edge : weight_by_edge.keySet()) {
weight_by_node.put(edge.get(1), INF);
weight_by_node.put(edge.get(0), INF);
}
weight_by_node.put(source, 0);
for (int i = 0; i < weight_by_node.size(); i++) {
for (List<String> edge : weight_by_edge.keySet()) {
int update_weight = Math.min(
weight_by_node.get(edge.get(0))
+ weight_by_edge.get(edge),
weight_by_node.get(edge.get(1)));
weight_by_edge.put(edge, update_weight);
}
}
return weight_by_node;
}
/**
* Rewrite shortest_paths method
* @param node
* @param weight_by_edge
* @return
*/
public static Map<String, Integer> shortest_paths(Node source, List<WeightedEdge> weight_by_edge) {
Map<String,Integer> weight_by_node = new HashMap<String,Integer>();
for (WeightedEdge edge : weight_by_edge) {
weight_by_node.put(edge.node1.toString(), INF);
weight_by_node.put(edge.node2.toString(), INF);
}
weight_by_node.put(source.getValue(), 0);
for (int i = 0; i < weight_by_node.size(); i++) {
for (WeightedEdge edge : weight_by_edge) {
int update_weight = Math.min(
weight_by_node.get(edge.node1.toString())
+ edge.weight,
weight_by_node.get(edge.node2.toString()));
edge.weight = update_weight;
}
}
return weight_by_node;
}
}
| QuixBugs/QuixBugs/java_programs/SHORTEST_PATHS.java |
quixbugs-java_data_20 | package java_programs;
import java.util.*;
/*
*/
public class KTH {
public static Integer kth(ArrayList<Integer> arr, int k) {
int pivot = arr.get(0);
ArrayList<Integer> below, above;
below = new ArrayList<Integer>(arr.size());
above = new ArrayList<Integer>(arr.size());
for (Integer x : arr) {
if (x < pivot) {
below.add(x);
} else if (x > pivot) {
above.add(x);
}
}
int num_less = below.size();
int num_lessoreq = arr.size() - above.size();
if (k < num_less) {
return kth(below, k);
} else if (k >= num_lessoreq) {
return kth(above, k);
} else {
return pivot;
}
}
}
| QuixBugs/QuixBugs/java_programs/KTH.java |
quixbugs-java_data_21 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class GCD {
public static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(a % b, b);
}
}
}
| QuixBugs/QuixBugs/java_programs/GCD.java |
quixbugs-java_data_22 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class SHUNTING_YARD {
public static List shunting_yard(ArrayList tokens) {
Map<String, Integer> precedence = new HashMap<String, Integer>();
precedence.put("+",1);
precedence.put("-",1);
precedence.put("*",2);
precedence.put("/",2);
ArrayList rpntokens = new ArrayList(100);
ArrayDeque opstack = new ArrayDeque();
for (Object token: tokens) {
if (Integer.class.isInstance(token)) {
// cover also Double case I guess?
rpntokens.add((Integer) token);
} else {
String operator = (String) token;
while (!opstack.isEmpty() && precedence.get(operator) <= precedence.get(opstack.getLast())) {
rpntokens.add(opstack.pop());
}
}
}
while (!opstack.isEmpty()) {
rpntokens.add(opstack.pop());
}
return rpntokens;
}
}
| QuixBugs/QuixBugs/java_programs/SHUNTING_YARD.java |
quixbugs-java_data_23 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class GET_FACTORS {
public static ArrayList<Integer> get_factors(int n) {
if (n == 1) {
return new ArrayList<Integer>();
}
int max = (int)(Math.sqrt(n) + 1.0);
for (int i=2; i < max; i++) {
if (n % i == 0) {
ArrayList<Integer> prepend = new ArrayList<Integer>(0);
prepend.add(i);
prepend.addAll(get_factors(n / i));
return prepend;
}
}
return new ArrayList<Integer>();
}
}
| QuixBugs/QuixBugs/java_programs/GET_FACTORS.java |
quixbugs-java_data_24 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class DEPTH_FIRST_SEARCH {
public static boolean depth_first_search(Node startnode, Node goalnode) {
Set<Node> nodesvisited = new HashSet<>();
class Search {
boolean search(Node node) {
if (nodesvisited.contains(node)) {
return false;
} else if (node == goalnode) {
return true;
} else {
for (Node successornodes : node.getSuccessors()) {
if (search(successornodes)) { return true; }
}
}
return false;
}
};
Search s = new Search();
return s.search(startnode);
}
}
| QuixBugs/QuixBugs/java_programs/DEPTH_FIRST_SEARCH.java |
quixbugs-java_data_25 | package java_programs;
import java.util.*;
public class Node {
private String value;
private ArrayList<Node> successors;
private ArrayList<Node> predecessors;
private Node successor;
public Node() {
this.successor = null;
this.successors = new ArrayList<Node>();
this.predecessors = new ArrayList<Node>();
this.value = null;
}
public Node(String value) {
this.value = value;
this.successor = null;
this.successors = new ArrayList<>();
this.predecessors = new ArrayList<>();
}
public Node(String value, Node successor) {
this.value = value;
this.successor = successor;
}
public Node(String value, ArrayList<Node> successors) {
this.value = value;
this.successors = successors;
}
public Node(String value, ArrayList<Node> predecessors, ArrayList<Node> successors) {
this.value = value;
this.predecessors = predecessors;
this.successors = successors;
}
public String getValue() {
return value;
}
public void setSuccessor(Node successor) {
this.successor = successor;
}
public void setSuccessors(ArrayList<Node> successors) {
this.successors = successors;
}
public void setPredecessors(ArrayList<Node> predecessors) {
this.predecessors = predecessors;
}
public Node getSuccessor() {
return successor;
}
public ArrayList<Node> getSuccessors() {
return successors;
}
public ArrayList<Node> getPredecessors() {
return predecessors;
}
}
| QuixBugs/QuixBugs/java_programs/Node.java |
quixbugs-java_data_26 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class MERGESORT {
public static ArrayList<Integer> merge(ArrayList<Integer> left, ArrayList<Integer> right) {
//System.out.println(String.valueOf(left));
//System.out.println(String.valueOf(right));
//System.out.println(String.valueOf(left.getClass()));
//System.out.println(String.valueOf(left.get(0)));
//System.out.println(String.valueOf(left.get(0).getClass()));
ArrayList<Integer> result = new ArrayList<Integer>(100);
int i = 0;
int j = 0;
while (i < left.size() && j < right.size()) {
if (left.get(i) <= right.get(j)) {
result.add(left.get(i));
i++;
} else {
result.add(right.get(j));
j++;
}
}
result.addAll(left.subList(i,left.size()).isEmpty() ? right.subList(j, right.size()) : left.subList(i, left.size()));
return result;
}
public static ArrayList<Integer> mergesort(ArrayList<Integer> arr) {
if (arr.size() == 0) { // <= 1 in correct version
return arr;
} else {
int middle = arr.size() / 2;
ArrayList<Integer> left = new ArrayList<Integer>(100);
left.addAll(arr.subList(0,middle));
left = mergesort(left);
ArrayList<Integer> right = new ArrayList<Integer>(100);
right.addAll(arr.subList(middle, arr.size()));
right = mergesort(right);
return merge(left, right);
}
}
}
| QuixBugs/QuixBugs/java_programs/MERGESORT.java |
quixbugs-java_data_27 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class KHEAPSORT {
// import heapq
// heap is data structure used for priority queue
// pq O(log n) to pull off lowest priority item
// heap is a type of binary tree
// every node its value smaller than everything below it
// priority queue in java is least-value first (at head)
public static ArrayList<Integer> kheapsort(ArrayList<Integer> arr, int k) {
PriorityQueue<Integer> heap = new PriorityQueue<Integer>();
for (Integer v : arr.subList(0,k)) {
heap.add(v);
}
ArrayList<Integer> output = new ArrayList<Integer>();
for (Integer x : arr) {
heap.add(x);
Integer popped = heap.poll();
output.add(popped);
}
while (!heap.isEmpty()) {
output.add(heap.poll());
}
return output;
}
}
| QuixBugs/QuixBugs/java_programs/KHEAPSORT.java |
quixbugs-java_data_28 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class WRAP {
public static void main(String[] args) {
System.out.println("abc".lastIndexOf("c",30));
}
public static ArrayList<String> wrap(String text, int cols) {
ArrayList<String> lines = new ArrayList<String>();
String line;
while (text.length() > cols) {
int end = text.lastIndexOf(" ", cols); // off by one?
if (end == -1) {
end = cols;
}
line = text.substring(0,end);
text = text.substring(end);
lines.add(line);
}
return lines;
}
}
| QuixBugs/QuixBugs/java_programs/WRAP.java |
quixbugs-java_data_29 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class SIEVE {
public static boolean all(ArrayList<Boolean> arr) {
for (boolean value : arr) {
if (!value) { return false; }
}
return true;
}
public static boolean any(ArrayList<Boolean> arr) {
for (boolean value: arr) {
if (value) { return true; }
}
return false;
}
public static ArrayList<Boolean> list_comp(int n, ArrayList<Integer> primes) {
ArrayList<Boolean> built_comprehension = new ArrayList<Boolean>();
for (Integer p : primes) {
built_comprehension.add(n % p > 0);
}
return built_comprehension;
}
public static ArrayList<Integer> sieve(Integer max) {
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int n=2; n<max+1; n++) {
if (any(list_comp(n, primes))) {
primes.add(n);
}
}
return primes;
}
}
| QuixBugs/QuixBugs/java_programs/SIEVE.java |
quixbugs-java_data_30 | package java_programs;
import java.util.*;
/**
* Minimum spanning tree
*/
public class MINIMUM_SPANNING_TREE {
public static Set<WeightedEdge> minimum_spanning_tree(List<WeightedEdge> weightedEdges) {
Map<Node,Set<Node>> groupByNode = new HashMap<>();
Set<WeightedEdge> minSpanningTree = new HashSet<>();
Collections.sort(weightedEdges);
for (WeightedEdge edge : weightedEdges) {
Node vertex_u = edge.node1;
Node vertex_v = edge.node2;
//System.out.printf("u: %s, v: %s weight: %d\n", vertex_u.getValue(), vertex_v.getValue(), edge.weight);
if (!groupByNode.containsKey(vertex_u)){
groupByNode.put(vertex_u, new HashSet<>(Arrays.asList(vertex_u)));
}
if (!groupByNode.containsKey(vertex_v)){
groupByNode.put(vertex_v, new HashSet<>(Arrays.asList(vertex_v)));
}
if (groupByNode.get(vertex_u) != groupByNode.get(vertex_v)) {
minSpanningTree.add(edge);
groupByNode = update(groupByNode, vertex_u, vertex_v);
for (Node node : groupByNode.get(vertex_v)) {
groupByNode = update(groupByNode, node, vertex_u);
}
}
}
return minSpanningTree;
}
public static Map<Node,Set<Node>> update(Map<Node,Set<Node>> groupByNode, Node vertex_u, Node vertex_v) {
Set<Node> vertex_u_span = groupByNode.get(vertex_u);
vertex_u_span.addAll(groupByNode.get(vertex_v));
return groupByNode;
}
}
| QuixBugs/QuixBugs/java_programs/MINIMUM_SPANNING_TREE.java |
quixbugs-java_data_31 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class SQRT {
public static double sqrt(double x, double epsilon) {
double approx = x / 2d;
while (Math.abs(x-approx) > epsilon) {
approx = 0.5d * (approx + x / approx);
}
return approx;
}
}
| QuixBugs/QuixBugs/java_programs/SQRT.java |
quixbugs-java_data_32 | package java_programs;
import java.util.*;
public class WeightedEdge implements Comparable<WeightedEdge>{
public Node node1;
public Node node2;
public int weight;
public WeightedEdge () {
node1 = null;
node2 = null;
weight = 0;
}
public WeightedEdge (Node node1, Node node2, int weight) {
this.node1 = node1;
this.node2 = node2;
this.weight = weight;
}
public int compareTo(WeightedEdge compareNode) {
int compareWeight= ((WeightedEdge) compareNode).weight;
//ascending order
return this.weight - compareWeight;
//descending order
//return compareWeight - this.weight;
}
} | QuixBugs/QuixBugs/java_programs/WeightedEdge.java |
quixbugs-java_data_33 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class LIS {
public static int lis(int[] arr) {
Map<Integer,Integer> ends = new HashMap<Integer, Integer>(100);
int longest = 0;
int i = 0;
for (int val : arr) {
ArrayList<Integer> prefix_lengths = new ArrayList<Integer>(100);
for (int j=1; j < longest+1; j++) {
if (arr[ends.get(j)] < val) {
prefix_lengths.add(j);
}
}
int length = !prefix_lengths.isEmpty() ? Collections.max(prefix_lengths) : 0;
if (length == longest || val < arr[ends.get(length+1)]) {
ends.put(length+1, i);
longest = length + 1;
}
i++;
}
return longest;
}
}
| QuixBugs/QuixBugs/java_programs/LIS.java |
quixbugs-java_data_34 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class TO_BASE {
public static String to_base(int num, int b) {
String result = "";
String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
while (num > 0) {
i = num % b;
num = num / b; // floor division?
result = result + String.valueOf(alphabet.charAt(i));
}
return result;
}
}
| QuixBugs/QuixBugs/java_programs/TO_BASE.java |
quixbugs-java_data_35 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class FLATTEN {
public static Object flatten(Object arr) {
if (arr instanceof ArrayList) {
ArrayList narr = (ArrayList) arr;
ArrayList result = new ArrayList(50);
for (Object x : narr) {
if (x instanceof ArrayList) {
result.addAll((ArrayList) flatten(x));
} else {
result.add(flatten(x));
}
}
return result;
} else {
return flatten(arr);
}
}
}
| QuixBugs/QuixBugs/java_programs/FLATTEN.java |
quixbugs-java_data_36 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class SUBSEQUENCES {
public static ArrayList<ArrayList> subsequences(int a, int b, int k) {
if (k == 0) {
return new ArrayList();
}
ArrayList ret = new ArrayList(50);
for (int i=a; i<b+1-k; i++) {
ArrayList base = new ArrayList(50);
for (ArrayList rest : subsequences(i+1, b, k-1)) {
rest.add(0,i);
base.add(rest);
}
ret.addAll(base);
}
return ret;
}
}
| QuixBugs/QuixBugs/java_programs/SUBSEQUENCES.java |
quixbugs-java_data_37 | package java_programs;
import java.util.*;
//import com.google.guava.Lists;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class NEXT_PERMUTATION {
public static ArrayList<Integer> next_permutation(ArrayList<Integer> perm) {
for (int i=perm.size()-2; i!=-1; i--) {
if (perm.get(i) < perm.get(i+1)) {
for (int j=perm.size()-1; j!=i; j--) {
if (perm.get(j) < perm.get(i)) {
ArrayList<Integer> next_perm = perm;
int temp_j = perm.get(j);
int temp_i = perm.get(i);
next_perm.set(i,temp_j);
next_perm.set(j,temp_i);
ArrayList<Integer> reversed = new ArrayList<Integer>(100);
for (int k=next_perm.size()-1; k!=i; k--) {
reversed.add(next_perm.get(k));
}
int q = i + 1;
for (Integer replace : reversed) {
next_perm.set(q, replace);
q++;
}
return next_perm;
}
}
}
}
return new ArrayList<Integer>();
}
}
| QuixBugs/QuixBugs/java_programs/NEXT_PERMUTATION.java |
quixbugs-java_data_38 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class POWERSET {
public static ArrayList<ArrayList> powerset(ArrayList arr) {
if (!arr.isEmpty()) {
Object first = arr.get(0);
arr.remove(0);
ArrayList rest = arr;
ArrayList<ArrayList> rest_subsets = powerset(rest);
ArrayList<ArrayList> output = new ArrayList<ArrayList>(100);
ArrayList to_add = new ArrayList(100);
to_add.add(first);
for (ArrayList subset : rest_subsets) {
to_add.addAll(subset);
}
output.add(to_add);
return output;
} else {
ArrayList empty_set = new ArrayList<ArrayList>();
empty_set.add(new ArrayList());
return empty_set;
}
}
}
| QuixBugs/QuixBugs/java_programs/POWERSET.java |
quixbugs-java_data_39 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class BUCKETSORT {
public static ArrayList<Integer> bucketsort(ArrayList<Integer> arr, int k) {
ArrayList<Integer> counts = new ArrayList<Integer>(Collections.nCopies(k,0));
for (Integer x : arr) {
counts.set(x,counts.get(x)+1);
}
ArrayList<Integer> sorted_arr = new ArrayList<Integer>(100);
int i = 0;
for (Integer count : arr) { // arr is counts in fixed version
sorted_arr.addAll(Collections.nCopies(count, i));
i++;
}
return sorted_arr;
}
}
| QuixBugs/QuixBugs/java_programs/BUCKETSORT.java |
quixbugs-java_data_40 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class DETECT_CYCLE {
public static boolean detect_cycle(Node node) {
Node hare = node;
Node tortoise = node;
while (true) {
if (hare.getSuccessor() == null)
return false;
tortoise = tortoise.getSuccessor();
hare = hare.getSuccessor().getSuccessor();
if (hare == tortoise)
return true;
}
}
}
| QuixBugs/QuixBugs/java_programs/DETECT_CYCLE.java |
quixbugs-java_data_41 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class FIND_FIRST_IN_SORTED {
public static int find_first_in_sorted(int[] arr, int x) {
int lo = 0;
int hi = arr.length;
while (lo <= hi) {
int mid = (lo + hi) / 2; // check if this is floor division
if (x == arr[mid] && (mid == 0 || x != arr[mid-1])) {
return mid;
} else if (x <= arr[mid]) {
hi = mid;
} else {
lo = mid + 1;
}
}
return -1;
}
}
| QuixBugs/QuixBugs/java_programs/FIND_FIRST_IN_SORTED.java |
quixbugs-java_data_42 | package java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class QUICKSORT {
public static ArrayList<Integer> quicksort(ArrayList<Integer> arr) {
if (arr.isEmpty()) {
return new ArrayList<Integer>();
}
Integer pivot = arr.get(0);
ArrayList<Integer> lesser = new ArrayList<Integer>();
ArrayList<Integer> greater = new ArrayList<Integer>();
for (Integer x : arr.subList(1, arr.size())) {
if (x < pivot) {
lesser.add(x);
} else if (x > pivot) {
greater.add(x);
}
}
ArrayList<Integer> middle = new ArrayList<Integer>();
middle.add(pivot);
lesser = quicksort(lesser);
greater = quicksort(greater);
middle.addAll(greater);
lesser.addAll(middle);
return lesser;
}
}
| QuixBugs/QuixBugs/java_programs/QUICKSORT.java |
quixbugs-java_data_43 | package correct_java_programs;
import java.util.*;
import java.lang.Math.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class NEXT_PALINDROME {
public static String next_palindrome(int[] digit_list) {
int high_mid = Math.floorDiv(digit_list.length, 2);
int low_mid = Math.floorDiv(digit_list.length - 1, 2);
while (high_mid < digit_list.length && low_mid >= 0) {
if (digit_list[high_mid] == 9) {
digit_list[high_mid] = 0;
digit_list[low_mid] = 0;
high_mid += 1;
low_mid -= 1;
} else {
digit_list[high_mid] += 1;
if (low_mid != high_mid) {
digit_list[low_mid] += 1;
}
return Arrays.toString(digit_list);
}
}
ArrayList<Integer> otherwise = new ArrayList<Integer>();
otherwise.add(1);
otherwise.addAll(Collections.nCopies(digit_list.length-1, 0));
otherwise.add(1);
return String.valueOf(otherwise);
}
}
| QuixBugs/QuixBugs/correct_java_programs/NEXT_PALINDROME.java |
quixbugs-java_data_44 | package correct_java_programs;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class FIND_IN_SORTED {
public static int binsearch(int[] arr, int x, int start, int end) {
if (start == end) {
return -1;
}
int mid = start + (end - start) / 2; // check this is floor division
if (x < arr[mid]) {
return binsearch(arr, x, start, mid);
} else if (x > arr[mid]) {
return binsearch(arr, x, mid+1, end);
} else {
return mid;
}
}
public static int find_in_sorted(int[] arr, int x) {
return binsearch(arr, x, 0, arr.length);
}
}
| QuixBugs/QuixBugs/correct_java_programs/FIND_IN_SORTED.java |
quixbugs-java_data_45 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class LEVENSHTEIN {
public static int levenshtein(String source, String target) {
if (source.isEmpty() || target.isEmpty()) {
return source.isEmpty() ? target.length() : source.length();
} else if (source.charAt(0) == target.charAt(0)) {
return levenshtein(source.substring(1), target.substring(1));
} else {
return 1 + Math.min(Math.min(
levenshtein(source, target.substring(1)),
levenshtein(source.substring(1), target.substring(1))),
levenshtein(source.substring(1), target)
);
}
}
}
| QuixBugs/QuixBugs/correct_java_programs/LEVENSHTEIN.java |
quixbugs-java_data_46 | package correct_java_programs;
import java.util.*;
import java.util.function.BinaryOperator;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class RPN_EVAL {
public static Double rpn_eval(ArrayList tokens) {
Map<String, BinaryOperator<Double>> op = new HashMap<String, BinaryOperator<Double>>();
op.put("+", (a, b) -> a + b);
op.put("-", (a, b) -> a - b);
op.put("*", (a, b) -> a * b);
op.put("/", (a, b) -> a / b);
Stack stack = new Stack();
for (Object token : tokens) {
if (Double.class.isInstance(token)) {
stack.push((Double) token);
} else {
token = (String) token;
Double a = (Double) stack.pop();
Double b = (Double) stack.pop();
Double c = 0.0;
BinaryOperator<Double> bin_op = op.get(token);
c = bin_op.apply(b,a);
stack.push(c);
}
}
return (Double) stack.pop();
}
}
| QuixBugs/QuixBugs/correct_java_programs/RPN_EVAL.java |
quixbugs-java_data_47 | package correct_java_programs;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class BITCOUNT {
public static int bitcount(int n) {
int count = 0;
while (n != 0) {
n = (n & (n - 1));
count++;
}
return count;
}
}
| QuixBugs/QuixBugs/correct_java_programs/BITCOUNT.java |
quixbugs-java_data_48 | package correct_java_programs;
import java.util.*;
import java.lang.Math.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Angela Chen
*/
public class SHORTEST_PATH_LENGTHS {
// Define Infinite as a large enough value. This value will be used
// for vertices not connected to each other
final static int INF = 99999;
public static Map<List<Integer>,Integer> shortest_path_lengths(int numNodes, Map<List<Integer>,Integer> length_by_edge) {
Map<List<Integer>,Integer> length_by_path = new HashMap<>();
for (int i = 0; i < numNodes; i++) {
for (int j =0; j < numNodes; j++) {
List<Integer> edge = new ArrayList<>(Arrays.asList(i,j));
if (i == j) {
length_by_path.put(edge, 0);
}
else if (length_by_edge.containsKey(edge) ) {
length_by_path.put(edge, length_by_edge.get(edge));
} else {
length_by_path.put(edge, INF);
}
}
}
for (int k = 0; k < numNodes; k++) {
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
int update_length = Math.min(length_by_path.get(Arrays.asList(i,j)),
sumLengths(length_by_path.get(Arrays.asList(i,k)),
length_by_path.get(Arrays.asList(k,j))));
length_by_path.put(Arrays.asList(i,j), update_length);
}
}
}
return length_by_path;
}
static private int sumLengths(int a, int b) {
if(a == INF || b == INF) {
return INF;
}
return a + b;
}
}
| QuixBugs/QuixBugs/correct_java_programs/SHORTEST_PATH_LENGTHS.java |
quixbugs-java_data_49 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class LONGEST_COMMON_SUBSEQUENCE {
public static String longest_common_subsequence(String a, String b) {
if (a.isEmpty() || b.isEmpty()) {
return "";
} else if (a.charAt(0) == b.charAt(0)) {
return a.charAt(0) + longest_common_subsequence(a.substring(1), b.substring(1));
} else {
String fst = longest_common_subsequence(a, b.substring(1));
String snd = longest_common_subsequence(a.substring(1), b);
return fst.length() >= snd.length() ? fst : snd;
}
}
}
| QuixBugs/QuixBugs/correct_java_programs/LONGEST_COMMON_SUBSEQUENCE.java |
quixbugs-java_data_50 | package correct_java_programs;
import java.util.*;
import java_programs.Node;
/**
*
* @author Angela Chen
*/
public class SHORTEST_PATH_LENGTH {
public static int shortest_path_length(Map<List<Node>, Integer> length_by_edge, Node startnode, Node goalnode) {
int n = length_by_edge.size();
// the shortest distance from source to each node
Map<Node, Integer> unvisitedNodes = new HashMap<>();
Set<Node> visitedNodes = new HashSet<>();
unvisitedNodes.put(startnode, 0);
while (!unvisitedNodes.isEmpty()) {
Node node = getNodeWithMinDistance(unvisitedNodes);
int distance = unvisitedNodes.get(node);
unvisitedNodes.remove(node);
if (node.getValue() == goalnode.getValue()) {
return distance;
}
visitedNodes.add(node);
for (Node nextnode : node.getSuccessors()) {
if (visitedNodes.contains(nextnode)) {
continue;
}
if (unvisitedNodes.get(nextnode) == null) {
unvisitedNodes.put(nextnode, Integer.MAX_VALUE);
}
unvisitedNodes.put(nextnode, Math.min(unvisitedNodes.get(nextnode),
distance + length_by_edge.get(Arrays.asList(node, nextnode))));
}
}
return Integer.MAX_VALUE;
}
public static Node getNodeWithMinDistance(Map<Node,Integer> list) {
Node minNode = null;
int minDistance = Integer.MAX_VALUE;
for (Node node : list.keySet()) {
int distance = list.get(node);
if (distance < minDistance) {
minDistance = distance;
minNode = node;
}
}
return minNode;
}
} | QuixBugs/QuixBugs/correct_java_programs/SHORTEST_PATH_LENGTH.java |
quixbugs-java_data_51 | package correct_java_programs;
import java.util.*;
import java_programs.Node;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class REVERSE_LINKED_LIST {
public static Node reverse_linked_list(Node node) {
Node prevnode = null;
Node nextnode;
while (node != null) {
nextnode = node.getSuccessor();
node.setSuccessor(prevnode);
prevnode = node;
node = nextnode;
}
return prevnode;
}
}
| QuixBugs/QuixBugs/correct_java_programs/REVERSE_LINKED_LIST.java |
quixbugs-java_data_52 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class LCS_LENGTH {
public static Integer lcs_length(String s, String t) {
// make a Counter
// pair? no! just hashtable to a hashtable.. woo.. currying
Map<Integer, Map<Integer,Integer>> dp = new HashMap<Integer,Map<Integer,Integer>>();
// just set all the internal maps to 0
for (int i=0; i < s.length(); i++) {
Map<Integer,Integer> initialize = new HashMap<Integer,Integer>();
dp.put(i, initialize);
for (int j=0; j < t.length(); j++) {
Map<Integer,Integer> internal_map = dp.get(i);
internal_map.put(j,0);
dp.put(i, internal_map);
}
}
// now the actual code
for (int i=0; i < s.length(); i++) {
for (int j=0; j < t.length(); j++) {
if (s.charAt(i) == t.charAt(j)) {
// dp.get(i-1).containsKey(j-1)
if (dp.containsKey(i-1)&&dp.get(i-1).containsKey(j-1)) {
Map<Integer, Integer> internal_map = dp.get(i);
int insert_value = dp.get(i-1).get(j-1) + 1;
internal_map.put(j, insert_value);
dp.put(i,internal_map);
} else {
Map<Integer, Integer> internal_map = dp.get(i);
internal_map.put(j,1);
dp.put(i,internal_map);
}
}
}
}
if (!dp.isEmpty()) {
List<Integer> ret_list = new ArrayList<Integer>();
for (int i=0; i<s.length(); i++) {
ret_list.add(!dp.get(i).isEmpty() ? Collections.max(dp.get(i).values()) : 0);
}
return Collections.max(ret_list);
} else {
return 0;
}
}
}
| QuixBugs/QuixBugs/correct_java_programs/LCS_LENGTH.java |
quixbugs-java_data_53 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class PASCAL {
public static ArrayList<ArrayList<Integer>> pascal(int n) {
ArrayList<ArrayList<Integer>> rows = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> init = new ArrayList<Integer>();
init.add(1);
rows.add(init);
for (int r=1; r<n; r++) {
ArrayList<Integer> row = new ArrayList<Integer>();
for (int c=0; c<r+1; c++) {
int upleft, upright;
if (c > 0) {
upleft = rows.get(r-1).get(c-1);
} else {
upleft = 0;
}
if (c < r) {
upright = rows.get(r-1).get(c);
} else {
upright = 0;
}
row.add(upleft+upright);
}
rows.add(row);
}
return rows;
}
}
| QuixBugs/QuixBugs/correct_java_programs/PASCAL.java |
quixbugs-java_data_54 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class IS_VALID_PARENTHESIZATION {
public static Boolean is_valid_parenthesization(String parens) {
int depth = 0;
for (int i = 0; i < parens.length(); i++) {
Character paren = parens.charAt(i);
if (paren.equals('(')) {
depth++;
} else {
depth--;
if (depth < 0) { return false; }
}
}
return depth==0;
}
}
| QuixBugs/QuixBugs/correct_java_programs/IS_VALID_PARENTHESIZATION.java |
quixbugs-java_data_55 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class MAX_SUBLIST_SUM {
public static int max_sublist_sum(int[] arr) {
int max_ending_here = 0;
int max_so_far = 0;
for (int x : arr) {
max_ending_here = Math.max(0,max_ending_here + x);
max_so_far = Math.max(max_so_far, max_ending_here);
}
return max_so_far;
}
}
| QuixBugs/QuixBugs/correct_java_programs/MAX_SUBLIST_SUM.java |
quixbugs-java_data_56 | package correct_java_programs;
import java.util.*;
import java.lang.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class KNAPSACK {
public static int knapsack(int capacity, int [][] items) {
int weight = 0, value = 0;
int n = items.length;
int memo[][] = new int[n + 1][capacity + 1];
for (int i = 0; i <= n ; i++)
{
if (i - 1 >= 0) {
weight = items[i - 1][0];
value = items[i - 1][1];
}
for (int j = 0; j <= capacity; j++)
{
if (i == 0 || j == 0) {
memo[i][j] = 0;
}
else if (weight <= j) {
memo[i][j] = Math.max(memo[i - 1][j], value + memo[i - 1][j - weight]);
}
else {
memo[i][j] = memo [i-1][j];
}
}
}
return memo[n][capacity];
}
}
| QuixBugs/QuixBugs/correct_java_programs/KNAPSACK.java |
quixbugs-java_data_57 | package correct_java_programs;
import java.util.*;
import java_programs.Node;
public class TOPOLOGICAL_ORDERING {
public static ArrayList<Node> topological_ordering (List<Node> directedGraph) {
ArrayList<Node> orderedNodes = new ArrayList<Node>();
for (Node node : directedGraph) {
if (node.getPredecessors().isEmpty()) {
orderedNodes.add(node);
}
}
int listSize = orderedNodes.size();
for (int i = 0; i < listSize; i++) {
Node node = orderedNodes.get(i);
for (Node nextNode : node.getSuccessors()) {
if (orderedNodes.containsAll(nextNode.getPredecessors()) && !orderedNodes.contains(nextNode)) {
orderedNodes.add(nextNode);
listSize++;
}
}
}
return orderedNodes;
}
}
| QuixBugs/QuixBugs/correct_java_programs/TOPOLOGICAL_ORDERING.java |
quixbugs-java_data_58 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class POSSIBLE_CHANGE {
public static int possible_change(int[] coins, int total) {
if (total == 0) {
return 1;
}
if (total < 0 ||coins.length==0) {
return 0;
}
int first = coins[0];
int[] rest = Arrays.copyOfRange(coins, 1, coins.length);
return possible_change(coins, total-first) + possible_change(rest, total);
}
}
| QuixBugs/QuixBugs/correct_java_programs/POSSIBLE_CHANGE.java |
quixbugs-java_data_59 | package correct_java_programs;
import java.util.*;
import java.util.ArrayDeque;
import java_programs.Node;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class BREADTH_FIRST_SEARCH {
public static Set<Node> nodesvisited = new HashSet<>();
public static boolean breadth_first_search(Node startnode, Node goalnode) {
Deque<Node> queue = new ArrayDeque<>();
queue.addLast(startnode);
nodesvisited.add(startnode);
while (!queue.isEmpty()) {
Node node = queue.removeFirst();
if (node == goalnode) {
return true;
} else {
for (Node successor_node : node.getSuccessors()) {
if (!nodesvisited.contains(successor_node)) {
queue.addFirst(successor_node);
nodesvisited.add(successor_node);
}
}
}
}
/**
* The buggy program always drops into while(true) loop and will not return false
* Removed below line to fix compilation error
*/
return false;
}
} | QuixBugs/QuixBugs/correct_java_programs/BREADTH_FIRST_SEARCH.java |
quixbugs-java_data_60 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class HANOI {
// default start=1, end=3
public static List<Pair<Integer,Integer>> hanoi(int height, int start, int end) {
ArrayList<Pair<Integer,Integer>> steps = new ArrayList<Pair<Integer,Integer>>();
if (height > 0) {
PriorityQueue<Integer> crap_set = new PriorityQueue<Integer>();
crap_set.add(1);
crap_set.add(2);
crap_set.add(3);
crap_set.remove(start);
crap_set.remove(end);
int helper = crap_set.poll();
steps.addAll(hanoi(height-1, start, helper));
steps.add(new Pair<Integer,Integer>(start, end));
steps.addAll(hanoi(height-1, helper, end));
}
return steps;
}
public static class Pair<F, S> {
private F first; //first member of pair
private S second; //second member of pair
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public void setFirst(F first) {
this.first = first;
}
public void setSecond(S second) {
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "(" + String.valueOf(first) + ", " + String.valueOf(second) + ")";
}
}
}
| QuixBugs/QuixBugs/correct_java_programs/HANOI.java |
quixbugs-java_data_61 | package correct_java_programs;
import java.util.*;
import java_programs.Node;
import java_programs.WeightedEdge;
import java.lang.Math.*;
/**
*
* @author Angela Chen
*/
public class SHORTEST_PATHS {
// Define Infinite as a large enough value. This value will be used
// for vertices not connected to each other
final static int INF = 99999;
public static Map<String, Integer> shortest_paths(String source, Map<List<String>,Integer> weight_by_edge) {
Map<String,Integer> weight_by_node = new HashMap<String,Integer>();
for (List<String> edge : weight_by_edge.keySet()) {
weight_by_node.put(edge.get(1), INF);
weight_by_node.put(edge.get(0), INF);
}
weight_by_node.put(source, 0);
for (int i = 0; i < weight_by_node.size(); i++) {
for (List<String> edge : weight_by_edge.keySet()) {
int update_weight = Math.min(
weight_by_node.get(edge.get(0))
+ weight_by_edge.get(edge),
weight_by_node.get(edge.get(1)));
weight_by_node.put(edge.get(1), update_weight);
}
}
return weight_by_node;
}
}
| QuixBugs/QuixBugs/correct_java_programs/SHORTEST_PATHS.java |
quixbugs-java_data_62 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class KTH {
public static Integer kth(ArrayList<Integer> arr, int k) {
int pivot = arr.get(0);
ArrayList<Integer> below, above;
below = new ArrayList<Integer>(arr.size());
above = new ArrayList<Integer>(arr.size());
for (Integer x : arr) {
if (x < pivot) {
below.add(x);
} else if (x > pivot) {
above.add(x);
}
}
int num_less = below.size();
int num_lessoreq = arr.size() - above.size();
if (k < num_less) {
return kth(below, k);
} else if (k >= num_lessoreq) {
return kth(above, k-num_lessoreq);
} else {
return pivot;
}
}
}
| QuixBugs/QuixBugs/correct_java_programs/KTH.java |
quixbugs-java_data_63 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class GCD {
public static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a%b);
}
}
}
| QuixBugs/QuixBugs/correct_java_programs/GCD.java |
quixbugs-java_data_64 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class SHUNTING_YARD {
public static List shunting_yard(ArrayList tokens) {
Map<String, Integer> precedence = new HashMap<String, Integer>();
precedence.put("+", 1);
precedence.put("-", 1);
precedence.put("*", 2);
precedence.put("/", 2);
ArrayList rpntokens = new ArrayList(100);
ArrayDeque opstack = new ArrayDeque();
for (Object token : tokens) {
if (Integer.class.isInstance(token)) {
// cover also Double case I guess?
rpntokens.add((Integer) token);
} else {
String operator = (String) token;
while (!opstack.isEmpty() && precedence.get(operator) <= precedence.get(opstack.getLast())) {
rpntokens.add(opstack.pop());
}
opstack.push(token);
}
}
while (!opstack.isEmpty()) {
rpntokens.add(opstack.pop());
}
return rpntokens;
}
}
| QuixBugs/QuixBugs/correct_java_programs/SHUNTING_YARD.java |
quixbugs-java_data_65 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class GET_FACTORS {
public static ArrayList<Integer> get_factors(int n) {
if (n == 1) {
return new ArrayList<Integer>();
}
int max = (int)(Math.sqrt(n) + 1.0);
for (int i=2; i < max; i++) {
if (n % i == 0) {
ArrayList<Integer> prepend = new ArrayList<Integer>(0);
prepend.add(i);
prepend.addAll(get_factors(n / i));
return prepend;
}
}
return new ArrayList<Integer>(Arrays.asList(n));
}
}
| QuixBugs/QuixBugs/correct_java_programs/GET_FACTORS.java |
quixbugs-java_data_66 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java_programs.Node;
/**
*
* @author derricklin
*/
public class DEPTH_FIRST_SEARCH {
public static boolean depth_first_search(Node startnode, Node goalnode) {
Set<Node> nodesvisited = new HashSet<>();
class Search {
boolean search(Node node) {
if (nodesvisited.contains(node)) {
return false;
} else if (node == goalnode) {
return true;
} else {
nodesvisited.add(node);
for (Node successornodes : node.getSuccessors()) {
if (search(successornodes)) { return true; }
}
}
return false;
}
};
Search s = new Search();
return s.search(startnode);
}
}
| QuixBugs/QuixBugs/correct_java_programs/DEPTH_FIRST_SEARCH.java |
quixbugs-java_data_67 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class MERGESORT {
public static ArrayList<Integer> merge(ArrayList<Integer> left, ArrayList<Integer> right) {
//System.out.println(String.valueOf(left));
//System.out.println(String.valueOf(right));
//System.out.println(String.valueOf(left.getClass()));
//System.out.println(String.valueOf(left.get(0)));
//System.out.println(String.valueOf(left.get(0).getClass()));
ArrayList<Integer> result = new ArrayList<Integer>(100);
int i = 0;
int j = 0;
while (i < left.size() && j < right.size()) {
if (left.get(i) <= right.get(j)) {
result.add(left.get(i));
i++;
} else {
result.add(right.get(j));
j++;
}
}
result.addAll(left.subList(i,left.size()).isEmpty() ? right.subList(j, right.size()) : left.subList(i, left.size()));
return result;
}
public static ArrayList<Integer> mergesort(ArrayList<Integer> arr) {
if (arr.size() <= 1) { // <= 1 in correct version
return arr;
} else {
int middle = arr.size() / 2;
ArrayList<Integer> left = new ArrayList<Integer>(100);
left.addAll(arr.subList(0,middle));
left = mergesort(left);
ArrayList<Integer> right = new ArrayList<Integer>(100);
right.addAll(arr.subList(middle, arr.size()));
right = mergesort(right);
return merge(left, right);
}
}
}
| QuixBugs/QuixBugs/correct_java_programs/MERGESORT.java |
quixbugs-java_data_68 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class KHEAPSORT {
// import heapq
// heap is data structure used for priority queue
// pq O(log n) to pull off lowest priority item
// heap is a type of binary tree
// every node its value smaller than everything below it
// priority queue in java is least-value first (at head)
public static ArrayList<Integer> kheapsort(ArrayList<Integer> arr, int k) {
PriorityQueue<Integer> heap = new PriorityQueue<Integer>();
for (Integer v : arr.subList(0,k)) {
heap.add(v);
}
ArrayList<Integer> output = new ArrayList<Integer>();
for (Integer x : arr.subList(k, arr.size())) {
heap.add(x);
Integer popped = heap.poll();
output.add(popped);
}
while (!heap.isEmpty()) {
output.add(heap.poll());
}
return output;
}
}
| QuixBugs/QuixBugs/correct_java_programs/KHEAPSORT.java |
quixbugs-java_data_69 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class WRAP {
public static void main(String[] args) {
System.out.println("abc".lastIndexOf("c",30));
}
public static ArrayList<String> wrap(String text, int cols) {
ArrayList<String> lines = new ArrayList<String>();
String line;
while (text.length() > cols) {
int end = text.lastIndexOf(" ", cols); // off by one?
if (end == -1) {
end = cols;
}
line = text.substring(0,end);
text = text.substring(end);
lines.add(line);
}
lines.add(text);
return lines;
}
}
| QuixBugs/QuixBugs/correct_java_programs/WRAP.java |
quixbugs-java_data_70 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class SIEVE {
public static boolean all(ArrayList<Boolean> arr) {
for (boolean value : arr) {
if (!value) { return false; }
}
return true;
}
public static boolean any(ArrayList<Boolean> arr) {
for (boolean value: arr) {
if (value) { return true; }
}
return false;
}
public static ArrayList<Boolean> list_comp(int n, ArrayList<Integer> primes) {
ArrayList<Boolean> built_comprehension = new ArrayList<Boolean>();
for (Integer p : primes) {
built_comprehension.add(n % p > 0);
}
return built_comprehension;
}
public static ArrayList<Integer> sieve(Integer max) {
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int n=2; n<max+1; n++) {
if (all(list_comp(n, primes))) {
primes.add(n);
}
}
return primes;
}
}
| QuixBugs/QuixBugs/correct_java_programs/SIEVE.java |
quixbugs-java_data_71 | package correct_java_programs;
import java.util.*;
import java_programs.Node;
import java_programs.WeightedEdge;
/**
* Minimum spanning tree
*/
public class MINIMUM_SPANNING_TREE {
public static Set<WeightedEdge> minimum_spanning_tree(List<WeightedEdge> weightedEdges) {
Map<Node,Set<Node>> groupByNode = new HashMap<>();
Set<WeightedEdge> minSpanningTree = new HashSet<>();
Collections.sort(weightedEdges);
for (WeightedEdge edge : weightedEdges) {
Node vertex_u = edge.node1;
Node vertex_v = edge.node2;
//System.out.printf("u: %s, v: %s weight: %d\n", vertex_u.getValue(), vertex_v.getValue(), edge.weight);
if (!groupByNode.containsKey(vertex_u)){
groupByNode.put(vertex_u, new HashSet<>(Arrays.asList(vertex_u)));
}
if (!groupByNode.containsKey(vertex_v)){
groupByNode.put(vertex_v, new HashSet<>(Arrays.asList(vertex_v)));
}
if (groupByNode.get(vertex_u) != groupByNode.get(vertex_v)) {
minSpanningTree.add(edge);
groupByNode = update(groupByNode, vertex_u, vertex_v);
for (Node node : groupByNode.get(vertex_v)) {
groupByNode.put(node, groupByNode.get(vertex_u));
}
}
}
return minSpanningTree;
}
public static Map<Node,Set<Node>> update(Map<Node,Set<Node>> groupByNode, Node vertex_u, Node vertex_v) {
Set<Node> vertex_u_span = groupByNode.get(vertex_u);
vertex_u_span.addAll(groupByNode.get(vertex_v));
return groupByNode;
}
}
| QuixBugs/QuixBugs/correct_java_programs/MINIMUM_SPANNING_TREE.java |
quixbugs-java_data_72 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class SQRT {
public static double sqrt(double x, double epsilon) {
double approx = x / 2d;
while (Math.abs(x-approx*approx) > epsilon) {
approx = 0.5d * (approx + x / approx);
}
return approx;
}
}
| QuixBugs/QuixBugs/correct_java_programs/SQRT.java |
quixbugs-java_data_73 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class LIS {
public static int lis(int[] arr) {
Map<Integer,Integer> ends = new HashMap<Integer, Integer>(100);
int longest = 0;
int i = 0;
for (int val : arr) {
ArrayList<Integer> prefix_lengths = new ArrayList<Integer>(100);
for (int j=1; j < longest+1; j++) {
if (arr[ends.get(j)] < val) {
prefix_lengths.add(j);
}
}
int length = !prefix_lengths.isEmpty() ? Collections.max(prefix_lengths) : 0;
if (length == longest || val < arr[ends.get(length+1)]) {
ends.put(length+1, i);
longest = Math.max(longest,length + 1);
}
i++;
}
return longest;
}
}
| QuixBugs/QuixBugs/correct_java_programs/LIS.java |
quixbugs-java_data_74 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class TO_BASE {
public static String to_base(int num, int b) {
String result = "";
String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
while (num > 0) {
i = num % b;
num = num / b; // floor division?
result = String.valueOf(alphabet.charAt(i))+result;
}
return result;
}
}
| QuixBugs/QuixBugs/correct_java_programs/TO_BASE.java |
quixbugs-java_data_75 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class FLATTEN {
public static Object flatten(Object arr) {
if (arr instanceof ArrayList) {
ArrayList narr = (ArrayList) arr;
ArrayList result = new ArrayList(50);
for (Object x : narr) {
if (x instanceof ArrayList) {
result.addAll((ArrayList) flatten(x));
} else {
result.add((x));
}
}
return result;
} else {
return arr;
}
}
}
| QuixBugs/QuixBugs/correct_java_programs/FLATTEN.java |
quixbugs-java_data_76 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class SUBSEQUENCES {
public static ArrayList<ArrayList> subsequences(int a, int b, int k) {
if (k == 0) {
ArrayList empty_set = new ArrayList<ArrayList>();
empty_set.add(new ArrayList());
return empty_set;
}
ArrayList ret = new ArrayList(50);
for (int i=a; i<b+1-k; i++) {
ArrayList base = new ArrayList(50);
for (ArrayList rest : subsequences(i+1, b, k-1)) {
rest.add(0,i);
base.add(rest);
}
ret.addAll(base);
}
return ret;
}
}
| QuixBugs/QuixBugs/correct_java_programs/SUBSEQUENCES.java |
quixbugs-java_data_77 | package correct_java_programs;
import java.util.*;
//import com.google.guava.Lists;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class NEXT_PERMUTATION {
public static ArrayList<Integer> next_permutation(ArrayList<Integer> perm) {
for (int i=perm.size()-2; i!=-1; i--) {
if (perm.get(i) < perm.get(i+1)) {
for (int j=perm.size()-1; j!=i; j--) {
if (perm.get(j) > perm.get(i)) {
ArrayList<Integer> next_perm = perm;
int temp_j = perm.get(j);
int temp_i = perm.get(i);
next_perm.set(i,temp_j);
next_perm.set(j,temp_i);
ArrayList<Integer> reversed = new ArrayList<Integer>(100);
for (int k=next_perm.size()-1; k!=i; k--) {
reversed.add(next_perm.get(k));
}
int q = i + 1;
for (Integer replace : reversed) {
next_perm.set(q, replace);
q++;
}
return next_perm;
}
}
}
}
return new ArrayList<Integer>();
}
}
| QuixBugs/QuixBugs/correct_java_programs/NEXT_PERMUTATION.java |
quixbugs-java_data_78 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class POWERSET {
public static ArrayList<ArrayList> powerset(ArrayList arr) {
if (!arr.isEmpty()) {
Object first = arr.get(0);
arr.remove(0);
ArrayList rest = arr;
ArrayList<ArrayList> rest_subsets = powerset(rest);
ArrayList<ArrayList> output = new ArrayList<ArrayList>(100);
ArrayList to_add = new ArrayList(100);
for (ArrayList subset : rest_subsets) {
ArrayList r = new ArrayList();
r.add(first);
r.addAll(subset);
to_add.add(r);
}
output.addAll(to_add);
rest_subsets.addAll(output);
return rest_subsets;
} else {
ArrayList empty_set = new ArrayList<ArrayList>();
empty_set.add(new ArrayList());
return empty_set;
}
}
}
| QuixBugs/QuixBugs/correct_java_programs/POWERSET.java |
quixbugs-java_data_79 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class BUCKETSORT {
public static ArrayList<Integer> bucketsort(ArrayList<Integer> arr, int k) {
ArrayList<Integer> counts = new ArrayList<Integer>(Collections.nCopies(k,0));
for (Integer x : arr) {
counts.set(x,counts.get(x)+1);
}
ArrayList<Integer> sorted_arr = new ArrayList<Integer>(100);
int i = 0;
for (Integer count : counts) {
sorted_arr.addAll(Collections.nCopies(count, i));
i++;
}
return sorted_arr;
}
}
| QuixBugs/QuixBugs/correct_java_programs/BUCKETSORT.java |
quixbugs-java_data_80 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java_programs.Node;
/**
*
* @author derricklin
*/
public class DETECT_CYCLE {
public static boolean detect_cycle(Node node) {
Node hare = node;
Node tortoise = node;
while (true) {
if (null==hare ||hare.getSuccessor() == null)
return false;
tortoise = tortoise.getSuccessor();
hare = hare.getSuccessor().getSuccessor();
if (hare == tortoise)
return true;
}
}
}
| QuixBugs/QuixBugs/correct_java_programs/DETECT_CYCLE.java |
quixbugs-java_data_81 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class FIND_FIRST_IN_SORTED {
public static int find_first_in_sorted(int[] arr, int x) {
int lo = 0;
int hi = arr.length;
while (lo < hi) {
int mid = (lo + hi) / 2; // check if this is floor division
if (x == arr[mid] && (mid == 0 || x != arr[mid-1])) {
return mid;
} else if (x <= arr[mid]) {
hi = mid;
} else {
lo = mid + 1;
}
}
return -1;
}
}
| QuixBugs/QuixBugs/correct_java_programs/FIND_FIRST_IN_SORTED.java |
quixbugs-java_data_82 | package correct_java_programs;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author derricklin
*/
public class QUICKSORT {
public static ArrayList<Integer> quicksort(ArrayList<Integer> arr) {
if (arr.isEmpty()) {
return new ArrayList<Integer>();
}
Integer pivot = arr.get(0);
ArrayList<Integer> lesser = new ArrayList<Integer>();
ArrayList<Integer> greater = new ArrayList<Integer>();
for (Integer x : arr.subList(1, arr.size())) {
if (x < pivot) {
lesser.add(x);
} else if (x >= pivot) {
greater.add(x);
}
}
ArrayList<Integer> middle = new ArrayList<Integer>();
middle.add(pivot);
lesser = quicksort(lesser);
greater = quicksort(greater);
middle.addAll(greater);
lesser.addAll(middle);
return lesser;
}
}
| QuixBugs/QuixBugs/correct_java_programs/QUICKSORT.java |
README.md exists but content is empty.
Use the Edit dataset card button to edit it.
- Downloads last month
- 97