example: add some tasks of Rosetta code
authorJean Privat <jean@pryen.org>
Mon, 27 Oct 2014 19:44:05 +0000 (15:44 -0400)
committerJean Privat <jean@pryen.org>
Tue, 28 Oct 2014 00:21:54 +0000 (20:21 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

25 files changed:
examples/rosettacode/README.md [new file with mode: 0644]
examples/rosettacode/ab.nit [new file with mode: 0644]
examples/rosettacode/abstract_type.nit [new file with mode: 0644]
examples/rosettacode/accumulator_factory.nit [new file with mode: 0644]
examples/rosettacode/ackermann_function.nit [new file with mode: 0644]
examples/rosettacode/align_columns.nit [new file with mode: 0644]
examples/rosettacode/array_concatenation.nit [new file with mode: 0644]
examples/rosettacode/arrays.nit [new file with mode: 0644]
examples/rosettacode/assertions.nit [new file with mode: 0644]
examples/rosettacode/doors.nit [new file with mode: 0644]
examples/rosettacode/reverse_a_string.nit [new file with mode: 0644]
examples/rosettacode/reverse_words.nit [new file with mode: 0644]
examples/rosettacode/unix_ls.nit [new file with mode: 0644]
tests/sav/ab.res [new file with mode: 0644]
tests/sav/abstract_type.res [new file with mode: 0644]
tests/sav/accumulator_factory.res [new file with mode: 0644]
tests/sav/ackermann_function.res [new file with mode: 0644]
tests/sav/align_columns.res [new file with mode: 0644]
tests/sav/array_concatenation.res [new file with mode: 0644]
tests/sav/arrays.res [new file with mode: 0644]
tests/sav/assertions.res [new file with mode: 0644]
tests/sav/doors.res [new file with mode: 0644]
tests/sav/reverse_a_string.res [new file with mode: 0644]
tests/sav/reverse_words.res [new file with mode: 0644]
tests/sav/unix_ls.res [new file with mode: 0644]

diff --git a/examples/rosettacode/README.md b/examples/rosettacode/README.md
new file mode 100644 (file)
index 0000000..ade40c7
--- /dev/null
@@ -0,0 +1,5 @@
+# Rosetta Code
+
+This are Nit programs that solve some tasks from the [Rosetta Code] project.
+
+  [Rosetta Code]: http://rosettacode.org/
diff --git a/examples/rosettacode/ab.nit b/examples/rosettacode/ab.nit
new file mode 100644 (file)
index 0000000..9138c0c
--- /dev/null
@@ -0,0 +1,13 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: A+B
+# SEE: <http://rosettacode.org/wiki/A%2BB>
+#
+# Generic non-robust version.
+module ab
+
+var words = gets.split(" ")
+print words[0].to_i + words[1].to_i
diff --git a/examples/rosettacode/abstract_type.nit b/examples/rosettacode/abstract_type.nit
new file mode 100644 (file)
index 0000000..8de3a04
--- /dev/null
@@ -0,0 +1,24 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: abstract type
+# SEE: <http://rosettacode.org/wiki/Abstract_type>
+#
+# Methods withouts implementation are annotated `abstract`.
+#
+# Abstract classes and interfaces can contain abstract methods and concrete (i.e. non-abstract) methods.
+# Abstract classes can also have attributes.
+module abstract_type
+
+interface Inter
+       fun method1: Int is abstract
+       fun method2: Int do return 1
+end
+
+abstract class Abs
+       fun method1: Int is abstract
+       fun method2: Int do return 1
+       var attr: Int
+end
diff --git a/examples/rosettacode/accumulator_factory.nit b/examples/rosettacode/accumulator_factory.nit
new file mode 100644 (file)
index 0000000..0b6cc64
--- /dev/null
@@ -0,0 +1,28 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# The `accumulator factory` task.
+# SEE: <http://rosettacode.org/wiki/Accumulator_factory>
+#
+# Nit has no first-class function.
+# A class is used to store the state.
+module accumulator_factory
+
+class Accumulator
+       # The accumulated sum
+       # Numeric is used, so Int and Float are accepted
+       private var sum: Numeric
+       fun call(n: Numeric): Numeric
+       do
+               # `add` is the safe `+` method on Numeric
+               sum = sum.add(n)
+               return sum
+       end
+end
+
+var x = new Accumulator(1)
+x.call(5)
+var y = new Accumulator(3)
+print x.call(2.3)
diff --git a/examples/rosettacode/ackermann_function.nit b/examples/rosettacode/ackermann_function.nit
new file mode 100644 (file)
index 0000000..9dd1685
--- /dev/null
@@ -0,0 +1,24 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Ackermann function
+# SEE: <http://rosettacode.org/wiki/Ackermann_function>
+#
+# A simple straightforward recursive implementation.
+module ackermann_function
+
+fun ack(m, n: Int): Int
+do
+       if m == 0 then return n + 1
+       if n == 0 then return ack(m-1,1)
+       return ack(m-1, ack(m, n-1))
+end
+
+for m in [0..3] do
+       for n in [0..6] do
+               print ack(m,n)
+       end
+       print ""
+end
diff --git a/examples/rosettacode/align_columns.nit b/examples/rosettacode/align_columns.nit
new file mode 100644 (file)
index 0000000..ec09a4c
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Align columns
+# SEE: <http://rosettacode.org/wiki/Align_columns>
+#
+# Use `Text::justify` for the standard library.
+module align_columns
+
+fun aligner(text: String, left: Float)
+do
+       # Each row is a sequence of fields
+       var rows = new Array[Array[String]]
+       var max = 0
+       for line in text.split('\n') do
+               rows.add line.split("$")
+       end
+
+       # Compute the final length of each column
+       var lengths = new Array[Int]
+       for fields in rows do
+               var i = 0
+               for field in fields do
+                       var fl = field.length
+                       if lengths.length <= i or fl > lengths[i] then
+                               lengths[i] = fl
+                       end
+                       i += 1
+               end
+       end
+
+       # Process each line and align each field
+       for fields in rows do
+               var line = new Array[String]
+               var i = 0
+               for field in fields do
+                       line.add field.justify(lengths[i], left)
+                       i += 1
+               end
+               print line.join(" ")
+       end
+end
+
+var text = """
+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."""
+
+aligner(text, 0.0)
+aligner(text, 1.0)
+aligner(text, 0.5)
diff --git a/examples/rosettacode/array_concatenation.nit b/examples/rosettacode/array_concatenation.nit
new file mode 100644 (file)
index 0000000..fdea5e0
--- /dev/null
@@ -0,0 +1,17 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Array concatenation
+# SEE: <http://rosettacode.org/wiki/Array_concatenation>
+module array_concatenation
+
+var arr1 = [1, 2, 3]
+var arr2 = [4, 5, 6]
+var arr3 = [7, 8, 9]
+var arr5 = [2, 1, 0]
+var arr4 = arr1 + arr2
+arr4.append arr3
+arr4.prepend arr5
+print arr4 # => 210123456789
diff --git a/examples/rosettacode/arrays.nit b/examples/rosettacode/arrays.nit
new file mode 100644 (file)
index 0000000..2cc3485
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Arrays
+# SEE: <http://rosettacode.org/wiki/Arrays>
+module arrays
+
+# Creation of an array with a single element
+var a = [1]
+
+# Add objects
+a.add 2
+a.add_all([3, 4, 5])
+
+# Set values at a specific index
+a[0] = 100
+
+# Get a specific elements
+print a[0]
diff --git a/examples/rosettacode/assertions.nit b/examples/rosettacode/assertions.nit
new file mode 100644 (file)
index 0000000..8005bce
--- /dev/null
@@ -0,0 +1,11 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Assertions
+# SEE: <http://rosettacode.org/wiki/Assertions>
+module assertions
+
+var n = 5
+assert n == 42
diff --git a/examples/rosettacode/doors.nit b/examples/rosettacode/doors.nit
new file mode 100644 (file)
index 0000000..b0e78b1
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: 100 doors
+# SEE: <http://rosettacode.org/wiki/100_doors>
+#
+# Generic unoptimized version.
+module doors
+
+var n = 100
+var doors = new Array[Bool].filled_with(false, n)
+for i in [0..n[ do
+       var j = i
+       while j < n do
+               doors[j] = not doors[j]
+               j += i+1
+       end
+end
+for i in [0..n[ do
+       var s
+       if doors[i] then s = "open" else s = "closed"
+       print "Door {i} is {s}."
+end
diff --git a/examples/rosettacode/reverse_a_string.nit b/examples/rosettacode/reverse_a_string.nit
new file mode 100644 (file)
index 0000000..ea83f73
--- /dev/null
@@ -0,0 +1,13 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Reverse a string
+# SEE: <http://rosettacode.org/wiki/Reverse_a_string>
+#
+# Unicode is not yet preserved :(
+print "asdf".reversed
+
+# does not work yet :(
+#print "as⃝df̅".reversed
diff --git a/examples/rosettacode/reverse_words.nit b/examples/rosettacode/reverse_words.nit
new file mode 100644 (file)
index 0000000..da2d4b4
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Reverse words in a string
+# SEE: <http://rosettacode.org/wiki/Reverse_words_in_a_string>
+module reverse_words
+var text = """
+---------- Ice and Fire ------------
+
+fire, in end will world the say Some
+ice. in say Some
+desire of tasted I've what From
+fire. favor who those with hold I
+
+... elided paragraph last ...
+
+Frost Robert -----------------------"""
+
+for l in text.split("\n") do print l.split(" ").reversed.join(" ")
diff --git a/examples/rosettacode/unix_ls.nit b/examples/rosettacode/unix_ls.nit
new file mode 100644 (file)
index 0000000..55c489c
--- /dev/null
@@ -0,0 +1,8 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Unix/ls
+# SEE: <http://rosettacode.org/wiki/Unix/ls>
+for f in ".".files do print f
diff --git a/tests/sav/ab.res b/tests/sav/ab.res
new file mode 100644 (file)
index 0000000..a9dbe22
--- /dev/null
@@ -0,0 +1 @@
+Runtime error: Assert 'index' failed (../lib/standard/collection/array.nit:258)
diff --git a/tests/sav/abstract_type.res b/tests/sav/abstract_type.res
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/sav/accumulator_factory.res b/tests/sav/accumulator_factory.res
new file mode 100644 (file)
index 0000000..cf02201
--- /dev/null
@@ -0,0 +1 @@
+8.3
diff --git a/tests/sav/ackermann_function.res b/tests/sav/ackermann_function.res
new file mode 100644 (file)
index 0000000..a7ffe8a
--- /dev/null
@@ -0,0 +1,32 @@
+1
+2
+3
+4
+5
+6
+7
+
+2
+3
+4
+5
+6
+7
+8
+
+3
+5
+7
+9
+11
+13
+15
+
+5
+13
+29
+61
+125
+253
+509
+
diff --git a/tests/sav/align_columns.res b/tests/sav/align_columns.res
new file mode 100644 (file)
index 0000000..bcf2f8b
--- /dev/null
@@ -0,0 +1,18 @@
+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.
+     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.
+  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.
diff --git a/tests/sav/array_concatenation.res b/tests/sav/array_concatenation.res
new file mode 100644 (file)
index 0000000..0556988
--- /dev/null
@@ -0,0 +1 @@
+210123456789
diff --git a/tests/sav/arrays.res b/tests/sav/arrays.res
new file mode 100644 (file)
index 0000000..29d6383
--- /dev/null
@@ -0,0 +1 @@
+100
diff --git a/tests/sav/assertions.res b/tests/sav/assertions.res
new file mode 100644 (file)
index 0000000..d75c441
--- /dev/null
@@ -0,0 +1 @@
+Runtime error: Assert failed (../examples/rosettacode/assertions.nit:11)
diff --git a/tests/sav/doors.res b/tests/sav/doors.res
new file mode 100644 (file)
index 0000000..2719131
--- /dev/null
@@ -0,0 +1,100 @@
+Door 0 is open.
+Door 1 is closed.
+Door 2 is closed.
+Door 3 is open.
+Door 4 is closed.
+Door 5 is closed.
+Door 6 is closed.
+Door 7 is closed.
+Door 8 is open.
+Door 9 is closed.
+Door 10 is closed.
+Door 11 is closed.
+Door 12 is closed.
+Door 13 is closed.
+Door 14 is closed.
+Door 15 is open.
+Door 16 is closed.
+Door 17 is closed.
+Door 18 is closed.
+Door 19 is closed.
+Door 20 is closed.
+Door 21 is closed.
+Door 22 is closed.
+Door 23 is closed.
+Door 24 is open.
+Door 25 is closed.
+Door 26 is closed.
+Door 27 is closed.
+Door 28 is closed.
+Door 29 is closed.
+Door 30 is closed.
+Door 31 is closed.
+Door 32 is closed.
+Door 33 is closed.
+Door 34 is closed.
+Door 35 is open.
+Door 36 is closed.
+Door 37 is closed.
+Door 38 is closed.
+Door 39 is closed.
+Door 40 is closed.
+Door 41 is closed.
+Door 42 is closed.
+Door 43 is closed.
+Door 44 is closed.
+Door 45 is closed.
+Door 46 is closed.
+Door 47 is closed.
+Door 48 is open.
+Door 49 is closed.
+Door 50 is closed.
+Door 51 is closed.
+Door 52 is closed.
+Door 53 is closed.
+Door 54 is closed.
+Door 55 is closed.
+Door 56 is closed.
+Door 57 is closed.
+Door 58 is closed.
+Door 59 is closed.
+Door 60 is closed.
+Door 61 is closed.
+Door 62 is closed.
+Door 63 is open.
+Door 64 is closed.
+Door 65 is closed.
+Door 66 is closed.
+Door 67 is closed.
+Door 68 is closed.
+Door 69 is closed.
+Door 70 is closed.
+Door 71 is closed.
+Door 72 is closed.
+Door 73 is closed.
+Door 74 is closed.
+Door 75 is closed.
+Door 76 is closed.
+Door 77 is closed.
+Door 78 is closed.
+Door 79 is closed.
+Door 80 is open.
+Door 81 is closed.
+Door 82 is closed.
+Door 83 is closed.
+Door 84 is closed.
+Door 85 is closed.
+Door 86 is closed.
+Door 87 is closed.
+Door 88 is closed.
+Door 89 is closed.
+Door 90 is closed.
+Door 91 is closed.
+Door 92 is closed.
+Door 93 is closed.
+Door 94 is closed.
+Door 95 is closed.
+Door 96 is closed.
+Door 97 is closed.
+Door 98 is closed.
+Door 99 is open.
diff --git a/tests/sav/reverse_a_string.res b/tests/sav/reverse_a_string.res
new file mode 100644 (file)
index 0000000..3c0b8a4
--- /dev/null
@@ -0,0 +1 @@
+fdsa
diff --git a/tests/sav/reverse_words.res b/tests/sav/reverse_words.res
new file mode 100644 (file)
index 0000000..de243a8
--- /dev/null
@@ -0,0 +1,10 @@
+------------ Fire and Ice ----------
+
+Some say the world will end in fire,
+Some say in ice.
+From what I've tasted of desire
+I hold with those who favor fire.
+
+... last paragraph elided ...
+
+----------------------- Robert Frost
diff --git a/tests/sav/unix_ls.res b/tests/sav/unix_ls.res
new file mode 100644 (file)
index 0000000..4ad3dc3
--- /dev/null
@@ -0,0 +1 @@
+UNDEFINED