Skip to content

Commit 0817ffb

Browse files
committed
Revised clockhands and permutations and added anagrams
1 parent e50fa10 commit 0817ffb

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

nim/anagrams.nim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import std/[strutils, sequtils, cmdline]
2+
3+
type InputArgumentError* = object of IOError
4+
5+
proc generatePermutations(a: var seq[char], n: int) =
6+
if n <= 0:
7+
echo a.join("")
8+
else:
9+
for i in 0 ..< n:
10+
generatePermutations(a, n - 1)
11+
let j = if n mod 2 == 0: 0 else: i
12+
swap(a[j], a[n])
13+
generatePermutations(a, n - 1)
14+
15+
when isMainModule:
16+
var args = commandLineParams()
17+
if len(args) != 1:
18+
raise newException(InputArgumentError, "ERROR: There must be exactly 1 argument.")
19+
var letters = toSeq(args[0].items)
20+
generatePermutations(letters, len(letters) - 1)

nim/clockhands.nim

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import std/strformat
22

33
for i in 0..10:
4-
let t = (43200 * i + 21600) div 11
5-
let h = t div 3600
6-
let m = (t div 60) mod 60
7-
let s = t mod 60
4+
let
5+
t = (43200 * i + 21600) div 11
6+
h = t div 3600
7+
m = (t div 60) mod 60
8+
s = t mod 60
89
echo &"{(if h == 0: 12 else: h):02}:{m:02}:{s:02}"

nim/permutations.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import std/[strutils, cmdline]
22

3-
type InputArgumentError* = object of Exception
3+
type InputArgumentError* = object of IOError
44

55
proc printPermutations(a: var seq[string], n: int) =
66
if n <= 0:

nim/test.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ function Assert-MatchTests {
99
}
1010

1111
$Error.clear()
12+
nim c "$PSScriptRoot\anagrams.nim" && . "$PSScriptRoot\anagrams.exe" rats |
13+
Compare-Object (Get-Content "$PSScriptRoot\..\test\rats_heap_expected") |
14+
Assert-MatchTests &&
1215
nim c "$PSScriptRoot\clockhands.nim" && . "$PSScriptRoot\clockhands.exe" |
1316
Compare-Object (Get-Content "$PSScriptRoot\..\test\clockhands_expected") |
1417
Assert-MatchTests &&

0 commit comments

Comments
 (0)