August 2009 Programming Challenge : Give it a bash!

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post July 29th, 2009, 1:24 am

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 :)
Watch me grow
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 29th, 2009, 1:24 am

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 29th, 2009, 6:12 am

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.
I'd love to change the world, but they won't give me the source code.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post July 29th, 2009, 6:17 am

Oops sorry, I figured it was a relatively small challenge so lets go with submitting the code here. Will update my post :)
Watch me grow
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post July 29th, 2009, 6:33 am

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)
Watch me grow
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 29th, 2009, 6:36 am

EDIT: OK, via PM I understand now. Note for others: The given code is not a fully functioning solution. Just a head's up. ;)
I'd love to change the world, but they won't give me the source code.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6128
  • Loc: Seattle, WA
  • Status: Offline

Post July 29th, 2009, 8:24 am

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
The Beer Monocle. Classy.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 29th, 2009, 8:33 am

Nice solution, spork. I didn't think of using radix. I think I'll have a swing at a perl solution.
I'd love to change the world, but they won't give me the source code.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post July 29th, 2009, 8:53 am

That is super impressive! Nice work Spork! The joys of scripting lanuages :)
Watch me grow
  • George L.
  • Bronze Member
  • Bronze Member
  • No Avatar
  • Joined: Nov 05, 2007
  • Posts: 2206
  • Loc: Malaysia
  • Status: Offline

Post July 29th, 2009, 8:56 am

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


'if' is an invalid syntax?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 29th, 2009, 9:09 am

George - I loaded and tested Spork's code. It runs and outputs the expected result.
I'd love to change the world, but they won't give me the source code.
  • George L.
  • Bronze Member
  • Bronze Member
  • No Avatar
  • Joined: Nov 05, 2007
  • Posts: 2206
  • Loc: Malaysia
  • Status: Offline

Post July 29th, 2009, 9:14 am

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
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post July 29th, 2009, 9:29 am

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

Image
I'd love to change the world, but they won't give me the source code.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6128
  • Loc: Seattle, WA
  • Status: Offline

Post July 29th, 2009, 9:49 am

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.
The Beer Monocle. Classy.
  • George L.
  • Bronze Member
  • Bronze Member
  • No Avatar
  • Joined: Nov 05, 2007
  • Posts: 2206
  • Loc: Malaysia
  • Status: Offline

Post July 29th, 2009, 9:54 am

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
  • No Avatar
  • Joined: Nov 05, 2007
  • Posts: 2206
  • Loc: Malaysia
  • Status: Offline

Post July 29th, 2009, 9:59 am

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:
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 29th, 2009, 9:59 am

Post Information

  • Total Posts in this topic: 50 posts
  • Users browsing this forum: Kurthead+1, Zealous and 89 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
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.