Author Topic: anyone done regex before?  (Read 7152 times)

0 Members and 2 Guests are viewing this topic.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: anyone done regex before?
« Reply #15 on: May 16, 2012, 11:19:01 am »
No, I was happy with the sorting approach to this problem, I just wanted to implement it :)

I am not a C expert, but well done.

Offline leewillz

  • Serf
  • *
  • Posts: 23
  • Cookies: 2
  • import java.help.me
    • View Profile
Re: anyone done regex before?
« Reply #16 on: May 16, 2012, 06:55:46 pm »
the code does work fine, bearing in mind it was 2:30 on me doing it, the toChar method is pointless i see it now ive changed it, the output i get is attatched. ive not corrected everything is it works fine even if its not to "java convention"
 thanks Lee
« Last Edit: May 16, 2012, 06:56:05 pm by leewillz »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: anyone done regex before?
« Reply #17 on: May 16, 2012, 09:22:04 pm »
the code does work fine, bearing in mind it was 2:30 on me doing it, the toChar method is pointless i see it now ive changed it, the output i get is attatched. ive not corrected everything is it works fine even if its not to "java convention"
 thanks Lee

Then let me tell you this again: The code you provided here does not compile. There is at least one curly bracket and a return statement missing in the end.
I didn't say that it doesn't run on your machine. Maybe something went wrong with copy&paste or whatever, but it doesn't work, no matter what you tell me.

And now to the working right part: That it works right for one input, doesn't mean it works right for every input. Testing can never tell you the absence of bugs. It only might tell you the presence.
However I can't prove anything, because your code doesn't compile.

Of course it works although you didn't care for java code conventions. You didn't even look what they are for, did you? Otherwise you would know that this has nothing to do with each other.

Offline leewillz

  • Serf
  • *
  • Posts: 23
  • Cookies: 2
  • import java.help.me
    • View Profile
