August 2009 Programming Challenge : Give it a bash!

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Good day ladies and gentlemen. Yesterday I came across a really nasty piece of code and had to rewrite it in a different language (VBScript -> C#) and still acheive the same result. I had a great deal of fun doing it and I am pretty certain you will too! I thought I would start with a relatively small challenge to gauge the interest. If the interest is big then we can look at doing team challenges or collaborative challenges which I figure might be quiet nice for figuring out how other people think and solve problems.

Ok here is the deal.

The problem:
Given the string (GUID/UUID):
8cdb2408-81e3-4b8c-9ba2-916c14927946

Convert it to the following string:
LPQOFHDL-LERG-HOLP-MONF-MEJPEHMFKMHJ

The original function looked like this:
Code: [ Select ]
 
Dim x, y, abfrom, abto
Encode = "" : abfrom = ""
 
For x = 0 To 25 : abfrom = abfrom & Chr(65 + x) : Next
For x = 0 To 25 : abfrom = abfrom & Chr(97 + x) : Next
For x = 0 To 9 : abfrom = abfrom & CStr(x) : Next
 
abto = Mid(abfrom, 14, Len(abfrom) - 13) & Left(abfrom, 13)
For x = 1 To Len(sIn) : y = InStr(abfrom, Mid(sIn, x, 1))
  If y = 0 Then
    Encode = Encode & Mid(sIn, x, 1)
  Else
    Encode = Encode & Mid(abto, y, 1)
  End If
Next
 
  1.  
  2. Dim x, y, abfrom, abto
  3. Encode = "" : abfrom = ""
  4.  
  5. For x = 0 To 25 : abfrom = abfrom & Chr(65 + x) : Next
  6. For x = 0 To 25 : abfrom = abfrom & Chr(97 + x) : Next
  7. For x = 0 To 9 : abfrom = abfrom & CStr(x) : Next
  8.  
  9. abto = Mid(abfrom, 14, Len(abfrom) - 13) & Left(abfrom, 13)
  10. For x = 1 To Len(sIn) : y = InStr(abfrom, Mid(sIn, x, 1))
  11.   If y = 0 Then
  12.     Encode = Encode & Mid(sIn, x, 1)
  13.   Else
  14.     Encode = Encode & Mid(abto, y, 1)
  15.   End If
  16. Next
  17.  


Your challenge:
Rewrite this in any other language (I had to do it in C# and it took me about 30/45 minutes).

Submission Format & Rules
Since this could probably be wrapped up in a single method/function, post your solutions in this topic. Once someone has posted a solution in a specific language you may not repost the same algorithim in that language. If it is a variation on the algorithm it is fine

In closing
It isn't that hard but it is cool when you get it right! Happy hunting and this challenge finishes end Aug 2009 or when everyone is bored with it :)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

What's the submission format? Do you want to see the code here or would you prefer external links to project files?

EDIT: Also, your solution doesn't match the original function. I'll pm as to keep from posting teasers.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Oops sorry, I figured it was a relatively small challenge so lets go with submitting the code here. Will update my post :)
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

UPSGuy wrote:
What's the submission format? Do you want to see the code here or would you prefer external links to project files?

EDIT: Also, your solution doesn't match the original function. I'll pm as to keep from posting teasers.


My solution doesn't match the original format? It isn't supposed to :) You are supposed to create the new string from the old string(UUID)
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

EDIT: OK, via PM I understand now. Note for others: The given code is not a fully functioning solution. Just a head's up. ;)
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Python:

PYTHON Code: [ Select ]
def guidToStr(str):
 retval = ""
 for i in range(len(str)):
   retval += '-' if str[i] == '-' else chr(int(str[i], 16) + 68)
 return retval
  1. def guidToStr(str):
  2.  retval = ""
  3.  for i in range(len(str)):
  4.    retval += '-' if str[i] == '-' else chr(int(str[i], 16) + 68)
  5.  return retval
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Nice solution, spork. I didn't think of using radix. I think I'll have a swing at a perl solution.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

That is super impressive! Nice work Spork! The joys of scripting lanuages :)
  • George L.
  • Bronze Member
  • Bronze Member
  • George L.
  • Posts: 2209
  • Loc: Malaysia

Post 3+ Months Ago

Code: [ Select ]
retval += '-' if str[i] == '-' else chr(int(str[i], 16) + 68)


'if' is an invalid syntax?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

George - I loaded and tested Spork's code. It runs and outputs the expected result.
  • George L.
  • Bronze Member
  • Bronze Member
  • George L.
  • Posts: 2209
  • Loc: Malaysia

Post 3+ Months Ago

Just I could not load pass this

Code: [ Select ]
retval += '-' if str[i] == '-' else chr(int(str[i], 16) + 68)
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Successful load and execution here. What IDE are you using George?

Image
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Rabid Dog wrote:
That is super impressive! Nice work Spork! The joys of scripting lanuages :)

Haha, absolutely. The ability to precisely express something in just a few lines of code is what makes them awesome. I'm going to post another solution in Haskell at some point; don't want to leave functional languages in the dark :)

@George, that line is Python's ternary syntax. It's the equivalent of C-style languages that use the "condition ? statement : statement" syntax. Basically it reads like this:

PYTHON Code: [ Select ]
var = this_value if this_condition_is_true else this_value


It's only available in Python >= 2.5, so make sure you aren't using an older version.
  • George L.
  • Bronze Member
  • Bronze Member
  • George L.
  • Posts: 2209
  • Loc: Malaysia

Post 3+ Months Ago

