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
61.3k14 gold badges131 silver badges193 bronze badges
asked Oct 30, 2020 at 11:43
1

1 Answer 1

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]))
answered Oct 30, 2020 at 13:05
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.