This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The following is an interesting problem I found in the book Introduction to Computer Theory (2nd edition), | |
by Daniel Cohen, Wiley, 1997. See what you think ... | |
“ In the English language, we can observe that some adjectives apply to themselves. For example, the word | |
“short” is a fairly short word. We mighy say, “short” is short. Also, the adjective “polysyllabic” is indeed | |
polysyllabic. Some other possible adjectives of this type are “unfrequent”, “melodious”, “arcane”, | |
“unhyphenated”, “English”, “non-palindromic”, and “harmless”. Let us call all these adjectives that describe | |
themselves homothetic. Let us call all other adjectives (those that do not decribe themselves) heterothetic. | |
For example, the words “gymnastic”, “myopic”, and “recursive” are all heterothetic adjectives The word | |
“heterothetic” is an adjective, and therefore like all adjectives it is either homothetic or heterothetic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Special for 15-bit values on 64bit processors | |
//with fast multiplication | |
//From Hacker's Delight, p. 72 | |
inline uint32_t popcount15(uint32_t x) | |
{ | |
uint64_t y; | |
y = x * 0x0002000400080010; | |
y = y & 0x1111111111111111; | |
y = y * 0x1111111111111111; | |
y = y >> 60; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(popcount_table). | |
-export([gen_table/1]). | |
gen_table(Bits) -> gen_table(1, 2 bsl (Bits - 1), {0}). | |
gen_table(Stop, Stop, Table) -> Table; | |
gen_table(Value, Stop, Table) -> | |
New = (Value band 1) + erlang:element((Value bsr 1) + 1, Table), | |
NewTable = erlang:append_element(Table, New), | |
gen_table(Value + 1, Stop, NewTable). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(popcount). | |
-compile({parse_transform, ct_expand}). | |
-compile({popcount_table}). | |
-export([popcount16/1, popcount32/1]). | |
-define(TABLE(B), ct_expand:term( popcount_table:gen_table(B) )). | |
-define(TABLE16, ct_expand:term( ?TABLE(16) )). | |
popcount16(V) -> erlang:element(V+1, ?TABLE16). | |
popcount32(V) -> popcount16( V band 16#ffff ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(my_binary). | |
-export([foreach/3]). | |
-export([fold/4]). | |
break(Blocksize, Bin) -> | |
<<Head:Blocksize/bits, Rest/bits>> = Bin, | |
{Head, Rest}. | |
foreach(_, _, <<>>) -> ok; | |
foreach(F, Blocksize, Bin) -> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
your_re = // # <= YOUR REGEX GOES BETWEEN THESE SLASHES************************* | |
#Example: /.*/ | |
# This wraps your regex so the whole binary string has to match it | |
@re = /^(#{your_re})$/ | |
# Helper code to convert numbers to binary strings | |
class Fixnum | |
def to_bin_s | |
return '0' if self == 0 | |
s = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fixnum | |
def to_bin_s | |
return '0' if self == 0 | |
s = '' | |
n = self | |
while n > 0 | |
s = (n % 2).to_s << s | |
n = n >> 1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Extracting nounphrase chunks from a parse tree | |
# from http://streamhacker.com/2009/02/23/chunk-extraction-with-nltk/ | |
# for each noun phrase sub tree in the parse tree | |
for subtree in tree.subtrees(filter=lambda t: t.node == 'NP'): | |
# print the noun phrase as a list of part-of-speech tagged words | |
print subtree.leaves() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_method(obj, f, fname): | |
"""Adds the instance method from function f to the object obj, callable by fname (i.e. obj.fname()) | |
example: | |
def func(self): | |
print 'test' | |
add_method(myObject, func, 'newmethodname') | |
myObject.newmethodname() |
NewerOlder