Author Topic: Coders, unite!  (Read 5837 times)

0 Members and 1 Guest are viewing this topic.

Offline Captain Jack Harkness

  • Petter, Brony, and All-Around Cartoon Addict
  • The Beast
  • *****
  • Posts: 2868
  • Gender: Male
  • Or as a friend calls him, Captain Jack Hotness!
Re: Coders, unite!
« Reply #30 on: April 28, 2013, 04:14:32 am »
So I was a bit bored and had a flash of inspiration on how to convert strings to all lowercase or uppercase with Perl.  Sure, you could use the built in lc() and uc() methods, but I wanted to do something a bit more fun.

Code: [Select]
use strict;
use warnings;

my $test;
my $x;
my ( $uppercaseTest, $lowercaseTest );
my $character;

$test = $ARGV[0];
$x = 0;

unless
( defined $test
)
{
$test = "pONy";
}

($uppercaseTest, $lowercaseTest) = ("", "");

while
( $x < length $test
)
{
$character = substr $test, $x, 1;

if
( $character =~ m/[a-z]/i
)
{
$uppercaseTest .= $character | " ";
$lowercaseTest .= $character & "_";
}
else
{
$uppercaseTest .= $character;
$lowercaseTest .= $character;
}
$x++;
}

print $uppercaseTest, "\n";
print $lowercaseTest, "\n";

So I realize this could be simpler with the built in functions for uppercase and lowercase conversion, but I felt like having fun.  I hope you guys get what's going on.  If you don't, feel free to ask.
« Last Edit: April 28, 2013, 04:23:27 am by B-Man »
My friend's blog.  Check it out!

I blame/credit The Doctor with inspiring my name change.

Offline Joey

  • Bishop
  • ***
  • Posts: 151
Re: Coders, unite!
« Reply #31 on: April 28, 2013, 05:40:07 am »
I see your Perl and raise you a Java:

Code: [Select]
package blah;

public class StringChanger {
   
    public static void main (String[] args) {
       
        String test = "pONy 2012";
        String upper = "";
        String lower = "";
       
        for (int i=0; i<test.length(); i++) {
       
            char x = test.charAt(i);
            lower += x >= 65 && x <= 90  ? (char)(x+32) : x;
            upper += x >= 97 && x <= 122 ? (char)(x-32) : x;
        }
       
        System.out.println(upper + "\n" + lower);
    }
}

Btw, can I ask what this part of your code does?

Code: [Select]
$uppercaseTest .= $character | " ";
$lowercaseTest .= $character & "_";

I get that it's appending the new strings with upper or lower-case characters, but what does the | " " and & "_" mean exactly?
"A human is a system for converting dust billions of years ago into dust billions of years from now via a roundabout process which involves checking email a lot." - XKCD

Offline Captain Jack Harkness

  • Petter, Brony, and All-Around Cartoon Addict
  • The Beast
  • *****
  • Posts: 2868
  • Gender: Male
  • Or as a friend calls him, Captain Jack Hotness!
Re: Coders, unite!
« Reply #32 on: April 28, 2013, 01:43:47 pm »
I see your Perl and raise you a Java:

Code: [Select]
package blah;

public class StringChanger {
   
    public static void main (String[] args) {
       
        String test = "pONy 2012";
        String upper = "";
        String lower = "";
       
        for (int i=0; i<test.length(); i++) {
       
            char x = test.charAt(i);
            lower += x >= 65 && x <= 90  ? (char)(x+32) : x;
            upper += x >= 97 && x <= 122 ? (char)(x-32) : x;
        }
       
        System.out.println(upper + "\n" + lower);
    }
}

Btw, can I ask what this part of your code does?

Code: [Select]
$uppercaseTest .= $character | " ";
$lowercaseTest .= $character & "_";

I get that it's appending the new strings with upper or lower-case characters, but what does the | " " and & "_" mean exactly?

Sure, I can explain that.  To understand what those mean, you have to understand that | and & are not boolean operators in their current context, but bitwise operators.  We're actually doing a bit of "binary math."  The explanation is a bit long, so have some spoiler tags.

(click to show/hide)

I hope that clears things up.

Also, " " and "_" can be written a bit less cryptically in hexidecimal as chr 0x20 and chr 0xDF, respectively. They can also be written in binary chr 0b010000 and chr 0b1011111, respectively.

Code: [Select]
$uppercaseTest .= $character | chr 0x20;
$lowercaseTest .= $character & chr 0xDF;

or

