Skip to content

Instantly share code, notes, and snippets.

import random
def roll_dice(number,sides):
for i in range(0,number):
rolled = random.randint(1,sides)
print("You rolled {}!".format(rolled))
while True:
number = int(input("How many dice do you want to roll? "))
sides = int(input("How many sides on each dice? "))
from random import choice
import time
def hit_or_stick():
while sum(your_cards)<22:
response = raw_input("Would you like to hit or stick? ")
if response.lower() == "hit":
your_cards.append(choice(cards))
print("Drawing...")
time.sleep(0.5)
@bentrevett
bentrevett / find_substring.c
Created January 13, 2017 12:48
Given string 'haystack', find if sub-string 'needle' exists.
char *strstr(const char *haystack, const char *needle)
{
size_t h_len = strlen(haystack);
size_t n_len = strlen(needle);
size_t i, j;
for (i = 0; i < h_len; i++)
{
size_t found = 0;
for (j = 0; j < n_len; j++)
{
@bentrevett
bentrevett / end_string.c
Last active February 1, 2017 20:33
Returns true if the string t occurs at the end of the string s, and false otherwise.
#include <string.h>
#include <stdbool.h>
bool strend(const char *s, const char *t)
{
size_t slen = strlen(s);
size_t tlen = strlen(t);
if (tlen > slen) return false;
else return !strcmp(s + (slen - tlen), t);
@bentrevett
bentrevett / euclidean_distance.py
Created January 17, 2017 03:35
Find Euclidean distance in one line in Python.
euclidian = lambda v1,v2: (sum([(a-b)**2 for a,b in zip(v1,v2)])**0.5)
print(euclidian([0.4,0.2,0.3],[0.5,0.6,0.7])) #0.574456264654
@bentrevett
bentrevett / oop.c
Created February 1, 2017 20:31
Example of OOP in C.
#include <stdlib.h>
#include <stdio.h>
typedef struct Array {
int* arr;
size_t length;
} Array;
Array* newArray(int len)
{
@bentrevett
bentrevett / print_ascii_table.cpp
Created February 1, 2017 20:37
"Write a program that prints a table of ASCII values for the characters in the ASCII character set from 33 to 126. The program should print the decimal, octal, hex and character value for each character."
for (char c = 33; c < 126; c++)
{
cout << dec << int(c) << hex << " " << int(hex) << int(c) << " " << oct << int(c) << " " << c << "\n";
}
#ifdef _WIN32
#include <Windows.h>
#define muhSleep(x) Sleep((x))
#else
#include <unistd.h>
#define muhSleep(x) sleep((x))
#endif
void fillOffsets( int **offsets, char *str )
{
import re
from sys import stdout
from random import randint, choice
from collections import defaultdict
#http://theorangeduck.com/page/17-line-markov-chain
#http://theorangeduck.com/media/uploads/other_stuff/poetry.zip
with open('poetry.txt') as f:
words = re.split(' +', f.read())
def get_num_params(model):
param_count = 0
for param in model.parameters():
param_count += np.product(param.data.shape)
return param_count