Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
26
Task/Fibonacci-word/EchoLisp/fibonacci-word.echolisp
Normal file
26
Task/Fibonacci-word/EchoLisp/fibonacci-word.echolisp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(lib 'struct)
|
||||
(struct FW ( count0 count1 length string)) ;; a fibonacci word
|
||||
(define (F-word n) ;; generator
|
||||
(define a (F-word (1- n)))
|
||||
(define b (F-word (- n 2)))
|
||||
(FW
|
||||
(+ (FW-count0 a) (FW-count0 b))
|
||||
(+ (FW-count1 a) (FW-count1 b))
|
||||
(+ (FW-length a) (FW-length b))
|
||||
(if (> n 9) "..." (string-append (FW-string a) (FW-string b)))))
|
||||
|
||||
(remember 'F-word (vector 0 (FW 0 1 1 "1") (FW 1 0 1 "0")))
|
||||
|
||||
(define (entropy fw)
|
||||
(define p (// (FW-count0 fw) (FW-length fw)))
|
||||
(cond
|
||||
((= p 0) 0)
|
||||
((= p 1) 0)
|
||||
(else (- 0 (* p (log2 p)) (* (- 1 p) (log2 (- 1 p)))))))
|
||||
|
||||
|
||||
(define (task (n 38) (fw))
|
||||
(for ((i (in-range 1 n)))
|
||||
(set! fw (F-word i))
|
||||
(printf "%3d %10d %24d %a"
|
||||
i (FW-length fw) (entropy fw) (FW-string fw))))
|
||||
61
Task/Fibonacci-word/FreeBASIC/fibonacci-word.freebasic
Normal file
61
Task/Fibonacci-word/FreeBASIC/fibonacci-word.freebasic
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
' version 25-06-2015
|
||||
' compile with: fbc -s console
|
||||
|
||||
Function calc_entropy(source As String, base_ As Integer) As Double
|
||||
|
||||
Dim As Integer i, sourcelen = Len(source), totalchar(255)
|
||||
Dim As Double prop, entropy
|
||||
|
||||
For i = 0 To sourcelen -1
|
||||
totalchar(source[i]) += 1
|
||||
Next
|
||||
|
||||
For i = 0 To 255
|
||||
If totalchar(i) = 0 Then Continue For
|
||||
prop = totalchar(i) / sourcelen
|
||||
entropy = entropy - (prop * Log (prop) / Log(base_))
|
||||
Next
|
||||
|
||||
Return entropy
|
||||
|
||||
End Function
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As String fw1 = "1" , fw2 = "0", fw3
|
||||
Dim As Integer i, n
|
||||
|
||||
Print" N Length Entropy Word"
|
||||
n = 1
|
||||
Print Using " ###";n; : Print Using " ###########"; Len(fw1);
|
||||
Print Using " ##.############### "; calc_entropy(fw1,2);
|
||||
Print fw1
|
||||
n = 2
|
||||
Print Using " ###";n ;: Print Using " ###########"; Len(fw2);
|
||||
Print Using " ##.############### "; calc_entropy(fw2,2);
|
||||
Print fw2
|
||||
|
||||
|
||||
For n = 1 To 35
|
||||
|
||||
fw1 = "1" : fw2 = "0" ' construct string
|
||||
For i = 1 To n
|
||||
fw3 = fw2 + fw1
|
||||
Swap fw1, fw2 ' swap pointers of fw1 and fw2
|
||||
Swap fw2, fw3 ' swap pointers of fw2 and fw3
|
||||
Next
|
||||
fw1 = "" : fw3 = "" ' free up memory
|
||||
|
||||
Print Using " ### ########### ##.############### "; n +2; Len(fw2);_
|
||||
calc_entropy(fw2, 2);
|
||||
If Len(fw2) < 55 Then Print fw2 Else Print
|
||||
|
||||
Next
|
||||
|
||||
Print
|
||||
|
||||
' empty keyboard buffer
|
||||
While InKey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
13
Task/Fibonacci-word/Oforth/fibonacci-word.oforth
Normal file
13
Task/Fibonacci-word/Oforth/fibonacci-word.oforth
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
: entropy(s) -- f
|
||||
| freq sz |
|
||||
s size dup ifZero: [ return ] asFloat ->sz
|
||||
ListBuffer initValue(255, 0) ->freq
|
||||
s apply( #[ dup freq at 1+ freq put ] )
|
||||
0.0 freq applyIf( #[ 0 <> ], #[ sz / dup ln * - ] ) Ln2 / ;
|
||||
|
||||
|
||||
: FWords(n)
|
||||
| ws i |
|
||||
ListBuffer new dup add("1") dup add("0") dup ->ws
|
||||
3 n for: i [ i 1- ws at i 2 - ws at + ws add ]
|
||||
dup map(#[ dup size swap entropy Pair new]) apply(#println) ;
|
||||
22
Task/Fibonacci-word/Sidef/fibonacci-word.sidef
Normal file
22
Task/Fibonacci-word/Sidef/fibonacci-word.sidef
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
func entropy(s) {
|
||||
[0] + (s.chars.freq.values »/» s.len) -> reduce { |a,b|
|
||||
a - b*b.log2
|
||||
}
|
||||
}
|
||||
|
||||
var n_max = 37
|
||||
var words = ['1', '0']
|
||||
|
||||
{
|
||||
words.append(words[-1] + words[-2])
|
||||
} * (n_max - words.len)
|
||||
|
||||
say ('%3s %10s %15s %s' % <N Length Entropy Fibword>...)
|
||||
|
||||
for i in ^words {
|
||||
var word = words[i]
|
||||
say ('%3i %10i %15.12f %s' % (i+1,
|
||||
word.len,
|
||||
entropy(word),
|
||||
word.len<30 ? word : '<too long>'))
|
||||
}
|
||||
10
Task/Fibonacci-word/jq/fibonacci-word-1.jq
Normal file
10
Task/Fibonacci-word/jq/fibonacci-word-1.jq
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Input: an array of strings.
|
||||
# Output: an object with the strings as keys,
|
||||
# the values of which are the corresponding frequencies.
|
||||
def counter:
|
||||
reduce .[] as $item ( {}; .[$item] += 1 ) ;
|
||||
|
||||
# entropy in bits of the input string
|
||||
def entropy:
|
||||
(explode | map( [.] | implode ) | counter | [ .[] | . * (.|log) ] | add) as $sum
|
||||
| ((length|log) - ($sum / length)) / (2|log) ;
|
||||
18
Task/Fibonacci-word/jq/fibonacci-word-2.jq
Normal file
18
Task/Fibonacci-word/jq/fibonacci-word-2.jq
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# truncate n places after the decimal point;
|
||||
# return a string since it can readily be converted back to a number
|
||||
def precision(n):
|
||||
tostring as $s | $s | index(".")
|
||||
| if . then $s[0:.+n+1] else $s end ;
|
||||
|
||||
# Right-justify but do not truncate
|
||||
def rjustify(n):
|
||||
tostring | length as $length
|
||||
| if n <= $length then . else " " * (n-$length) + . end;
|
||||
|
||||
# Attempt to align decimals so integer part is in a field of width n
|
||||
def align(n):
|
||||
tostring | index(".") as $ix
|
||||
| if n < $ix then .
|
||||
elif $ix then (.[0:$ix]|rjustify(n)) +.[$ix:]
|
||||
else rjustify(n)
|
||||
end ;
|
||||
23
Task/Fibonacci-word/jq/fibonacci-word-3.jq
Normal file
23
Task/Fibonacci-word/jq/fibonacci-word-3.jq
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# Generate the first n terms of the Fibonacci word sequence
|
||||
# as a stream of arrays of the form [index, word]
|
||||
def fibonacci_words(n):
|
||||
# input: [f(i-2), f(i-1), countdown, counter]
|
||||
def fib:
|
||||
if .[2] == 1 then [.[3], .[0]]
|
||||
else
|
||||
(.[1] + .[0]) as $sum
|
||||
| [ .[3], .[0]], ([ .[1], $sum, (.[2] - 1), (.[3] + 1) ] | fib)
|
||||
end;
|
||||
if n <= 0 then empty
|
||||
else (["1", "0", n, 1] | fib)
|
||||
end;
|
||||
|
||||
def task(n):
|
||||
fibonacci_words(n)
|
||||
| .[0] as $i
|
||||
| (.[1]|length) as $len
|
||||
| (.[1]|entropy) as $e
|
||||
| "\($i|rjustify(3)) \($len|rjustify(10)) \($e|precision(6))"
|
||||
;
|
||||
|
||||
task(37)
|
||||
Loading…
Add table
Add a link
Reference in a new issue