Iterating in a closed range [a, b] in python - python

I want to iterate over a closed range of integers [a, b] in python, ie. iterating from a to b including both a and b.
I know the following way of doing it:
for i in range(a, b+1):
do_something(i)
For iterating in the reverse direction (ie. in the order b, b-1, b-2, ..., a), I do the following:
for i in range(b, a-1, -1):
do_something(i)
I don't like this addition (b+1 in the example) and subtraction (a-1 in the example) to reach the closed end of the range. I find it less readable than the c/c++/Java counterpart (usage of <= in a loop).
Do you have something in python which can be used to iterate between the closed ranges without manual intervention of the boundaries?

It's a simple matter to define your own function and use it:
def closed_range(start, stop, step=1):
dir = 1 if (step > 0) else -1
return range(start, stop + dir, step):
In action:
>>> list(closed_range(1, 10))
0: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(closed_range(1, 10, 2))
1: [1, 3, 5, 7, 9]
>>> list(closed_range(1, 10, 3))
2: [1, 4, 7, 10]
>>> list(closed_range(10, 1, -1))
3: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> list(closed_range(10, 1, -2))
4: [10, 8, 6, 4, 2]
Save to a .py file in \PythonXX\Lib\site-packages and then you can import it for use elsewhere.

Related

Return a list of Element by discarding the Sequential Occurrence of Elements

i/p 1:
test_list = [1, 1, 3, 4, 4, 4, 5,6, 6, 7, 8, 8, 6]
o/p
[3, 5, 7, 6]
Exp: Since (1 1), (4 4 4) (6 6) (8 8) are in consecutive occurrence so resultant list has no addition of 6 but for last occurrence where 8, 6 are not in multiple consecutive occurrence so 6 is valid
in last iteration
i/p 2:
test_list = [1, 1, 3, 4, 4, 4, 5,4,6, 6, 7, 8, 8, 6]
o/p
[3, 5,4, 7, 6]
** like wise for 2nd input 4,4,4 is not valid but 5,4 is valid
Any suggestion for the expected o/p?
(I am looking for bit elaborated algorithm)
You can use itertools.groupby to group adjacent identical values, then only keep values that have group length of 1.
>>> from itertools import groupby
>>> test_list = [1, 1, 3, 4, 4, 4, 5,6, 6, 7, 8, 8, 6]
>>> [k for k, g in groupby(test_list) if len(list(g)) == 1]
[3, 5, 7, 6]
>>> test_list = [1, 1, 3, 4, 4, 4, 5,4,6, 6, 7, 8, 8, 6]
>>> [k for k, g in groupby(test_list) if len(list(g)) == 1]
[3, 5, 4, 7, 6]
First of all, you need to know that increasing i in your for loop does not change the value of i.
You can check it by runin this code:
for i in range(5):
print(i)
i = 2
This code will print 0 1 2 3 4 not 0 2 2 2 2 as you might think.
Going back to your question. I would use groupby from itertools, but since you specified you don't want to use it, I would do something like this:
if test_list[0] != test_list[1]: # <-- check if first element should belong to result
res_list.append(test_list[0])
for i in range(len(test_list[1:-1])): # Here we use input list, but without first and last element.
if test_list[i+1] == test_list[i+2] or test_list[i+1] == test_list[i]:
continue
else:
res_list.append(test_list[i+1])
if test_list[-2] != test_list[-1]: # <-- check if last element should belong to result
res_list.append(test_list[-1])

How to make ascending sublists in a list of integers go in descending order?

