Renumber to make the flow better

This commit is contained in:
Crista Lopes
2014-01-05 07:40:17 -08:00
parent ce0bef372e
commit f0445b3c61
13 changed files with 0 additions and 0 deletions

11
06-code-golf/README.md Normal file
View File

@@ -0,0 +1,11 @@
Style #4
==============================
Constraints:
- As few lines of code as possible
Possible names:
- Code golf
- Try hard

8
06-code-golf/tf-06-1.py Normal file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env python
import re, string, sys
stops = set(open("../stop_words.txt").read().split(",") + list(string.ascii_lowercase))
words = [x.lower() for x in re.split("[^a-zA-Z]+", open(sys.argv[1]).read()) if len(x) > 0 and x.lower() not in stops]
unique_words = list(set(words))
unique_words.sort(lambda x, y: cmp(words.count(y), words.count(x)))
print "\n".join(["%s - %s" % (x, words.count(x)) for x in unique_words[:25]])

11
06-code-golf/tf-06-pn.py Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env python
# My golf score is slightly lower!
# Best wishes, Peter Norvig
import re, sys, collections
stopwords = set(open('../stop_words.txt').read().split(','))
words = re.findall('[a-z]{2,}', open(sys.argv[1]).read().lower())
counts = collections.Counter(w for w in words if w not in stopwords)
for (w, c) in counts.most_common(25):
print w, '-', c

6
06-code-golf/tf-06.py Executable file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/env python
import heapq, re, sys
words = re.findall("[a-z]{2,}", open(sys.argv[1]).read().lower())
for w in heapq.nlargest(25, set(words) - set(open("../stop_words.txt").read().split(",")), words.count):
print w, "-", words.count(w)

43
06-code-golf/tf-06.scala Normal file
View File

@@ -0,0 +1,43 @@
/**
Faithful conversion of tf-04.py to scala, avg execution time 5.2 seconds
$ time scala tf04a ../pride-and-prejudice.txt
(Mr,786)
(Elizabeth,635)
(very,473)
(Darcy,417)
(such,378)
(Mrs,343)
(much,325)
(more,325)
(Bennet,322)
(Bingley,305)
(Jane,295)
(Miss,281)
(one,261)
(know,239)
(herself,227)
(before,225)
(sister,218)
(soon,214)
(never,214)
(though,212)
(think,210)
(time,203)
(now,197)
(Wickham,194)
(well,188)
real 0m5.237s
*/
object tf04 extends App {
def l(f:String) = io.Source.fromFile(f).getLines.mkString(",")
val s = l("../stop_words.txt").split(",") ++ (1 to 26).map(i=>String.valueOf(Character.toChars(96+i)))
l(args(0)).split("[^a-zA-Z]+").filter(x => !s.contains(x.toLowerCase))
.distinct
.map(u=> (u,l(args(0)).split("[^a-zA-Z]+").filter(x => !s.contains(x.toLowerCase)).count(_==u)))
.sortBy(-_._2)
.take(25)
.foreach(println)
}