MrJoe
MrJoe

Reputation: 445

Understanding scoping - Leetcode 1640

I am attempting Leetcode challenge 1640 - found here

This is my first attempt at the solution. However I keep getting a runtime error because it says

local variable "found" referenced before assignment

I thought that outer scope variables can be accessed by inner scopes. I don't understand why I'm getting this problem.

Please can you help. Thanks

class Solution(object):
    def canBeEqual(self, target, arr):
        s = set()
        found = False
        def reverse(target, arr):
            global s, found
            if found:
                return found
            if arr == target:
                found = True
                return found
            if tuple(arr) in s: return False
            s.add((tuple(arr)))
            for start in range(len(arr)):
                for end in range(start + 1, len(arr)):
                    arr = arr[:start] + arr[start:end+1][::-1] + arr[end+1:]
                    reverse(target, arr)
            return found
        return (reverse(target, arr))

Upvotes: 0

Views: 52

Answers (1)

Emma
Emma

Reputation: 27723

For solving this problem, we can just use the collection.Counter:

class Solution:
    def canBeEqual(self, target, arr):
        return collections.Counter(target) == collections.Counter(arr)
  • We can change global to nonlocal for found:
from typing import List
import collections
import itertools
import functools
import math
import string
import random
import bisect
import re
import operator
import heapq
import queue

from queue import PriorityQueue
from itertools import combinations, permutations
from functools import lru_cache
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from collections import Counter


class Solution(object):
    def canBeEqual(self, target, arr):
        s = set()
        found = False

        def reverse(target, arr):
            nonlocal found
            if found:
                return found
            if arr == target:
                found = True
                return found
            if tuple(arr) in s:
                return False
            s.add((tuple(arr)))
            for start in range(len(arr)):
                for end in range(start + 1, len(arr)):
                    arr = arr[:start] + arr[start:end + 1][::-1] + arr[end + 1:]
                    reverse(target, arr)
            return found
        return reverse(target, arr)


print(Solution().canBeEqual(target = [1, 2, 3, 4], arr = [2, 4, 1, 3]))

Explains it in this post


We can also use self simply:

from typing import List
import collections
import itertools
import functools
import math
import string
import random
import bisect
import re
import operator
import heapq
import queue

from queue import PriorityQueue
from itertools import combinations, permutations
from functools import lru_cache
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from collections import Counter


class Solution(object):
    def canBeEqual(self, target, arr):
        s = set()
        self.found = False

        def reverse(target, arr):
            if self.found:
                return self.found
            if arr == target:
                self.found = True
                return self.found
            if tuple(arr) in s:
                return False
            s.add((tuple(arr)))
            for start in range(len(arr)):
                for end in range(start + 1, len(arr)):
                    arr = arr[:start] + arr[start:end + 1][::-1] + arr[end + 1:]
                    reverse(target, arr)
            return self.found
        return reverse(target, arr)


print(Solution().canBeEqual(target = [1, 2, 3, 4], arr = [2, 4, 1, 3]))

Upvotes: 1

Related Questions