Code: [Select]
$uppercaseTest .= $character | chr 0b0100000;
$lowercaseTest .= $character & chr 0b1011111;

Haha, okay, I just realized another way of doing things.  Instead of explicitely stating the inverse, you can use the unary "not" bitwise operator.  You can use this on the hex or binary code,, or you can use decimal numbering to represent the flag value.

Code: [Select]
$uppercaseTest .= $character | chr  32;
$lowercaseTest .= $character & chr ~32;
« Last Edit: April 28, 2013, 04:55:49 pm by B-Man »
My friend's blog.  Check it out!

I blame/credit The Doctor with inspiring my name change.

Offline Joey

  • Bishop
  • ***
  • Posts: 151
Re: Coders, unite!
« Reply #33 on: April 28, 2013, 08:24:40 pm »
Makes perfect sense. I'd be willing to be it is slightly more efficient than adding numerical values to characters.
"A human is a system for converting dust billions of years ago into dust billions of years from now via a roundabout process which involves checking email a lot." - XKCD

Offline Captain Jack Harkness

  • Petter, Brony, and All-Around Cartoon Addict
  • The Beast
  • *****
  • Posts: 2868
  • Gender: Male
  • Or as a friend calls him, Captain Jack Hotness!
Re: Coders, unite!
« Reply #34 on: April 28, 2013, 08:30:44 pm »
Makes perfect sense. I'd be willing to be it is slightly more efficient than adding numerical values to characters.

Cool, I'm glad you get it.  Now that I think about it, I want to use bitwise operators outside of an ASCII character context now. XD
My friend's blog.  Check it out!

I blame/credit The Doctor with inspiring my name change.

Offline Yla

  • The Beast
  • *****
  • Posts: 809
  • Gender: Male
Re: Coders, unite!
« Reply #35 on: April 29, 2013, 06:41:32 am »
Makes perfect sense. I'd be willing to be it is slightly more efficient than adding numerical values to characters.

Cool, I'm glad you get it.  Now that I think about it, I want to use bitwise operators outside of an ASCII character context now. XD
Bitwise operators are often used for poor man's flag field or compressed bitmaps. Store up to 32 booleans inside one int and use these to access.

Also, both of you could pimp your code to be more understandable. Joey: A person reading your code would end up wondering about the significance of the numbers 65, 90, 97 and 122. You need to know the ASCII table in detail for the why. Simply write 'a', 'z', 'A' and 'Z' and it's immediately clear what these checks are doing and why. B-Man: In your case I'd recommend the reverse. You OR (or add) 32 to the character. That 32 is whitespace in ASCII is irrelevant for that, and confusing to someone trying to understand your code. Even moreso for the underscore. I'd recommend writing it in binary, since then you immediately see which bits are being manipulated.
That said, I've stopped trying to anticipate what people around here want a while ago, I've found it makes things smoother.
For I was an hungred, and ye told me to pull myself up by my bootstraps: I was thirsty, and ye demanded payment for the privilege of thine urine: I was a stranger, and ye deported me: naked, and ye arrested me for indecency.

Offline Captain Jack Harkness

  • Petter, Brony, and All-Around Cartoon Addict
  • The Beast
  • *****
  • Posts: 2868
  • Gender: Male
  • Or as a friend calls him, Captain Jack Hotness!
Re: Coders, unite!
« Reply #36 on: April 29, 2013, 08:43:47 am »
Makes perfect sense. I'd be willing to be it is slightly more efficient than adding numerical values to characters.

Cool, I'm glad you get it.  Now that I think about it, I want to use bitwise operators outside of an ASCII character context now. XD
Bitwise operators are often used for poor man's flag field or compressed bitmaps. Store up to 32 booleans inside one int and use these to access.

Also, both of you could pimp your code to be more understandable. Joey: A person reading your code would end up wondering about the significance of the numbers 65, 90, 97 and 122. You need to know the ASCII table in detail for the why. Simply write 'a', 'z', 'A' and 'Z' and it's immediately clear what these checks are doing and why. B-Man: In your case I'd recommend the reverse. You OR (or add) 32 to the character. That 32 is whitespace in ASCII is irrelevant for that, and confusing to someone trying to understand your code. Even moreso for the underscore. I'd recommend writing it in binary, since then you immediately see which bits are being manipulated.

I was thinking about commenting my code.  I originally came up with it in a flash of inspiration, which is why I initially didn't.  Besides, I wanted to see if you could figure out what's going on first.
My friend's blog.  Check it out!

I blame/credit The Doctor with inspiring my name change.