summary refs log tree commit diff stats
path: root/tests/iter/tpermutations.nim
blob: 30a66460fb9f9765711883a581a79683431709c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
discard """
output: '''
@[@[1.0, 2.0], @[3.0, 4.0]]
perm: 10.0 det: -2.0
@[@[1.0, 2.0, 3.0, 4.0], @[4.0, 5.0, 6.0, 7.0], @[7.0, 8.0, 9.0, 10.0], @[10.0, 11.0, 12.0, 13.0]]
perm: 29556.0 det: 0.0
@[@[0.0, 1.0, 2.0, 3.0, 4.0], @[5.0, 6.0, 7.0, 8.0, 9.0], @[10.0, 11.0, 12.0, 13.0, 14.0], @[15.0, 16.0, 17.0, 18.0, 19.0], @[20.0, 21.0, 22.0, 23.0, 24.0]]
perm: 6778800.0 det: 0.0
'''
"""


import sequtils, sugar

iterator permutations*[T](ys: openArray[T]): tuple[perm: seq[T], sign: int] =
  var
    d = 1
    c = newSeq[int](ys.len)
    xs = newSeq[T](ys.len)
    sign = 1

  for i, y in ys: xs[i] = y
  yield (xs, sign)

  block outter:
    while true:
      while d > 1:
        dec d
        c[d] = 0
      while c[d] >= d:
        inc d
        if d >= ys.len: break outter

      let i = if (d and 1) == 1: c[d] else: 0
      swap xs[i], xs[d]
      sign *= -1
      yield (xs, sign)
      inc c[d]

proc det(a: seq[seq[float]]): float =
  let n = toSeq 0..a.high
  for sigma, sign in n.permutations:
    result += sign.float * n.map((i: int) => a[i][sigma[i]]).foldl(a * b)

proc perm(a: seq[seq[float]]): float =
  let n = toSeq 0..a.high
  for sigma, sign in n.permutations:
    result += n.map((i: int) => a[i][sigma[i]]).foldl(a * b)

for a in [
    @[ @[1.0, 2.0]
     , @[3.0, 4.0]
    ],
    @[ @[ 1.0,  2,  3,  4]
     , @[ 4.0,  5,  6,  7]
     , @[ 7.0,  8,  9, 10]
     , @[10.0, 11, 12, 13]
    ],
    @[ @[ 0.0,  1,  2,  3,  4]
     , @[ 5.0,  6,  7,  8,  9]
     , @[10.0, 11, 12, 13, 14]
     , @[15.0, 16, 17, 18, 19]
     , @[20.0, 21, 22, 23, 24]
    ] ]:
  echo a
  echo "perm: ", a.perm, " det: ", a.det

# bug #3499 last snippet fixed
# bug 705  last snippet fixed
s="p">: discard except: doAssert(false, "HttpRequestError should have been raised") when false: # w3.org now blocks travis, so disabled: # Multipart test. var data = newMultipartData() data["output"] = "soap12" data["uploaded_file"] = ("test.html", "text/html", "<html><head></head><body><p>test</p></body></html>") resp = await client.post("http://validator.w3.org/check", multipart = data) doAssert(resp.code.is2xx) # onProgressChanged when manualTests: proc onProgressChanged(total, progress, speed: BiggestInt) {.async.} = echo("Downloaded ", progress, " of ", total) echo("Current rate: ", speed div 1000, "kb/s") client.onProgressChanged = onProgressChanged await client.downloadFile("http://speedtest-ams2.digitalocean.com/100mb.test", "100mb.test") # HTTP/1.1 without Content-Length - issue #10726 var serverFd = makeIPv6HttpServer("::1", Port(18473), "HTTP/1.1 200 \c\L" & "\c\L" & "Here comes reply") resp = await client.request("http://[::1]:18473/") body = await resp.body doAssert(body == "Here comes reply") serverFd.closeSocket() client.close() # Proxy test #when manualTests: # client = newAsyncHttpClient(proxy = newProxy("http://51.254.106.76:80/")) # var resp = await client.request("https://github.com") # echo resp proc syncTest() = var client = newHttpClient() var resp = client.request("http://example.com/", HttpGet) doAssert(resp.code.is2xx) doAssert("<title>Example Domain</title>" in resp.body) resp = client.request("http://example.com/404") doAssert(resp.code.is4xx) doAssert(resp.code == Http404) doAssert(resp.status == $Http404) resp = client.request("https://google.com/") doAssert(resp.code.is2xx or resp.code.is3xx) # getContent try: discard client.getContent("https://google.com/404") doAssert(false, "HttpRequestError should have been raised") except HttpRequestError: discard except: doAssert(false, "HttpRequestError should have been raised") when false: # w3.org now blocks travis, so disabled: # Multipart test. var data = newMultipartData() data["output"] = "soap12" data["uploaded_file"] = ("test.html", "text/html", "<html><head></head><body><p>test</p></body></html>") resp = client.post("http://validator.w3.org/check", multipart = data) doAssert(resp.code.is2xx) # onProgressChanged when manualTests: proc onProgressChanged(total, progress, speed: BiggestInt) = echo("Downloaded ", progress, " of ", total) echo("Current rate: ", speed div 1000, "kb/s") client.onProgressChanged = onProgressChanged client.downloadFile("http://speedtest-ams2.digitalocean.com/100mb.test", "100mb.test") client.close() # SIGSEGV on HEAD body read: issue #16743 block: let client = newHttpClient() let resp = client.head("http://httpbin.org/head") doAssert(resp.body == "") when false: # Disabled for now because it causes troubles with AppVeyor # Timeout test. client = newHttpClient(timeout = 1) try: resp = client.request("http://example.com/") doAssert false, "TimeoutError should have been raised." except TimeoutError: discard except: doAssert false, "TimeoutError should have been raised." proc ipv6Test() = var client = newAsyncHttpClient() let serverFd = makeIPv6HttpServer("::1", Port(18473), "HTTP/1.1 200 OK\r\LContent-Length: 0\r\LConnection: Closed\r\L\r\L") var resp = waitFor client.request("http://[::1]:18473/") doAssert(resp.status == "200 OK") serverFd.closeSocket() client.close() ipv6Test() when enableRemoteNetworking: syncTest() waitFor(asyncTest())