Skip to content

Instantly share code, notes, and snippets.

View samjarman's full-sized avatar

Sam Jarman samjarman

View GitHub Profile
@samjarman
samjarman / suffixForDayInDate.m
Created February 9, 2014 20:49
Returns a suffix for a day of the month, eg 1st, 2nd, 2rd, 4th
- (NSString *)suffixForDayInDate:(NSDate *)date{
NSInteger day = [[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] components:NSDayCalendarUnit fromDate:date] day];
if (day >= 11 && day <= 13) {
return @"th";
} else if (day % 10 == 1) {
return @"st";
} else if (day % 10 == 2) {
return @"nd";
} else if (day % 10 == 3) {
return @"rd";
@samjarman
samjarman / Hangman
Created March 31, 2014 06:00
A simple hangman game
import random
def print_word(word):
string = ""
for letter in word:
string += letter + ' '
print(string)
def get_word():
word_file = "/usr/share/dict/words" #only works for *nix users
@samjarman
samjarman / starts
Created April 3, 2014 07:29
Stars - COSC 121 Challenge
def square_of_stars(n):
line = " * " * n
for i in range(n):
print (line)
def triangle(n):
for i in range(1, n+1):
print(" * " * i)
@samjarman
samjarman / Tablet-Sim
Created April 6, 2014 22:56
A simple simulation to determine the best way to allocate tablets, failure cards, success cards and delay cards in Tablets of Stone
import random
tablets = 5
chance_dnd = 0.30
chance_delayed = 0.30
chance_arrived = 1.0 - chance_dnd - chance_delayed
runs = 1000
total_failed = 0
@samjarman
samjarman / TopPostsHitCount.sql
Created December 10, 2013 23:26
Useful query for finding out your top posts on your blog (for all posts) if you use Statpress for Wordpress.
SELECT urlrequested, COUNT(urlrequested)
FROM `wp_statpress`
WHERE urlrequested LIKE '%THEPOSTTITLES%'
GROUP BY urlrequested
ORDER BY COUNT(urlrequested) DESC
@samjarman
samjarman / RoundedCorners.m
Created January 9, 2014 20:33
Rounding corners on UIViews (and their subclasses, UIImageView, etc)
UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wood.jpg"]];
// Get the Layer of any view
CALayer * l = [roundedView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
@samjarman
samjarman / allnotifications.m
Created February 29, 2016 23:15
A snippet to listen for all notifications inside an app for learning/exploring frameworks.
NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter];
[notifyCenter addObserverForName:nil
object:nil
queue:nil
usingBlock:^(NSNotification* notification){
// Explore notification
NSLog(@"Notification found with:"
"\r\n name: %@"
"\r\n object: %@"
"\r\n userInfo: %@",
@samjarman
samjarman / misc.ex
Last active May 9, 2016 22:56
A simple elixir function and its test
defmodule Misc do
def multiply(x, y) do
x * y
end
end
defmodule MiscTest do
use ExUnit.Case
doctest Misc
test "multiplying two numbers returns the product" do
@samjarman
samjarman / miscwithdoctest.ex
Last active May 9, 2016 22:58
Showing a Elixir Module and Method with a Doctest
defmodule Misc do
@moduledoc """
This contains some functions for learning Elixir.
"""
@doc """
This multiplies two values.
Examples:
iex> Misc.multiply(3,4)
@samjarman
samjarman / sum.ex
Last active May 9, 2016 22:58
Sums a lit of numbers in elixir
@doc """
iex>Misc.sum([1,1,1])
3
iex>Misc.sum([1, 2, 3, 4, 5])
15
"""
def sum(list) do
_sum(list, 0)
end