Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,6 +1,22 @@
Provide code that produces a list of numbers which is the n-th order forward difference, given a non-negative integer (specifying the order) and a list of numbers.
The first-order forward difference of a list of numbers (A) is a new list (B) where B<sub>n</sub> = A<sub>n+1</sub> - A<sub>n</sub>. List B should have one fewer element as a result.
The second-order forward difference of A will be the same as the first-order forward difference of B.
The second-order forward difference of A will be tdefmodule Diff do
def forward(arr,i\\1) do
forward(arr,[],i)
end
def forward([_|[]],diffs,i) do
if i == 1 do
IO.inspect diffs
else
forward(diffs,[],i-1)
end
end
def forward([val1|[val2|vals]],diffs,i) do
forward([val2|vals],diffs++[val2-val1],i)
end
endhe same as the first-order forward difference of B.
That new list will have two fewer elements than A and one less than B.
The goal of this task is to repeat this process up to the desired order.

View file

@ -0,0 +1,15 @@
defmodule Diff do
def forward(list,i\\1) do
forward(list,[],i)
end
def forward([_],diffs,1), do: IO.inspect diffs
def forward([_],diffs,i), do: forward(diffs,[],i-1)
def forward([val1,val2|vals],diffs,i) do
forward([val2|vals],diffs++[val2-val1],i)
end
end
Enum.each(1..9, fn i ->
Diff.forward([90, 47, 58, 29, 22, 32, 55, 5, 55, 73],i)
end)

View file

