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
61.3k14 gold badges131 silver badges193 bronze badges
asked Oct 30, 2020 at 11:43
-
Does this answer your question? Is it possible to modify a variable in python that is in an outer (enclosing), but not global, scope?Karl Knechtel– Karl Knechtel2022年09月13日 14:49:41 +00:00Commented Sep 13, 2022 at 14:49
1 Answer 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
tononlocal
forfound
:
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]))
answered Oct 30, 2020 at 13:05
Sign up to request clarification or add additional context in comments.
1 Comment
MrJoe
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.
lang-py