Results 1 to 22 of 22

Thread: need an algorithm

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    need an algorithm

    Does any one happen to have an algorithm to pick
    only the characters that appear in a String one time?
    Im trying to write one but im having a hard time.

    Code:
      for(int index = 0; index <= key.length() - 1; ++index){
          char matchingchar = key.charAt(index);
                
          for(int k = 1; k <= key.length() - (2 + index); ++k){
               if(matchingchar == key.charAt(k)){
                 charmatch = true;
                   break; 
               }
             }
               
           if(charmatch == false){++onlyappearsonce;} 
              charmatch = false;     
           }

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    are you using a character array or the string header?

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Actually im doing it in Java, but the Java fourm is dead as usual
    Im not too sure what you mean by header. Im just passing a string to a function that is supposed to calculate hoe many characters appear only once within the string.

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I came up with this and it should work. But i cant get the
    if(matchingchar == key.charAt(q)) part because say for a string
    "abcde" q would equal 3 then 2 then 1 so i would be compairing
    matchingchar with the 3rd index which is c instead of b. not what i want. so ill have to work on this. Thanks though.

    Code:
    for(int index = 0; index <= key.length() - 1; ++index){
            char matchingchar = key.charAt(index);
                  
              x  =  (key.length() * (key.length() - 2));
              x =+  (key.length() - 2);
            while(q != 0){ 
               q = x % key.length();
                 if(matchingchar == key.charAt(q)){
                    ++duplicates;
                     break; 
                 }
                 --x;
                 
            }
        } 
         int nonduplicatechars = (key.length() - (duplicates));

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    How long are the strings? How many types of characters are used and how are they used?
    Generally I think the fastest way for not too short strings or Strings with generally few reoccurring characters, would be to set up a table of trinary values for each character, and then run trough the string once flagging on characters from 0(not used) to 1(used once) and from 1 to 2(used more than once). Then count the 1's in the table.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Hey kedaman. you seen to know C++ pretty well. How could i make this expression legal? q and x are integer types.

    while(q = x % key.length() != 0){

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    This is somthing that i thought might work, but maybe im just going crazy thinking im on the right track.
    For the first loop matchingchar = key.charAt(index);
    will give me the first character then
    x = (key.length() * (key.length() - 2));
    x =+ (key.length() - 2);
    will give me for a string say "ABCDF"
    15 = 5 * 3
    18 =+ 5 - 2
    then
    q = x % key.length() != 0
    3 = 18 % 5
    2 = 17 % 5
    1 = 16 % 5
    that would give me the correct amount of iterations to get to the end of the string each time no matter the size of the string.
    I think

    Code:
    for(int index = 0; index <= key.length() - 1; ++index){
             matchingchar = key.charAt(index);
                  
              x =  (key.length() * (key.length() - 2));
              x =+  (key.length() - 2);
    
            while(q = x % key.length() != 0){ 
              if(matchingchar == key.charAt(z)){
                  ++duplicates;
                 }
                 --x;
                 ++z;
            }   
        } 
         int nonduplicatechars = (key.length() - (duplicates));

  8. #8
    Zaei
    Guest
    while((q = x % key.length()) != 0) { .. }

    Z.

  9. #9

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I have no idea what solution you're doing, may i even don't know what the problem is. If you fail, I could give you a hand finding the amount of duplicates
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11

  12. #12

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I came up with this and it seems to work pretty good but the problem i am having as far as i can see is that i have to take account not only for duplicate characters but for multiple characters (3 or more) I tried this out on words like Kaaat (not that this is a word) and the results are off. And also i have to figure out how to take into account if the multiple characters are seperated. for instance. Kaata. Right now all i am doing is subtracting 2 which takes care of the original and the duplicate.
    Any ideas?


    Code:
    for(int index = 0; index <= key.length() - 2; ++index){
            matchingchar = key.charAt(index);
    
             x = (((key.length() - 2) + (key.length() * (key.length() - 2))) - index);
          
               while((q = x % key.length()) != 0){ 
                 if(key.charAt(z) == matchingchar){
                     duplicates =+ 2;
                 }
                 --x;
                 ++z;
              }
              z =+ 1; 
                
        }
        int nonduplicatechars = (key.length() - duplicates);

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    screw it and go with the trinary table
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    An array of 3 different values, you could use char for instance.
    each value represents a character and their value represents:
    0 - Not used
    1 - Used once
    2 - Used twice or more
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16

  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Of course, I know some Java, but I'm rusty on the string class, so you could make an array of char's instead. I probably will type something wrong but you'll know what I mean.
    PHP Code:
    char ttable[256] = new char[256]

    int l key.length();
    for (
    int x 0x<; ++x; ){
         
    char matchingchar key.charAt(x);
         if (
    ttable[matchingchar]<2) ++ttable[matchingchar];
    }
    for (
    0x<256 ; ++x; ){ char count += ttable[x] == 1;} 
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  18. #18
    jim mcnamara
    Guest
    in C and skipping the ternary aspect, simply adding.

    PHP Code:
    void blah(char *ch)
    {
        
    int arr[256];
        
    char tmp[256];
        
    int j 0;
        
    int i 0;
        for (
    0;256;i++) { arr[i] = 0; }
        for (
    i=0;i< (int) strlen(ch);i++)  { 
            ++
    arrch[i] ];
        }
        for (
    i=33;i<256;i++) { 
            if (
    arr[i] == 1) { tmp[j++] = i; } //33 to ignore sdpaces
        

        
    tmp[j] = '\0';  // make it a valid sz string



  19. #19
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Thumbs up that's elegant for the sake of speed.

    here again in Java
    PHP Code:
    int ttable[256] = new int[256]

    int l key.length();
    for (
    int x 0x<; ++x; )
        ++
    ttable[key.charAt(x)];
    for (
    0x<256 ; ++x; )
        
    char count += ttable[x] == 1// note no conditional stuff here either 
    but it depends what you count as "character" so i'll leave that
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  20. #20

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Hey thanks guys for posting so code. Mabey you should start asking for some $$ for your help. Anyway..... kedaman the code that you posted, im not too sure if i understand.

    Code:
            int ttable[256] = new int[256]
    
           int l = key.length();
           for (int x = 0; x<l ; ++x; )
                ++ttable[key.charAt(x)];
           for (x = 0; x<256 ; ++x; )
                char count  += ttable[x] == 1;
    for instance ++ttable[key.charAt(x)]; you would be storing the whole string within the array. correct?

    and this line char count += ttable[x] == 1; why would we be testing a char to see if it is equal to the length of the string?

  21. #21
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    No actually, from what I understood you want to count the amount of non duplicated characters, that is characters that are used only once. If that's not the case then you have to explain again.

    ++ttable[key.charAt(x)];

    ttable is a table of all the characters from 0 to 255, each element specifying how many times it has been used. key.charAt(x) gets the current character and then that characters counter is incremented in the table.

    char count += ttable[x] == 1;

    here in this loop the count is incremented only when the current character count is 1, == returns 1 if the comparation is true otherways 0. see the logic? There's no need for conditional code
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  22. #22

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width