# August 2009 Programming Challenge : Give it a bash!

• Web Master
• Posts: 3243
• Loc: South Africa

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
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

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.
• Web Master
• Posts: 3243
• Loc: South Africa

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
• Web Master
• Posts: 3243
• Loc: South Africa

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)
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

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.
• Brewmaster
• Posts: 6299
• Loc: Seattle, WA

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
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

3+ Months Ago

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

3+ Months Ago

That is super impressive! Nice work Spork! The joys of scripting lanuages
• Bronze Member
• Posts: 2208
• Loc: Malaysia

3+ Months Ago

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

'if' is an invalid syntax?
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

3+ Months Ago

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

3+ Months Ago

Just I could not load pass this

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

3+ Months Ago

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

• Brewmaster
• Posts: 6299
• Loc: Seattle, WA

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.
• Bronze Member
• Posts: 2208
• Loc: Malaysia

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.

• Bronze Member
• Posts: 2208
• Loc: Malaysia

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.

• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

3+ Months Ago

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

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.
• Genius
• Posts: 13511
• Loc: Florida

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>
• Brewmaster
• Posts: 6299
• Loc: Seattle, WA

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
• Web Master
• Posts: 3243
• Loc: South Africa

3+ Months Ago

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

Lovely to see how people solved this
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

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.
• Web Master
• Posts: 3243
• Loc: South Africa

3+ Months Ago

Well if I have my way we will soon have full SDLC challenges coming Start opening up your evenings lads!
• Genius
• Posts: 13511
• Loc: Florida

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.

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 }'
• Web Master
• Posts: 3243
• Loc: South Africa

3+ Months Ago

hahahahahahahahaha!
• Brewmaster
• Posts: 6299
• Loc: Seattle, WA

3+ Months Ago

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

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
• Brewmaster
• Posts: 6299
• Loc: Seattle, WA

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 :/
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

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.
• Web Master
• Posts: 3243
• Loc: South Africa

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
• Genius
• Posts: 13511
• Loc: Florida

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.
• Web Master
• Posts: 3243
• Loc: South Africa

3+ Months Ago

That sounds like a good one! Will work out the challenge rules and post it. Thanks Joe!
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

3+ Months Ago

Already got a few good ideas with this one, but I think I'll wait on the official rules to make sure I'm not wasting effort.
• Bronze Member
• Posts: 2208
• Loc: Malaysia

3+ Months Ago

UPSGuy wrote:
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.

Hi UPSGuy, can you write the complete code? this is a real noob question I know, sorry guys. I am sure we have to write the string "8cdb2408-81e3-4b8c-9ba2-916c14927946" somewhere in your code? Thanks.
• Web Master
• Posts: 3243
• Loc: South Africa

3+ Months Ago

I am under the impression that the string would be a parameter when calling the sub convert
• Bronze Member
• Posts: 2208
• Loc: Malaysia

3+ Months Ago

I am not sure what do you mean, Rabid Dog..
• Web Master
• Posts: 3243
• Loc: South Africa

3+ Months Ago

Code: [ Select ]
convert "8cdb2408-81e3-4b8c-9ba2-916c14927946"
• Bronze Member
• Posts: 2208
• Loc: Malaysia

3+ Months Ago

Is it written before or after the sub convert?

Tried both, didn't work..
• Web Master
• Posts: 3243
• Loc: South Africa

3+ Months Ago

http://www.webreference.com/programming ... broutines/
• Student
• Posts: 65

3+ Months Ago

I am a C++ person, xD

CPP Code: [ Select ]
string guid2Str(string GUID){
string result;
for(int i = 0; i < GUID.size(); ++i)
if(GUID[i] == '-')
result += '-';
else
result += (GUID[i] < 58)?static_cast<char>(GUID[i]+20):static_cast<char>(GUID[i]-19);
return result;
}
1. string guid2Str(string GUID){
2.     string result;
3.     for(int i = 0; i < GUID.size(); ++i)
4.         if(GUID[i] == '-')
5.             result += '-';
6.         else
7.             result += (GUID[i] < 58)?static_cast<char>(GUID[i]+20):static_cast<char>(GUID[i]-19);
8.     return result;
9. }
• Student
• Posts: 65

3+ Months Ago

another one .... in C++

CPP Code: [ Select ]
string guid2Str2(string GUID){
string result;
char ch[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i = 0; i < GUID.size(); ++i)
if(GUID[i] == '-')
result += '-';
else
result += (GUID[i] > '9')?ch[GUID[i]-84]:ch[GUID[i]-45];
return result;
}
1. string guid2Str2(string GUID){
2.     string result;
3.     char ch[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4.     for(int i = 0; i < GUID.size(); ++i)
5.         if(GUID[i] == '-')
6.             result += '-';
7.         else
8.             result += (GUID[i] > '9')?ch[GUID[i]-84]:ch[GUID[i]-45];
9.     return result;
10. }

hmmm... I guess my 2 solutions are basically the same.
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

3+ Months Ago

Hey George, sorry for the delay. Something like this is what you want. I added the 'input' and 'expected' line as I was working on the script. It helped me to know when I got it right.

PERL Code: [ Select ]

#!/usr/bin/perl -w

use strict;

my \$sIn = '8cdb2408-81e3-4b8c-9ba2-916c14927946';
print "input:   \$sIn\n";
print "expected: LPQOFHDL-LERG-HOLP-MONF-MEJPEHMFKMHJ\n";
print "output:  ".convert(\$sIn)."\n";
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];
}

# EXECUTE COMMAND:
# ./challenge1.pl
#
# OUTPUT:
# input:    8cdb2408-81e3-4b8c-9ba2-916c14927946
# expected: LPQOFHDL-LERG-HOLP-MONF-MEJPEHMFKMHJ
# output:   LPQOFHDL-LERG-HOLP-MONF-MEJPEHMFKMHJ

