Find the missing permutation: Difference between revisions

Content added Content deleted
(added FreeBASIC)
Line 904: Line 904:
{{out}}
{{out}}
<pre>DBAC</pre>
<pre>DBAC</pre>
=={{header|FreeBASIC}}==
===Simple count===
<lang freebasic>' version 30-03-2017
' compile with: fbc -s console

Data "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD"
Data "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA"
Data "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD"
Data "BADC", "BDAC", "CBDA", "DBCA", "DCAB"

' ------=< MAIN >=------

Dim As ulong total(3, Asc("A") To Asc("D")) ' total(0 to 3, 65 to 68)
Dim As ULong i, j, n = 24 \ 4 ' n! \ n
Dim As String tmp

For i = 1 To 23
Read tmp
For j = 0 To 3
total(j, tmp[j]) += 1
Next
Next

tmp = Space(4)
For i = 0 To 3
For j = Asc("A") To Asc("D")
If total(i, j) <> n Then
tmp[i] = j
End If
Next
Next

Print "The missing permutation is : "; tmp

' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>The missing permutation is : DBAC</pre>
===Add the value's===
<lang freebasic>' version 30-03-2017
' compile with: fbc -s console

Data "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD"
Data "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA"
Data "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD"
Data "BADC", "BDAC", "CBDA", "DBCA", "DCAB"

' ------=< MAIN >=------

Dim As ULong total(3) ' total(0 to 3)
Dim As ULong i, j, n = 24 \ 4 ' n! \ n
Dim As ULong total_val = (Asc("A") + Asc("B") + Asc("C") + Asc("D")) * n
Dim As String tmp

For i = 1 To 23
Read tmp
For j = 0 To 3
total(j) += tmp[j]
Next
Next

tmp = Space(4)
For i = 0 To 3
tmp[i] = total_val - total(i)
Next

Print "The missing permutation is : "; tmp

' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
<pre>output is same as the first version</pre>
===Using Xor===
<lang freebasic>' version 30-03-2017
' compile with: fbc -s console

Data "ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD"
Data "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA"
Data "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD"
Data "BADC", "BDAC", "CBDA", "DBCA", "DCAB"

' ------=< MAIN >=------

Dim As ULong i,j
Dim As String tmp, missing = chr(0, 0, 0, 0) ' or string(4, 0)

For i = 1 To 23
Read tmp
For j = 0 To 3
missing[j] Xor= tmp[j]
Next
Next

Print "The missing permutation is : "; missing

' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
<pre>Output is the same as the first version</pre>


=={{header|GAP}}==
=={{header|GAP}}==