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 August 5th, 2009, 5:42 am

That sounds like a good one! Will work out the challenge rules and post it. Thanks Joe!
Watch me grow
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 5th, 2009, 5:42 am

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

Post August 5th, 2009, 6:06 am

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. :D
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 August 19th, 2009, 7:14 am

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.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post August 19th, 2009, 7:29 am

I am under the impression that the string would be a parameter when calling the sub convert
Watch me grow
  • George L.
  • Bronze Member
  • Bronze Member
  • No Avatar
  • Joined: Nov 05, 2007
  • Posts: 2206
  • Loc: Malaysia
  • Status: Offline

Post August 19th, 2009, 7:51 am

I am not sure what do you mean, Rabid Dog..
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post August 19th, 2009, 7:56 am

Code: [ Select ]
convert "8cdb2408-81e3-4b8c-9ba2-916c14927946"
Watch me grow
  • George L.
  • Bronze Member
  • Bronze Member
  • No Avatar
  • Joined: Nov 05, 2007
  • Posts: 2206
  • Loc: Malaysia
  • Status: Offline

Post August 19th, 2009, 8:37 am

Is it written before or after the sub convert?

Tried both, didn't work..
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post August 19th, 2009, 9:25 am

http://www.webreference.com/programming ... broutines/
Watch me grow
  • IcyDragoon
  • Student
  • Student
  • No Avatar
  • Joined: Mar 12, 2008
  • Posts: 65
  • Status: Offline

Post August 21st, 2009, 1:37 am

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. }
  • IcyDragoon
  • Student
  • Student
  • No Avatar
  • Joined: Mar 12, 2008
  • Posts: 65
  • Status: Offline

Post August 21st, 2009, 2:15 am

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

Post August 21st, 2009, 6:23 am

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.
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 August 21st, 2009, 10:46 am

UPSGuy, Thanks very much I am late myself - reading this message.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post September 4th, 2009, 5:23 am

We're into September now, what say you all to wrapping this one up and moving on to another?
I'd love to change the world, but they won't give me the source code.
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jul 18, 2008
  • Posts: 239
  • Loc: Western Australia
  • Status: Offline

Post October 1st, 2009, 7:13 am

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.     }
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post October 1st, 2009, 1:46 pm

Nice! I am working on a new challenege lads, just bear with me :)
Watch me grow
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post October 1st, 2009, 1:46 pm

Post Information

  • Total Posts in this topic: 50 posts
  • Users browsing this forum: No registered users and 228 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.