@ -1,41 +1,24 @@
butchers/mangles some of the characters in the documentation box. -->
<lang>/*REXX program computes the forward difference of a list of numbers.
/\ n n n-k
/ \ n [ƒ] (x) Σ C (-1) ƒ(x+k)
/____\ k=0 k
{delta} {n=order} {C=comb or binomial coeff.}
*/
numeric digits 100 /*ensure enough accuracy (digits)*/
parse arg xxx ',' N /*input: ε1 ε2 ε3 ε4 ··· , order*/
if xxx=='' then xxx='90 47 58 29 22 32 55 5 55 73' /*default numbers.*/
w=words(xxx) /*set W to # of numbers in list.*/
/* [↓] validate the input numbers*/
do i=1 for w; _=word(xxx,i) /*process each number 1 at a time*/
if \datatype(_,'N') then call ser _ "isn't a valid number"
@.i=_/1 /*normalize the #, prettify the #*/
end /*i*/ /* [↑] removes superfluous stuff*/
/* [↓] process (optional) order.*/
if w==0 then call ser 'no numbers were specified.'
if N\=='' & N<0 then call ser N "(order) can't be negative."
if N\=='' & N>w then call ser N "(order) can't be greater than" w
say right(w 'numbers:', 44) xxx /*display the header ··· */
say left('', 44)copies('', length(xxx)+2) /* and the header fence.*/
if N=='' then do; bot=0; top=w; end /*define default orders. */
else do; bot=N; top=N; end /*just a specific order? */
/*═════════════════════════════════════════where da rubber meets da road*/
do #=bot to top; do r=1 for w; !.r=@.r; end; $=
do j=1 for #; d=!.j; do k=j+1 to w
parse value !.k !.k-d with d !.k
end /*k*/
end /*j*/
do i=#+1 to w; $=$ !.i/1; end
if $=='' then $='[null]'
say right(#, 7)th(#)'-order forward difference vector = ' strip($)
end /*o*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────one─liner subroutines───────────────*/
ser: say; say '***error!***'; say arg(1); say; exit 13
th: arg ?; return word('th st nd rd',1+?//10*(?//100%10\==1)*(?//10<4))
/*REXX program computes the forward difference of a list of numbers. */
numeric digits 100 /*ensure enough accuracy (decimal digs)*/
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */
if e=='' then e='90 47 58 29 22 32 55 5 55 73' /*use some default numbers. */
#=words(e) /*# is the number of elements in list.*/
/* [↓] assign list numbers to @ array.*/
do i=1 for #; @.i=word(e,i)/1; end /*process each number one at a time. */
/* [↓] process the optional order. */
if N=='' then parse value 0 # # with bot top N /*define default order range. */
else parse var N bot 1 top /*Specified? Use only 1 order*/
say right(# 'numbers:', 44) e /*display the header (title) and ··· */
say left('',44)copies('',length(e)+2) /*display the header fence. */
/* [↓] where da rubber meets da road. */
do o=bot to top; do r=1 for #; !.r=@.r; end; $=
do j=1 for o; d=!.j; do k=j+1 to #; parse value !.k !.k-d with d !.k; end
end /*j*/
do i=o+1 to #; $=$ !.i/1; end
if $=='' then $='[null]'
say right(o,7)th(o)'order forward difference vector =' $
end /*o*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
th: arg ?; return word('th st nd rd',1+?//10*(?//100%10\==1)*(?//10<4))

View file

@ -1,35 +1,30 @@
/* REXX ***************************************************************
* Forward differences
* 18.08.2012 Walter Pachl derived from PL/I
**********************************************************************/
Do n=-1 To 11
Call differences '90 47 58 29 22 32 55 5 55 73',n
End
Exit
/*REXX program computes the forward difference of a list of numbers. */
numeric digits 100 /*ensure enough accuracy (decimal digs)*/
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */
if e=='' then e='90 47 58 29 22 32 55 5 55 73' /*use some default numbers. */
#=words(e) /*# is the number of elements in list.*/
/* [↓] verify list items are numeric. */
do i=1 for #; _=word(e,i) /*process each number one at a time. */
if \datatype(_,'N') then call ser _ "isn't a valid number"; @.i=_/1
end /*i*/ /* [↑] removes superfluous stuff. */
/* [↓] process the optional order. */
if N=='' then parse value 0 # # with bot top N /*define default order range. */
else parse var N bot 1 top /*Specified? Use only 1 order*/
if #==0 then call ser "no numbers were specified."
if N<0 then call ser N "(order) can't be negative."
if N># then call ser N "(order) can't be greater than" #
say right(# 'numbers:', 44) e /*display the header (title) and ··· */
say left('',44)copies('',length(e)+2) /*display the header fence. */
/* [↓] where da rubber meets da road. */
do o=bot to top; do r=1 for #; !.r=@.r; end; $=
do j=1 for o; d=!.j; do k=j+1 to #; parse value !.k !.k-d with d !.k; end
end /*j*/
do i=o+1 to #; $=$ !.i/1; end
if $=='' then $=' [null]'
say right(o,7)th(o)'order forward difference vector =' $
end /*o*/
differences: Procedure
Parse Arg a,n
m=words(a)
Select
When n<0 Then Say 'n is negative:' n '<' 0
When n>m Then Say 'n is too large:' n '>' m
Otherwise Do
Do i=1 By 1 while a<>''
Parse Var a a.i a
End
Do i = 1 to n;
t = a.i;
Do j = i+1 to m;
u = a.j
a.j = a.j-t;
t = u;
end;
end;
ol=''
Do k=n+1 to m
ol=ol a.k
End
Say n ol
End
End
Return
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
ser: say; say '***error!***'; say arg(1); say; exit 13
th: arg ?; return word('th st nd rd',1+?//10*(?//100%10\==1)*(?//10<4))

View file

@ -0,0 +1,34 @@
/*REXX program computes the forward difference of a list of numbers. */
numeric digits 100 /*ensure enough accuracy (decimal digs)*/
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */
if e=='' then e='90 47 58 29 22 32 55 5 55 73' /*use some default numbers. */
#=words(e); w=5 /*# is the number of elements in list.*/
/* [↓] verify list items are numeric. */
do i=1 for #; _=word(e,i) /*process each number one at a time. */
if \datatype(_,'N') then call ser _ "isn't a valid number"; @.i=_/1
w=max(w,length(@.i)) /*use the maximum length of an element.*/
end /*i*/ /* [↑] removes superfluous stuff. */
/* [↓] process the optional order. */
if N=='' then parse value 0 # # with bot top N /*define default order range. */
else parse var N bot 1 top /*Specified? Use only 1 order*/
if #==0 then call ser "no numbers were specified."
if N<0 then call ser N "(order) can't be negative."
if N># then call ser N "(order) can't be greater than" #
_=; do k=1 for #; _=_ right(@.k,w); end /*k*/; _=substr(_,2)
say right(# 'numbers:', 44) _ /*display the header (title) and ··· */
say left('',44)copies('',w*#+#) /*display the header fence. */
/* [↓] where da rubber meets da road. */
do o=bot to top; do r=1 for #; !.r=@.r; end /*r*/; $=
do j=1 for o; d=!.j; do k=j+1 to #; parse value !.k !.k-d with d !.k
w=max(w,length(!.k))
end /*k*/
end /*j*/
do i=o+1 to #; $=$ right(!.i/1,w); end /*i*/
if $=='' then $='[null]'
say right(o,7)th(o)'order forward difference vector =' $
end /*o*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
ser: say; say '***error!***'; say arg(1); say; exit 13
th: arg ?; return word('th st nd rd',1+?//10*(?//100%10\==1)*(?//10<4))

View file

@ -0,0 +1,35 @@
/* REXX ***************************************************************
* Forward differences
* 18.08.2012 Walter Pachl derived from PL/I
**********************************************************************/
Do n=-1 To 11
Call differences '90 47 58 29 22 32 55 5 55 73',n
End
Exit
differences: Procedure
Parse Arg a,n
m=words(a)
Select
When n<0 Then Say 'n is negative:' n '<' 0
When n>m Then Say 'n is too large:' n '>' m
Otherwise Do
Do i=1 By 1 while a<>''
Parse Var a a.i a
End
Do i = 1 to n;
t = a.i;
Do j = i+1 to m;
u = a.j
a.j = a.j-t;
t = u;
end;
end;
ol=''
Do k=n+1 to m
ol=ol a.k
End
Say n ol
End
End
Return