I have this python code that comes from here https://github.com/joelgrus/data-science-from-scratch/blob/master/scratch/k_nearest_neighbors.py
<code>from typing import List
from collections import Counter
def majority_vote(labels: List[str]) -> str:
"""Assumes that labels are ordered from nearest to farthest."""
vote_counts = Counter(labels)
winner, winner_count = vote_counts.most_common(1)[0]
count = [count for count in vote_counts.values()
if count == winner_count]
return majority_vote(labels[:-1])
# Tie, so look at first 4, then 'b'
assert majority_vote(['a', 'b', 'c', 'b', 'a']) == 'b'
<code>from typing import List
from collections import Counter
def majority_vote(labels: List[str]) -> str:
"""Assumes that labels are ordered from nearest to farthest."""
vote_counts = Counter(labels)
winner, winner_count = vote_counts.most_common(1)[0]
count = [count for count in vote_counts.values()
if count == winner_count]
num_winners = len(count)
if num_winners == 1:
return winner
else:
return majority_vote(labels[:-1])
# Tie, so look at first 4, then 'b'
assert majority_vote(['a', 'b', 'c', 'b', 'a']) == 'b'
</code>
from typing import List
from collections import Counter
def majority_vote(labels: List[str]) -> str:
"""Assumes that labels are ordered from nearest to farthest."""
vote_counts = Counter(labels)
winner, winner_count = vote_counts.most_common(1)[0]
count = [count for count in vote_counts.values()
if count == winner_count]
num_winners = len(count)
if num_winners == 1:
return winner
else:
return majority_vote(labels[:-1])
# Tie, so look at first 4, then 'b'
assert majority_vote(['a', 'b', 'c', 'b', 'a']) == 'b'
When I select it all in Visual Studio Code and press MAJ + ENTER, I get the following error :
<code>SyntaxError: invalid syntax
>>> from typing import List
>>> from collections import Counter
>>> def majority_vote(labels: List[str]) -> str:
... """Assumes that labels are ordered from nearest to farthest."""
... vote_counts = Counter(labels)
... winner, winner_count = vote_counts.most_common(1)[0]
... count = [count for count in vote_counts.values()
... if count == winner_count]
... num_winners = len(count)
... return majority_vote(labels[:-1])
... # Tie, so look at first 4, then 'b'
... assert majority_vote(['a', 'b', 'c', 'b', 'a']) == 'b'
assert majority_vote(['a', 'b', 'c', 'b', 'a']) == 'b'
SyntaxError: invalid syntax
<code>SyntaxError: invalid syntax
>>> from typing import List
>>> from collections import Counter
>>>
>>> def majority_vote(labels: List[str]) -> str:
... """Assumes that labels are ordered from nearest to farthest."""
... vote_counts = Counter(labels)
... winner, winner_count = vote_counts.most_common(1)[0]
... count = [count for count in vote_counts.values()
... if count == winner_count]
... num_winners = len(count)
... if num_winners == 1:
... return winner
... else:
... return majority_vote(labels[:-1])
...
... # Tie, so look at first 4, then 'b'
... assert majority_vote(['a', 'b', 'c', 'b', 'a']) == 'b'
File "<stdin>", line 14
assert majority_vote(['a', 'b', 'c', 'b', 'a']) == 'b'
^^^^^^
SyntaxError: invalid syntax
</code>
SyntaxError: invalid syntax
>>> from typing import List
>>> from collections import Counter
>>>
>>> def majority_vote(labels: List[str]) -> str:
... """Assumes that labels are ordered from nearest to farthest."""
... vote_counts = Counter(labels)
... winner, winner_count = vote_counts.most_common(1)[0]
... count = [count for count in vote_counts.values()
... if count == winner_count]
... num_winners = len(count)
... if num_winners == 1:
... return winner
... else:
... return majority_vote(labels[:-1])
...
... # Tie, so look at first 4, then 'b'
... assert majority_vote(['a', 'b', 'c', 'b', 'a']) == 'b'
File "<stdin>", line 14
assert majority_vote(['a', 'b', 'c', 'b', 'a']) == 'b'
^^^^^^
SyntaxError: invalid syntax
But if I run it from the command line with python vote.py
it runs with no errors.
Can you help me understand this behaviour please?
Python version : Python 3.12.4 | packaged by Anaconda, Inc. | (main, Jun 18 2024, 15:03:56) [MSC v.1929 64 bit (AMD64)] on win32
Thanks !
I tried to copy-paste the whole file from Joel Grus’ GitHub repository and run it in Visual Studio Code but I get the same error as when I manually entered everything.