June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -6,7 +6,7 @@ entropy(data b)
|
|||
|
||||
ones = zeros = 0;
|
||||
|
||||
i = -(count = b_length(b));
|
||||
i = -(count = ~b);
|
||||
while (i) {
|
||||
if (b[i] == '0') {
|
||||
zeros += 1;
|
||||
|
|
@ -29,15 +29,15 @@ main(void)
|
|||
a = "1";
|
||||
b = "0";
|
||||
|
||||
o_form("%2d %9d /w12p10d10/ ~\n", 1, b_length(a), 0r, a);
|
||||
o_form("%2d %9d /w12p10d10/ ~\n", 2, b_length(b), 0r, b);
|
||||
o_form("%2d %9d /w12p10d10/ ~\n", 1, ~a, 0r, a);
|
||||
o_form("%2d %9d /w12p10d10/ ~\n", 2, ~b, 0r, b);
|
||||
i = 3;
|
||||
while (i <= 37) {
|
||||
b_stock(a, 0, b);
|
||||
o_form("%2d %9d /w12p10d10/ ~\n", i, b_length(a), entropy(a),
|
||||
__hold(i < 10, a, ""));
|
||||
bu_copy(a, 0, b);
|
||||
o_form("%2d %9d /w12p10d10/ ~\n", i, ~a, entropy(a),
|
||||
i < 10 ? a.string : "");
|
||||
i += 1;
|
||||
b_swap(a, b);
|
||||
b.swap(a);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
71
Task/Fibonacci-word/C-sharp/fibonacci-word.cs
Normal file
71
Task/Fibonacci-word/C-sharp/fibonacci-word.cs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
using SYS = System;
|
||||
using SCG = System.Collections.Generic;
|
||||
|
||||
//
|
||||
// Basically a port of the C++ solution as posted
|
||||
// 2017-11-12.
|
||||
//
|
||||
namespace FibonacciWord
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main( string[] args )
|
||||
{
|
||||
PrintHeading();
|
||||
string firstString = "1";
|
||||
int n = 1;
|
||||
PrintLine( n, firstString );
|
||||
string secondString = "0";
|
||||
++n;
|
||||
PrintLine( n, secondString );
|
||||
while ( n < 37 )
|
||||
{
|
||||
string resultString = firstString + secondString;
|
||||
firstString = secondString;
|
||||
secondString = resultString;
|
||||
++n;
|
||||
PrintLine( n, resultString );
|
||||
}
|
||||
}
|
||||
|
||||
private static void PrintLine( int n, string result )
|
||||
{
|
||||
SYS.Console.Write( "{0,-5}", n );
|
||||
SYS.Console.Write( "{0,12}", result.Length );
|
||||
SYS.Console.WriteLine( " {0,-16}", GetEntropy( result ) );
|
||||
}
|
||||
|
||||
private static double GetEntropy( string result )
|
||||
{
|
||||
SCG.Dictionary<char, int> frequencies = new SCG.Dictionary<char, int>();
|
||||
foreach ( char c in result )
|
||||
{
|
||||
if ( frequencies.ContainsKey( c ) )
|
||||
{
|
||||
++frequencies[c];
|
||||
}
|
||||
else
|
||||
{
|
||||
frequencies[c] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
int length = result.Length;
|
||||
double entropy = 0;
|
||||
foreach ( var keyValue in frequencies )
|
||||
{
|
||||
double freq = (double)keyValue.Value / length;
|
||||
entropy += freq * SYS.Math.Log( freq, 2 );
|
||||
}
|
||||
|
||||
return -entropy;
|
||||
}
|
||||
|
||||
private static void PrintHeading()
|
||||
{
|
||||
SYS.Console.Write( "{0,-5}", "N" );
|
||||
SYS.Console.Write( "{0,12}", "Length" );
|
||||
SYS.Console.WriteLine( " {0,-16}", "Entropy" );
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Task/Fibonacci-word/Factor/fibonacci-word.factor
Normal file
36
Task/Fibonacci-word/Factor/fibonacci-word.factor
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
USING: assocs combinators formatting kernel math math.functions
|
||||
math.ranges math.statistics namespaces pair-rocket sequences ;
|
||||
IN: rosetta-code.fibonacci-word
|
||||
|
||||
SYMBOL: 37th-fib-word
|
||||
|
||||
: fib ( n -- m )
|
||||
{
|
||||
1 => [ 1 ]
|
||||
2 => [ 1 ]
|
||||
[ [ 1 - fib ] [ 2 - fib ] bi + ]
|
||||
} case ;
|
||||
|
||||
: fib-word ( n -- seq )
|
||||
{
|
||||
1 => [ "1" ]
|
||||
2 => [ "0" ]
|
||||
[ [ 1 - fib-word ] [ 2 - fib-word ] bi append ]
|
||||
} case ;
|
||||
|
||||
: nth-fib-word ( n -- seq )
|
||||
dup 1 =
|
||||
[ drop "1" ] [ 37th-fib-word get swap fib head ] if ;
|
||||
|
||||
: entropy ( seq -- entropy )
|
||||
[ length ] [ histogram >alist [ second ] map ] bi
|
||||
[ swap / ] with map
|
||||
[ dup log 2 log / * ] map-sum
|
||||
dup 0. = [ neg ] unless ;
|
||||
|
||||
37 fib-word 37th-fib-word set
|
||||
"N" "Length" "Entropy" "%2s %8s %10s\n" printf
|
||||
37 [1,b] [
|
||||
dup nth-fib-word [ length ] [ entropy ] bi
|
||||
"%2d %8d %.8f\n" printf
|
||||
] each
|
||||
|
|
@ -1,26 +1,27 @@
|
|||
# Code for "entropy" taken from https://rosettacode.org/wiki/Entropy#Julia
|
||||
entropy(s::String)::Float32 = -sum(x -> x * log(2, x), [count(x -> x == c, s) / length(s) for c in unique(s)])
|
||||
using DataStructures
|
||||
entropy(s::AbstractString) = -sum(x -> x / length(s) * log2(x / length(s)), values(counter(s)))
|
||||
|
||||
function fibboword(n::Int64)::Array{String}
|
||||
# Initialize the result
|
||||
r = Array{String}(n)
|
||||
# First element
|
||||
r[1] = "0"
|
||||
# If more than 2, set the second element
|
||||
if (n ≥ 2)
|
||||
r[2] = "1"
|
||||
end
|
||||
# Recursively create elements > 3
|
||||
for i in 3:n
|
||||
r[i] = r[i - 1] * r[i - 2]
|
||||
end
|
||||
return r
|
||||
function fibboword(n::Int64)
|
||||
# Initialize the result
|
||||
r = Array{String}(n)
|
||||
# First element
|
||||
r[1] = "0"
|
||||
# If more than 2, set the second element
|
||||
if n ≥ 2 r[2] = "1" end
|
||||
# Recursively create elements > 3
|
||||
for i in 3:n
|
||||
r[i] = r[i - 1] * r[i - 2]
|
||||
end
|
||||
return r
|
||||
end
|
||||
|
||||
function testfibbo(n::Int64)
|
||||
fib = fibboword(n)
|
||||
for i in 1:length(fib)
|
||||
println(i, "\t", length(fib[i]), "\t", entropy(fib[i]))
|
||||
end
|
||||
return 0
|
||||
function testfibbo(n::Integer)
|
||||
fib = fibboword(n)
|
||||
for i in 1:length(fib)
|
||||
@printf("%3d%9d%12.6f\n", i, length(fib[i]), entropy(fib[i]))
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
println(" n\tlength\tentropy")
|
||||
testfibbo(37)
|
||||
|
|
|
|||
58
Task/Fibonacci-word/Objeck/fibonacci-word.objeck
Normal file
58
Task/Fibonacci-word/Objeck/fibonacci-word.objeck
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
use Collection;
|
||||
|
||||
class FibonacciWord {
|
||||
function : native : GetEntropy(result : String) ~ Float {
|
||||
frequencies := IntMap->New();
|
||||
|
||||
each(i : result) {
|
||||
c := result->Get(i);
|
||||
|
||||
if(frequencies->Has(c)) {
|
||||
count := frequencies->Find(c)->As(IntHolder);
|
||||
count->Set(count->Get() + 1);
|
||||
}
|
||||
else {
|
||||
frequencies->Insert(c, IntHolder->New(1));
|
||||
};
|
||||
};
|
||||
|
||||
length := result->Size();
|
||||
entropy := 0.0;
|
||||
|
||||
counts := frequencies->GetValues();
|
||||
each(i : counts) {
|
||||
count := counts->Get(i)->As(IntHolder)->Get();
|
||||
freq := count->As(Float) / length;
|
||||
entropy += freq * (freq->Log() / 2.0->Log());
|
||||
};
|
||||
|
||||
return -1 * entropy;
|
||||
}
|
||||
|
||||
function : native : PrintLine(n : Int, result : String) ~ Nil {
|
||||
n->Print();
|
||||
'\t'->Print();
|
||||
|
||||
result->Size()->Print();
|
||||
"\t\t"->Print();
|
||||
|
||||
GetEntropy(result)->PrintLine();
|
||||
}
|
||||
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
firstString := "1";
|
||||
n := 1;
|
||||
PrintLine( n, firstString );
|
||||
secondString := "0";
|
||||
n += 1;
|
||||
PrintLine( n, secondString );
|
||||
|
||||
while(n < 37) {
|
||||
resultString := "{$secondString}{$firstString}";
|
||||
firstString := secondString;
|
||||
secondString := resultString;
|
||||
n += 1;
|
||||
PrintLine( n, resultString );
|
||||
};
|
||||
}
|
||||
}
|
||||
54
Task/Fibonacci-word/Ring/fibonacci-word.ring
Normal file
54
Task/Fibonacci-word/Ring/fibonacci-word.ring
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
# Project : Fibonacci word
|
||||
# Date : 2018/01/23
|
||||
# Author : Gal Zsolt [~ CalmoSoft ~]
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
fw1 = "1"
|
||||
fw2 = "0"
|
||||
|
||||
see "N Length Entropy Word" + nl
|
||||
n = 1
|
||||
see "" + n + " " + len(fw1) + " " + calcentropy(fw1,2) + " " + fw1 + nl
|
||||
n = 2
|
||||
see "" + n + " " + len(fw2) + " " + calcentropy(fw2,2) + " " + fw2 + nl
|
||||
|
||||
for n = 1 to 55
|
||||
fw3 = fw2 + fw1
|
||||
temp = fw2
|
||||
fw2 = fw3
|
||||
fw1 = temp
|
||||
if len(fw3) < 55
|
||||
see "" + (n+2) + " " + len(fw3) + " " + calcentropy(fw3,2) + " " + fw3 + nl
|
||||
ok
|
||||
next
|
||||
|
||||
func calcentropy(source,b)
|
||||
decimals(11)
|
||||
entropy = 0
|
||||
countOfChar = list(255)
|
||||
charCount =len( source)
|
||||
usedChar =""
|
||||
for i =1 to len( source)
|
||||
ch =substr(source, i, 1)
|
||||
if not(substr( usedChar, ch))
|
||||
usedChar =usedChar +ch
|
||||
ok
|
||||
j =substr( usedChar, ch)
|
||||
countOfChar[j] =countOfChar[j] +1
|
||||
next
|
||||
l =len(usedChar)
|
||||
for i =1 to l
|
||||
probability =countOfChar[i] /charCount
|
||||
entropy =entropy - (probability *logBase(probability, 2))
|
||||
next
|
||||
return entropy
|
||||
|
||||
func swap(a, b)
|
||||
temp = a
|
||||
a = b
|
||||
b = temp
|
||||
return [a, b]
|
||||
|
||||
func logBase (x, b)
|
||||
logBase =log( x) /log( 2)
|
||||
return logBase
|
||||
Loading…
Add table
Add a link
Reference in a new issue