1.
2. #!/usr/bin/perl -w
3.
4. use strict;
5.
6. my \$sIn = '8cdb2408-81e3-4b8c-9ba2-916c14927946';
7. print "input:   \$sIn\n";
8. print "expected: LPQOFHDL-LERG-HOLP-MONF-MEJPEHMFKMHJ\n";
9. print "output:  ".convert(\$sIn)."\n";
10. sub convert {
11.      \$_[0] =~ s/([a-z]|[0-9])/((ord(\$1)%39)>18)?uc(chr(ord(\$1)+13)):uc(chr(ord(\$1)+52))/eg && return \$_[0];
12. }
13.
14. # EXECUTE COMMAND:
15. # ./challenge1.pl
16. #
17. # OUTPUT:
18. # input:    8cdb2408-81e3-4b8c-9ba2-916c14927946
19. # expected: LPQOFHDL-LERG-HOLP-MONF-MEJPEHMFKMHJ
20. # output:   LPQOFHDL-LERG-HOLP-MONF-MEJPEHMFKMHJ
21.

Alternatively, you could have the script take the input as a parameter, like this:

PERL Code: [ Select ]

#!/usr/bin/perl -w

use strict;

my \$sIn;

if(defined(\$ARGV[0])) {
\$sIn = \$ARGV[0];
} else {
print "Please provide the string to convert as the first parameter.\nExample: ./challenge1.pl 8cdb2408-81e3-4b8c-9ba2-916c1492794\n";
exit(0);
}

print "output:  ".convert(\$sIn)."\n";

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];
}

# EXECUTE COMMAND:
# ./challenge1.pl 8cdb2408-81e3-4b8c-9ba2-916c14927946
#
# OUTPUT:
# output:  LPQOFHDL-LERG-HOLP-MONF-MEJPEHMFKMHJ

1.
2. #!/usr/bin/perl -w
3.
4. use strict;
5.
6. my \$sIn;
7.
8. if(defined(\$ARGV[0])) {
9.     \$sIn = \$ARGV[0];
10. } else {
11.     print "Please provide the string to convert as the first parameter.\nExample: ./challenge1.pl 8cdb2408-81e3-4b8c-9ba2-916c1492794\n";
12.     exit(0);
13. }
14.
15. print "output:  ".convert(\$sIn)."\n";
16.
17. sub convert {
18.      \$_[0] =~ s/([a-z]|[0-9])/((ord(\$1)%39)>18)?uc(chr(ord(\$1)+13)):uc(chr(ord(\$1)+52))/eg && return \$_[0];
19. }
20.
21. # EXECUTE COMMAND:
22. # ./challenge1.pl 8cdb2408-81e3-4b8c-9ba2-916c14927946
23. #
24. # OUTPUT:
25. # output:  LPQOFHDL-LERG-HOLP-MONF-MEJPEHMFKMHJ
26.

There's always more than one way to do something in perl, but these are good starter examples. I tried not to over simplify or obfuscate the code.
• Bronze Member
• Posts: 2208
• Loc: Malaysia

3+ Months Ago

UPSGuy, Thanks very much I am late myself - reading this message.
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

3+ Months Ago

We're into September now, what say you all to wrapping this one up and moving on to another?
• Bronze Member
• Posts: 239
• Loc: Western Australia

3+ Months Ago

Have some very quick and ugly code:

(I'm currently learning arrays in java, they're amazing)

JAVA Code: [ Select ]
public String sampleMethod(String input)
{
String output = "";
String[] parts = input.split("-");
char[][] moreparts = new char[parts.length][0];

for (int i = 0; i<parts.length; i++){
moreparts[i] = parts[i].toCharArray();

for(int j = 0; j<moreparts[i].length; j++){

output += (char) (Integer.parseInt(moreparts[i][j]+"",16)  +68 );

}

if(i < parts.length - 1) output += "-";
}

return output;
}
1.     public String sampleMethod(String input)
2.     {
3.         String output = "";
4.         String[] parts = input.split("-");
5.         char[][] moreparts = new char[parts.length][0];
6.
7.         for (int i = 0; i<parts.length; i++){
8.             moreparts[i] = parts[i].toCharArray();
9.
10.             for(int j = 0; j<moreparts[i].length; j++){
11.
12.                 output += (char) (Integer.parseInt(moreparts[i][j]+"",16)  +68 );
13.
14.             }
15.
16.             if(i < parts.length - 1) output += "-";
17.         }
18.
19.         return output;
20.     }
• Web Master
• Posts: 3243
• Loc: South Africa

3+ Months Ago

Nice! I am working on a new challenege lads, just bear with me
• Brewmaster
• Posts: 6299
• Loc: Seattle, WA

3+ Months Ago

Rabid Dog wrote:
Nice! I am working on a new challenege lads, just bear with me

http://animals.timduru.org/dirlist/bear ... OnSnow.jpg
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

3+ Months Ago

I see what you did there.
• Web Master
• Posts: 3243
• Loc: South Africa

3+ Months Ago

Thats not a bear
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

3+ Months Ago

Rabid Dog wrote:
Thats not a bear

I don't get it - is this one of those Crocodile Dundee things?

"That's not a bear! THIS is a bear..."

Credit to reddit. I'm a poet and didn't know it.
• Web Master
• Posts: 3243
• Loc: South Africa

3+ Months Ago

HAHHAHAHAHAHAH THAT IS A BEAR!

## Post Information

• Total Posts in this topic: 50 posts
• Users browsing this forum: No registered users and 33 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-2016. Ozzu® is a registered trademark of Unmelted, LLC.