2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -7,6 +7,13 @@ A ''continuous'' subsequence is one in which no elements are missing between the
|
|||
Note: Subsequences are defined ''structurally'', not by their contents.
|
||||
So a sequence ''a,b,c,d'' will always have the same subsequences and continuous subsequences, no matter which values are substituted; it may even be the same value.
|
||||
|
||||
'''Task''': Find all non-continuous subsequences for a given sequence. Example: For the sequence ''1,2,3,4'', there are five non-continuous subsequences, namely ''1,3''; ''1,4''; ''2,4''; ''1,3,4'' and ''1,2,4''.
|
||||
'''Task''': Find all non-continuous subsequences for a given sequence.
|
||||
|
||||
Example: For the sequence ''1,2,3,4'', there are five non-continuous subsequences, namely:
|
||||
::::* ''1,3''
|
||||
::::* ''1,4''
|
||||
::::* ''2,4''
|
||||
::::* ''1,3,4''
|
||||
::::* ''1,2,4''
|
||||
|
||||
'''Goal''': There are different ways to calculate those subsequences. Demonstrate algorithm(s) that are natural for the language.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
defmodule RC do
|
||||
defp masks(n) do
|
||||
maxmask = trunc(:math.pow(2, n)) - 1
|
||||
Enum.map(3..maxmask, &Integer.to_string(&1, 2))
|
||||
|> Enum.filter_map(&contains_noncont(&1), &String.rjust(&1, n, ?0)) # padding
|
||||
end
|
||||
|
||||
defp contains_noncont(n) do
|
||||
Regex.match?(~r/10+1/, n)
|
||||
end
|
||||
|
||||
defp apply_mask_to_list(mask, list) do
|
||||
Enum.zip(to_char_list(mask), list)
|
||||
|> Enum.filter_map(fn {include, _} -> include > ?0 end, fn {_, value} -> value end)
|
||||
end
|
||||
|
||||
def ncs(list) do
|
||||
Enum.map(masks(length(list)), fn mask -> apply_mask_to_list(mask, list) end)
|
||||
end
|
||||
end
|
||||
|
||||
IO.inspect RC.ncs([1,2,3])
|
||||
IO.inspect RC.ncs([1,2,3,4])
|
||||
IO.inspect RC.ncs('abcd')
|
||||
|
|
@ -1,33 +1,32 @@
|
|||
/*REXX program lists non-continuous subsequences (NCS), given a sequence. */
|
||||
parse arg list /*obtain the list from the C.L. */
|
||||
if list='' then list=1 2 3 4 5 /*Not specified? Use the default*/
|
||||
say 'list=' space(list); say /*display the list to terminal. */
|
||||
w=words(list) ; #=0 /*W: words in list; # of NCS. */
|
||||
$=left(123456789,w) /*build a string of decimal digs.*/
|
||||
tail=right($,max(0,w-2)) /*construct a "fast" tail. */
|
||||
/*REXX program lists all the non─continuous subsequences (NCS), given a sequence. */
|
||||
parse arg list /*obtain the arguments from the C. L. */
|
||||
if list='' | list==',' then list=1 2 3 4 5 /*Not specified? Then use the default.*/
|
||||
say 'list=' space(list); say /*display the list to the terminal. */
|
||||
w=words(list) /*W: is the number of items in list. */
|
||||
$=left(123456789, w) /*build a string of decimal digits. */
|
||||
tail=right($, max(0, w-2)) /*construct a fast tail for comparisons*/
|
||||
#=0 /* [↓] L: length of Jth item. */
|
||||
do j=13 to left($,1) || tail; L=length(j) /*step through list (using smart start)*/
|
||||
if verify(j, $)\==0 then iterate /*Not one of the chosen (sequences) ? */
|
||||
f=left(j,1) /*use the fist decimal digit of J. */
|
||||
NCS=0 /*there isn't a non─continuous subseq. */
|
||||
do k=2 to L; _=substr(j, k, 1) /*extract a single decimal digit of J.*/
|
||||
if _ <= f then iterate j /*if next digit ≤, then skip this digit*/
|
||||
if _ \== f+1 then NCS=1 /*it's OK as of now (that is, so far).*/
|
||||
f=_ /*now have a new next decimal digit. */
|
||||
end /*k*/
|
||||
|
||||
do j=13 to left($,1) || tail /*step through the list. */
|
||||
if verify(j,$)\==0 then iterate /*Not one of the chosen? */
|
||||
f=left(j,1) /*use the 1st decimal digit of J.*/
|
||||
NCS=0 /*not non-continuous subsequence.*/
|
||||
do k=2 to length(j); _=substr(j,k,1) /*pick off a single decimal digit*/
|
||||
if _ <= f then iterate j /*if next digit ≤, then skip it.*/
|
||||
if _ \== f+1 then NCS=1 /*it's OK as of now. */
|
||||
f=_ /*now have a new next decimal dig*/
|
||||
end /*k*/
|
||||
if \NCS then iterate /*not OK? Then skip this number (item)*/
|
||||
#=#+1 /*Eureka! We found a number (or item).*/
|
||||
@=; do m=1 for L /*build a sequence string to display. */
|
||||
@=@ word(list, substr(j, m, 1)) /*pick off a number (item) to display. */
|
||||
end /*m*/
|
||||
|
||||
if \NCS then iterate /*not OK? Then skip this number.*/
|
||||
#=#+1 /*Eureka! We found onea digit.*/
|
||||
x= /*the beginning of the NCS. */
|
||||
do m=1 for length(j) /*build a sequence string to show*/
|
||||
x=x word(list,substr(j,m,1)) /*pick off a number to display. */
|
||||
end /*m*/
|
||||
|
||||
say 'a non-continuous subsequence: ' x /*show non─continous subsequence.*/
|
||||
end /*j*/
|
||||
|
||||
if #==0 then #='no' /*make it more gooder Anglesh. */
|
||||
say; say # "non-continuous subsequence"s(#) 'were found.'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
s: if arg(1)==1 then return ''; return word(arg(2) 's',1) /*plurals.*/
|
||||
say 'a non─continuous subsequence: ' @ /*show the non─continuous subsequence. */
|
||||
end /*j*/
|
||||
say
|
||||
if #==0 then #='no' /*make it look more gooder Angleshy. */
|
||||
say # "non─continuous subsequence"s(#) 'were found.'
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
s: if arg(1)==1 then return ''; return word(arg(2) 's', 1) /*pluralizer.*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue