0

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))
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
EML
  • 395
  • 1
  • 6
  • 15
  • Does this answer your question? [Is it possible to modify a variable in python that is in an outer (enclosing), but not global, scope?](https://stackoverflow.com/questions/8447947/is-it-possible-to-modify-a-variable-in-python-that-is-in-an-outer-enclosing-b) – Karl Knechtel Sep 13 '22 at 14:49

1 Answers1

1

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]))

Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    Thanks. That's quite smart. Out of interest though, do you know why my scoping issue happens? I have found using "nonlocal" in place of "global" fixes it but I don't know why. – EML Oct 30 '20 at 13:26