Results 1 to 8 of 8

Math Help - Invalid syntax in Python

  1. #1
    Junior Member
    Joined
    Aug 2010
    Posts
    32

    Invalid syntax in Python

    Can someone tell me what the syntax error in this program is? The program attempts to check a list for duplicates and return True if it has any, False if it has none.

    def dupl(L):
    ----if len(L) <= 1:
    --------return False
    ----elif L[0] in L[1:]:
    --------return True
    ----else:
    --------return dupl(L[1:])

    The syntax error is marked in bold.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by quiney View Post
    Can someone tell me what the syntax error in this program is? The program attempts to check a list for duplicates and return True if it has any, False if it has none.

    def dupl(L):
    ----if len(L) <= 1:
    --------return False
    ----elif L[0] in L[1:]:
    --------return True
    ----else:
    --------return dupl(L[1:])

    The syntax error is marked in bold.
    Works perfectly fine on my computer. Not sure why you would be getting a syntax error.

    I'm assuming those hyphens are just in order to preserve indentation on this forum. You can use [code][/code] tags, like so:

    Code:
    def dupl(L):
        if len(L) <= 1:
            return False
        elif L[0] in L[1:]:
            return True
        else:
            return dupl(L[1:])
    
    L1 = [1,2,3,4,5]
    L2 = [1,2,3,2,5]
    
    if dupl(L1):
        print "hello"
    if dupl(L2):
        print "goodbye"
    Output:

    Code:
    goodbye
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Aug 2010
    Posts
    32
    Thanks. The program works fine now. I have one more question. Would something like the following program accomplish what I'm trying to do?

    Code:
    def dup(L):
    	i=1
    	dup=False
    	if len(L)==0:
    		return dup
    	else:
    		while i<=len(L)-1:
    			if sorted(L)[i]==sorted(L)[i-1]:
    				dup=True
    			else:
    				i=i+1
    		return dup
    When I run this program, for some lists, it returns the correct answer, but for others, the program freezes up.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by quiney View Post
    Thanks. The program works fine now. I have one more question. Would something like the following program accomplish what I'm trying to do?

    Code:
    def dup(L):
    	i=1
    	dup=False
    	if len(L)==0:
    		return dup
    	else:
    		while i<=len(L)-1:
    			if sorted(L)[i]==sorted(L)[i-1]:
    				dup=True
    			else:
    				i=i+1
    		return dup
    When I run this program, for some lists, it returns the correct answer, but for others, the program freezes up.
    Probably where you wrote dup=True you meant to write return True. What you have causes an infinite loop in the event of a duplicate.

    Or maybe you meant to write a break statement, but might as well just return, it's simpler.

    Instead of calling the sorted method so many times, why not just write L2 = sorted(L) near the beginning of the function?

    Also after you replace dup=True with return True you'll see that the variable dup is superfluous.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    Aug 2010
    Posts
    32
    Thanks! So far I have this:
    Code:
    def dup(L):
    	i=1
    	L2=sorted(L)
    	if len(L)==0:
    		return False
    	else:
    		while i<=len(L)-1:
    			if L2[i]==L2[i-1]:
    				return True
    			else:
    				i=i+1
    This returns True whenever there is a duplicate, but I still don't know how to get it to return false when there is no duplicate and the list is greater than 0.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by quiney View Post
    Thanks! So far I have this:

    This returns True whenever there is a duplicate, but I still don't know how to get it to return false when there is no duplicate and the list is greater than 0.
    I took the liberty of changing a few things around. Compare, and use whatever aspects of it you like

    Code:
    def dupl(L):
        if len(L)<2:
            return False
        i=0
        L2=sorted(L)
        while i<len(L)-1:
            if L2[i]==L2[i+1]:
                return True
            i=i+1
        return False
    
    L1 = [1,2,3,4,5]
    L2 = [1,2,3,2,5]
    
    if dupl(L1):
        print "hello"
    if dupl(L2):
        print "goodbye"
    Note in particular that a "return" can render an "else" unnecessary.
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Junior Member
    Joined
    Aug 2010
    Posts
    32
    Thanks. I also figured out a much shorter way to do it:
    Code:
    def dupl(L):
    	if len(L)==len(set(L)):
    		return False
    	return True
    Follow Math Help Forum on Facebook and Google+

  8. #8
    MHF Contributor undefined's Avatar
    Joined
    Mar 2010
    From
    Chicago
    Posts
    2,340
    Awards
    1
    Quote Originally Posted by quiney View Post
    Thanks. I also figured out a much shorter way to do it:
    Code:
    def dupl(L):
    	if len(L)==len(set(L)):
    		return False
    	return True
    Nice! I come from a non-Python background so I have not seen this before, but it makes perfect sense. However, you can save some keystrokes as follows:

    Code:
    def dupl(L):
        return len(L)!=len(set(L))
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Python nth prime
    Posted in the Math Software Forum
    Replies: 1
    Last Post: August 16th 2011, 11:06 PM
  2. python questions...please help!!
    Posted in the Math Software Forum
    Replies: 3
    Last Post: December 9th 2007, 08:06 AM
  3. Python-conditions!!!
    Posted in the Math Software Forum
    Replies: 1
    Last Post: December 8th 2007, 05:27 AM
  4. invalid range
    Posted in the Pre-Calculus Forum
    Replies: 4
    Last Post: December 6th 2007, 12:29 AM
  5. valid or invalid ?
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: October 20th 2007, 09:55 AM

Search Tags


/mathhelpforum @mathhelpforum