Skip to content

Instantly share code, notes, and snippets.

View scottashipp's full-sized avatar

Scott Shipp scottashipp

View GitHub Profile
@scottashipp
scottashipp / AnagramChecker.java
Created November 20, 2018 16:33
Check if two words are anagrams of each other. What is the Big O space complexity of this code?
package com.scottshipp.code;
import java.util.HashMap;
import java.util.Map;
public class AnagramChecker {
public boolean isAnagram(String firstWord, String secondWord) {
if(firstWord == null || secondWord == null || firstWord.length() != secondWord.length()) {
return false;
@scottashipp
scottashipp / Solution.java
Created October 13, 2018 14:28
Validate a Binary Search Tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
@scottashipp
scottashipp / FindMinComparing.java
Last active June 27, 2018 20:31
Find minimum in a sorted rotated array in O(log n) time
class FindMinComparing {
public int findMin(int[] arr) {
if(arr == null) {
throw new IllegalArgumentException("Received null array");
}
if(arr.length < 1) {
throw new IllegalArgumentException("Received zero-length array");
}
if(arr.length == 1) {
@scottashipp
scottashipp / softwareengineering
Last active June 9, 2022 22:21
Software Engineering Quotes for fortune
# from https://codeburst.io/how-i-hacked-my-terminal-so-a-happy-whale-would-spout-software-quotes-at-me-6791e6c74fc6
%
Simple things should be simple, complex things should be possible.
The Wiki Way: Quick Collaboration on the Web, Bo Leuf, Ward
Cunningham
%
Simplicity is the soul of efficiency.
Austin Freeman
%
@scottashipp
scottashipp / softwareengineering
Created January 18, 2018 17:43
Snippet from Software Engineering Quotes Fortune File
%
Simple things should be simple, complex things should be possible.
The Wiki Way: Quick Collaboration on the Web, Bo Leuf, Ward
Cunningham
%
Simplicity is the soul of efficiency.
Austin Freeman
%
I have yet to see any problem, however complicated, which, when you
looked at it in the right way, did not become still more complicated.
@scottashipp
scottashipp / findandreplace.sh
Created January 18, 2018 17:41
find and replace in a directory
find . -type f -exec sed -i -e 's/world/universe/g' {} \;
@scottashipp
scottashipp / gist:fe908ca121cd0783e6f25ce341874bc6
Created January 18, 2018 17:39
awk command to take a directory of files and collect them into one file with % delimiters
awk 'FNR==1{print "%"}{print}' quote* > softwareengineering
@scottashipp
scottashipp / PermutationGenerator1.java
Created August 5, 2016 14:58
Permutations Generator in Java....not as good as Scala
import java.util.ArrayList;
import java.util.Scanner;
public class PermutationGenerator1 {
private static String inputWithoutC(String input, char c) {
String inputWithoutC = "";
char[] temp = input.toCharArray();
for(char d : temp) {
if(d !=c ) {
inputWithoutC += d;
@scottashipp
scottashipp / NullSafe.scala
Created July 27, 2016 20:25
Null-safety in Scala
implicit class NullSafeOption[A,B](val a: Option[A]) extends AnyVal {
def mapNullSafe(f: A => B): Option[B] = { a.flatMap(x => Option(x).map(f)) }
}
implicit class NullSafe[A,B](val a: A) extends AnyVal {
def ?(f: A => B): B = { if(a == null) null.asInstanceOf[B] else f(a) }
}
//Examples with Option + Map
scala> val s: String = null
@scottashipp
scottashipp / gist:a2e76d98bb86ed054c0fb8317c2117ce
Last active April 25, 2020 18:18
Null-safe operator for Scala?
// from https://code.scottshipp.com/2016/07/28/does-scala-have-the-safe-navigation-null-conditional-operator/
implicit class NullSafe[A, B](val a: A) extends AnyVal {
def ?(f: A => B): B = { if(a == null) null.asInstanceOf[B] else f(a) }
}
//example
val s = "Hello, World!"
s?(_.reverse)?(_.reverse) //returns "Hello, World!"
val sNull: String = null