class PyWordSort(object):
def __init__(self, fileName):
print "init " + fileName
self.dataFile = open(fileName)
self.words = []
def sortedWords(self):
if len(self.words) == 0:
for line in self.dataFile:
for word in line.split():
self.words.append(word)
self.words.sort(lambda w,w1: cmp(w.lower(), w1.lower()))
return self.words
Can't resist... the Scala version:
ReplyDeletecase class WordSort(filename: String) {
import scala.io._
lazy val sortedWords =
(for {
line <- Source.fromFile(filename).getLines.toList
word <- line.trim.split(" ")
} yield word.trim).sort(_ < _)
}