02-12-2024, 10:08 PM
And this version?
Should be around 3-4 times faster. What's interesting is that the old version takes around 30 sec. on my laptop. How is it possible? My laptop is pretty old and slow, so how come it is faster than your machines??? The new one takes 8 sec., btw.
Code:
unwanted_characters = []
counter = 0
for a = 0 to 96
unwanted_characters[counter] = chr(a)
counter = counter + 1
next
for a = 123 to 127
unwanted_characters[counter] = chr(a)
counter = counter + 1
next
open file 1, ("Hamlet.txt")
pln "Reading the file..."
x = fread(1)
while x
whole_text = whole_text + " " + x
x = fread(1)
wend
free file 1
whole_text = lower(whole_text)
foreach y in unwanted_characters
whole_text = replace(whole_text, y, " ")
next
all_words = split(whole_text, " ")
pln "Counting..."
words_number = 0
unique_words = []
foreach n in all_words
if len(n) > 1 or n = "a" or n = "i"
unique_words[n] = unique_words[n] + 1
words_number = words_number + 1
endif
next
pln "Saving the result..."
create file 2, "Words.txt"
wln file 2, "All words - " + words_number
wln file 2, "Unique words - " + sizeof(unique_words)
wln file 2, ""
y = 1
while sizeof(unique_words)
foreach a, b in unique_words
if b = y then
wln file 2, a + " - " + b
endif
next
free val unique_words, y
y = y + 1
wend
free file 2
Should be around 3-4 times faster. What's interesting is that the old version takes around 30 sec. on my laptop. How is it possible? My laptop is pretty old and slow, so how come it is faster than your machines??? The new one takes 8 sec., btw.