Thread: Psuedocode

  1. #16
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    my code does work I figured out on my own just today this is all I had to do to validate the code what the heck is all that stuff your talking about


    Code:
     Regex validate = new Regex( @"[A-Za-z]\d\d\d\d");
    
    
                fileOut.WriteLine("Record Code         Part #           Description                Balance");
                fileOut.WriteLine("-----------------------------------------------------------------------");
    
                for(int i = 0; i <= records; i++)
                {
                    if (validate.IsMatch(partNumber[i]) && Convert.ToInt32(recordCode[i]) == 11 && Convert.ToInt32(balance[i]) == 0)
                        fileOut.WriteLine("{0}                  {1}          {2,12}              {3,3}", recordCode[i], partNumber[i], partDescription[i], balance[i]);
                }
                fileOut.WriteLine("-----------------------------------------------------------------------");

  2. #17
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I don't know why this code works can someone explain why it does (@"[A-Za-z]\d\d\d\d") I don't see how come I don't have to have a place holder for the characters I don't know why this works but it works perfectly even if I change the data around

  3. #18
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I get it now this works perfectly

    @"[A-Za-z][A-Za-z]\d\d\d\d"

    me gets why this works now

  4. #19
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Cat sorry about me saying what the heck is that stuff
    that code is a little too sophisticated for me it goes over my head what I really wanted was just to validate a string with 2 characters and four digits not really the length like the psuedocode states but to validate the string itself which it does validate the length also or checks it anyway

  5. #20
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    In my opinion, regex is not the best solution anyways and it's also going to be slower than regular string manipulation.
    Code:
    public static bool ValidID(string id)
    {
      int letters, digits;
      int visible = letters = digits = 0;
      foreach (char ch in id)
      {
        if (char.IsWhiteSpace(ch)) continue;
    
        ++visible;
        if (letters != 2)
        {
          if (char.IsLetter(ch)) ++letters;
          else return false;
        }
        else
        {
          if (char.IsDigit(ch)) ++digits;
          else return false;
        }
      }
    
      return visible == 6
             && letters == 2
             && digits == 4;
    }
    I wrote that assuming spaces were possible, otherwise you can just check the Length property of the id.

    edit: Btw, if you use regex, this:
    Code:
    \d\d\d\d
    Can be replaced with:
    Code:
    \d{4}
    And same with the beginning... i.e.
    Code:
    [A-Za-z]{2}
    Last edited by cstryx; 08-29-2015 at 02:11 PM.

  6. #21
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Wow whoever you are you know your ........ this code is kickass I haven't learned string manipulation yet but I hope to learn it pretty soon also I thought this
    Code:
    [A-Za-z]{2}\d{4}
    
    ] {2}\d {4}
    wouldn't work but it does, Do I not put a space between maybe that's why it wouldn't work before because I put spaces between that Thanks for enlightning me and everyone else I have a lot to learn still

  7. #22
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    cstryx I'm curious why you say Regular Expressions are slow my book acts like Regular Expressions are very useful but I still like your approach to it

  8. #23
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by artistunknown View Post
    also I thought this
    Code:
    [A-Za-z]{2}\d{4}
    
    ] {2}\d {4}
    wouldn't work but it does, Do I not put a space between maybe that's why it wouldn't work before because I put spaces between that
    Did you not actually read the multiple replies on the first page of this thread telling you not to put the space in? How are you still confused about this?
    If you understand what you're doing, you're not learning anything.

  9. #24
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by artistunknown View Post
    cstryx I'm curious why you say Regular Expressions are slow my book acts like Regular Expressions are very useful but I still like your approach to it
    They are useful for pattern matching, but the overhead of using regex in this case is going to be significantly slower. It may only be faster if you are using compiled regex on a much larger piece of string data.

  10. #25
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by cstryx View Post
    They are useful for pattern matching, but the overhead of using regex in this case is going to be significantly slower. It may only be faster if you are using compiled regex on a much larger piece of string data.
    I wouldn't say "significantly" slower, since either of them is fast enough to be effectively instantaneous to human perception.

    Personally, as long as I'm not using a wildly inappropriate algorithm (like an O(N^3) linked list removal or something) my focus is always on readable, maintainable code. If there are actually performance concerns, then I'll rewrite if necessary, but in a lot of cases, the actual processing takes tiny fractions of a second and then idles waiting for user input. Whether it takes 100 nanoseconds or 10,000 nanoseconds, it's still effectively instantaneous to human perception, which finds anything less than about 100 microseconds to be "immediate".

    The printing to the console is going to take orders of magnitude longer than either the regular expression or the character-based pattern matching.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #26
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by Cat View Post
    I wouldn't say "significantly" slower, since either of them is fast enough to be effectively instantaneous to human perception.

    Personally, as long as I'm not using a wildly inappropriate algorithm (like an O(N^3) linked list removal or something) my focus is always on readable, maintainable code. If there are actually performance concerns, then I'll rewrite if necessary, but in a lot of cases, the actual processing takes tiny fractions of a second and then idles waiting for user input. Whether it takes 100 nanoseconds or 10,000 nanoseconds, it's still effectively instantaneous to human perception, which finds anything less than about 100 microseconds to be "immediate".

    The printing to the console is going to take orders of magnitude longer than either the regular expression or the character-based pattern matching.
    Significantly slower was a measurement of performance in context with my comparison between simple string manipulation and regex itself, which has nothing to do with human perception. Also, it's not clarified as to whether he needs to validate a bunch of ID's or just a single one, so performance may or may not be accumulative here. Still, regex is not the proper choice IMHO; it wouldn't be mine.

    It's a string of 6 characters in length. You wouldn't turn the barbecue back on to heat up leftover chicken the day after, you would probably just use the microwave. Same principle applies here.

    Code:
    public static bool ValidID(string id)
    {
      return id.Length == 6
            && char.IsLetter(id[0]) && char.IsLetter(id[1])
            && id.Skip(2).All(char.IsDigit);
    }
    You could parse the last 4 chars as an int and see if it was successful, and index[0] and [1] to check them for letters. Many ways to get the job done and still be faster but just as readable as regex.
    Last edited by cstryx; 08-29-2015 at 09:54 PM.

  12. #27
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    sorry itsme86 I know you were right all along about the spaces my bad
    I read the posts but I think I got a little mixed up about what it meant

  13. #28
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Code:
    I kind of thought that spacing didn't matter in this statement or declaration whatever its called  " Regex validate = new Regex(@"[A-Za-z]{2}\d{4}"); "
    I was getting mixed up about this code "  while (Regex.IsMatch(lineIn, "[ ] {2}")) "  and this also (Regex.IsMatch(partnumber, "[A-Z] {2} [0-9] {4}")) can you guys tell me if spacing matters in the IsMatch() method  am I correct that IsMatch(lineIn, "[ ] {2}")) checks for two spaces and the space between right bracket ] and first { does that mean anything not the space between the brackets

  14. #29
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Spacing does absolutely matter.

    If you have

    Code:
    "[a-z] {2}"
    It will parse this as:

    Code:
    "[a-z]"
    This matches one lowercase letter

    Code:
    " "
    This matches a space

    Code:
    "{2}"
    This means the previous match must match twice.

    So overall, the expression
    Code:
    "[a-z] {2}"
    doesn't match two lowercase letters. It matches one lowercase letter followed by two spaces, because the repetition count applies to the token immediately preceding it, which in this case happens to be the space character.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  15. #30
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I didn't realize what I was writing makes sense
    Thanks everyone for teaching me a little about Regular expressions.
    I probably would have giving up by now if it wasn't for this forum

Popular pages Recent additions subscribe to a feed