Play the function game...

Monkey Forums/Monkey Programming/Play the function game...

Paul - Taiphoz(Posted 2014) [#1]
Write a function in less than 20 lines of code, and then suggest a change to the function immediately above your post.

Function can be or do anything you like.

Print Leet("HELLO THIS IS A TEST BLEH.")

Function Leet:String(_text:string)
	local sypher:string="4871£0" , dict:string="ABTLEO" , char:int =0, newtext:string
	Repeat
		local match:bool = false
		for local looper:int = 0 to sypher.Length()-1
			if _text[char] = dict[looper]
				newtext+= string.FromChar(sypher[looper])
				match=true
			endif			
		next
		if NOT match then newtext+= string.FromChar(_text[char]) 				
		char+=1
	until char >= _text.Length()
	return newtext
end function



Nobuyuki(Posted 2014) [#2]
Clever. I would suggest creating a local for newtext as IntStack instead of String to reduce GC thrash on Android, since strings are immutable, or an int array of the input length, then generating the string at the very end from the chars.

Here's an old piece of code from my vault:
Function Main:Int()
	Print ChimpCipher("You are now totally encoded, good work.", "Longer keys are better")
	Print ChimpCipher(ChimpCipher("You are now totally encoded, good work.", "Longer keys are better"), "Longer keys are better")
End Function

'Summary: Encrypts a string using a XOR table.
Function ChimpCipher:String(input:String, key:String)
	Local out:int[input.Length] 'output string chars
	Local k:Int 'Key iterator
	For Local i:Int = 0 Until input.Length
		Local ochr:Int = input[i]
		If k >= key.Length Then k = 0 'cycle
		out[i] = ochr ~ key[k]
		k += 1
	Next
	Return String.FromChars(out)
End Function