-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bec44c4
Showing
286 changed files
with
29,970 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,4 @@ | ||
var x = 12 | ||
var xptr = addr(x) # Get address of variable | ||
echo cast[int](xptr) # and print it | ||
xptr = cast[ptr int](0xFFFE) # Set the address |
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,22 @@ | ||
import strutils, sequtils, strfmt | ||
|
||
let textinfile = """Given$a$text$file$of$many$lines,$where$fields$within$a$line$ | ||
are$delineated$by$a$single$'dollar'$character,$write$a$program | ||
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$ | ||
column$are$separated$by$at$least$one$space. | ||
Further,$allow$for$each$word$in$a$column$to$be$either$left$ | ||
justified,$right$justified,$or$center$justified$within$its$column.""" | ||
|
||
var words = textinfile.splitLines.mapIt(seq[string], it.split '$') | ||
var maxs = newSeq[int](max words.mapIt(int, it.len)) | ||
|
||
for l in words: | ||
for j,w in l: | ||
maxs[j] = max(maxs[j], w.len+1) | ||
|
||
for i, align in ["<",">","^"]: | ||
echo(["Left", "Center", "Right"][i], " column-aligned output:") | ||
for l in words: | ||
for j,w in l: | ||
stdout.write w.format align & $maxs[j] | ||
stdout.write "\n" |
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,20 @@ | ||
#include <stdio.h> | ||
|
||
extern int Query (char * Data, size_t * Length); | ||
|
||
int main (int argc, char * argv []) | ||
{ | ||
char Buffer [1024]; | ||
size_t Size = sizeof (Buffer); | ||
|
||
if (0 == Query (Buffer, &Size)) | ||
{ | ||
printf ("failed to call Query\n"); | ||
} | ||
else | ||
{ | ||
char * Ptr = Buffer; | ||
while (Size-- > 0) putchar (*Ptr++); | ||
putchar ('\n'); | ||
} | ||
} |
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,9 @@ | ||
proc Query*(data: var array[1024, char], length: var cint): cint {.exportc.} = | ||
const text = "Here am I" | ||
if length < text.len: | ||
return 0 | ||
|
||
for i in 0 .. <text.len: | ||
data[i] = text[i] | ||
length = text.len | ||
return 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,48 @@ | ||
import math | ||
|
||
type Circle = tuple[x, y, r: float] | ||
|
||
proc solveApollonius(c1, c2, c3: Circle; s1, s2, s3: float): Circle = | ||
let | ||
v11 = 2*c2.x - 2*c1.x | ||
v12 = 2*c2.y - 2*c1.y | ||
v13 = c1.x*c1.x - c2.x*c2.x + c1.y*c1.y - c2.y*c2.y - c1.r*c1.r + c2.r*c2.r | ||
v14 = 2*s2*c2.r - 2*s1*c1.r | ||
|
||
v21 = 2*c3.x - 2*c2.x | ||
v22 = 2*c3.y - 2*c2.y | ||
v23 = c2.x*c2.x - c3.x*c3.x + c2.y*c2.y - c3.y*c3.y - c2.r*c2.r + c3.r*c3.r | ||
v24 = 2*s3*c3.r - 2*s2*c2.r | ||
|
||
w12 = v12/v11 | ||
w13 = v13/v11 | ||
w14 = v14/v11 | ||
|
||
w22 = v22/v21-w12 | ||
w23 = v23/v21-w13 | ||
w24 = v24/v21-w14 | ||
|
||
p = -w23/w22 | ||
q = w24/w22 | ||
m = -w12*p-w13 | ||
n = w14 - w12*q | ||
|
||
a = n*n + q*q - 1 | ||
b = 2*m*n - 2*n*c1.x + 2*p*q - 2*q*c1.y + 2*s1*c1.r | ||
c = c1.x*c1.x + m*m - 2*m*c1.x + p*p + c1.y*c1.y - 2*p*c1.y - c1.r*c1.r | ||
|
||
d = b*b-4*a*c | ||
rs = (-b-sqrt(d))/(2*a) | ||
|
||
xs = m+n*rs | ||
ys = p+q*rs | ||
|
||
return (xs, ys, rs) | ||
|
||
let | ||
c1: Circle = (0.0, 0.0, 1.0) | ||
c2: Circle = (4.0, 0.0, 1.0) | ||
c3: Circle = (2.0, 4.0, 2.0) | ||
|
||
echo solveApollonius(c1, c2, c3, 1.0, 1.0, 1.0) | ||
echo solveApollonius(c1, c2, c3, -1.0, -1.0, -1.0) |
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,3 @@ | ||
var str = "123456" | ||
str.add("78") # two ways | ||
str &= "9!" # to append |
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,22 @@ | ||
var # fixed size arrays | ||
x = [1,2,3,4,5,6,7,8,9,10] # type and size automatically inferred | ||
y: array[1..5, int] = [1,2,3,4,5] # starts at 1 instead of 0 | ||
z: array['a'..'z', int] # indexed using characters | ||
|
||
x[0] = x[1] + 1 | ||
echo x[0] | ||
echo z['d'] | ||
|
||
x[7..9] = y[3..5] # copy part of array | ||
|
||
var # variable size sequences | ||
a = @[1,2,3,4,5,6,7,8,9,10] | ||
b: seq[int] = @[1,2,3,4,5] | ||
|
||
a[0] = a[1] + 1 | ||
echo a[0] | ||
|
||
a.add(b) # append another sequence | ||
a.add(200) # append another element | ||
echo a.pop() # pop last item, removing and returning it | ||
echo a |
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,28 @@ | ||
import tables | ||
|
||
var | ||
hash = initTable[string, int]() # empty hash table | ||
hash2 = {"key1": 1, "key2": 2}.toTable # hash table with two keys | ||
hash3 = [("key1", 1), ("key2", 2)].toTable # hash table from tuple array | ||
hash4 = @[("key1", 1), ("key2", 2)].toTable # hash table from tuple seq | ||
value = hash2["key1"] | ||
|
||
hash["spam"] = 1 | ||
hash["eggs"] = 2 | ||
hash.add("foo", 3) | ||
|
||
echo "hash has ", hash.len, " elements" | ||
echo "hash has key foo? ", hash.hasKey("foo") | ||
echo "hash has key bar? ", hash.hasKey("bar") | ||
|
||
echo "iterate pairs:" # iterating over (key, value) pairs | ||
for key, value in hash: | ||
echo key, ": ", value | ||
|
||
echo "iterate keys:" # iterating over keys | ||
for key in hash.keys: | ||
echo key | ||
|
||
echo "iterate values:" # iterating over values | ||
for key in hash.values: | ||
echo key |
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,32 @@ | ||
# Using a local buffer, slower unfortunately | ||
# But fgets does not regard CR, so this is different from readLine | ||
# Same as Python though | ||
|
||
proc fgets(c: cstring, n: int, f: TFile): cstring {. | ||
importc: "fgets", header: "<stdio.h>", tags: [FReadIO].} | ||
|
||
proc myReadLine(f: TFile, line: var TaintedString): bool = | ||
var buf {.noinit.}: array[8192, char] | ||
setLen(line.string, 0) | ||
result = true | ||
while true: | ||
if fgets(buf, 8192, f) == nil: | ||
result = false | ||
break | ||
if buf[cstring(buf).len-1] == '\l': | ||
buf[cstring(buf).len-1] = '\0' | ||
add(line, cstring(buf)) | ||
break | ||
add(line, cstring(buf)) | ||
|
||
# compute average line length | ||
var count = 0 | ||
var sum = 0 | ||
|
||
var line = "" | ||
while stdin.myReadLine(line): | ||
count += 1 | ||
sum += line.len | ||
|
||
echo "Average line length: ", | ||
if count > 0: sum / count else: 0 |
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,19 @@ | ||
import tables, math | ||
|
||
#proc `~` * [IDX, A, B](arr: array[IDX, tuple[key: A, val: B]]): TTable[A, B] = | ||
# toTable(arr) | ||
# | ||
#var t = ~[("foo", 13), ("bar", 14)] | ||
#echo t | ||
#echo t["foo"] | ||
# | ||
#proc `{}`[A,B](t: var TTable[A, B], pairs: varargs[tuple[key: A, val: B]]) = | ||
# t = initTable[A, B](nextPowerOfTwo(pairs.len+10)) | ||
# for key, val in items(pairs): t[key] = val | ||
|
||
#var t: TTable[string, int] | ||
#t{("foo", 12)} | ||
#echo t | ||
|
||
var x = {"key1": 14, "key2", "key3": 12}.toTable | ||
echo x["key1"] |
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,30 @@ | ||
import math | ||
randomize() | ||
|
||
proc shuffle(s: var string) = | ||
for i in countdown(s.high, 0): | ||
swap(s[i], s[random(s.len)]) | ||
|
||
proc gen(n): string = | ||
result = newString(2 * n) | ||
for i in 0 .. <n: | ||
result[i] = '[' | ||
for i in n .. <(2*n): | ||
result[i] = ']' | ||
shuffle(result) | ||
|
||
proc balanced(txt): bool = | ||
var b = 0 | ||
for c in txt: | ||
case c | ||
of '[': | ||
inc(b) | ||
of ']': | ||
dec(b) | ||
if b < 0: return false | ||
else: discard | ||
b == 0 | ||
|
||
for n in 0..9: | ||
let s = gen(n) | ||
echo "'", s, "' is ", (if balanced(s): "balanced" else: "not balanced") |
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,6 @@ | ||
import strutils | ||
|
||
for i in -100..100: | ||
echo toBin(i) | ||
echo toOct(i) | ||
echo toHex(i) |
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,12 @@ | ||
import algorithm | ||
|
||
let s = @[2,3,4,5,6,7,8,9,10,12,14,16,18,20,22,25,27,30] | ||
echo binarySearch(s, 10) | ||
|
||
proc binarySearch[T](a: openArray[T], key: T): int = | ||
var b = len(a) | ||
while result < b: | ||
var mid = (result + b) div 2 | ||
if a[mid] < key: result = mid + 1 | ||
else: b = mid | ||
if result >= len(a) or a[result] != key: result = -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,6 @@ | ||
proc binomialCoeff(n, k): int = | ||
result = 1 | ||
for i in 1..k: | ||
result = result * (n-i+1) div i | ||
|
||
echo binomialCoeff(5, 3) |
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,9 @@ | ||
proc bitwise(a, b) = | ||
echo "a and b: " , a and b | ||
echo "a or b: ", a or b | ||
echo "a xor b: ", a xor b | ||
echo "not a: ", not a | ||
echo "a << b: ", a shl b | ||
echo "a >> b: ", a shr b | ||
|
||
bitwise(12, 13) |
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,22 @@ | ||
import math | ||
randomize() | ||
|
||
proc shuffle[T](x: var openarray[T]) = | ||
for i in countdown(x.high, 0): | ||
let j = random(i + 1) | ||
swap(x[i], x[j]) | ||
|
||
proc isSorted[T](s: openarray[T]): bool = | ||
var last = low(T) | ||
for c in s: | ||
if c < last: | ||
return false | ||
last = c | ||
return true | ||
|
||
proc bogoSort[T](a: var openarray[T]) = | ||
while not isSorted a: shuffle a | ||
|
||
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782] | ||
bogoSort a | ||
echo a |
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,5 @@ | ||
if true: echo "yes" | ||
if false: echo "no" | ||
|
||
# Other objects never represent true or false: | ||
if 2: echo "compile error" |
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,12 @@ | ||
proc bottles(n): string = | ||
case n | ||
of 0: "no more bottles" | ||
of 1: "1 bottle" | ||
else: $n & " bottles" | ||
|
||
for n in countdown(99, 1): | ||
echo n.bottles, " of beer on the wall, ", n.bottles, " of beer." | ||
echo "Take one down and pass it around, ", (n-1).bottles, " on the wall.\n" | ||
|
||
echo "No more bottles of beer on the wall, no more bottles of beer." | ||
echo "Go to the store and buy some more, 99 bottles of beer on the wall." |
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,18 @@ | ||
import strutils, strfmt | ||
|
||
const names = [ | ||
"North", "North by east", "North-northeast", "Northeast by north", | ||
"Northeast", "Northeast by east", "East-northeast", "East by north", | ||
"East", "East by south", "East-southeast", "Southeast by east", | ||
"Southeast", "Southeast by south","South-southeast", "South by east", | ||
"South", "South by west", "South-southwest", "Southwest by south", | ||
"Southwest", "Southwest by west", "West-southwest", "West by south", | ||
"West", "West by north", "West-northwest", "Northwest by west", | ||
"Northwest", "Northwest by north", "North-northwest", "North by west", "North"] | ||
|
||
for i in 0..32: | ||
let j = i mod 32 | ||
var d = float(i) * 11.25 | ||
if i mod 3 == 1: d += 5.62 | ||
if i mod 3 == 2: d -= 5.62 | ||
printlnfmt("{:2} {:18} {:>6}", j + 1, names[j], d.formatFloat(ffDecimal, 2)) |
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,9 @@ | ||
import math | ||
|
||
while True: | ||
let a = random(20) | ||
echo a | ||
if a == 10: | ||
break | ||
let b = random(20) | ||
echo b |
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,15 @@ | ||
proc bubbleSort[T](a: var openarray[T]) = | ||
var | ||
j = 1 | ||
t = true | ||
for n in countdown(a.len-2, 0): | ||
if not t: break | ||
t = false | ||
for j in 0..n: | ||
if a[j] <= a[j+1]: continue | ||
swap a[j], a[j+1] | ||
t = true | ||
|
||
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782] | ||
bubbleSort a | ||
echo a |
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,14 @@ | ||
import strutils | ||
|
||
proc caesar(s: string, k: int, decode = false): string = | ||
var k = if decode: 26 - k else: k | ||
result = "" | ||
for i in toUpper(s): | ||
if ord(i) >= 65 and ord(i) <= 90: | ||
result.add(chr((ord(i) - 65 + k) mod 26 + 65)) | ||
|
||
let msg = "The quick brown fox jumped over the lazy dogs" | ||
echo msg | ||
let enc = caesar(msg, 11) | ||
echo enc | ||
echo caesar(enc, 11, decode = true) |
Oops, something went wrong.