I see now, UPSGuy.

Hi spork, (Jeff) I see I am indeed using 2.4.3, an older version. No wonder. Sorry, to everybody for making a few more unnecessary posts in here.

I am trying to work on something here before I sleep.

Thank you very much.

:thumbsup:
  • George L.
  • Bronze Member
  • Bronze Member
  • George L.
  • Posts: 2209
  • Loc: Malaysia

Post 3+ Months Ago

UPSGuy, Oh..I overlooked your question. I am using I am not sure what is that. It is just Python.exe that fire up this CMD-like black background, white text. I downloaded quite some time ago, I forget when. Thank you for showing the output, UPSGuy.

:thumbsup:
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Long as we got you straight. ;) I don't mess with Python much, so probably better that spork stepped back in. :D
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Batter up!

Perl:
Code: [ Select ]
 
sub convert {
     $_[0] =~ s/([a-z]|[0-9])/((ord($1)%39)>18)?uc(chr(ord($1)+13)):uc(chr(ord($1)+52))/eg && return $_[0];
}
 
  1.  
  2. sub convert {
  3.      $_[0] =~ s/([a-z]|[0-9])/((ord($1)%39)>18)?uc(chr(ord($1)+13)):uc(chr(ord($1)+52))/eg && return $_[0];
  4. }
  5.  
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I originally toyed around with awk so I could play on bash in the title. I've got a lot to learn about that interpreter though and I've been awake for too many hours waiting for the window guy to get here.

So, I better just do it with a few things I know. :)

Code: [ Select ]
#!/usr/bin/php
<?php echo preg_replace_callback('#[\da-f]#',create_function('$c','return chr(intval($c[0],16)+68);'),$argv[1])."\n"; ?>
  1. #!/usr/bin/php
  2. <?php echo preg_replace_callback('#[\da-f]#',create_function('$c','return chr(intval($c[0],16)+68);'),$argv[1])."\n"; ?>


Code: [ Select ]
<script type="text/javascript">
    var str = "8cdb2408-81e3-4b8c-9ba2-916c14927946";
    document.write(str.replace(/[\da-f]/gi,function(a){return String.fromCharCode(parseInt(a,16)+68);}));
</script>
  1. <script type="text/javascript">
  2.     var str = "8cdb2408-81e3-4b8c-9ba2-916c14927946";
  3.     document.write(str.replace(/[\da-f]/gi,function(a){return String.fromCharCode(parseInt(a,16)+68);}));
  4. </script>
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

I had a feeling we'd see a JS solution from you :)

As promised, here's a solution in Haskell:

HASKELL Code: [ Select ]
import Char
guidToStr [] = ""
guidToStr (x:xs) = [(if x/='-' then chr ((digitToInt x) + 68) else '-')] ++ guidToStr xs
  1. import Char
  2. guidToStr [] = ""
  3. guidToStr (x:xs) = [(if x/='-' then chr ((digitToInt x) + 68) else '-')] ++ guidToStr xs
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

I guess I best keep the challenges to scripying languages huh LOL

Lovely to see how people solved this
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Rabid Dog wrote:
I guess I best keep the challenges to scripying languages huh LOL

Lovely to see how people solved this


meh. I can do several others, too (and I know these other guys can as well), but I wanted to leave the less abstract (PHP, C#, Java, etc) to the newer members so that they could participate. Gives us all a way to take part.

I hope someone else will vary from spork's basic algorithm. The most challenging part for me was finding a short way to do it without anything that was already demonstrated. There has to be other ways lying in the numbers.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Well if I have my way we will soon have full SDLC challenges coming :) Start opening up your evenings lads!
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I guess I just needed to go a little longer without sleep to get an awk one working. Albeit an ugly awk one. :lol:

Code: [ Select ]
echo '8cdb2408-81e3-4b8c-9ba2-916c14927946' | awk '{gsub("3","G");gsub("4","H");gsub("5","I");gsub("6","J");gsub("7","K");gsub("8","L");gsub("9","M");gsub("a","N");gsub("b","O");gsub("c","P");gsub("d","Q");gsub("e","R");gsub("f","S");gsub("0","D");gsub("1","E");gsub("2","F"); print }'
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

hahahahahahahahaha!
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

I'd really like to see someone solve this in brainf*ck or whitespace.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Ok so I assume everyone is bored with this then?

My solutions was pretty much a direct transfer of the initially logic with a change in array counters and method calls inside the .Net framework.

So guys, seeing as the only guys that took part where the old hats, any suggestions for the next one? I was thinking going the whole hog, business use cases, system design, best design gets implemented then we end up with an open source system?

Feedback would be greatly appreciated :)
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

I had another solution in C... highly obfuscated and confusing... but I got bored tracking down a segfault so I never finished it.

If we did a full project from reqs&specs to implementation, I likely won't have time to participate :/
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Same here. I took some time here and there to piece together the perl one-liner and debug it, but an entire project would either be too time consuming, tedious, or just forgotten about, I fear.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

hmmm any other ideas then? a challenge is supposed to last an entire month :) Prehaps we could break it down even further if we do go the full system route? Just difficult trying to find challenges and it seems the only guys participating are the guys with a great deal of experience
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Take an image as input, and return the "mood" of the image as output.

Practical application: The genre of a movie might be guessed using only the cover art of a movie. Bright "hot" colors like red or yellow may mean an Action or Comedy movie. Dark colors such as violet may signify horror movies or stories of the occult.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 50 posts
  • Users browsing this forum: No registered users and 119 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.