Question about Python


  • 0
    D

    The below code works but I'm a little confused about the definition of my inner function find. If I don't pass the counter variable c into it, the function complains about not knowing what c is, i.e. find(letter, word, number) doesn't work and throws an UnboundLocalError. But I thought functions defined inside others have access to all the local variables of the outer function? And before you suggest that it's just because the function definition is before the c = Counter(s) line, that's not it, as I've tried moving the function declaration lower and see the same error.

    	def originalDigits(self, s):
    		def find(c, letter, word, number):
    			d[number] = c[letter]
    			for char in word:
    				c[char] -= d[number]
    			return c
    
    		c = Counter(s)
    		d = [0]*10
    		
    		c = find(c, 'g', 'eight', 8)
    		c = find(c, 'u', 'four', 4)
    		c = find(c, 'w', 'two', 2)
    		c = find(c, 'x', 'six', 6)
    		c = find(c, 'z', 'zero', 0)
    		c = find(c, 'f', 'five', 5)
    		c = find(c, 'i', 'nine', 9)
    		c = find(c, 'h', 'three', 3)
    		c = find(c, 's', 'seven', 7)
    		c = find(c, 'o', 'one', 1)
    		
    		return ''.join(str(n)*d[n] for n in xrange(10))
    

  • 0

    Can you show the code that doesn't work? And an example input where it fails?


  • 0
    D

    If I change the inner function definition like below, it throws the UnboundLocalError.

    def originalDigits(self, s):
    	def find(letter, word, number):
    		d[number] = c[letter]
    		for char in word:
    			c[char] -= d[number]
    
    	c = Counter(s)
    	d = [0]*10
    		
    	find('g', 'eight', 8)
    	find('u', 'four', 4)
    	find('w', 'two', 2)
    	find('x', 'six', 6)
    	find('z', 'zero', 0)
    	find('f', 'five', 5)
    	find('i', 'nine', 9)
    	find('h', 'three', 3)
    	find('s', 'seven', 7)
    	find('o', 'one', 1)
    	
    	return ''.join(str(n)*d[n] for n in xrange(10))
    

  • 0

    @david120 Works for me, like it should. No idea what you're doing. Please post the entire code you're submitting, exactly as you're submitting it.


  • 0
    D

    Sorry here is the code that didn't work.

    from collections import Counter
    class Solution(object):
    	def originalDigits(self, s):
    		def find(letter, word, number):
    			d[number] = c[letter]
    			c -= Counter(word*d[number])
    
    		c = Counter(s)
    		d = [0]*10
    			
    		find('g', 'eight', 8)
    		find('u', 'four', 4)
    		find('w', 'two', 2)
    		find('x', 'six', 6)
    		find('z', 'zero', 0)
    		find('f', 'five', 5)
    		find('i', 'nine', 9)
    		find('h', 'three', 3)
    		find('s', 'seven', 7)
    		find('o', 'one', 1)
    		
    		return ''.join(str(n)*d[n] for n in xrange(10))
    

  • 0

    @david120 Well that doesn't work because the c -= ... assigns to c, making it a local variable. And then of course c[letter] fails. Use c.subtract(...) instead.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.