Working on some example questions, the particular one asks to make a function which would take a list and return a new one which would make every ascending sublist in the list go in descending order and leave the descending sublists as they are. For example, given the list [1,2,3,4,5], I need the list [5,4,3,2,1] or given a list like [1,2,3,5,4,6,7,9,8] would return [5,3,2,1,9,7,6,4,8]
Here's what I have so far, but it does not do anything close to what I'd like it to do:
def example3(items):
sublst = list()
for i in items:
current_element = [i]
next_element = [i+1]
if next_element > current_element:
sublst = items.reverse()
else:
return items
return sublst
print (example3([1,2,3,2])) #[[1, 2, 3, 2], [1, 2, 3, 2], [1, 2, 3, 2], [1, 2, 3, 2]]
EDIT:
I feel like people are a little confused as to what I want to do in this case, heres a better example of what I'd like my function to do. Given a list like: [5, 7, 10, 4, 2, 7, 8, 1, 3] I would like it to return [10, 7, 5, 4, 8, 7, 2, 3, 1]. As you can see all the sublists that are in descending order such as ([5,7,10]) gets reversed to [10, 7, 5].
It was a bit challenging to figure out what you need.
I think you want something like as follows:
import random
l = [5, 7, 10, 4, 2, 7, 8, 1, 3]
bl =[]
while True:
if len(l) == 0:
break
r = random.randint(0, len(l))
bl.extend(l[r:None:-1])
l = l[r+1:]
print(bl)
Out1:
[10, 7, 5, 4, 8, 7, 2, 3, 1]
Out2:
[10, 7, 5, 2, 4, 1, 8, 7, 3]
Out3:
[3, 1, 8, 7, 2, 4, 10, 7, 5]
Out4:
[2, 4, 10, 7, 5, 3, 1, 8, 7]
etc.
If you want a specific reverse random list:
import random
loop_number = 0
while True:
l = [5, 7, 10, 4, 2, 7, 8, 1, 3]
bl =[]
while True:
if len(l) == 0:
break
r = random.randint(0, len(l))
bl.extend(l[r:None:-1])
l = l[r+1:]
loop_number += 1
if bl == [10, 7, 5, 4, 8, 7, 2, 3, 1]:
print(bl)
print("I tried {} times".format(loop_number))
break
Out:
[10, 7, 5, 4, 8, 7, 2, 3, 1]
I tried 336 times
The general algorithm is to keep track of the current ascending sublist you are processing using 2 pointers, perhaps a "start" and "curr" pointer. curr iterates over each element of the list. As long as the current element is greater than the previous element, you have an ascending sublist, and you move curr to the next number. If the curr number is less than the previous number, you know your ascending sublist has ended, so you collect all numbers from start to curr - 1 (because array[curr] is less than array[curr - 1] so it can't be part of the ascending sublist) and reverse them. You then set start = curr before incrementing curr.
You will have to deal with the details of the most efficient way of reversing them, as well as the edge cases with the pointers like what should the initial value of start be, as well as how to deal with the case that the current ascending sublist extends past the end of the array. But the above paragraph should be sufficient in getting you to think in the right direction.

Constructive Insertion Sort

I having trouble with insertion sort and I feel I might be missing the point of the sort and misunderstanding the fundamentals.
We were given a insertion sort which edited the array which was fed into it. We are tasked with then modifying the code we have been given to then create a constructive insertion sort which will not edit the original array
This is the code I have so far
def append(A, x):
return A + [x]
def insertionSort(A):
B = []
B = append(B, A[0])
for i in range(1, len(A)):
B = insert(B, A[i], i, A)
return str(B)
def insert(B, k, hi, A):
print(B, k, hi)
for x in range(hi-1, -1, -1):
if B[x] <= k:
B = append(B, k)
return B
B = append(B, A[x])
B[0] = k
return B
print(insertionSort([2,4,6,8,10,1,3,5,7,9]))
However after the third or forth element in the list it begins adding all the items to the end of the list in reverse order
[2] 4 1
[2, 4] 6 2
[2, 4, 6] 8 3
[2, 4, 6, 8] 10 4
[2, 4, 6, 8, 10] 1 5
[1, 4, 6, 8, 10, 10, 8, 6, 4, 2] 3 6
[1, 4, 6, 8, 10, 10, 8, 6, 4, 2, 1, 10, 8, 6, 4, 3] 5 7
[1, 4, 6, 8, 10, 10, 8, 6, 4, 2, 1, 10, 8, 6, 4, 3, 3, 1, 10, 8, 6, 5] 7 8
[1, 4, 6, 8, 10, 10, 8, 6, 4, 2, 1, 10, 8, 6, 4, 3, 3, 1, 10, 8, 6, 5, 7] 9 9
[1, 4, 6, 8, 10, 10, 8, 6, 4, 2, 1, 10, 8, 6, 4, 3, 3, 1, 10, 8, 6, 5, 7, 9]
I cannot wrap my head around why this is wrong.
Thanks dearly to anyone who can help.
The reverse problem is at the foor loop in the insert function
when your loop hit those values it starts the reverse mode
def insert(B, k, hi, A):
# when hi=5
for x in range(hi-1, -1, -1):
# x = 4
# here B[4] is 10 and k=1 so B[4] <= 1 is False
# you program does not execute the inside of if
# instead it jumps to B = append(B, A[x]) where A[4] == 10
# and the this loop goes in reverse mode from 4 to 0
# when x = 3
# B[x] = 8 so 8 is not less or equal of k where k = 1
# so it jumps again to B = append(B, A[x]) where A[x] = A[3] = 8
# so it append 8
# and so on
# when this loop is finished your list will look like [1,4,6,8,10,10,8,6,4,2]
# the 1 gets added when the loop is finished at B[0] = k
# and then rest of the outputs are result of the loop inside the insertionSort func
if B[x] <= k:
B = append(B, k)
return B
B = append(B, A[x])
B[0] = k
return B
Here is a solution:
def insertionSort(A):
copy_sort = A.copy()
for i in range(1, len(copy_sort)):
item = copy_sort[i]
j = i - 1
while j >= 0 and copy_sort[j] > item:
copy_sort[j + 1] = copy_sort[j]
j -= 1
copy_sort[j + 1] = item
return copy_sort
your_array = [2,4,6,8,10,1,3,5,7,9]
sorted = insertionSort(your_array)
print(your_array)
print(sorted)
You need to work out your algorithm on paper, and then translate those steps to Python code. What you've implemented is convoluted and incorrect.
Most of all, insert is very confused as to the information it needs and how it should do its job. As best I can see from your code, you want this routine to insert a given value k into the appropriate location in list B. For some reason, you've also passed in list A and the value's location in that list, neither of which is applicable.
What your routine does then is strange; starting from the end of B (using i instead of B itself), the code checks the elements of B; every time it finds a value in the list less than the new one, it appends the new one to the end of B. Regardless of that comparison, it appends the corresponding element of A to B.
Nowhere do you insert the element in the proper place.
Rewrite this code. Start with the minimum necessary information:
def insert(arr, new_val):
# insert new_val into the list arr
Now, your function has two steps to carry out:
Find the proper position for new_val
Make a new list with the value inserted into that spot.
You return that new list.
Can you move on from there?

Reverse a slice of an array in-place [duplicate]

This question already has answers here:
How do I reverse a part (slice) of a list in Python?
(8 answers)
Reverse part of an array using NumPy
(3 answers)
Closed 3 years ago.
What is the best (fastest/most pythonic) way to reverse a part of an array in-place?
E.g.,
def reverse_loop(l,a,b):
while a < b:
l[a],l[b] = l[b],l[a]
a += 1
b -= 1
now after
l = list(range(10))
reverse_loop(l,2,6)
l is [0, 1, 6, 5, 4, 3, 2, 7, 8, 9] as desired.
Alas, looping in Python is inefficient, so a better way is needed, e.g.,
def reverse_slice(l,a,b):
l[a:b+1] = l[b:a-1:-1]
and reverse_slice(l,2,6) restores l to its original value.
Alas, this does not work for the border cases: reverse_slice(l,0,6)
truncates l to [7, 8, 9] because l[a:-1:-1] should be l[a::-1].
So, what is The Right Way?
How about this?
def reverse_slice(l, a, b):
l[a:b] = l[a:b][::-1]
l = list(range(10))
reverse_slice(l, 0, 6) # excludes l[6]
print(l)
Output:
[5, 4, 3, 2, 1, 0, 6, 7, 8, 9]
An alternative with the inbuilt function reversed:
def reverse_func(l, a, b):
l[a:b] = reversed(l[a:b])
In my tests, slicing is faster than using reversed by a factor of 1.2x-1.5x.
[6::-1] can be written as [6:None:-1]:
def reverse_slice(l,a,b):
a1 = None if a==0 else a-1
l[a:b+1] = l[b:a1:-1]
In [164]: y=x.copy(); reverse_slice(y,1,6);y
Out[164]: [0, 6, 5, 4, 3, 2, 1, 7, 8, 9]
In [165]: y=x.copy(); reverse_slice(y,0,6);y
Out[165]: [6, 5, 4, 3, 2, 1, 0, 7, 8, 9]

swapping one number with two numbers in a list in python

If I am given a list of numbers and I want to swap one of them with the next two numbers.
Is there a way to do this in one shot, without swapping the first number twice?
To be more specific, let's say I have the following swap function:
def swap_number(list, index):
'''Swap a number at the given index with the number that follows it.
Precondition: the position of the number being asked to swap cannot be the last
or the second last'''
if index != ((len(list) - 2) and (len(list) - 1)):
temp = list[index]
list[index] = list[index+1]
list[index+1] = temp
Now, how do I use this function to swap a number with the next two numbers, without calling swap on the number twice.
For example: I have the following list: list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Now, how do I swap 3 with the 4 and 5 in one shot?
The expected output would be
list = [0, 1, 2, 4, 5, 3, 6, 7, 8, 9]
Something like this?
def swap(lis, ind):
lis.insert(ind+2, lis.pop(ind)) #in-place operation, returns `None`
return lis
>>> lis = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lis = swap(lis, 3)
>>> lis
[0, 1, 2, 4, 5, 3, 6, 7, 8, 9]

Categories

Resources