Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
UniverseFly committed Oct 5, 2023
0 parents commit 29a121a
Show file tree
Hide file tree
Showing 430 changed files with 14,903 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Java/buggy/BITCOUNT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public static int bitcount(int n) {
int count = 0;
while (n != 0) {
n = (n ^ (n - 1));
count++;
}
return count;
}
26 changes: 26 additions & 0 deletions Java/buggy/BREADTH_FIRST_SEARCH.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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;
}
15 changes: 15 additions & 0 deletions Java/buggy/BUCKETSORT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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;
}
20 changes: 20 additions & 0 deletions Java/buggy/DEPTH_FIRST_SEARCH.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public 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);
}
15 changes: 15 additions & 0 deletions Java/buggy/DETECT_CYCLE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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;
}
}
18 changes: 18 additions & 0 deletions Java/buggy/FIND_FIRST_IN_SORTED.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;
}
13 changes: 13 additions & 0 deletions Java/buggy/FIND_IN_SORTED.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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;
}
}
16 changes: 16 additions & 0 deletions Java/buggy/FLATTEN.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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);
}
}
7 changes: 7 additions & 0 deletions Java/buggy/GCD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(a % b, b);
}
}
15 changes: 15 additions & 0 deletions Java/buggy/GET_FACTORS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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>();
}
18 changes: 18 additions & 0 deletions Java/buggy/HANOI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;
}
13 changes: 13 additions & 0 deletions Java/buggy/IS_VALID_PARENTHESIZATION.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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;
}
20 changes: 20 additions & 0 deletions Java/buggy/KHEAPSORT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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;

}
28 changes: 28 additions & 0 deletions Java/buggy/KNAPSACK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

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];
}
23 changes: 23 additions & 0 deletions Java/buggy/KTH.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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;
}
}
46 changes: 46 additions & 0 deletions Java/buggy/LCS_LENGTH.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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;
}
}
13 changes: 13 additions & 0 deletions Java/buggy/LEVENSHTEIN.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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)
);
}
}
25 changes: 25 additions & 0 deletions Java/buggy/LIS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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;
}
12 changes: 12 additions & 0 deletions Java/buggy/LONGEST_COMMON_SUBSEQUENCE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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;
}

}
11 changes: 11 additions & 0 deletions Java/buggy/MAX_SUBLIST_SUM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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;
}
15 changes: 15 additions & 0 deletions Java/buggy/MERGESORT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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);
}
}
Loading

0 comments on commit 29a121a

Please sign in to comment.