diff options
| author | Archargelod <archargelod@gmail.com> | 2024-04-07 22:49:46 +0800 |
|---|---|---|
| committer | Archargelod <archargelod@gmail.com> | 2024-04-07 22:49:46 +0800 |
| commit | f0a59983d7ca27d1303de7ace94007491afc60a2 (patch) | |
| tree | ee7050d14b0af506b084c67490b2ae05cb921e20 /challenge-263 | |
| parent | 0495c7534995083a63370f8e93d3834bbe1f054d (diff) | |
| download | perlweeklychallenge-club-f0a59983d7ca27d1303de7ace94007491afc60a2.tar.gz perlweeklychallenge-club-f0a59983d7ca27d1303de7ace94007491afc60a2.tar.bz2 perlweeklychallenge-club-f0a59983d7ca27d1303de7ace94007491afc60a2.zip | |
weeks 49-52, 263 in Nim
Diffstat (limited to 'challenge-263')
| -rwxr-xr-x | challenge-263/archargelod/nim/ch_1.nim | 39 | ||||
| -rwxr-xr-x | challenge-263/archargelod/nim/ch_2.nim | 41 |
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-263/archargelod/nim/ch_1.nim b/challenge-263/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..1b7ccd008d --- /dev/null +++ b/challenge-263/archargelod/nim/ch_1.nim @@ -0,0 +1,39 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +# task: Write a script to return the list of indices in the sorted array where the element is same as the given target element. +# solution: no sorting, count elements that are less than target, that's our starting index, then for each copy add ind+1 to result + +proc sortedIndexes*(arr: openarray[int], target: int): seq[int] = + var firstInd = 0 + var count = 0 + for elem in arr: + if elem < target: + inc firstInd + elif elem == target: + inc count + + for i in 1..count: + result.add firstInd + inc firstInd + +when isMainModule: + import std/unittest + + const + Test = [ + (@[1, 5, 3, 2, 4, 2], 2), + (@[1, 2, 4, 3, 5], 6), + (@[5, 3, 2, 4, 2, 1], 4), + ] + Expected = [ + @[1, 2], + @[], + @[4], + ] + + suite "Target Index": + test "Example 1": + check sortedIndexes(Test[0][0], Test[0][1]) == Expected[0] + test "Example 2": + check sortedIndexes(Test[1][0], Test[1][1]) == Expected[1] + test "Example 3": + check sortedIndexes(Test[2][0], Test[2][1]) == Expected[2] diff --git a/challenge-263/archargelod/nim/ch_2.nim b/challenge-263/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..ceb635913a --- /dev/null +++ b/challenge-263/archargelod/nim/ch_2.nim @@ -0,0 +1,41 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/tables + +type + ItemGroup = tuple[id, count: int] + ItemCollection = seq[ItemGroup] + +proc merge*(collection1, collection2: openArray[ItemGroup]): ItemCollection = + var collected: OrderedTable[int, int] + + for items in [collection1, collection2]: + for item in items: + if collected.hasKeyOrPut(item.id, item.count): + collected[item.id] += item.count + + for key, val in collected: + result.add (key, val) + +when isMainModule: + import std/unittest + + const + Test = [ + (@[(1,1), (2,1), (3,2)], @[(2,2), (1,3)]), + (@[(1,2), (2,3), (1,3), (3,2)], @[(3,1), (1,3)]), + (@[(1,1), (2,2), (3,3)], @[(2,3), (2,4)]), + ] + Expected = [ + @[(1, 4), (2, 3), (3, 2)], + @[(1, 8), (2, 3), (3, 3)], + @[(1, 1), (2, 9), (3, 3)], + ] + + suite "Merge Items": + test "Example 1": + check merge(Test[0][0], Test[0][1]) == Expected[0] + test "Example 2": + check merge(Test[1][0], Test[1][1]) == Expected[1] + test "Example 2": + check merge(Test[2][0], Test[2][1]) == Expected[2] + |
