Skip to content

Instantly share code, notes, and snippets.

View jsgoller1's full-sized avatar
🖖
I hate computers

Joshua Goller (he/him) jsgoller1

🖖
I hate computers
View GitHub Profile
@jsgoller1
jsgoller1 / n_free_string.py
Last active October 15, 2017 07:22
n-free strings
"""
An n-free binary list is a list of binary strings in which there is no string
contains n consecutive 1s or 0s.
Write a function that takes integers n and m as input, and returns all n-free
strings of length m (assume m can be any size but for testing doesn't need to
be larger than 10).
The solution here generates all possible binary strings, then removes any that
have n consecutive 1s or 0s.
@jsgoller1
jsgoller1 / sprial_print.py
Last active October 15, 2017 10:00
Sprial print
"""
Examples of spiral-form printing:
Input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
@jsgoller1
jsgoller1 / calcs.go
Created January 17, 2018 03:35
pools example
package main
import (
"fmt"
"sync"
)
func main() {
var numCalcsCreated int
calcPool := &sync.Pool{
@jsgoller1
jsgoller1 / panic.go
Created January 18, 2018 09:23
panic at the disco
package main
import "fmt"
func main() {
mychan := make(chan int)
mychan <- 666
go func() {
mychan <- 1
mychan <- 2
@jsgoller1
jsgoller1 / loops.c
Created February 13, 2018 08:21
Never forget for-loops again!
#include <stdio.h>
int main(){
int i;
int nums[5] = {0,1,2,3,4};
printf("Array is {0,1,2,3,4}, so the first char is arr[0] and the last one is arr[4], but len(arr) is 5.\n\n");
printf("Generally, your options for going through arrays are: \n\n");
printf("for (i = 0; i < len; i++)\n");
@jsgoller1
jsgoller1 / 5.6.py
Created March 16, 2018 08:56
A python program that solves 5.6 from Stroustrup's "The C++ Programming Language"
people = []
more = 'y'
while (more == 'y'):
name = raw_input("What is your name? ")
age = raw_input("What is your age? ")
people.append([name, age])
more = raw_input("Add another person? [y/n] ")
for each in people:
print each[0]+",", "age", each[1]
@jsgoller1
jsgoller1 / srs.py
Created April 22, 2018 00:23
HackerRank: "Super Reduced String"
#!/bin/python
# Problem statement: https://www.hackerrank.com/challenges/reduced-string/problem
import sys
def remove_first_duplicates(s):
for i in range(0, len(s)-1):
if s[i] == s[i+1]:
return s[:i] + s[i+2:], True
return s, False
@jsgoller1
jsgoller1 / collections_ex.py
Last active May 4, 2018 06:53
Examples (not completely tested) of places where I'd use collections during an interview
#!/usr//bin/python
from collections import *
"""
Question: "Write a function that left-shifts (or right-shifts) an array arr by n places."
Answer: Use a deque. Note that you can do this too with normal arrays, but when you get
asked about performance, deques have O(1) performance for popping or appending from
either side, and are as easy to use as normal arrays.
@jsgoller1
jsgoller1 / brace_match.py
Created May 5, 2018 06:30
HackerRank issue or just me?
"""
Go to the following link and paste the below code: https://www.hackerrank.com/challenges/ctci-balanced-brackets/problem
Issue: An empty deque (or list) "magically" has uninserted values present when initialized.
Steps to reproduce:
1) Run this code to see it passes the test cases.
2) Comment out the __init__() method in the stack, and replace "contents = None" with "contents = deque()" or "contents = []"
3) Run to observe it fails the test cases.
4) Beneath "s = stack()" in is_matched(), create a new line and put "s.dump()" on it.
@jsgoller1
jsgoller1 / tries_contact.py
Created May 9, 2018 01:24
HackerRank Tries: Contacts
class t_node():
def __init__(self):
self.children = {}
self.is_complete = False
self.valid_children = 0
def add_child(self, char):
self.children[char] = t_node()
return self.children[char]