Created
June 23, 2016 02:42
-
-
Save zmarvel/d47f4ad4526ec65803f032127fa5bbe0 to your computer and use it in GitHub Desktop.
A Clojure solution to a Google programming challenge my roommate was telling me about.
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
; Given a list of "passwords" for which e.g. "abc" and "cba" or "cat" and "tac" | |
; are not considered unique but "abc" and "bac" are considered unique, count the | |
; unique words in the list. | |
; | |
; Run it like this: `clojure 2.clj < words.txt` | |
(require 'clojure.string) | |
(defn normalize-word | |
[word] | |
(let [head (first word) | |
tail (last word)] | |
(if (< (int head) (int tail)) | |
word | |
(clojure.string/reverse word)))) | |
(defn count-words | |
[words] | |
(let [unique-words (reduce (fn [st word] | |
(conj st (normalize-word word))) | |
(hash-set) | |
words)] | |
(count unique-words))) | |
(let [words (take-while (complement nil?) (repeatedly read-line))] | |
(println (count-words words))) |
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
# Take a newline-separated list of words in a file, randomly include their | |
# reverse, and shuffle the result. Then overwrite the original file. | |
# | |
# Usage: `python shuffle_words.py words.txt` | |
import sys | |
import random | |
def main(): | |
filename = sys.argv[1] | |
random.seed() | |
with open(filename, 'r+') as f: | |
words = [line.rstrip() for line in f] | |
for word in words: | |
if random.random() < 0.5: | |
words.append(''.join(reversed(word))) | |
random.shuffle(words) | |
f.seek(0) | |
f.writelines(map(lambda w: '{}\n'.format(w), words)) | |
if __name__ == '__main__': | |
main() |
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
erusnirevo | |
polythene | |
pumpernickel | |
cigolonorhc | |
invigilation | |
ssensuourgnocni | |
homogenized | |
yrtainyg | |
carlyle | |
lettice | |
gnisigele | |
hamburg | |
disseminating | |
gnidirtseb | |
lanoitulove | |
beltless | |
noitaligivni | |
cuteness | |
nairatiuqibu | |
zaddikim | |
aerobacteriology | |
arion | |
adjustably | |
mckim | |
eirever | |
eisoolf | |
overvoltage | |
sehsif | |
furlong | |
detanimile | |
naisicerp | |
noitcejbuser | |
invigilation | |
ichthyophagous | |
ylnevaehnu | |
kiblah | |
rekcol | |
nawh | |
equilibrant | |
floosie | |
lanoitairav | |
motorcycle | |
noitcejbuser | |
decorated | |
sublobular | |
labiovelarise | |
unheavenly | |
draobrettulf | |
resubjection | |
suoecacalims | |
semipermanent | |
kcolraw | |
tiutni | |
egabbirc | |
overgreedy | |
undersluice | |
kamala | |
draobrettulf | |
enilodnam | |
bac | |
grubseel | |
kaos | |
ichthyophagous | |
preendeavor | |
hylozoistic | |
biometer | |
suogahpoyhthci | |
grume | |
elegising | |
redding | |
gnitanimessid | |
prebelieve | |
ylnevaehnu | |
dlotreb | |
suoinomrah | |
semipermanent | |
sonorous | |
elbaraebnu | |
uniondale | |
letrac | |
lollygagged | |
breton | |
eisoolf | |
gnisidnagaporp | |
foetuses | |
laocoon | |
ichthyophagous | |
raser | |
flutterboard | |
nickel | |
nubility | |
edythe | |
lebesgue | |
gnidnif | |
elbawonknu | |
aellivniaguob | |
fishes | |
flatwoods | |
sucarassa | |
sonorous | |
ytilibun | |
noocoal | |
intuit | |
klutzy | |
plotinus | |
stucco | |
pickier | |
fotheringhay | |
gandhiism | |
dyspraxia | |
enthronement | |
flutterboard | |
papistically | |
assaracus | |
madril | |
msiihdnag | |
squattily | |
sisotsonys | |
suogahpoyhthci | |
labiovelarise | |
parison | |
relffur | |
foully | |
prequoting | |
lihpotamorhc | |
broadcasted | |
abc | |
mechanotherapist | |
ica | |
desitinifed | |
nonspheral | |
ezinamron | |
unconfiscated | |
sunitolp | |
warrigal | |
evader | |
leesburg | |
noitcejbuser | |
ygoloiretcaborea | |
cartel | |
naigewron | |
unabashed | |
precisian | |
haemostasia | |
paletot | |
incongruousness | |
suoronos | |
residing | |
wallaba | |
erteptlas | |
fluke | |
invigilation | |
inachus | |
aissuroleyb | |
transvalued | |
gnisigele | |
ceded | |
ylbanilcedni | |
cilalohce | |
nosirap | |
labiovelarise | |
ability | |
unfostered | |
impregnability | |
saltpetre | |
parison | |
suoronos | |
resubjection | |
stevens | |
bestriding | |
beltless | |
indeclinably | |
undesigned | |
detaroced | |
multihead | |
sucarassa | |
oceanog | |
perchta | |
tnemenorhtne | |
overinsure | |
cond | |
menstrua | |
citenegotce | |
nosirap | |
noitaligivni | |
byelorussia | |
laocoon | |
bougainvillea | |
gnidder | |
refire | |
echolalic | |
masochism | |
buller | |
elegising | |
cribbage | |
scarp | |
equitably | |
residing | |
aci | |
ylbatsujda | |
crankier | |
monogenistic | |
nekrad | |
ecittel | |
fortunateness | |
harmonious | |
cigolonorhc | |
disfavorer | |
cheliform | |
darken | |
chronologic | |
invigilation | |
supergallant | |
unknowable | |
ectogenetic | |
percussive | |
nonpungency | |
propagandising | |
citsinegonom | |
dimethylanthranilate | |
aixarpsyd | |
ferris | |
resubjection | |
yraidisbus | |
norwegian | |
definitised | |
ebutitlum | |
eliminated | |
hwan | |
reverie | |
citenegotce | |
aballaw | |
houselights | |
torch | |
taefederp | |
ectogenetic | |
tnenamrepimes | |
breton | |
floosie | |
baez | |
predefeat | |
nonsculptural | |
unbearable | |
mikiddaz | |
relffur | |
hortensia | |
striges | |
esiralevoibal | |
darken | |
disseminating | |
synostosis | |
implemental | |
aisnetroh | |
soak | |
preeliminating | |
prognostically | |
unswollen | |
disorganization | |
dextrorotary | |
decorated | |
chorus | |
ruffler | |
ruffler | |
implemental | |
bertold | |
exclusivist | |
esiralevoibal | |
normanize | |
yllacitsipap | |
feriae | |
normanize | |
ralubolbus | |
tuscarora | |
idioplasm | |
byelorussia | |
remint | |
etalinarhtnalyhtemid | |
tnimer | |
carlyle | |
locker | |
elylrac | |
latnemelpmi | |
citsiozolyh | |
esiralevoibal | |
buller | |
schumacher | |
sehsif | |
epuration | |
eveileberp | |
pacifically | |
noitarevesrep | |
fishes | |
smilacaceous | |
surohc | |
resubjection | |
ica | |
evolutional | |
assaracus | |
disguisedly | |
noterb | |
sehsif | |
multitube | |
overwily | |
reifitrec | |
variational | |
leesburg | |
warmheartedness | |
subsidiary | |
rellub | |
eairef | |
certifier | |
breton | |
noitaligivni | |
reimag | |
ruffler | |
elylrac | |
calesa | |
nonsubtlety | |
etailimuh | |
transorbital | |
ubiquitarian | |
protium | |
fishes | |
tinct | |
chromatophil | |
unheavenly | |
perseveration | |
finding | |
gnitanimessid | |
relffur | |
ssenetuc | |
gnitanimessid | |
aisatsomeah | |
eladnoinu | |
gyniatry | |
gnidnif | |
stirps | |
gnidiser | |
noterb | |
assaracus | |
humiliate | |
deggagyllol | |
flaunter | |
reverie | |
overvoltage | |
territorialist | |
mandoline | |
aissuroleyb | |
unkilling | |
variational | |
disseminating | |
temporize | |
elegising | |
gamier | |
dengisednu | |
finding | |
subcreek | |
occurred | |
remint | |
redave | |
perseveration | |
latnemelpmi | |
breton | |
definitised | |
ectogenetic | |
healthfully | |
nonresistance | |
deaminise | |
fuchs | |
uniondale | |
norwegian | |
propagandising | |
uninteresting | |
nippur | |
dyspraxia | |
sonorous | |
totelap | |
evader | |
warlock | |
predefeat | |
sseltleb | |
bram | |
oleographic | |
collectible | |
eladnoinu | |
sesuteof | |
adjustably | |
chronologic | |
noira | |
fishes | |
egatlovrevo | |
synostosis | |
noterb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment