1. Laurens Rodriguez
  2. love

Commits

Laurens Rodriguez  committed 59278dc

Updating demo script, deleting all other test scripts. (They are in github)

  • Participants
  • Parent commits 95b7f00
  • Branches default

Comments (0)

Files changed (3)

File bin/windows/decrypt.kia

  • Ignore whitespace
-/*========================*/
-/*  Prob 01.              */
-/*  Decipher the text.    */
-/*------------------------*/
-
-// The text to decipher.
-var text = [[
-Uid nx, aex jcdjipx iu wzux zp, ta wxtpa jtdaws, ai etkx vis.
-Dcos zyexdzaxr aex Jxdw jezwipijes iu etkzyg nidx aety iyx hts
-ai ri aex ptnx aezyg. Z zyexdzaxr aeta jezwipijes udin Wtdds Htww,
-hei zp ns exdi tqactwws. Z htya ai ntfx Dcos cpxdp udxx. Z htya ai
-gzkx aexn aex udxxrin ai qeiipx. Jxijwx tdx rzuuxdxya. Jxijwx qeiipx
-rzuuxdxya qdzaxdzt. Oca zu aexdx zp t oxaaxd hts tniyg ntys
-twaxdytazkxp, Z htya ai xyqicdtgx aeta hts os ntfzyg za qinuidatowx.
-Pi aeta'p heta Z'kx adzxr ai ri.
-Z htya ai piwkx jdiowxnp Z nxxa zy aex rtzws wzux os cpzyg qinjcaxdp,
-pi Z yxxr ai hdzax jdigdtnp. Os cpzyg Dcos, Z htya ai qiyqxyadtax aex
-aezygp Z ri, yia aex ntgzqtw dcwxp iu aex wtygctgx, wzfx patdazyg hzae
-jcowzq kizr  pinxaezyg pinxaezyg pinxaezyg ai pts, "jdzya exwwi hidwr."
-Z vcpa htya ai pts, "jdzya aezp!" Z riy'a htya tww aex pcddicyrzyg
-ntgzq fxshidrp. Z vcpa htya ai qiyqxyadtax iy aex atpf. Aeta'p aex otpzq
-zrxt. Pi Z etkx adzxr ai ntfx Dcos qirx qiyqzpx tyr pcqqzyqa.
-Scfzezdi Ntapcniai. (hhh.tdaznt.qin/zyak/dcos)
-]]
-
-// This is the frequency table of the original text (from more frequent to less frequent).
-var freqLang = "TEOIARNSHLMYUCWDGPFBVKJ"
-
-// Create frequency table of the encrypted text.
-var len = string.len(text)
-var frequency = []
-
-for each (var k = 1 to len) {
-	var c = string.upper(string.sub(text, k, k));
-    // Check if character is an uppercase letter.
-	if (string.find(c, "%u") != null) {
-		if (frequency[c] == null) {
-            frequency[c] = 1
-        }
-		else {
-            frequency[c] += 1
-        }
-	}
-}
-
-
-// Create table for sorting the text frequency.
-var sortFreq = {}
-for each (var k,v in pairs(frequency)) {
-    table.insert(sortFreq, {key: k, value: v})
-}
-
-// Sort in descending order.
-table.sort(sortFreq, function(a, b) { return (a.value > b.value) })
-
-// Create dictionary for deciphering.
-var dic = {}
-var freqText = ''
-var index = 1
-
-for each (var k,v in pairs(sortFreq)) {
-    ////print(v.key, v.value)
-    freqText ..= v.key
-    dic[v.key] = string.sub(freqLang, index, index)
-    index += 1
-}
-
-////print(freqText); // encrypted text frequency
-////print(freqLang); // original text frequency
-
-var decrypted = ''
-
-for each (var k = 1 to len) {
-	var upper = false;
-	var c = string.sub(text, k, k)
-
-    if (string.find(c, "%u")) {
-        upper = true
-    }
-	else {
-        c = string.upper(c);
-    }
-
-	if (dic[c] != null) {
-		if (upper)	{
-            decrypted ..= dic[c]
-        }
-		else {
-            decrypted ..= string.lower(dic[c])
-        }
-    }
-	else {
-        decrypted ..= c
-    }
-}
-
-print(decrypted)

File bin/windows/test.kia