Re: anyone done regex before?
« Reply #18 on: May 16, 2012, 11:10:08 pm »
ive just downloaded the file and tried to compile, the problem is the comment in the else statement,
else
                {//do nothing basically}
if u want it to run then get rid of the comment or the else in total, up to you but basically the comment is commenting out the bracket, work after that.
« Last Edit: May 16, 2012, 11:48:31 pm by Kulverstukas »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: anyone done regex before?
« Reply #19 on: May 17, 2012, 06:20:30 am »
ive just downloaded the file and tried to compile, the problem is the comment in the else statement,
else
                {//do nothing basically}
if u want it to run then get rid of the comment or the else in total, up to you but basically the comment is commenting out the bracket, work after that.

You are right, that was the reason it didn't work.

Now try "array" and "aaray". They will match, because your algorithm doesn't care how often the characters occur. As long as the length is the same and every distinct character is at least one time in the scrambled word, it will match.

Quote
Testing: world
Testing: araay
array is araay !

Offline leewillz

  • Serf
  • *
  • Posts: 23
  • Cookies: 2
  • import java.help.me
    • View Profile
Re: anyone done regex before?
« Reply #20 on: May 17, 2012, 11:34:33 am »
indeed, i have thought of a way to overcome this, i shall test this now and post the results.

Offline leewillz

  • Serf
  • *
  • Posts: 23
  • Cookies: 2
  • import java.help.me
    • View Profile
Re: anyone done regex before?
« Reply #21 on: May 17, 2012, 01:16:25 pm »
i think this has sored it out, basically ive changed it to an array list of the characters and when if finds the element it removes it from the arraylist therefore matching the correct ammount of letters in the word. let me know if it works for you  :)

Code: [Select]
import java.util.ArrayList;
import java.util.Scanner;

public class Scrambled {

    public static String scrambled_word;
    public static String[] dictionary = {"world", "array", "scrambled", "testing", "scrambley", "largeword", "abitlargerword", "download"};
    public static ArrayList<Character> orig_word = new ArrayList<Character>();
    public static ArrayList<Character> array_elem = new ArrayList<Character>();
    public static char[] array_elem1 = new char[100];
    public static char[] orig_word1 = new char[100];
    public static boolean quit = false;

    public static void main(String[] args) {
        while (!quit) {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter a string to look for:>");
            scrambled_word = s.nextLine();
            if (scrambled_word.equalsIgnoreCase("q")) {
                quit = true;
            } else {
                orig_word1 = scrambled_word.toCharArray();
                for (int i = 0; i < orig_word1.length; i++) {
                    orig_word.add(orig_word1[i]);
                }

                for (int i = 0; i < dictionary.length; i++) {

                    if (compare(scrambled_word.length(), dictionary[i].length()) == 0) {
                        System.out.println("Testing: " + dictionary[i]);
                        array_elem1 = dictionary[i].toCharArray();
                        swap(array_elem1);

                        if (matchWord()) {
                            System.out.println(dictionary[i] + " is the word you are loooking for");

                        } else {
                            System.out.println("this is not the scrambled word");
                        }
                    }
                    removeElem();
                }

            }
        }
    }

    public static int compare(int a, int b) {

        return a - b;

    }

    public static void swap(char[] words) {
        for (int i = 0; i < words.length; i++) {
            array_elem.add(words[i]);
        }


    }

    public static void removeElem() {
        for (int i = array_elem.size() - 1; i >= 0; i--) {
            array_elem.remove(i);
        }
    }

    public static boolean matchWord() {
        // assuming scrambled is aready sorted

        int m = 0;
        for (int i = 0; i < orig_word.size(); i++) {
            for (int j = 0; j < array_elem.size(); j++) {

                if (orig_word.get(i) == array_elem.get(j)) {

                    m = 1;
                    array_elem.remove(j);
                    array_elem.trimToSize();
                    break;
                } else {
                }
            }
            if (m == 0) {
                return false;
            } else {
                m = 0;
            }
        }
        return true;
    }
}

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: anyone done regex before?
« Reply #22 on: May 17, 2012, 01:45:49 pm »
yrraa should match array, but it doesn't.

Quote
Enter a string to look for:>
yrraa
Testing: world
this is not the scrambled word
Testing: array
this is not the scrambled word

Edit: It is a good thing that you try to solve this yourself. You could have just used the sorting approach that s3myon and I suggested, but what you do is a complete different way. Go on, you will get it your way.
« Last Edit: May 17, 2012, 01:50:09 pm by Deque »

Offline leewillz

  • Serf
  • *
  • Posts: 23
  • Cookies: 2
  • import java.help.me
    • View Profile
Re: anyone done regex before?
« Reply #23 on: May 17, 2012, 01:53:05 pm »
hi  idont know whats going on there but ive just had a go on yrraa and it seems to pick it up :S ive done a few tweeks again ill post it now, this time ive used a text file with the words and it reads from the file to an array but main code is still the same.
Code: [Select]
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Scrambled {

    public static String scrambled_word;
    public static ArrayList<String> dictionary = new ArrayList<String>();
    public static ArrayList<Character> orig_word = new ArrayList<Character>();
    public static ArrayList<Character> array_elem = new ArrayList<Character>();
    public static char[] array_elem1 = new char[100];
    public static char[] orig_word1 = new char[100];
    public static boolean quit = false;

    public static void main(String[] args) throws FileNotFoundException, IOException {
        while (!quit) {
            try{
               removeElem(dictionary);
             BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Lee\\Documents\\NetBeansProjects\\Lab12\\src\\lab15\\read.txt"));
            int add = 0;
             while(in.ready()){               
                 dictionary.add(add, in.readLine());
                 add++;
               
             }
            }catch(FileNotFoundException e)
            {
                System.out.println("File not found");
            }
            Scanner s = new Scanner(System.in);
            System.out.println("Enter a string to look for:>");
            scrambled_word = s.nextLine();
            if (scrambled_word.equalsIgnoreCase("q")) {
                quit = true;
            } else {
                orig_word1 = scrambled_word.toCharArray();
                for (int i = 0; i < orig_word1.length; i++) {
                    orig_word.add(orig_word1[i]);
                }

                for (int i = 0; i < dictionary.size(); i++) {

                    if (compare(scrambled_word.length(), dictionary.get(i).length()) == 0) {
                     
                        array_elem1 = dictionary.get(i).toCharArray();
                        swap(array_elem1);

                        if (matchWord()) {
                            System.out.println(dictionary.get(i) + " is the word you are loooking for");

                        } else {
                           
                        }
                    }
                    removeElem(array_elem);
                }

            }
        }
    }

    public static int compare(int a, int b) {

        return a - b;

    }

    public static void swap(char[] words) {
        for (int i = 0; i < words.length; i++) {
            array_elem.add(words[i]);
        }


    }

    public static void removeElem(ArrayList array) {
        for (int i = array.size() - 1; i >= 0; i--) {
            array.remove(i);
        }
    }

    public static boolean matchWord() {
        // assuming scrambled is aready sorted

        int m = 0;
        for (int i = 0; i < orig_word.size(); i++) {
            for (int j = 0; j < array_elem.size(); j++) {

                if (orig_word.get(i) == array_elem.get(j)) {

                    m = 1;
                    array_elem.remove(j);
                    array_elem.trimToSize();
                    break;
                } else {
                }
            }
            if (m == 0) {
                return false;
            } else {
                m = 0;
            }
        }
        return true;
    }
}
« Last Edit: May 17, 2012, 01:57:11 pm by leewillz »

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: anyone done regex before?
« Reply #24 on: May 17, 2012, 02:04:53 pm »
I solved this problem with a hash table. Something like (wants to be perl syntax):
Initialization:
foreach (@word) {
    $w = $_;
    $table{sort_letters_alphabetically($w)} = $w;
}
Lookup:
echo $table{sort_letters_alphabetically($scrambled_word)};
Where sort_letters_alphabetically("testingword") = "deginorsttw"

Just giving another way. IDK if would work fine in every testing case.
« Last Edit: May 17, 2012, 02:05:08 pm by ca0s »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: anyone done regex before?
« Reply #25 on: May 17, 2012, 02:12:29 pm »
Alright. I used the updated code and yrraa works, but raary doesn't.

Quote
Enter a string to look for:>
yrraa
array is the word you are loooking for
Enter a string to look for:>
raary
Enter a string to look for:>

It may help you to create a test suite, that covers a lot of cases at once while you are working on your algorithm.
« Last Edit: May 17, 2012, 02:13:39 pm by Deque »

Offline leewillz

  • Serf
  • *
  • Posts: 23
  • Cookies: 2
  • import java.help.me
    • View Profile
Re: anyone done regex before?
« Reply #26 on: May 17, 2012, 02:20:32 pm »
i know why that is aswell, when it finds array once it gets deleted from the arraylist so some more tweeking i think.

Offline leewillz

  • Serf
  • *
  • Posts: 23
  • Cookies: 2
  • import java.help.me
    • View Profile
Re: anyone done regex before?
« Reply #27 on: May 17, 2012, 02:36:29 pm »
correction, the reason was very simple i wasnt clearing the orig_word arraylist so basically when you were enterring yrraa it was looking for yrraa but then when you enter raary it was actually looking for yrraaraary because it wasnt clearing the array this should solve it.
Code: [Select]
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Scrambled {

    public static String scrambled_word;
    public static ArrayList<String> dictionary = new ArrayList<String>();
    public static ArrayList<Character> orig_word = new ArrayList<Character>();
    public static ArrayList<Character> array_elem = new ArrayList<Character>();
    public static char[] array_elem1 = new char[100];
    public static char[] orig_word1 = new char[100];
    public static boolean quit = false;

    public static void main(String[] args) throws FileNotFoundException, IOException {
       
            try{
               
             BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Lee\\Documents\\NetBeansProjects\\Lab12\\src\\lab15\\read.txt"));
            int add = 0;
             while(in.ready()){               
                 dictionary.add(add, in.readLine());
                   add++;             
             }
             in=null;
             
            }catch(FileNotFoundException e)
            {
                System.out.println("File not found");
            }
             while (!quit) {
            Scanner s = new Scanner(System.in);
            removeElem(orig_word);
            removeElem(array_elem);
            System.out.println("Enter a string to look for:>");
            scrambled_word = s.nextLine();
            if (scrambled_word.equalsIgnoreCase("q")) {
                quit = true;
            } else {
                orig_word1 = scrambled_word.toCharArray();
                for (int i = 0; i < orig_word1.length; i++) {
                    orig_word.add(orig_word1[i]);
                }

                for (int i = 0; i < dictionary.size(); i++) {

                    if (compare(scrambled_word.length(), dictionary.get(i).length()) == 0) {
                     
                        array_elem1 = dictionary.get(i).toCharArray();
                        swap(array_elem1);

                        if (matchWord()) {
                            System.out.println(dictionary.get(i) + " is the word you are loooking for");

                        } else {
                           
                        }
                    }
                    removeElem(array_elem);
                }

            }
        }
    }

    public static int compare(int a, int b) {

        return a - b;

    }

    public static void swap(char[] words) {
        for (int i = 0; i < words.length; i++) {
            array_elem.add(words[i]);
        }


    }

    public static void removeElem(ArrayList array) {
        for (int i = array.size() - 1; i >= 0; i--) {
            array.remove(i);
        }
    }

    public static boolean matchWord() {
        // assuming scrambled is aready sorted

        int m = 0;
        for (int i = 0; i < orig_word.size(); i++) {
            for (int j = 0; j < array_elem.size(); j++) {

                if (orig_word.get(i) == array_elem.get(j)) {

                    m = 1;
                    array_elem.remove(j);
                    array_elem.trimToSize();
                    break;
                } else {
                }
            }
            if (m == 0) {
                return false;
            } else {
                m = 0;
            }
        }
        return true;
    }
}

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: anyone done regex before?
« Reply #28 on: May 17, 2012, 03:05:45 pm »
Congrats, it works now.
(but the code got pretty messy)

Offline leewillz

  • Serf
  • *
  • Posts: 23
  • Cookies: 2
  • import java.help.me
    • View Profile
Re: anyone done regex before?
« Reply #29 on: May 17, 2012, 11:36:11 pm »
haha excellent (just how i like it)  ;)