Move-to-front algorithm: Difference between revisions
m
fix
(add FreeBASIC) |
m (fix) |
||
Line 969:
bananaaa -> { 1 1 13 1 1 1 0 0 } -> bananaaa
hiphophiphop -> { 7 8 15 2 15 2 2 3 2 2 3 2 } -> hiphophiphop
</pre>▼
=={{header|Gambas}}==▼
'''[https://gambas-playground.proko.eu/?gist=4268c779c36def03ea10764630cdc401 Click this link to run this code]'''▼
<lang gambas>Public Sub Main()▼
Dim sToCode As String[] = ["broood", "bananaaa", "hiphophiphop"] 'Samples to process▼
Dim sHold As New String[] 'To store results▼
Dim siCount, siCounter, siPos As Short 'Various variables▼
Dim sOutput, sCode, sWork, sEach As String 'Various variables▼
For siCounter = 0 To sToCode.Max 'To loop through each 'Sample' ▼
sCode = "abcdefghijklmnopqrstuvwxyz" 'Set sCode to default setting▼
For siCount = 1 To Len(sToCode[siCounter]) 'Loop through each letter in 'Sample'▼
sWork = Mid(sToCode[siCounter], siCount, 1) 'sWork to store the Letter▼
siPos = InStr(scode, sWork) - 1 'Find the position of the letter in sCode, -1 for '0' based array▼
sOutput &= Str(siPos) & " " 'Add the postion to sOutput▼
sCode = Mid(sCode, siPos + 1, 1) & Replace(sCode, sWork, "") 'sCode = the letter + the rest of sCode except the letter▼
Next▼
Print sToCode[siCounter] & " = " & sOutput 'Print the 'Sample' and the coded version▼
sHold.Add(Trim(sOutput)) 'Add the code to the sHold array▼
sOutput = "" 'Clear sOutput▼
Next▼
Print 'Print a blank line▼
For siCounter = 0 To sHold.Max 'To loop through each coded 'Sample' ▼
sCode = "abcdefghijklmnopqrstuvwxyz" 'Set sCode to default setting▼
For Each sEach In Split(sHold[siCounter], " ") 'For each 'code' in coded 'Sample'▼
sWork = Mid(sCode, Val(sEach) + 1, 1) 'sWork = the decoded letter▼
sOutput &= sWork 'Add the decoded letter to sOutput▼
sCode = sWork & Replace(sCode, sWork, "") 'sCode = the decoded letter + the rest of sCode except the letter▼
Next▼
Print sHold[siCounter] & " = " & sOutput 'Print the coded and decoded result▼
sOutput = "" 'Clear sOutput▼
Next▼
End</lang>▼
Output:▼
<pre>▼
broood = 1 17 15 0 0 5 ▼
bananaaa = 1 1 13 1 1 1 0 0 ▼
hiphophiphop = 7 8 15 2 15 2 2 3 2 2 3 2 ▼
1 17 15 0 0 5 = broood▼
1 1 13 1 1 1 0 0 = bananaaa▼
7 8 15 2 15 2 2 3 2 2 3 2 = hiphophiphop▼
</pre>
Line 1,128 ⟶ 1,082:
hiphophiphop
</pre>
▲=={{header|Gambas}}==
▲'''[https://gambas-playground.proko.eu/?gist=4268c779c36def03ea10764630cdc401 Click this link to run this code]'''
▲<lang gambas>Public Sub Main()
▲Dim sToCode As String[] = ["broood", "bananaaa", "hiphophiphop"] 'Samples to process
▲Dim sHold As New String[] 'To store results
▲Dim siCount, siCounter, siPos As Short 'Various variables
▲Dim sOutput, sCode, sWork, sEach As String 'Various variables
▲For siCounter = 0 To sToCode.Max 'To loop through each 'Sample'
▲ sCode = "abcdefghijklmnopqrstuvwxyz" 'Set sCode to default setting
▲ For siCount = 1 To Len(sToCode[siCounter]) 'Loop through each letter in 'Sample'
▲ sWork = Mid(sToCode[siCounter], siCount, 1) 'sWork to store the Letter
▲ siPos = InStr(scode, sWork) - 1 'Find the position of the letter in sCode, -1 for '0' based array
▲ sOutput &= Str(siPos) & " " 'Add the postion to sOutput
▲ sCode = Mid(sCode, siPos + 1, 1) & Replace(sCode, sWork, "") 'sCode = the letter + the rest of sCode except the letter
▲ Next
▲ Print sToCode[siCounter] & " = " & sOutput 'Print the 'Sample' and the coded version
▲ sHold.Add(Trim(sOutput)) 'Add the code to the sHold array
▲ sOutput = "" 'Clear sOutput
▲Next
▲Print 'Print a blank line
▲For siCounter = 0 To sHold.Max 'To loop through each coded 'Sample'
▲ sCode = "abcdefghijklmnopqrstuvwxyz" 'Set sCode to default setting
▲ For Each sEach In Split(sHold[siCounter], " ") 'For each 'code' in coded 'Sample'
▲ sWork = Mid(sCode, Val(sEach) + 1, 1) 'sWork = the decoded letter
▲ sOutput &= sWork 'Add the decoded letter to sOutput
▲ sCode = sWork & Replace(sCode, sWork, "") 'sCode = the decoded letter + the rest of sCode except the letter
▲ Next
▲ Print sHold[siCounter] & " = " & sOutput 'Print the coded and decoded result
▲ sOutput = "" 'Clear sOutput
▲Next
▲End</lang>
▲Output:
▲<pre>
▲broood = 1 17 15 0 0 5
▲bananaaa = 1 1 13 1 1 1 0 0
▲hiphophiphop = 7 8 15 2 15 2 2 3 2 2 3 2
▲1 17 15 0 0 5 = broood
▲1 1 13 1 1 1 0 0 = bananaaa
▲7 8 15 2 15 2 2 3 2 2 3 2 = hiphophiphop
▲</pre>
=={{header|Go}}==
{{trans|Python}}
|