View file
  • Ignore whitespace
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 var x, y = "private x", "private y"
 public var x = ["asdasdad"]
 global.x.gg = "gg"
 print(global.x.gg, global.y)
 print(x, y)
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 var q = {}
 public var q = {}
 function q.f() {
 global.q.f()
 q.f()
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 var t = [100,200,300]
 for each (var k = 0 to 2) {
     print(k ..") "..t[k])
     print(i .."] "..tostring(q[i]))
 }
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 var A = {tt: {x: 20}}
 function A::ax(yy) {
+    // NOTE TOFIX 
     ////this.tt.x += yy
 }
 A::ax(10)
 print(A.tt.x)
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 function fibonacci(n) {
     if (n <= 1) {
         return 1
 }
 print(fibonacci(10))
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 function fact(n, ans) {
     call:
     if (n == 0) {
 }
 print(fact(5, 1))
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 for each (var z = 1 to 10) {
     for each (var y = 1 to 10) {
         for each (var x = 1 to 10) {
     zcontinue:
 }
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 for each (var i = 2 to 9) {
     var s = i .. ") "
     for (var j,p = 1,1; p < 1_000_000; p *= i) {
     print(s)
 }
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 function list_iterator(t) {
     var i = -1
     var n = $t
     print(element)
 }
 
-//----------------------------------------------------------------------------------------
-dofile('to_string.kia');
-dofile('decrypt.kia');
-
+//------------------------------------------------------------------------------
 // Sieve of Eratosthenes
 function primes(limit) {
     var sieve = []
 }
 print(table.concat(primes(100),","))
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 function printPowers(base, count) {
     for (var k = 0; k < count; k += 1) {
         print(base.."^"..k.." = "..(base ** k))
 }
 printPowers(2, 10)
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 function fizzbuzz(min, max) {
     var i = min
     do {
 }
 fizzbuzz(5, 15)
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 function fibonacci(n) {
     if (n < 3) {
         return n - 1
     n += 1
 } while (n <= 20)
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 assert($"0123456789" == 10)
 assert($[1,2,3,4,5,6] == 6)
 assert(${key:1} == 0)
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 public var x = 10     // global variable
 {                     // new block
     var x = global.x  // new 'x', with value 10
 }
 print(global.x)       // 10  (the global one)
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 var closures = []
 var cx = 20
 for (var i = 0; i < 10; i += 1) {
 print("closures[0](): "..closures[0]())
 print("closures[1](): "..closures[1]())
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 function trueExpr() { 
     print("trueExpr")
     return 10
 var x = (y < 6)? "less than 6" : (y % 2 == 0)? "even" : "odd"
 print(x) // Output: even
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 var s1 = 'testing '
 s1 ..= "concatenation"
 print(s1) // prints: "testing concatenation"
 
-////var mixed = {1, 3, name: "foo", ['new']: 234}  // mixed table constructor disallowed
+//------------------------------------------------------------------------------
+// NOTE mixed table construction is disallowed:
+////var mixed = {1, 3, name: "foo", ['new']: 234}  
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 function getAverage(array) {
     var sum = 0
-    var length = $array
-    if (length < 1) {
+    var lengthArray = $array
+    if (lengthArray < 1) {
         return null
     }
-    for (var k = 0; k < length; k += 1) {
+    for (var k = 0; k < lengthArray; k += 1) {
         sum += array[k]
     }
-    return sum / length
+    return sum / lengthArray
 }
 print(getAverage([10, 20, 40, 50])) 
 print(getAverage([]))
 
-//----------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
 // 99 Bottles of beer: http://99-bottles-of-beer.net/lyrics.html
 function songOfBeers(beers) {
     
     function bottlesOfBeer(i)    { return i .. ' bottles of beer' }
     function bottlesOnTheWall(i) { return bottlesOfBeer(i) .. ' on the wall' }
-    function takeOneDown()       { return '\nTake one down and pass it around, ' }
+    function takeOneDown()   { return '\nTake one down and pass it around, ' }
 
     function createVerse(i) {
         return function() {
         }
         else {
             table.insert(verses, function() {
-                return bottlesOnTheWall('No more') .. ', ' .. bottlesOfBeer('no more') 
+                return bottlesOnTheWall('No more') .. ', ' 
+                       .. bottlesOfBeer('no more') 
                        .. '.\nGo to the store and buy some more, ' 
                        .. bottlesOnTheWall(99) .. '.\n'
             })
     return verses
 }
 
-for each (var k, verse in ipairs(songOfBeers(4))) { // must go with 99!
+for each (var k, verse in ipairs(songOfBeers(4))) { // real song goes with 99!
     print(verse())
 }
 
-//*///------------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
+var arg = 'T'
+var vehicle = ((arg == 'B')? 'bus' :
+               (arg == 'A')? 'airplane' :
+               (arg == 'T')? 'train' :
+               (arg == 'C')? 'car' :
+               (arg == 'H')? 'horse' : 'feet')
+assert(vehicle == 'train')
+
+//------------------------------------------------------------------------------
+// Given that PI can be estimated using the function:
+// PI = 4 * (1 � 1/3 + 1/5 � 1/7 + ...) with more terms giving greater accuracy, 
+// write a function that calculates PI to an accuracy of 5 decimal places.
+
+function pi(precision) {
+    var a = 4
+    var b
+    var k = 1
+    precision /= 10 // this is just guessing...
+    do {
+        b = a + ((k % 2 == 0)? 4 : -4) / (2 * k + 1)
+        a , b = b , a
+        k += 1
+    } while (math.abs(a - b) >= precision)
+    print(k)
+    return a
+}
+
+print(pi(10e-5))
+print(2*math.asin(1))
+
+//*///--------------------------------------------------------------------------

File bin/windows/to_string.kia

  • Ignore whitespace
-
-function table_print(tt, indent, done) {
-    done = done || {}
-    indent = indent || 0
-    if (type(tt) == "table") {
-        var sb = {}
-        for each (var key, value in pairs(tt)) {
-            table.insert(sb, string.rep(" ", indent)) // indent it
-            if (type(value) == "table" && ! done[value]) {
-                done[value] = true
-                table.insert(sb, "{\n");
-                table.insert(sb, table_print(value, indent + 2, done))
-                table.insert(sb, string.rep(" ", indent)) // indent it
-                table.insert(sb, "}\n");
-            }
-            else if ("number" == type(key)) {
-                table.insert(sb, string.format("%s ", tostring(value)))
-            }
-            else {
-                table.insert(sb, string.format("%s = \"%s\"\n", 
-                                               tostring(key), 
-                                               tostring(value)))
-            }
-        }
-        return table.concat(sb)
-    }
-    else {
-        return tt .. "\n"
-    }
-}
-
-public function to_string(tbl) {
-    if ("null" == type(tbl)) {
-        return "null"
-    }
-    else if ("table" == type(tbl)) {
-        return table_print(tbl)
-    }
-    else if ("string" == type(tbl)) {
-        return tbl
-    }
-    else {
-        return tostring(tbl)
-    }
-}