-1

I have this problem for homework and I have almost gotten it but not quite. I have to develop some code that takes a list and a number as parameters. The function returns a copy of the list with the first number of items reversed. I can not use in-built functions, slicing or reverse, I can only use append.() and range(). Would really appreciate someone's help with fixing my current code and maybe explaining how you fixed it? Thankyou!!!

str_list6 = ['e', 'd', 'u', 'd']

def length(my_list):
    total = 0
    for c in my_list:
        total += 1
    return total

def remove_value(my_list):
    res = []

    for i in range(length(my_list) -1, -1, -1):
        res.append((my_list)[i])

    return res

the example given:

numList = [1, 2, 3, 4, 5, 6, 7]
number = 4

The call to reverse(numList, number) should 
    return the new list
     [4, 3, 2, 1, 5, 6, 7]. 

So my code currently simply reverses the list however (its hard to explain), it needs to reverse with the shift of the 'number'. Hopefully this make sense!

3
  • I can see a remove_value in there, but not a reverse Commented Jun 5, 2019 at 15:39
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Commented Jun 5, 2019 at 15:40
  • Try having two loops in the reverse function: one to reverse what you need to reverse, and one to copy the rest. Or make a new function which reverses the part it needs to, with the code you have shared, and another part that copies the items over, without reversing - which is a range going up instead of down Commented Jun 5, 2019 at 15:48

4 Answers 4

1

The problem here is that you are not taking into account the number to shift. In your example you are given two variables, the list and the number to shift (is this the position or the actual number you are looking for? - assuming position), yet your code only takes the list.

def remove_value(my_list, pos_to_reverse):
    res = []

    for i in range(pos_to_reverse-1, -1):
        res.append(my_list[i])

    for i in range(pos_to_reverse, length(my_list)-1):
        res.append(my_list[i])

    return res
Sign up to request clarification or add additional context in comments.

Comments

0

You were getting close! I think the function below does what you want.

def reverse(numList,number):
    # Make an empty list
    reversedlist=[]

    for i in range(number):
    # i goes from 0 to number-1
        reversedlist.append(numList[number-i-1])
        # build a reversed list by adding each element
        # of numList going in reverse order.

    for i in range(number):
    # i goes from 0 to number-1
        numList[i]=reversedlist[i]
        # change the first 'number' elements of the
        # original list with the reversed list.

    return numList

numList = [1, 2, 3, 4, 5, 6, 7]
number = 4

print(reverse(numList,number))
# prints: [4, 3, 2, 1, 5, 6, 7]

Comments

0

You can probably do like this.

l = [1, 2, 3, 4, 5, 6, 7]
n = 4

def solution(lst,n):     
    size = n             
    hiindex = size - 1
    its = size//2            
    for i in range(0, its):    
        temp = lst[hiindex]     
        lst[hiindex] = lst[i]
        lst[i] = temp
        hiindex -= 1  
    return lst 
print(solution(l,n))

Comments

0

You can use this tiny loop:

numList = [1, 2, 3, 4, 5, 6, 7]
number = 4

def reverse(l, n):
  for i in range((n + 1) // 2):
    l[i], l[n - i - 1] = l[n - i - 1], l[i]
  return l

print(reverse(numList, number))

Output:

[4, 3, 2, 1, 5, 6, 7]

3 Comments

Thankyou, however I can not use slicing, (forgot to put it in the description)
This is one of my problem solving questions for the week.
This worked perfectly thankyou all for your help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.