Python Forum
Help with function and sep( , ) value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with function and sep( , ) value
#11
Good job on remove_value()!

As for reverse():
1) I'm not sure what you're trying to accomplish in lines 32-33. It appears that if the variable is equal to -1, you are reassigning it the same value of -1. From the assignment and your comment in the program, it seems like the only check you need here is whether the entered number is less than 2.

2) There are problems with your approach in your for loop on lines 45-50. Keep in mind that when you use "for element in my_list:", you are iterating through the values in the list ('e', 'd', 'u', 'd' in the case of str_list6). That means the element variable is a string, and you therefore can't add or subtract the integer value 1. If your plan is to iterate through a list in reverse order, you can do that using range() and the length of the list. Just to illustrate, this code would print the values from my_list one at a time in reverse order:
for i in range((list_length - 1), -1, -1):
    print(my_list[i])
Reply
#12
for lines 32-33, it's referencing this part "If the default argument for number is not provided in the function call, then the entire list is reversed. Check for the number value exceeding the list bounds (i.e. is greater than the length of the list)."

But it also states "If the number value entered is less than two, then return a copy of the list with no items reversed."

so ...-3,-2,0,1 returns the original list and -1 reverses the whole list (i.e. starts at position -1?). Am I reading that wrong?

I would be also interested in your thoughts on 39-40. Does that make sense or need to be there?

Thanks for the tip about using range. I didn't know that I could use that to reverse the order but that makes sense and I'm sure I'll be able to figure that out when I go at it to finish up tomorrow morning :)

With your example for using range, should the second parameter be -1 for my problem? i.e. what makes sense to me is a range starting at the end of the list (list_length -1) and finishing at the start(0), moving in steps of -1?

What am I missing now? I don't get the required output at all :/

def reverse(my_list, number=-1):
    
    new_list = []
    list_length = length(my_list)
    list_index = 0
    reverse_pos = number


    #if number value entered is less than 2, return copy of list (nothing reversed)
    if reverse_pos == -1:
        reverse_pos = -1
    elif reverse_pos <= 1:
        new_list.append(my_list)
    #if the position exceeds list length, make the reverse pos to be the length of the list. 
    elif reverse_pos > list_length -1:
        reverse_pos = list_length -1
    else:
        reverse_pos = number

    while list_index < list_length:
        #iterate through and once we reach the reverse_pos
        for element in range((list_length-1),0,-1):
            if element == reverse_pos:
                new_list.append(my_list[element])
        for element in range((list_length-1),0,1):
            if element > reverse_pos:
                new_list.append(my_list[element])    
        list_index += 1

    return new_list
Reply
#13
Ok, I see where you are coming from with lines 32-33 now. You basically aren't making a change if the value is already -1, but you use that initial if condition to set up your elif conditions. That works, or you could also just do:
    if reverse_pos != -1 and reverse_pos <= 1:
        new_list = my_list  ##*NOTE* - You have my_list = new_list here, but I think you want new_list = my_list instead.
    elif reverse_pos > list_length -1:
        reverse_pos = list_length -1
Good question about lines 39-40, and I think the answer is no, you don't need them. You've already assigned reverse_pos = number, and that will not be changed if your if and elif conditions are not met.

Also, I don't think you are reading the assignment wrong, but I find the assignment wording a little cryptic myself. The way you've interpreted it makes sense.

(Jun-02-2020, 02:22 PM)drewbty Wrote: With your example for using range, should the second parameter be -1 for my problem? i.e. what makes sense to me is a range starting at the end of the list (list_length -1) and finishing at the start(0), moving in steps of -1?

Yes, the second parameter should be -1. Remember that the stop value is not included in the range, so using -1 as the stop and -1 as the step means you will end at 0.

Note that you don't need to enter your while loop at all if you've already assigned new_list to my_list (since you are just returning the original list), so you'll probably want to add an if condition for that.

To help you figure out the for loop, here is the code you'd use to get new_list to be a copy of my_list with all values in reverse order:
        for i in range((list_length - 1), -1, -1):
                new_list.append(my_list[i])
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020