Merge: Third batch of rosettacode
authorJean Privat <jean@pryen.org>
Mon, 29 Jun 2015 12:20:43 +0000 (08:20 -0400)
committerJean Privat <jean@pryen.org>
Mon, 29 Jun 2015 12:20:43 +0000 (08:20 -0400)
Cleaning, small fixes and/or improved tests

Pull-Request: #1535
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>

examples/rosettacode/ethiopian_multiplication.nit [new file with mode: 0644]
examples/rosettacode/one_dimensional_cellular_automata.nit [new file with mode: 0644]
examples/rosettacode/pig_the_dice_game.nit [new file with mode: 0644]
examples/rosettacode/vignere_cipher.nit [new file with mode: 0644]
tests/ethiopian_multiplication.inputs [new file with mode: 0644]
tests/pig_the_dice_game.inputs [new file with mode: 0644]
tests/sav/ethiopian_multiplication.res [new file with mode: 0644]
tests/sav/one_dimensional_cellular_automata.res [new file with mode: 0644]
tests/sav/pig_the_dice_game.res [new file with mode: 0644]
tests/sav/vignere_cipher.res [new file with mode: 0644]

diff --git a/examples/rosettacode/ethiopian_multiplication.nit b/examples/rosettacode/ethiopian_multiplication.nit
new file mode 100644 (file)
index 0000000..0e7f848
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Ethiopian Multiplication
+# SEE: <http://rosettacode.org/wiki/ethiopian_multiplication>
+#
+# Generic non-robust version.
+module ethiopian_multiplication
+
+fun ethiopian(x, y: Int): Int
+do
+       print "{x}; {0}"
+       var sum: Int
+       if x.is_even then
+               sum = 0
+       else
+               sum = y
+       end
+
+       while x>1 do
+               x /= 2
+               y *= 2
+               print "{x}; {y}"
+               if not x.is_even then sum += y
+       end
+
+       return sum
+end
+
+var words = gets.split(" ")
+print ethiopian(words[0].to_i, words[1].to_i)
diff --git a/examples/rosettacode/one_dimensional_cellular_automata.nit b/examples/rosettacode/one_dimensional_cellular_automata.nit
new file mode 100644 (file)
index 0000000..77b2ed2
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: One-dimensional_cellular_automata
+# SEE: <http://rosettacode.org/wiki/One-dimensional_cellular_automata>
+module one_dimensional_cellular_automata
+
+fun evolve(cells: SequenceRead[Char]): SequenceRead[Char]
+do
+       var res = new Array[Char]
+       for i in [0..cells.length[ do
+               var n = neighboors(i, cells)
+               if cells[i] == '#' then
+                       res.add(b_to_char(n == 1))
+               else
+                       res.add(b_to_char(n == 2))
+               end
+       end
+       return res
+end
+
+fun b_to_char(b: Bool): Char do if b then return '#' else return '_'
+
+fun neighboors(i: Int, cells: SequenceRead[Char]): Int
+do
+       var res = 0
+       if i > 0 and cells[i-1] == '#' then res += 1
+       if i < cells.length - 1 and cells[i+1] == '#' then res += 1
+       return res
+end
+
+var ary = "_###_##_#_#_#_#__#__".chars
+loop
+       print ary.join("")
+       var nxt = evolve(ary)
+       if ary == nxt then break
+       ary = nxt
+end
diff --git a/examples/rosettacode/pig_the_dice_game.nit b/examples/rosettacode/pig_the_dice_game.nit
new file mode 100644 (file)
index 0000000..da4c218
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Pig the dice game
+# SEE: http://rosettacode.org/wiki/Pig_the_dice_game
+
+var player = 0
+var score = [0, 0]
+var turnscore = 0
+var max_score = 100
+loop
+       if (score[player] + turnscore) >= max_score then
+                       print "Congratulation Player {player + 1}! You won with {score[player]} points! "
+                       return
+       end
+       print "Player {player +1}"
+       print "Total score : {score[player]}"
+       print "What do you want to do? (\"1\" to roll, \"0\" to hold)"
+       if stdin.eof then break
+       var input = gets
+       if input == "1" then
+               var roll = 6.rand + 1
+               if roll == 1 then
+                       turnscore = 0
+                       print "You rolled 1! all points lost for this turn"
+                       print ""
+                       player = 1 - player
+               else
+                       turnscore += roll
+                       print "rolled: {roll}"
+                       print "score this turn: {turnscore}"
+                       print ""
+               end
+       else if input == "0" then
+               score[player] += turnscore
+               print "Scored: {turnscore}"
+               print "New Score: {score[player]}"
+               print ""
+               player = 1 - player
+               turnscore = 0
+       end
+end
diff --git a/examples/rosettacode/vignere_cipher.nit b/examples/rosettacode/vignere_cipher.nit
new file mode 100644 (file)
index 0000000..73a3f41
--- /dev/null
@@ -0,0 +1,58 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Vigenère cipher
+#
+# See: <http://rosettacode.org/wiki/Vigenère_cipher>
+module vignere_cipher
+
+fun encrypt(src, key: String): String do
+       var out = new Buffer
+       var j = 0
+
+       for i in [0..src.length - 1] do
+               var c = src[i]
+
+               if c >= 'a' and c <= 'z' then
+                       c = c.to_upper
+               else if c < 'A' or c > 'Z' then
+                       continue
+               end
+
+               out.add(((c.ascii + key[j].ascii - 2 * 'A'.ascii) % 26 + 'A'.ascii).ascii)
+               j = (j + 1) % key.length
+       end
+
+       return out.to_s
+end
+
+fun decrypt(src, key: String): String do
+       var out = new Buffer
+       var j = 0
+
+       for i in [0..src.length - 1] do
+               var c = src[i]
+
+               if c >= 'a' and c <= 'z' then
+                       c = c.to_upper
+               else if c < 'A' or c > 'Z' then
+                       continue
+               end
+
+               out.add(((c.ascii - key[j].ascii + 26) % 26 + 'A'.ascii).ascii)
+               j = (j + 1) % key.length
+       end
+
+       return out.write_to_string
+end
+
+# Main part
+var str = "All your base are belong to us"
+var key = "CRYPTONIT"
+var code = encrypt(str, key)
+var decode = decrypt(code, key)
+
+print "Text: " + str
+print "Key : " + key
+print "Code: " + code
+print "Back: " + decode
diff --git a/tests/ethiopian_multiplication.inputs b/tests/ethiopian_multiplication.inputs
new file mode 100644 (file)
index 0000000..25bf134
--- /dev/null
@@ -0,0 +1 @@
+8 6
diff --git a/tests/pig_the_dice_game.inputs b/tests/pig_the_dice_game.inputs
new file mode 100644 (file)
index 0000000..b338c4c
--- /dev/null
@@ -0,0 +1,39 @@
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+0
+1
+1
+1
+0
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+1
diff --git a/tests/sav/ethiopian_multiplication.res b/tests/sav/ethiopian_multiplication.res
new file mode 100644 (file)
index 0000000..1f2aa01
--- /dev/null
@@ -0,0 +1,5 @@
+8; 0
+4; 12
+2; 24
+1; 48
+48
diff --git a/tests/sav/one_dimensional_cellular_automata.res b/tests/sav/one_dimensional_cellular_automata.res
new file mode 100644 (file)
index 0000000..eb4bb2e
--- /dev/null
@@ -0,0 +1,9 @@
+_###_##_#_#_#_#__#__
+_#_#####_#_#_#______
+__##___##_#_#_______
+__##___###_#________
+__##___#_##_________
+__##____###_________
+__##____#_#_________
+__##_____#__________
+__##________________
diff --git a/tests/sav/pig_the_dice_game.res b/tests/sav/pig_the_dice_game.res
new file mode 100644 (file)
index 0000000..d9a44a4
--- /dev/null
@@ -0,0 +1,217 @@
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 6
+score this turn: 6
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 3
+score this turn: 9
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 5
+score this turn: 14
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 5
+score this turn: 19
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 6
+score this turn: 25
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 2
+score this turn: 27
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 3
+score this turn: 30
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 5
+score this turn: 35
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 2
+score this turn: 37
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 4
+score this turn: 41
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 3
+score this turn: 44
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 4
+score this turn: 48
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 3
+score this turn: 51
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 4
+score this turn: 55
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 6
+score this turn: 61
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 6
+score this turn: 67
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 4
+score this turn: 71
+
+Player 1
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+Scored: 71
+New Score: 71
+
+Player 2
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 5
+score this turn: 5
+
+Player 2
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+You rolled 1! all points lost for this turn
+
+Player 1
+Total score : 71
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 4
+score this turn: 4
+
+Player 1
+Total score : 71
+What do you want to do? ("1" to roll, "0" to hold)
+Scored: 4
+New Score: 75
+
+Player 2
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+You rolled 1! all points lost for this turn
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 2
+score this turn: 2
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+You rolled 1! all points lost for this turn
+
+Player 2
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 5
+score this turn: 5
+
+Player 2
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+You rolled 1! all points lost for this turn
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 3
+score this turn: 3
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+You rolled 1! all points lost for this turn
+
+Player 2
+Total score : 0
+What do you want to do? ("1" to roll, "0" to hold)
+You rolled 1! all points lost for this turn
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 6
+score this turn: 6
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 2
+score this turn: 8
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 4
+score this turn: 12
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 6
+score this turn: 18
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 4
+score this turn: 22
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 2
+score this turn: 24
+
+Player 1
+Total score : 75
+What do you want to do? ("1" to roll, "0" to hold)
+rolled: 4
+score this turn: 28
+
+Congratulation Player 1! You won with 75 points! 
diff --git a/tests/sav/vignere_cipher.res b/tests/sav/vignere_cipher.res
new file mode 100644 (file)
index 0000000..4f03d3d
--- /dev/null
@@ -0,0 +1,4 @@
+Text: All your base are belong to us
+Key : CRYPTONIT
+Code: CCJNHIEJTUVYGXPRTHPXRDNG
+Back: ALLYOURBASEAREBELONGTOUS