Click here to Skip to main content
13,558,063 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Every time I run it, it says return outside function. What do I do?

What I have tried:

chars = "ACGT"

def neighbors(pattern, d):
    assert(d <= len(pattern))

if d == 0:
    return [pattern]

r2 = neighbors(pattern[1:], d-1)
r = [c + r3 for r3 in r2 for c in chars if c != pattern[0]]

if (d < len(pattern)):
    r2 = neighbors(pattern[1:], d)
    r += [pattern[0] + r3 for r3 in r2]

return r
Posted 17-May-18 22:35pm
Updated 17-May-18 23:18pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

If you get an error message you don;t understand, Google it: return outside function - Google Search[^] and follow a few links.
It's pretty obvious if you just read the error message!
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

In python the indentation is important. Everything that belongs in the definition of neighbors should be indented at least once.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Quote:
Every time I run it, it says return outside function. What do I do?

With Python, indentation matters, it is the structure of your program, it is not just presentation. Need to be extra careful with it.
chars = "ACGT"

def neighbors(pattern, d):
    assert(d <= len(pattern))

    if d == 0:
        return [pattern]
    
    r2 = neighbors(pattern[1:], d-1)
    r = [c + r3 for r3 in r2 for c in chars if c != pattern[0]]
    
    if (d < len(pattern)):
        r2 = neighbors(pattern[1:], d)
        r += [pattern[0] + r3 for r3 in r2]
    
    return r
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.180515.1 | Last Updated 18 May 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100