-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tickled the bash assoc array bug that Koichi worked around! Not sure if this is version-specific.
- Loading branch information
Andy Chu
committed
Jul 27, 2020
1 parent
6dc0cee
commit 288661d
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python2 | ||
""" | ||
word_freq.py | ||
""" | ||
from __future__ import print_function | ||
|
||
import sys | ||
|
||
|
||
def main(argv): | ||
try: | ||
iters = int(argv[1]) | ||
except IndexError: | ||
iters = 1000 | ||
|
||
text = sys.stdin.read() | ||
|
||
words = {} | ||
|
||
for i in xrange(iters): | ||
for word in text.split(): | ||
if word in words: | ||
words[word] += 1 | ||
else: | ||
words[word] = 1 | ||
|
||
for word in words: | ||
print("%d %s" % (words[word], word)) | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
main(sys.argv) | ||
except RuntimeError as e: | ||
print('FATAL: %s' % e, file=sys.stderr) | ||
sys.exit(1) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/bin/bash | ||
|
||
main() { | ||
iters=${1:-1000} | ||
|
||
# read it once | ||
read -d '' text | ||
|
||
declare -A words | ||
|
||
# do it a bunch of times | ||
for (( i = 0; i < iters; ++i )); do | ||
for word in $text; do # relies on word splitting | ||
|
||
# Hm this isn't correct in bash! | ||
#(( words["$word"] += 1 )) | ||
|
||
# This seems to work? wtf? | ||
# This causes a parse error in OSH though... Do we need two benchmarks? | ||
|
||
# Or maybe we need something to turn of static parsing? | ||
# similar to git | ||
|
||
(( words[\$word] += 1 )) | ||
done | ||
done | ||
|
||
# note: we can sort the output in the benchmark and assert that it's the same? | ||
|
||
for word in "${!words[@]}"; do | ||
echo "${words["$word"]} $word" | ||
done | ||
} | ||
|
||
main "$@" |