--- /dev/null
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Entropy
+# SEE: <http://rosettacode.org/wiki/Entropy>
+module entropy
+
+import counter
+
+# Basic implementation with a hashmap of chars to count them
+fun entropy(string: String): Float
+do
+ var cpt = new HashMap[Char, Int]
+ for char in string.chars do
+ var occ = cpt.get_or_default(char, 0)
+ cpt[char] = occ + 1
+ end
+
+ var len = string.length.to_f
+ var e = 0.0
+ for char, occ in cpt do
+ var freq = occ.to_f / len
+ e = e - freq * freq.log_base(2.0)
+ end
+ return e
+end
+print entropy("1223334444")
+
+# Alternative one-liner implementation using the `Counter::entropy` method of the library `counter`
+print "1223334444".chars.to_counter.entropy
--- /dev/null
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Entropy/Narcissist
+# SEE: <http://rosettacode.org/wiki/Entropy/Narcissist>
+module entropy_narcissist
+
+import counter
+
+# Should be run in the right directory
+print "entropy_narcissist.nit".to_path.read_all.chars.to_counter.entropy
--- /dev/null
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Fibonacci_word
+# SEE: <http://rosettacode.org/wiki/Fibonacci_word>
+module fibonacci_word
+
+import counter
+
+var words = new Array[String]
+words[0] = ""
+words[1] = "1"
+words[2] = "0"
+
+for i in [1..37] do
+ var w
+ if i >= words.length then
+ w = words[i-1] + words[i-2]
+ words[i] = w
+ else
+ w = words[i]
+ end
+ var out = w
+ if w.length > 40 then out = "..."
+ print "{i}\t{w.length}\t{w.chars.to_counter.entropy.to_precision(16)}\t{out}"
+end
end
return (sum / map.length.to_f).sqrt
end
+
+ # The information entropy (Shannon entropy) of the elements in the counter (in bits).
+ fun entropy: Float
+ do
+ var res = 0.0
+ var sum = self.sum.to_f
+ for k, v in self do
+ var f = v.to_f / sum
+ res = res - f * f.log_base(2.0)
+ end
+ return res
+ end
+end
+
+redef class Collection[E]
+ # Create and fill up a counter with the elements of `self.
+ #
+ # ~~~
+ # var cpt = "abaa".chars.to_counter
+ # assert cpt['a'] == 3
+ # assert cpt['b'] == 1
+ # assert cpt.length == 2
+ # assert cpt.sum == 4
+ # ~~~
+ fun to_counter: Counter[E]
+ do
+ var res = new Counter[E]
+ res.inc_all(self)
+ return res
+ end
end
private class CounterComparator[E]
init(ns, 0, 0)
end
+ # Init a `Bytes` with capacity `cap`
init with_capacity(cap: Int) do
var ns = new NativeString(cap)
init(ns, 0, cap)
end
end
+ # var b = new Bytes.empty
+ # b.append([0x41u8, 0x41u8, 0x18u8])
+ # b.pop
+ # assert b.to_s == "AA"
+ redef fun pop do
+ assert length >= 1
+ length -= 1
+ return items[length]
+ end
+
redef fun clear do length = 0
# Regenerates the buffer, necessary when it was persisted
end
redef class NativeString
+ # Creates a new `Bytes` object from `self` with `strlen` as length
fun to_bytes: Bytes do
var len = cstring_length
return new Bytes(self, len, len)
blink
input
first_letter_last_letter
+fibonacci_word
blink
input
first_letter_last_letter
+fibonacci_word
--- /dev/null
+1.846
+1.846
--- /dev/null
+1 1 0.0000000000000000 1
+2 1 0.0000000000000000 0
+3 2 1.0000000000000000 01
+4 3 0.9182958340544894 010
+5 5 0.9709505944546686 01001
+6 8 0.9544340029249650 01001010
+7 13 0.9612366047228760 0100101001001
+8 21 0.9587118829771316 010010100100101001010
+9 34 0.9596868937742170 0100101001001010010100100101001001
+10 55 0.9593160320543778 ...
+11 89 0.9594579158386696 ...
+12 144 0.9594037542210228 ...
+13 233 0.9594244469559864 ...
+14 377 0.9594165437404406 ...
+15 610 0.9594195626031440 ...
+16 987 0.9594184095152248 ...
+17 1597 0.9594188499578102 ...
+18 2584 0.9594186817240320 ...
+19 4181 0.9594187459836638 ...
+20 6765 0.9594187214386752 ...
+21 10946 0.9594187308140276 ...
+22 17711 0.9594187272329618 ...
+23 28657 0.9594187286008076 ...
+24 46368 0.9594187280783368 ...
+25 75025 0.9594187282779028 ...
+26 121393 0.9594187282016752 ...
+27 196418 0.9594187282307916 ...
+28 317811 0.9594187282196702 ...
+29 514229 0.9594187282239182 ...
+30 832040 0.9594187282222962 ...
+31 1346269 0.9594187282229156 ...
+32 2178309 0.9594187282226794 ...
+33 3524578 0.9594187282227690 ...
+34 5702887 0.9594187282227344 ...
+35 9227465 0.9594187282227478 ...
+36 14930352 0.9594187282227430 ...
+37 24157817 0.9594187282227448 ...
-../lib/standard/bytes.nit:49,7--19: Documentation warning: Undocumented property `with_capacity`
-../lib/standard/bytes.nit:162,6--13: Documentation warning: Undocumented property `to_bytes`
../lib/standard/stream.nit:426,6--17: Documentation warning: Undocumented property `buffer_reset`
../lib/standard/file.nit:455,6--19: Documentation warning: Undocumented property `read_all_bytes`
test_advice_repeated_types.nit:36,15--20: Warning: useless type repetition on redefined attribute `_a`