[Challenge] Rainbow BBCode Generator
Today I'll be posting up another programming challenge. This one will be slightly more difficult than the last, so I hope you're ready. This one will probably be more fun as well since it's a challenge I thought of completely.
I'll get straight to the point.
Criteria:
-Define a function or procedure that takes as input a string that you will convert to arbitrary "rainbow" BBCode. Optionally, throw in an option to specify the size of your generated output.
Subjective difficulty: Easy.
As a bonus, reward is 3x +rep from me.
As before, post your code in this thread (preferably enclosed in
I'll get straight to the point.
Criteria:
-Define a function or procedure that takes as input a string that you will convert to arbitrary "rainbow" BBCode. Optionally, throw in an option to specify the size of your generated output.
Subjective difficulty: Easy.
As a bonus, reward is 3x +rep from me.
As before, post your code in this thread (preferably enclosed in
Spoiler
tags by convention).
No deadline. Feel free to do this anytime.
Any programming language of your choice can be used.
My solution in Lua:
For better viewing, here it is on github.
https://gist.github.com/3351178
My algorithm is pretty straightforward and crude, but hey, it works. I was going t
rk on it a little more to expand on it, but meh.
I've also included a conventional release as an attachment that includes an executable and the source code for those of you who are interested.
I strongly implore you all to take a shot at this challenge. I'm curious to see all the different solutions out there.
Note: I was unable to post this in the programming puzzle section, so if you could do your thing Artificial that would be great.
Good luck, guys.
Download: Attachment #417
No deadline. Feel free to do this anytime.
Any programming language of your choice can be used.
My solution in Lua:
Spoiler
#!/usr/bin/env lua
--------------------------------------------------------------
-- @Description: Pedagogical Lua script to color code your text using arbitrary "rainbow" BBCode
-- @Author: Forget Me Not (The Unintelligible)
-- @Contact: xxprotozoid@live.com
-- @Version: v0.1
-- @Remarks: Primarily intended for Windows use. However, due to the fact that it's distributed free,
-- as in freedom, you are able to safely modify and re-distribute this script free of charge
-- as long as it abides by the GPLv2 terms.
-- @Dependencies: clipboard DLL module - http://files.luaforge.net/releases/jaslatrix/clipboard/1.0.0/clipboard-1.0.0-Lua51.zip
-- @License: GNU General Public License v2 (GPLv2) - http://www.gnu.org/copyleft/gpl.html
--------------------------------------------------------------
require "clipboard"
----------------------------------------------
--- Internal methods
----------------------------------------------
--- Returns Lua table from tokenized string
-- @param s Lua string to pass as input
-- @usage tokenize("I am a string")
local function tokenize(s)
local t = {}
for i = 1,#s do
t[i] = s:sub(i, i)
end
return t
end
--- Returns string color coded in rainbow BBCode and corresponding size
-- @param s Lua string to pass as input
-- @param size Lua number for the size of the text to output
-- @usage color_code("Lorem ipsum dolor sit amet, consectetur, adipisci velit.")
local function color_code(s, size)
local bool, err = assert(type(s) == "string", "Invalid data supplied.") -- If given data not string, throw an exception & terminate our function
if (not bool) then return end
local char_array = tokenize(s)
local buff = ""
for i = 1, #char_array do
-- Create a matrix of hues of the different colors of the rainbow
-- Each row is as follows: Red, Orange, Yellow, Green, Blue, Purple
local color_matrix = {
"#C11B17", "#F87A17", "#FFFF00", "#00FF00", "#2B60DE","#893BFF",
"#F63817", "#E78E17", "#FFFC17", "#5EFB6E", "#1531EC", "#8E35EF"
}
local ran_num = math.random(1, #color_matrix) -- Create a pseudo-random number ranging from 1 to the size of the color matrix
local color = color_matrix[ran_num]
if (string.find(char_array[i], ' ')) then whitespace = true end
if (not whitespace) then -- For resource conservation, we won't enclose whitespace in BBCode
bbcode_str = "[color= .. color .. ]" .. char_array[i] .. "[/i][/color]"
end
if (whitespace) then -- If whitespace is in our token we'll simply leave it as is. Else we wrap it up with BBCode
buff = buff .. char_array[i]
else
buff = buff .. bbcode_str
end
whitespace = false
end
if (not size) then size = 3 end -- If no size parameter passed, defaults to 3
buff = "[size= .. tostring(size) .. ]".. buff .. "[/size]" -- Finalize process by prepending and appending size tags to our string
return buff, #buff
end
----------------------------------------------
--- Main program
----------------------------------------------
local function main()
io.write("Enter your text to be color-coded: ") -- Prompt user to feed input to program for our process
input_text = io.read() -- Wait for input from the STDIN/STDOUT stream
-- Rinse, repeat
io.write("Now enter your size attribute for the text: ")
input_size = io.read()
print("Now initializing the process...\n") -- Print out user-friendly message while the process unfolds under the hood
result = color_code(input_text, input_size)
clipboard.settext(result)
print("Done! Your legendary rainbow text has been copied to your clipboard.\n\nPress enter to exit.\n") -- Done!
io.read() -- Idle
end
main()
[/i][/i][/i]For better viewing, here it is on github.
https://gist.github.com/3351178
My algorithm is pretty straightforward and crude, but hey, it works. I was going t
rk on it a little more to expand on it, but meh.I've also included a conventional release as an attachment that includes an executable and the source code for those of you who are interested.
I strongly implore you all to take a shot at this challenge. I'm curious to see all the different solutions out there.
Note: I was unable to post this in the programming puzzle section, so if you could do your thing Artificial that would be great.
Good luck, guys.
Download: Attachment #417
