0

I'm trying to write a function that will slice a list between two given values. If the stop value exceeds the list bounds, the stop value is the length of the list. If the start value exceeds the list bounds, I want to return an empty list. If the start value is less than the stop value, I want to return an empty list. I do not want to use any built-in functions or methods other than range() and list.append()

The code that is calling the function is:

str_list = ['s', 'h', 'r', 'u', 'g', 'g', 'e', 'd']

print("\nget_slice Test")
my_slice = list_function.get_slice(str_list, 2, 100)
print(list_function.to_string(str_list))
print(list_function.to_string(my_slice))

my_slice = list_function.get_slice(str_list, 6, 1)
print(list_function.to_string(my_slice))

my_slice = list_function.get_slice(str_list, 2, 5)
print(list_function.to_string(str_list))
print(list_function.to_string(my_slice))

The output I want to produce is:

get_slice Test
List is: s, h, r, u, g, g, e, d
List is: r, u, g, g, e, d
List is:
List is: s, h, r, u, g, g, e, d
List is: r, u, g

I'm defining the function as:

def get_slice(my_list, start, stop):

I've tried my_list = my_list[start:stop] but I get a 'NoneType' object is not iterable error. I also wanted to try using for k in range(0, length(my_list)):, as it was suggested that I use range(), however I can't think of a way that could be used for this function just yet.

Any assistance with this would be greatly appreciated.

Dylsbo
  • 25
  • 3
  • hint: look at `range(start, stop)`.... – Corley Brigman Nov 05 '13 at 14:36
  • 2
    Why are you doing that, when Python already supports slice notation? Is it homework? Learning? What have you tried so far? Show some code. – Michael Foukarakis Nov 05 '13 at 14:36
  • 1
    Please show your attempts! Also, show your expected input. – aIKid Nov 05 '13 at 14:38
  • I've tried `my_list = my_list[start:stop]` but I get a 'NoneType' object is not iterable error. I also wanted to try using `for k in range(0, length(my_list)):`, as it was suggested that I use `range()`, however I can't think of a way that could be used for this function just yet. – Dylsbo Nov 05 '13 at 14:43
  • Edit your question to include your attempts, so it's clearer. – aIKid Nov 05 '13 at 14:45

2 Answers2

0

You just need to use the slice notation. That was covered here in this thread:

Explain Python's slice notation

Here is the solution printing the correct output:

def get_slice(my_list, start, stop):
        return my_list[start:stop][:]

def to_string(my_list):
        return 'List is: %s' % ''.join(my_list[:])

str_list = ['s', 'h', 'r', 'u', 'g', 'g', 'e', 'd']

print("\nget_slice Test")
my_slice = get_slice(str_list, 2, 100)
print(to_string(str_list))
print(to_string(my_slice))

my_slice = get_slice(str_list, 6, 1)
print(to_string(my_slice))

my_slice = get_slice(str_list, 2, 5)
print(to_string(str_list))
print(to_string(my_slice))
Community
  • 1
  • 1
Flavio Garcia
  • 1,491
  • 1
  • 14
  • 20
0

I was not able to reproduce your problem, you should have posted the full code. Maybe a missing return and a fix on the variables? I don't know. But here's my solution:

def get_slice(my_list, start, stop):
    return my_list[start:stop]

def to_string(my_list):
    return "List is: " + ', '.join(my_list)

str_list = ['s', 'h', 'r', 'u', 'g', 'g', 'e', 'd']

print("\nget_slice Test")
my_slice = get_slice(str_list, 2, 100)
print(to_string(str_list))
print(to_string(my_slice))

my_slice = get_slice(str_list, 6, 1)
print(to_string(my_slice))

my_slice = get_slice(str_list, 2, 5)
print(to_string(str_list))
print(to_string(my_slice))

output:

get_slice Test
List is: s, h, r, u, g, g, e, d
List is: r, u, g, g, e, d
List is: 
List is: s, h, r, u, g, g, e, d
List is: r, u, g
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75