Skip to content

Instantly share code, notes, and snippets.

@libbkmz
Created August 29, 2012 17:35
Show Gist options
  • Save libbkmz/3515980 to your computer and use it in GitHub Desktop.
Save libbkmz/3515980 to your computer and use it in GitHub Desktop.
crypt and decrypt to bitmask
string = "abcdefabcdeabcddcbacdbacdbbabdcfbcdabfbcdf"
result = []
mask = 0
rules = {
1: "a",
2: "b",
4: "c",
8: "d",
16: "e",
32: "f",
}
# removing duples in string
string = "".join(set([x for x in string]))
# adding bitmask for strings
for i,vl in iter(reversed(sorted(rules.iteritems()))):
if vl in string:
mask += i
while mask > 0:
for i,vl in iter(reversed(sorted(rules.iteritems()))):
if mask - i >= 0:
mask -= i
result.append(vl)
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment