-1

hello guys I wrote a function for this task:

Task

You've just moved into a perfectly straight street with exactly n identical houses on either side of the road. Naturally, you would like to find out the house number of the people on the other side of the street.

The street looks something like this:

Street
1|   |6
3|   |4
5|   |2
  • Evens increase on the right;
  • Odds decrease on the left. H
  • House numbers start at 1 and increase without gaps.
  • When n = 3, 1 is opposite 6, 3 opposite 4, and 5 opposite 2.

Example

Given your house number address and length of street n, give the house number on the opposite side of the street.

over_the_road(1, 3) = 6

My solution:

def over_the_road(address, n):
    even = list(range(n + 1))
    for number in even:
        if number % 2 == 0:
            even.remove(number)
    odds = list(range(n + 1))
    odds.pop(0)
    for number in odds:
        if not number % 2 == 0:
            odds.remove(number)
    odds.reverse()
    if address == even:
        return odds[even.index(address)]
    elif address == odds:
        return even[odds.index(address)]

Can someone please explain why is my function returning none?

Federico Baù
  • 6,013
  • 5
  • 30
  • 38

2 Answers2

2

Adding what @Lucas Infante regarding the instatement.

The algorithm is actually not right, the results should be 6 hence here some adjustments:

def over_the_road(address, n):
    print(range(n))
    even = []
    odds = []
    for number in range(n+1):
        if number != 0:
            even_ = number*2
            even.append(even_)
            odds.append(even_-1)
    odds.reverse()
    if address in even:
        return odds[even.index(address)]
    elif address in odds:
        return even[odds.index(address)]


print(over_the_road(1, 3)) # :> 6
Federico Baù
  • 6,013
  • 5
  • 30
  • 38
1

Your code is returning None because the conditionals you have in place are not being met, so the code reaches the end of the function, returning None by default.

As other people mentioned you should use if address in even and elif address in odds as your conditionals.

Lucas Infante
  • 798
  • 6
  • 21