diff options
| author | Archargelod <archargelod@gmail.com> | 2024-02-21 01:33:32 +0800 |
|---|---|---|
| committer | Archargelod <archargelod@gmail.com> | 2024-02-21 01:33:32 +0800 |
| commit | 77f7f91c73773aa6cfd2713dea32c705a1971b58 (patch) | |
| tree | 368b987ea819a1083930390e2cef19151827c4d6 /challenge-005 | |
| parent | ddd42e0db3017a5ec3108d09efba477f19e7f04b (diff) | |
| download | perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.tar.gz perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.tar.bz2 perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.zip | |
Weeks 5-13, 257 in Nim
Diffstat (limited to 'challenge-005')
| -rw-r--r-- | challenge-005/archargelod/README | 1 | ||||
| -rwxr-xr-x | challenge-005/archargelod/nim/ch_1.nim | 30 | ||||
| -rwxr-xr-x | challenge-005/archargelod/nim/ch_2.nim | 16 |
3 files changed, 47 insertions, 0 deletions
diff --git a/challenge-005/archargelod/README b/challenge-005/archargelod/README new file mode 100644 index 0000000000..6cd57e1074 --- /dev/null +++ b/challenge-005/archargelod/README @@ -0,0 +1 @@ +Solution by archargelod diff --git a/challenge-005/archargelod/nim/ch_1.nim b/challenge-005/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..43f865180d --- /dev/null +++ b/challenge-005/archargelod/nim/ch_1.nim @@ -0,0 +1,30 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/[strutils, algorithm, sets] + +let dict = block: + var tmp: Hashset[string] + for word in lines "/usr/share/dict/words": + tmp.incl word + tmp + +proc allAnagrams(word: string): seq[string] = + var tmp = word + while tmp.nextPermutation(): + let word = tmp.join() + if word in dict: result.add word + + tmp = word + while tmp.prevPermutation(): + let word = tmp.join() + if word in dict: result.add word + +when isMainModule: + import std/unittest + + const + TestWord1 = "god" + ExpectedAnagrams1 = sorted ["dog"] + + suite "All anagrams for word": + test "should return all anagrams for \'god\'": + check sorted(allAnagrams(TestWord1)) == ExpectedAnagrams1 diff --git a/challenge-005/archargelod/nim/ch_2.nim b/challenge-005/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..dbe7b063fe --- /dev/null +++ b/challenge-005/archargelod/nim/ch_2.nim @@ -0,0 +1,16 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/[algorithm, strutils, strformat, tables] + +proc mostAnagramWord(dictfile: string): (seq[char], int) = + var counts: CountTable[seq[char]] + for word in lines dictFile: + counts.inc word.sorted() + + counts.largest + +proc main = + let (val, cnt) = mostAnagramWord("/usr/share/dict/words") + echo &"Word with most anagrams is '{val.join()}' with count of {$cnt} possible anagrams." + +when isMainModule: + main() |
