Skip to content

Instantly share code, notes, and snippets.

def anagrams(n)
text = File.open('words.txt').read
candidates = []
text.each_line do |line|
if (line.length - 1) == n.length
candidates << line.gsub("\n",'')
end
end
@Ikhan
Ikhan / github-create
Created November 26, 2016 21:43
Bash Script for creating github repos in terminal
github-create(){
repo_name=$1
dir_name=`basename $(pwd)`
invalid_credentials=""
if ["$repo_name" = ""]; then
echo "Repo Name (hit return to use) '$dir_name' ?"
read repo_name
fi
@Ikhan
Ikhan / gist:ab4a62aa0e57e7b75ecb1b09cdded7da
Created May 17, 2017 22:34
helper function for routes files in nodejs
//helper/controller.js
var pathModule = require('path');
var fs = require('fs');
exports.pathLocation = function () {
var basePath = pathModule.resolve(__dirname,'../controllers');
var controllers = {};
function binarySearch(ar, elem) {
let start = 0;
let end = ar.length -1;
let mid = Math.floor((start + end) / 2);
while(ar[mid] !== elem && start < end ) {
if (elem < ar[mid]) end = mid - 1;
else start = mid + 1;
mid = Math.floor((start + end) / 2);
function naiveSearch(long, short) {
let count = 0;
for (let i = 0; i<long.length; i++) {
for(let j=0; j<short.length; j++) {
if (short[j] !== long[i+j]) {
console.log('break');
break;
}
if (j === short.length -1) {
count++;
//naive solution
function bubbleSort(arr) {
for (let i=0; i< arr.length; i++) {
for (let j=0; j< arr.length; j++) {
console.log(arr, arr[j], arr[j+1]);
if (arr[j] > arr[j+1]) {
//swap
let temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
function selectionSort(arr) {
for (let i=0; i < arr.length; i++) {
let lowest = i;
for(let j = i+1; j < arr.length; j++) {
if (arr[j] < arr[lowest]) {
lowest = j;
}
}
if (i !== lowest) {
let temp = arr[i];
function insertionSort(arr) {
for (let i =1; i < arr.length; i++) {
let currentVal = arr[i];
let j;
for (j=i-1; j>=0 && arr[j] > currentVal;j--) {
arr[j+1] = arr[j];
}
arr[j+1] = currentVal;
console.log(arr);
}
@Ikhan
Ikhan / gist:eb60bb4eafd3dd5cb26cf0ee63506204
Created August 27, 2023 03:27
functional options (pattern)
package main
import "fmt"
type UserProfile struct {
username string
email string
age int
avatarURL string
}
package main
import (
"fmt"
"strings"
s "strings"
)
func main() {