# 11/17 Extra credit: calculating slicing indices

#1

The instructions for this exercise include slicing out the middle one-third of a list of numbers from 1 to 21. You can explicitly pass the indices (7 and 14), but I thought it would be fun to calculate them:

to_21 = [x for x in range(1, 22)]
middle_third = to_21[(len(to_21) / 3):(len(to_21) * 2 / 3):]

Indices must be integers, so if you try this on lists not evenly divisible by 2 or 3 or whatever divisors then you might outsmart yourself. You might be able to use float division and the int() command to account for it.

#2

to_21 = range(1,22)
odds = to_21[::2]
middle_third = to_21[7:14]

print odds
print middle_third

#3

@ferretey. Really good point. Computations are a better way of automating processes. Ultimately, programmers aim to automate all processes. If you know the types of data a list will carry, then a formula would make the program more flexible to process different types of list lengths.

#4

I recieved an error when trying this. My way around it was to wrap the formuals in int().

***
middle_third = to_21[(int(len(to_21)/3)):(int((len(to_21)/3)*2))]
***

#5

It's as simple as this

to_21 = [i for i in range(1,22)]
odds = to_21[::2]
middle_third = to_21[7:14]

#6

@digitalcoder04174 Yes it is as simple a what you have written but what @ferretey wrote for his middle_third will still be true if the size of the list changes(i.e he will not have to change anything when the size of the list changes)

But with the way you wrote it, if the size of the list changes you will have to modify your middle_third code to get the right answer(and you will have to do this every time the size of the list changes)

You can try it. change the size of the list to_21 and test it

#7

Yeah cheers for the feedback man. The issue is that they give us the size of list to begin with. I feel as though I would have taken a different approach if it was for a more general case.

#8

Yeah I know The constraints provided determines our logic. I solved it the same way you did when i did that exercise

#9

Nice explanation, it's quite helpful; but can you (or someone else here explain how (len(to_21) * 2 / 3) gets you to identifying the end of the middle third? I'm just not getting my brain to catch up on that part.