-4

what I have done is -

def reverse(i, j, lstr):

    if j == 0:
        return []
    if j == 1:
        return lstr    
    if i>j:
        return lstr
    temp = lstr[i]
    lstr[i] = lstr[j]
    lstr[j] = temp

    print(reverse(i+1, j-1, lstr))

It is returning -

['o', 'l', 'l', 'e', 'h']
None
None
None

why is it returning None 3 times? How can I fix this?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Format your code so it makes sense please – Mad Physicist May 21 '22 at 19:27
  • 3
    By not having a print *in* the method but only outside of it. (Or only print when `i == 0`). – luk2302 May 21 '22 at 19:27
  • What does your function return when it falls through to the end? – Mad Physicist May 21 '22 at 19:28
  • If j is neither zero nor one, and i is not bigger than j, then the function doesn't return anything, and therefore it returns `None` by default. – John Gordon May 21 '22 at 19:28
  • How are you calling your function? – quamrana May 21 '22 at 19:29
  • Welcome to Stack Overflow. It `return` multiple times because it **is called** multiple times. It also `return`s `None` **every time**, including the time that you see the reversed list output. Please make sure you understand that the output you see - which comes from the `print` function - has **absolutely nothing to do with** `return`ing a value. `print`ing and `return`ing are unrelated things. – Karl Knechtel May 21 '22 at 20:24
  • That said, there are many much easier ways to reverse a list, and no good reason to use recursion for the task. – Karl Knechtel May 21 '22 at 20:26

1 Answers1

-2
def reverse (s):
    if len (s) <= 1:
        return s
    else:
        return s[-1] + reverse(s[1:-1]) + s[0]
glk0
  • 17
  • 6
  • why the extra `+ s[0]` instead of just `reverse(s[:-1])` – Mark May 21 '22 at 19:40
  • Test them and you'll get it. – glk0 May 21 '22 at 22:05
  • Well...of course I did, I get the same result. Do you have a test case where changing your last line to the simpler `return s[-1] + reverse(s[:-1])` does something different? – Mark May 21 '22 at 22:13
  • benchmark for speed and memory consumption – glk0 May 21 '22 at 22:47
  • 2
    I think you're just trolling me now — first it's, "test them and you'll get it"…now it's "benchmarks". timeit benchmarks on a 2600-length string: `1.3 ms ± 56.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)` vs `1.27 ms ± 28.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)` The simpler one is faster, but it negligible. If speed was important this wouldn't be a a recursive function. – Mark May 21 '22 at 23:32
  • Sorry. This is straightforward and i don't wanna be thinking about it more than a minute. I wrote down the first solution that came to my mind. If you're unhappy with it or wondering how it works, it's up to you to figure out what it's doing, and decide whether you accept or reject it. That's what I have been trying to say. – glk0 May 22 '22 at 08:34
  • "it's up to you to figure out what it's doing". Well…okay, but **you** posted an **answer**. Code-only answers are often considered [low quality](https://meta.stackoverflow.com/questions/310219/are-answers-that-contain-only-commented-code-acceptable) here, so I was simply asking for you to add some clarity to what seemed like an unusual choice. – Mark May 22 '22 at 14:43