Ordered partitions: Difference between revisions

Ordered partitions en FreeBASIC
(Added Wren)
(Ordered partitions en FreeBASIC)
Line 916:
[[1, 2], [], [3, 4]]
</pre>
 
 
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<lang FreeBASIC>Function Perm(x() As Integer) As Boolean
Dim As Integer i, j
For i = Ubound(x,1)-1 To 0 Step -1
If x(i) < x(i+1) Then Exit For
Next i
If i < 0 Then Return False
j = Ubound(x,1)
While x(j) <= x(i)
j -= 1
Wend
Swap x(i), x(j)
i += 1
j = Ubound(x,1)
While i < j
Swap x(i), x(j)
i += 1
j -= 1
Wend
Return True
End Function
 
Function Particiones(list() As Integer) As String
Dim As Integer i, j, n, p ', x()
Dim As String oSS = ""
n = Ubound(list)
Dim As Integer x(n)
For i = 0 To n
If list(i) Then
For j = 1 To list(i)
x(p) = i
p += 1
Next j
End If
Next i
Do
For i = 0 To n
oSS += " ( "
For j = 0 To Ubound(x,1)
If x(j) = i Then oSS += Str(j+1) + " "
Next j
oSS += ")"
Next i
oSS += Chr(13) + Chr(10)
Loop Until Not Perm(x())
Return oSS
End Function
 
Dim list2(2) As Integer = {1, 1, 1}
Print "Particiones(1, 1, 1):"
Print Particiones(list2())
Dim list3(3) As Integer = {1, 2, 0, 1}
Print !"\nParticiones(1, 2, 0, 1):"
Print Particiones(list3())
 
Sleep</lang>
{{out}}
<pre>
Particiones(1, 1, 1):
( 1 ) ( 2 ) ( 3 )
( 1 ) ( 3 ) ( 2 )
( 2 ) ( 1 ) ( 3 )
( 3 ) ( 1 ) ( 2 )
( 2 ) ( 3 ) ( 1 )
( 3 ) ( 2 ) ( 1 )
 
 
Particiones(1, 2, 0, 1):
( 1 ) ( 2 3 ) ( ) ( 4 )
( 1 ) ( 2 4 ) ( ) ( 3 )
( 1 ) ( 3 4 ) ( ) ( 2 )
( 2 ) ( 1 3 ) ( ) ( 4 )
( 2 ) ( 1 4 ) ( ) ( 3 )
( 3 ) ( 1 2 ) ( ) ( 4 )
( 4 ) ( 1 2 ) ( ) ( 3 )
( 3 ) ( 1 4 ) ( ) ( 2 )
( 4 ) ( 1 3 ) ( ) ( 2 )
( 2 ) ( 3 4 ) ( ) ( 1 )
( 3 ) ( 2 4 ) ( ) ( 1 )
( 4 ) ( 2 3 ) ( ) ( 1 )
</pre>
 
 
=={{header|GAP}}==
2,122

edits