Skip to content

Instantly share code, notes, and snippets.

View davepkennedy's full-sized avatar

Dave Kennedy davepkennedy

View GitHub Profile
public class SwitchFlipping {
public static void main (String[] args) {
doSlowWay();
doFastWay();
}
private static boolean isSquare (int i) {
int s = (int)Math.sqrt(i);
return (s*s) == i;
}
static boolean contains (int[] h, int n) {
int low = 0;
int high = h.length;
int d;
do {
d = (high - low) / 2;
int mid = low + d;
if (h[mid] == n) {
return true;
} else if (h[mid] < n) {
@davepkennedy
davepkennedy / divide.py
Created September 8, 2020 19:59
Find a quotient and a remainder without using division or modulo
# Quotient and Remainder without using division
def divide (num, div):
low, high = 0, num
d = high - low
# Keep going until there's no more midpoints between high and low
# Need to find the value for x such that x*div ~ num
while d > 1:
# Calculate d here or there's one iteration too few
@davepkennedy
davepkennedy / Application.cpp
Created May 20, 2020 10:19
Some Windowing snippets
#include "Application.h"
#include "BaseWindow.h"
Application::Application(BaseWindow* window)
: _window(window)
{
}
Application::~Application()
@davepkennedy
davepkennedy / Application.h
Created May 5, 2018 07:56
Basic code for GL4 on Window
#pragma once
#include <Windows.h>
#define DECLARE_APP(WinType) \
int CALLBACK wWinMain( \
HINSTANCE hInst, \
HINSTANCE hPrevInst, \
LPWSTR lpCmdLine, \
int nCmdShow \
public class IcosahedronBuilder extends PolygonBuilder {
@Override
protected void buildVertices() {
// create 12 vertices of a icosahedron
float t = (float)((1.0 + Math.sqrt(5.0)) / 2.0);
addVertex(new Vec3d(-1, t, 0));
addVertex(new Vec3d( 1, t, 0));
addVertex(new Vec3d(-1, -t, 0));
addVertex(new Vec3d( 1, -t, 0));
//
// main.c
// rotate
//
// Created by Dave Kennedy on 30/11/2016.
// Copyright © 2016 Dave Kennedy. All rights reserved.
//
#include <stdio.h>
#include <string.h>
@davepkennedy
davepkennedy / anagrammer.cpp
Created November 29, 2016 16:08
Anagrams in C++
//
// main.cpp
// anagrammer
//
// Created by Dave Kennedy on 29/11/2016.
// Copyright © 2016 Dave Kennedy. All rights reserved.
//
#include <string>
#include <vector>
@davepkennedy
davepkennedy / Anagrammer.scala
Last active December 1, 2016 14:38
Create anagrams in Scala
package io.github.davepkennedy
/**
* Created by dakennedy on 29/11/2016.
*/
object Anagrammer {
def insertAt[T](element: T, position: Int, collection: List[T]): List[T] = {
val (left,right) = collection.splitAt(position)
left ++ List(element) ++ right
}
@davepkennedy
davepkennedy / Rotator.java
Last active November 30, 2016 12:55
Rotating an array by a certain amount (2 ways)
package io.github.davepkennedy;
import java.util.ArrayList;
import java.util.List;
/**
* Created by dakennedy on 29/11/2016.
*/
public class Rotator {
public static List<Integer> simpleRotate (List<Integer> numbers, int amount) {