Non-continuous subsequences: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 2,008: | Line 2,008: | ||
42 non-continuous subsequences were found. |
42 non-continuous subsequences were found. |
||
</pre> |
|||
=={{header|Ring}}== |
|||
<lang ring> |
|||
# Project : Non-continuous subsequences |
|||
# Date : 2018/02/03 |
|||
# Author : Gal Zsolt [~ CalmoSoft ~] |
|||
# Email : <calmosoft@gmail.com> |
|||
load "stdlib.ring" |
|||
list = [1,2,3,4] |
|||
items = newlist(pow(2,len(list))-1,len(list)) |
|||
see "For [1, 2, 3, 4] non-continuous subsequences are:" + nl |
|||
powerset(list,4) |
|||
showarray(items,4) |
|||
see nl |
|||
list = [1,2,3,4,5] |
|||
items = newlist(pow(2,len(list))-1,len(list)) |
|||
see "For [1, 2, 3, 4, 5] non-continuous subsequences are:" + nl |
|||
powerset(list,5) |
|||
showarray(items,5) |
|||
func showarray(items,ind) |
|||
for n = 1 to len(items) |
|||
flag = 0 |
|||
for m = 1 to ind - 1 |
|||
if items[n][m] = 0 or items[n][m+1] = 0 |
|||
exit |
|||
ok |
|||
if (items[n][m] + 1) != items[n][m+1] |
|||
flag = 1 |
|||
exit |
|||
ok |
|||
next |
|||
if flag = 1 |
|||
see "[" |
|||
str = "" |
|||
for x = 1 to len(items[n]) |
|||
if items[n][x] != 0 |
|||
str = str + items[n][x] + " " |
|||
ok |
|||
next |
|||
str = left(str, len(str) - 1) |
|||
see str + "]" + nl |
|||
ok |
|||
next |
|||
func powerset(list,ind) |
|||
num = 0 |
|||
num2 = 0 |
|||
items = newlist(pow(2,len(list))-1,ind) |
|||
for i = 2 to (2 << len(list)) - 1 step 2 |
|||
num2 = 0 |
|||
num = num + 1 |
|||
for j = 1 to len(list) |
|||
if i & (1 << j) |
|||
num2 = num2 + 1 |
|||
if list[j] != 0 |
|||
items[num][num2] = list[j] |
|||
ok |
|||
ok |
|||
next |
|||
next |
|||
return items |
|||
</lang> |
|||
Output: |
|||
<pre> |
|||
For [1, 2, 3, 4] non-continuous subsequences are: |
|||
[1 3] |
|||
[1 4] |
|||
[2 4] |
|||
[1 2 4] |
|||
[1 3 4] |
|||
For [1, 2, 3, 4, 5] non-continuous subsequences are: |
|||
[1 3] |
|||
[1 4] |
|||
[2 4] |
|||
[1 2 4] |
|||
[1 3 4] |
|||
[1 5] |
|||
[2 5] |
|||
[1 2 5] |
|||
[3 5] |
|||
[1 3 5] |
|||
[2 3 5] |
|||
[1 2 3 5] |
|||
[1 4 5] |
|||
[2 4 5] |
|||
[1 2 4 5] |
|||
[1 3 4 5] |
|||
</pre> |
</pre> |
||