Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2

    PLEASE HELP (Matching two strings in python)


    I believe i have asked this question already but i am using a new thread to make it less confusing to answer...

    i made some mistakes in putting the examples in the code before

    Okay so in this q, we are basically supposed to match puzzle with view...

    it makes absolutely no sense to me right now, because we are supposed to see whether the hidden '^' actually holds a consonant or a vowel...

    So basically in the function is_match, we have to check whether all the characters in puzzle match the hidden characters in view...for example:

    puzzle = 'apple'
    view = '^ppl^'
    The return result will be True
    but if:
    puzzle = 'apple'
    view = 'a^^la'
    The return result would be false even if the the lengths are the same...


    these are just some examples:
    >>>is_match ('apples','^pp^^^')
    True
    >>>is_match ('apples-bananas','^^^^^^^^^^^^^') #diff length and "-" is hidden
    False
    >>>is_match ('apples-i', '^^^^^^-^') Same length + "-" shows
    True
    >>>is_match ('apples-i', '^^^^^^^^') <-- '-' should not be hidden...
    False

    So far my code is:
    Code:
     
        
    def is_match (puzzle, view):
        a = 0
        for i in range(len(puzzle)):
            if (puzzle[i] == view[i]): #or (view[i] == HIDDEN or view[i] != puzzle[i].islower()) and (len(puzzle) == len(view)):
                return True
            elif len(puzzle)==len(view) and (puzzle[i] != view[i]):
                return False
            else: 
                return False
            #elif puzzle == " " or "-" or "/" and view == HIDDEN:
                #return False
    PLEASE HELP ME...Can someone please fix my code...thanks, and tell me what i am doing wrong.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    First, are you suggesting that when "-" is present it will always show?

    You also seem to be going about it with the right idea, based on what you describe. Break the string into a list and check each value in order.

    The only thing I would caution, if you are running a loop through say "puzzle" and it is a different size from "view" if you call a value from "view" which exceeds the size it will error. The fix to this, I would suggest your first check be this:
    Code:
    if len(view) == len(puzzle):
        #they are both the same size continue with checks
    else:
        return False
    You can add that into the rest of your big if statement, just make sure it is listed first, since calling view[i] out of bounds will error.

    The rest of it though, just look for a match on the characters. You can do a
    Code:
    if puzzle[i] == view[i] or view[i] == '^':
    which will give you the results you are seeking.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    Originally Posted by ChickensEvil
    First, are you suggesting that when "-" is present it will always show?

    You also seem to be going about it with the right idea, based on what you describe. Break the string into a list and check each value in order.

    The only thing I would caution, if you are running a loop through say "puzzle" and it is a different size from "view" if you call a value from "view" which exceeds the size it will error. The fix to this, I would suggest your first check be this:
    Code:
    if len(view) == len(puzzle):
        #they are both the same size continue with checks
    else:
        return False
    You can add that into the rest of your big if statement, just make sure it is listed first, since calling view[i] out of bounds will error.

    The rest of it though, just look for a match on the characters. You can do a
    Code:
    if puzzle[i] == view[i] or view[i] == '^':
    which will give you the results you are seeking.
    I tried what you told me and all the examples are coming out as true I cant seem to make them right.

    My new code is:
    Code:
    def is_match (puzzle,view):
        '''(str,str)->bool
    
        returns true iff puzzle and view match in the place
        where the letters are hidden
    
        
    ALL THESE EXAMPLES ARE COMING OUT AS TRUE:(
    >>>is_match ('apples','^pp^^^')
        True
        >>>is_match ('apples-bananas','^^^^^^^^^^^^^')
        False
        >>>is_match ('apples-i', '^^^^^^-^')
        True
        >>>is_match ('apples-i', '^^^^^^^^')
        False
        >>>is_match ('apples','^pp^a^')
        False
        
        '''
        a = 0
        for i in range(len(puzzle)):
            if len(view) == len(puzzle):
                return True
            elif puzzle[i] == view[i] or view[i] == HIDDEN:
                return True
            else:
                return False
    IF YOU can alter the code where needed that would help me greatly, i appreciate your effort so far too
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    Code:
        a = 0
        for i in range(len(puzzle)):
            if len(view) == len(puzzle):
                return True
            elif puzzle[i] == view[i] or view[i] == HIDDEN:
                return True
            else:
                return False
    I suppose I should have been more clear, you want to put your size check around the other checks like so:

    Code:
        a = 0
        if len(view) == len(puzzle):
            for i in range(len(puzzle)):
                if puzzle[i] == view[i] or view[i] == HIDDEN:
                    return True
                else:
                    return False
        else:
            return False
    You see the problem was it was checking as true on the size check which is why those other results were coming back as true. Nested if statements are your friend
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    also I am still trying to find out how exactly you are wanting to handle the "-" character... seems to me like that is a special condition...

    If you want to single that character out as something different try this:

    Code:
    def is_match (puzzle,view):
        a = 0
        if len(view) == len(puzzle):
            for i in range(len(puzzle)):
                if puzzle[i] == "-" and view[i] != "-":
                    return False
                elif puzzle[i] == view[i] or view[i] == HIDDEN:
                    return True
                else:
                    return False
        else:
            return False
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    Okay so for the "-", there is no specific way would want to handle it, just that any thing hidden besides and alphabetic character will result as False..

    so even if its a space, /, or anything, if it is hidden('^') in view, it will be False...

    I hope that makes it clearer, perhaps you can do something to only math alphabetic characs, length, letters, and placement(index)... basically everything needs to match and the condition is that ONLY ALPHABETIC CHARACTERS CAN BE HIDDEN('^') nothing else

    and when i tried the latest code, it still gave me error on the last 3 examples, seems as though very certain conditions must be met because the last two examples came as True when they should have been False
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    Well, that's what I get for just reading through code and not testing it... haha... So the problem here is that the for loop exits on the first condition of true... solve this problem by only exiting on false, unless it makes it through the loop like so:

    Code:
    def is_match (puzzle,view):
        a = 0
        if len(view) == len(puzzle):
            for i in range(len(puzzle)):
                if puzzle[i] != view[i] and view[i] != HIDDEN:
                    return False
        else:
            return False
        return True
    The other bit seems like it might be best handled by input validation when you are receiving updates to the view variable (which I assume is taking place elsewhere in the program).

    If puzzle is generated by the program, and I am assuming this is the case, you wouldn't have the situation where you have hyphened words right?

    Anyway, if you look under the python documentation on strings there are all kinds of things you can look at when dealing with them. If somehow you cannot get what you need from that, import re (regular expressions) and look there. You can do re.search() to find those wrong characters and tell the user that they provided a bad value.

    I would go more in depth, but I really don't know what your requirements are, and how the entire program is set up.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    Originally Posted by ChickensEvil
    Well, that's what I get for just reading through code and not testing it... haha... So the problem here is that the for loop exits on the first condition of true... solve this problem by only exiting on false, unless it makes it through the loop like so:

    Code:
    def is_match (puzzle,view):
        a = 0
        if len(view) == len(puzzle):
            for i in range(len(puzzle)):
                if puzzle[i] != view[i] and view[i] != HIDDEN:
                    return False
        else:
            return False
        return True
    The other bit seems like it might be best handled by input validation when you are receiving updates to the view variable (which I assume is taking place elsewhere in the program).

    If puzzle is generated by the program, and I am assuming this is the case, you wouldn't have the situation where you have hyphened words right?

    Anyway, if you look under the python documentation on strings there are all kinds of things you can look at when dealing with them. If somehow you cannot get what you need from that, import re (regular expressions) and look there. You can do re.search() to find those wrong characters and tell the user that they provided a bad value.

    I would go more in depth, but I really don't know what your requirements are, and how the entire program is set up.
    Well actually in the other program i will actually get situations where i have to deal with hyphens and other non-alphabetic terms..also...with regards to the string functions..as a matter of fact, i think i need to use S.isalpha() except, whenever i put it in my code, it doesnt seem to give me the right output, can you tell me where and how i can put if condition with puzzle.isalpha()? Thank You so much..

    I am almost done with this and hopefully wont have to see it again... I guess my teacher is really testing me with this
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    You should make sure to position it outside your for loop since doing so will check on the value itself. Otherwise it is also a simple true/false.

    You could likely stick it with the first "if" like so:
    Code:
    if len(view) == len(puzzle) and puzzle.isalpha():
    See this is why I was originally trying to be vague but point you in the right direction, since I figured it was part of an assignment. Me giving you the answer isn't going to help you learn.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    Originally Posted by ChickensEvil
    You should make sure to position it outside your for loop since doing so will check on the value itself. Otherwise it is also a simple true/false.

    You could likely stick it with the first "if" like so:
    Code:
    if len(view) == len(puzzle) and puzzle.isalpha():
    See this is why I was originally trying to be vague but point you in the right direction, since I figured it was part of an assignment. Me giving you the answer isn't going to help you learn.
    I actually was hoping to learn from vague answers and didnt originally want the code, but since this was a topic discussed very vaguely i didnt get it...plus i have issue with loops, still gotta practice them

    Actually when i tested the code i am getting the wrong output for this example:
    >>>is_match ('apples-i', '^^^^^^-^')
    should give me True but is giving me False...
    the rest is working fine..

    my code now is:
    Code:
    def is_match (puzzle, view):
    a = 0
        if len(view) == len(puzzle) and puzzle.isalpha()==view:
            for i in range(len(puzzle)):
                if (puzzle[i] != view[i] and view[i] != HIDDEN):
                    return False
                elif (puzzle[i].isalpha()==HIDDEN):
                    return True
        else:
            return False
        return True
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    What I recommend is try to break things out very logically. Think about everything you need to have done and lay out a plan of attack to cover every condition. Once you have that, then build up the code around what you want to have happen.

    The more you do it, the better you will be at just piecing things together in your head and condensing the code down into a small segment.

    So like how you had your four examples, if that is all you need to consider, then that is fine, and build up around that. If something isn't working like you think it should, run the code in debugging mode and step through it line by line, watching as the variables change. When something unexpected happens that should be the point where you narrow down your search and rethink the attack plan.

    Debugging is your friend! No matter what language it is, learn how to use the debugging.

    Just some tips from the peanut gallery here

    Anyway, I hope that I have helped to solve your little issues, and more importantly you understand what is being done. If you need anything further feel free to ask!
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    Thank You,

    you helped alot...

    although i know i am making a very minor mistake now, because one or the other example is giving me wrong output, i jut dont know how to fix it,

    I will try my best though i dont know whether i can do it
    Thanks for all your help
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    well no, it will give you false, because "puzzle.isalpha()" is false when you have "-" inside it.

    If you need to specifically handle "-" differently, then try the approach I recommended earlier which was inside the loop put a check like this:
    Code:
    if puzzle[i] == "-" and view[i] != "-":
    This way if view didn't already have the "-" revealed then it would return false.... which appears to satisfy the two examples you gave:
    Code:
        >>>is_match ('apples-i', '^^^^^^-^')
        True
        >>>is_match ('apples-i', '^^^^^^^^')
        False
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    Originally Posted by ChickensEvil
    well no, it will give you false, because "puzzle.isalpha()" is false when you have "-" inside it.

    If you need to specifically handle "-" differently, then try the approach I recommended earlier which was inside the loop put a check like this:
    Code:
    if puzzle[i] == "-" and view[i] != "-":
    This way if view didn't already have the "-" revealed then it would return false.... which appears to satisfy the two examples you gave:
    Code:
        >>>is_match ('apples-i', '^^^^^^-^')
        True
        >>>is_match ('apples-i', '^^^^^^^^')
        False
    I could do that and i know, but the problem is i can have a situation where i dont ONLY have to deal with hyphens...you get what i mean...like there can be / in the middle too instead of hyphen and the result should be same...thats why i thought isalpha would work...I feel as though my brain is melting...
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0
    So then add to the check, it may get messy, but it would work...
    Code:
    if puzzle[i] == "-" and view[i] != "-":
        return False
    elif puzzle[i] == "/" and view[i] != "/":
        return False
    elif puzzle[i] == "_" and view[i] != "_":
        return False
    As I said before you could look at your other options with string methods, I haven't looked them up, there may be something else you can which would work... alternatively, you may also be able to use Regular Expressions... but since this is just for an assignment, making it work is probably all you need...
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo