Skip to content

Instantly share code, notes, and snippets.

View zachwhaley's full-sized avatar
🐳
Hi!

Zachary Whaley zachwhaley

🐳
Hi!
View GitHub Profile
@zachwhaley
zachwhaley / strrev.c
Last active August 29, 2015 13:57
C program to reverse a string
#include <stdio.h>
#include <string.h>
void strrev(char *str)
{
char tmp, *end;
if (str) {
end = str + strlen(str) - 1;
while (str < end) {
@zachwhaley
zachwhaley / iglatinpay.cpp
Last active January 22, 2020 18:54
A C++ program to convert a sentence to Pig Latin
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
// is_vowel returns true if the given
// character is a vowel; false otherwise.
bool is_vowel(char c)
{
@zachwhaley
zachwhaley / Makefile
Last active December 24, 2023 01:48
Simple C++ Makefile
CXXFLAGS = -g -Wall -Werror -std=c++11
LDLIBS =
PRGM = project
SRCS := $(wildcard *.cpp)
OBJS := $(SRCS:.cpp=.o)
DEPS := $(OBJS:.o=.d)
.PHONY: all clean
@zachwhaley
zachwhaley / slurp.cpp
Last active July 12, 2016 01:09
File slurping, C++ stye
std::string slurp(const std::string& filename)
{
// Open the file into an input file stream
std::ifstream ifile(filename, std::ios::in | std::ios::binary | std::ios::ate);
// Check that the file exists
if (ifile) {
std::string str;
// Move to the end of the file
ifile.seekg(0, std::ios::end);
@zachwhaley
zachwhaley / p4_completion.sh
Created April 1, 2014 15:41
perforce bash completion
#!/bin/bash
function _p4()
{
local cmds='add annotate attribute branch branches change changes changelist changelists client clients copy
counter counters cstat delete depot depots describe diff diff2 dirs edit filelog files fix fixes flush fstat grep
group groups have help info integrate integrated interchanges istat job jobs key keys label labels labelsync list
lock logger login logout merge move open opened passwd populate print protect protects reconcile rename reopen
resolve resolved revert review reviews set shelve status sizes stream streams submit sync tag tickets unlock
unshelve update user users where workspace workspaces'
@zachwhaley
zachwhaley / mktags
Last active February 1, 2017 17:22
Make cscope C/C++ tags
#!/usr/bin/env bash
[[ $# -eq 1 ]] && cd $1
find `pwd` -name "*.[ch]" -o -name "*.[ch]pp" -o -name "*.cc" -o -name "*.hh" > cscope.files
cscope -b -q -u
cat cscope.files | xargs ctags
@zachwhaley
zachwhaley / bears.py
Created February 19, 2015 13:21
Pair mating bears and avoid common ancestors
import sys
import re
class Bear:
def __init__(self, attrs):
self.parents = []
self.name = attrs[0]
if len(attrs) > 1:
self.sex = attrs[1]
self.parents = attrs[2:4]
@zachwhaley
zachwhaley / rcv.py
Last active October 3, 2017 21:44
Ranked Choice Voting script
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import json
def tally(voters):
results = {}
for votes in voters.itervalues():
if votes:
@zachwhaley
zachwhaley / mergesort.cpp
Last active March 13, 2024 09:07
C++ mergesort with iterators
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
template<typename Iter>
void print(const Iter& beg, const Iter& end, bool new_line = true)
{
std::for_each(beg, end, [](auto& i) { std::cout << i << " "; });
if (new_line) {
@zachwhaley
zachwhaley / quicksort.cpp
Last active March 13, 2024 09:06
C++ quicksort with iterators
#include <algorithm>
#include <iostream>
#include <list>
template<typename Iter>
void print(const Iter& beg, const Iter& end)
{
std::for_each(beg, end, [](auto& i) { std::cout << i << " "; });
std::cout << std::endl;
}