Skip to content

Instantly share code, notes, and snippets.

View mccricardo's full-sized avatar

Ricardo Castro mccricardo

View GitHub Profile
"""
The MIT License (MIT)
Copyright (c) 2014 Ricardo Castro <mcc.ricardo@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@mccricardo
mccricardo / naive_string_search.py
Created November 17, 2013 23:47
Naive string search
def naive_search(substring, string):
for i in range(len(string)-len(substring)+1):
if string[i:i+len(substring)] == substring:
return i
return -1
@mccricardo
mccricardo / sets_problem23.py
Created November 12, 2013 23:01
Problem 23 solution, using sets.
s = 0
abundant_numbers = set()
def get_divisors(number):
return [x for x in xrange(1, number/2 +1) if number % x == 0]
for i in xrange(1, 28123):
if sum(get_divisors(i)) > i:
abundant_numbers.add(i)
if not any((i-a in abundant_numbers) for a in abundant_numbers):
@mccricardo
mccricardo / problem23.py
Last active December 28, 2015 04:09
Possible solution (using the wrong data structure for this approach) for Project Euler, Problem 23
s = 0
abundant_numbers = []
def get_divisors(number):
return [x for x in xrange(1, number/2 +1) if number % x == 0]
for i in xrange(1, 28124):
if sum(get_divisors(i)) > i:
abundant_numbers.append(i)
if not any((i-a in abundant_numbers) for a in abundant_numbers):
@mccricardo
mccricardo / merge_sort.py
Created October 30, 2013 19:58
Merge Sort example
def merge(l1, l2):
result = []
i, j = 0, 0
while i < len(l1) and j < len(l2):
if l1[i] > l2[j]:
result.append(l2[j])
j += 1
else:
result.append(l1[i])
i += 1
from threading import Thread
from Queue import Queue
class Adding:
def __init__(self):
self.added = False
self.value = 0
def has_added(self):
return self.added
from threading import Thread, Lock
class Adding:
def __init__(self):
self.added = False
self.value = 0
def has_added(self):
return self.added