Skip to content

Instantly share code, notes, and snippets.

Avatar

David Godfrey dgodfrey95

View GitHub Profile
@dgodfrey95
dgodfrey95 / Board.java
Created Dec 25, 2019
8-Puzzle Board.java
View Board.java
import java.util.ArrayList;
public class Board {
private int[][] board;
private final int n;
private int hammingDist;
private int manhattanDist;
private int blankX, blankY;
// create a board from an n-by-n array of tiles,
// where tiles[row][col] = tile at (row, col)
@dgodfrey95
dgodfrey95 / addressbook.cpp
Last active Dec 19, 2019
Address book program I did for someone
View addressbook.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <limits>
#include <locale>
#include <sstream>
#include <iomanip>
#include <cassert>
template<class T>
View myString.java
public class MyString {
private char[] buffer;
private MyString next = null;
private int len = 0;
public MyString(char[] data) {
buffer = data.clone();
len += data.length;
View mile.js
function calculate() {
var total_miles = 0;
var avg_daily_miles = 0;
for (var i=1; i<=7; i++) {
var dist = +document.getElementsByName("value" + i)[0].value;
total_miles += dist;
avg_daily_miles += dist/7.0;
}
document.getElementById("totalM").value = total_miles;
document.getElementById("avgM").value = avg_daily_miles;
View cipher.java
import java.util.*;
/*public*/ interface Cipher {
public abstract String encode(String plainText);
}
/*public*/ class ShiftN_Cipher implements Cipher {
public ShiftN_Cipher(int shifts) {
this.shifts = shifts;
}
@dgodfrey95
dgodfrey95 / sortStack.cpp
Created Oct 7, 2019
Sort a stack using recursion
View sortStack.cpp
#include <iostream>
#include <string>
#include <vector>
#include<bits/stdc++.h>
using namespace std;
void insertSorted(stack<int>& s, int e) {
stack<int> temp;
while (true) {
if (s.empty() || e >= s.top()) {
@dgodfrey95
dgodfrey95 / tofromhex.cpp
Last active Sep 4, 2019
Converting to and from hex
View tofromhex.cpp
#include<bits/stdc++.h>
using namespace std;
string toHex(int n) {
string result;
while (n) {
int rem = n % 16;
result += (rem < 10) ? (rem + '0') : ('A' + rem - 10);
n /= 16;
}
View distinctksubarray.cpp
#include <iostream>
#include <initializer_list>
#include<bits/stdc++.h>
using namespace std;
vector<int> longestDistinctKSubarray(vector<int> nums, int k) {
vector<int> ret;
if (k == 0)
return ret;
View sumparts.cpp
#include <iostream>
#include <string>
#include <map>
#include<bits/stdc++.h>
using namespace std;
struct Node {
int val;
Node*next=0;
};
@dgodfrey95
dgodfrey95 / consecutive_find.cpp
Created Jun 21, 2019
New consecutive_find. More efficient
View consecutive_find.cpp
int consecutive_find(vector<int> arr, int n) {
for (size_t s = 0, e = 0; e < arr.size(); s = e) {
while (e < arr.size() && arr[s] == arr[e]) {
e++;
}
if (e - s == (size_t)n) {
return s;
}
}
return -1;
You can’t perform that action at this time.