Duplicate problems with Selection Sort - python

If I have a list where all values are unique, the code runs fine. However, if there is a duplicate value within the list, when finding the minimum value for the next iteration, it pulls from the entire list rather than just the remainder of the list.
for n in range(0,len(lst)):
a = min(lst[n:]) #minimum value within remainder of set
i = lst.index(a) #index value for minimum value within remainder of set
temp = lst[n]
lst[n] = a
lst[i] = temp
Results look like this:
lst = [6, 8, 9, 1, 3, 4, 7, 5, 4]
[1, 8, 9, 6, 3, 4, 7, 5, 4]
[1, 3, 9, 6, 8, 4, 7, 5, 4]
[1, 3, 4, 6, 8, 9, 7, 5, 4]
[1, 3, 6, 4, 8, 9, 7, 5, 4]
[1, 3, 6, 8, 4, 9, 7, 5, 4]
[1, 3, 6, 8, 9, 4, 7, 5, 4]
[1, 3, 6, 8, 9, 7, 4, 5, 4]
[1, 3, 6, 8, 9, 7, 5, 4, 4]
[1, 3, 6, 8, 9, 7, 5, 4, 4]
I'm looking for it to return this:
[1, 3, 4, 4, 5, 6, 7, 8, 9]

When n is 4, the next minimum is 4 again, but lst.index() finds the first 4 at position 3 instead.
Start searching for the miminum from n; the .index() method takes a second argument start, from where to start searching:
i = lst.index(a, n)
Note that Python can assign to two targets in place, no need to use a temporary intermediate. range() with just one argument starts from 0:
for n in range(len(lst)):
a = min(lst[n:])
i = lst.index(a, n)
lst[n], lst[i] = a, lst[n]
Demo:
>>> lst = [6, 8, 9, 1, 3, 4, 7, 5, 4]
>>> for n in range(0,len(lst)):
... a = min(lst[n:])
... i = lst.index(a, n)
... lst[n], lst[i] = a, lst[n]
...
>>> lst
[1, 3, 4, 4, 5, 6, 7, 8, 9]

Related

How can I make this python function generate such a list [[1], [1, 2]...[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]?

all = []
def generate(i, current):
if i < 11:
current.append(i)
all.append(current)
i+= 1
generate(i, current)
generate(1, [])
print(all)
I want this function to generate
[[1], [1, 2]...[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
instead of
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]],
but don't know how to fix it.
Do you know the solution?
Here's my go:
def listGen(start, stop):
res = []
for i in range(start, stop+1):
res.append([x for x in range(start, i+1)])
return res
You could also simplify this to:
def listGen(start, stop):
return [[x for x in range(start, i+1)] for i in range(start, stop+1)]
Input: print(listGen(1, 10))
Output: [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
def generate_array():
result = []
for i in range(1, 11):
current_array = []
for j in range(1, i + 1):
current_array.append(j)
result.append(current_array)
return result
print(generate_array())
The code uses two nested for loops, where the outer loop iterates over range(1, 11) and the inner loop iterates over range(1, i + 1). The values of i and j are used to generate the sublists and append them to the result list, which is returned at the end of the function.
The core issue you have is that when you do:
all.append(current)
current is the exact same list all over the place so when you append to it in the prior line you effectively append to it everywhere. To fix that and the lightest change to your code you would append to copy of it.:
all = []
def generate(i, current):
if i < 11:
current.append(i)
all.append(current.copy()) ## <--- append a copy
i+= 1
generate(i, current)
generate(1, [])
print(all)
alternatively you could pass a copy like:
all = []
def generate(i, current):
if i < 11:
current.append(i)
all.append(current)
i+= 1
generate(i, current.copy()) ## <--- pass a copy
generate(1, [])
print(all)
In either case, the important part is that we get a distinct current to work with.
Note that the use of all as a variable clobbers the function all() and you might not want to do that. As I'm sure lots of others will point out, there are many ways to skin this cat.

New line in List

Lets say I have a list called l1:
l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
And I want to print it as
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
How would I do that in python on 3.9
I tried to us a \n, but that has not worked for me.
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list[:5])
print(list[5:])
outputs:
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
if you want them to be printed on the same line then you can use:
print(list[:5],list[5:])
which will return:
[1, 2, 3, 4, 5] [6, 7, 8, 9, 10]
Slicing would probably work for you.
l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(l1[:5]) # print first half
print(l1[5:]) # print second half
This should print both halves on separate lines, which is what I think you're trying to do with \n?
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]

Mutually Exclusive Lists Python

I'm trying to simplify this code so it doesn't use two for loops. The aim is to end up with a list of numbers that exist in one list but not the other ie. mutually exclusive.
Here is the code:
list1 = [1, 1, 2, 4, 6, 6, 9]
list2 = [1, 2, 3, 5, 6, 7, 8]
def mutually_exclusive(list1,list2):
new_list = []
for x in list1:
if x not in list2:
new_list.append(x)
else:
pass
for y in list2:
if y not in list1:
new_list.append(y)
else:
pass
return new_list
mutually_exclusive(list1,list2)
and the desired result:
[4, 9, 3, 5, 7, 8]
any help much appreciated thanks.
I have tried zip but doesn't yield all results.
You could also do it like this:
list1 = [1, 1, 2, 4, 6, 6, 9]
list2 = [1, 2, 3, 5, 6, 7, 8]
def mutually_exclusive(list1,list2):
return list(set(list1)^set(list2))
print(mutually_exclusive(list1, list2))
Result:
[3, 4, 5, 7, 8, 9]
You can do the following using symmetric_difference:
l1 = [1, 1, 2, 4, 6, 6, 9]
l2 = [1, 2, 3, 5, 6, 7, 8]
list(set(l1).symmetric_difference(set(l2)))
In [7]: l1
Out[7]: [1, 1, 2, 4, 6, 6, 9]
In [8]: l2
Out[8]: [1, 2, 3, 5, 6, 7, 8]
In [9]: list(set(l1).symmetric_difference(set(l2)))
Out[9]: [3, 4, 5, 7, 8, 9]

Trying to increase item in a python list to get all options

I'm trying to take a list of numbers and increase them by a certain amount(1 in my example) up to a certain range(5 in my example) but for some reason it's not doing it the way I was expecting:
Given this list:
[1, 5, 7, 9, 3]
I expect:
[1, 5, 7, 9, 3]
[2, 5, 7, 9, 3]
[3, 5, 7, 9, 3]
[4, 5, 7, 9, 3]
[5, 5, 7, 9, 3]
[1, 6, 7, 9, 3]
[2, 7, 7, 9, 3]
[3, 8, 7, 9, 3]
[4, 9, 7, 9, 3]
[5, 10, 7, 9, 3]
... and so on
Here's my code:
# create list
list_of_nums = [1, 5, 7, 9, 3]
# variable of increase
increase_range = 5
range_per_increase = 1
for idx, i in enumerate(list_of_nums):
current_item = i
while current_item < i+increase_range:
copy_of_list = list_of_nums
current_item = current_item+range_per_increase
copy_of_list[idx] = current_item
print(copy_of_list)
break
Output:
[4, 7, 9, 11, 5]
[4, 8, 9, 11, 5]
[4, 8, 10, 11, 5]
[4, 8, 10, 12, 5]
[4, 8, 10, 12, 6]
What am I doing wrong or is there a easier way to get the output I want?
Update: I think my above example I get incrementals for each item then move on, but is it possible to get all combinations so the first item and the others item are incremented. I want all combinations for the lists within each numeric range while preserving the order.
There is an easier way to get this. The built in itertools.product method can do most of the work.
from itertools import product
list_of_nums = [1, 5, 7, 9, 3]
increase_range = 5
range_per_increase = 1
choices_list = []
for num in list_of_nums:
choices_list.append([])
currentAdder = num
while currentAdder < num + increase_range:
choices_list[-1].append(currentAdder)
currentAdder += range_per_increase
print(choices_list)
# Prints [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], ...] with all the options for each item.
print("\n\n\n")
for nums in product(*choices_list): # Unpack choices_list into product
print(list(nums)) # Since each output should be a list, convert from tuple to list

Need help understanding bubble sort solution code

So, I was learning about Bubble Sort and saw this code:
def bubblesort(list_a):
indexing_length = len(list_a) - 1
sorted = False
while not sorted:
sorted = True
for i in range(0, indexing_length):
if list_a[i] > list_a[i+1]:
sorted = False
list_a[i], list_a[i+1] = list_a[i+1], list_a[i] # flip position
return list_a
I tried running this code on my notebook so I could understand this better. I tried 5, 2, 3, 7, 10, 1, 8 and the process was like this:
--> 2, 5, 3, 7, 10, 1, 8
--> 2, 3, 5, 7, 10, 1, 8
--> 2, 3, 5, 7, 1, 10, 8
--> 2, 3, 5, 7, 1, 8, 10
I ended up with unsorted array because I thought for loop does only one iteration. Am I understanding something wrong? Could anyone explain it to me little bit easier please?
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. This sorting algorithm also known as Brute Force Approach, the reason is that during sorting each element of list will compare with the every other element of the same list and if the compared number are not in order so it will swap the position. Lets have a look on your example list [5, 2, 3, 7, 10, 1, 8].
1st pass: compare 0th index(5) and 1st index(2) and compare it
if 0th index(5) > 1st index(2) then it will swap the position else no swap counter will increase. [5, 2, 3, 7, 10, 1, 8] condition True(5 > 2), SWAP [2, 5, 3, 7, 10, 1, 8], then again compare [2, 5, 3, 7, 10, 1, 8] condition True(5 > 3), SWAP [2, 3, 5, 7, 10, 1, 8], and so on.
[2, 3, 5, 7, 10, 1, 8] True(10>1).
[2, 3, 5, 7, 1, 10, 8] True(10>8).
[2, 3, 5, 7, 1, 8, 10]
2nd pass:
[2, 3, 5, 7, 1, 8, 10]
[2, 3, 5, 7, 1, 8, 10]
[2, 3, 5, 7, 1, 8, 10] True(7>1).
[2, 3, 5, 1, 7, 8, 10]
[2, 3, 5, 1, 7, 8, 10]
3rd pass:
[2, 3, 5, 1, 7, 8, 10]
[2, 3, 5, 1, 7, 8, 10] True(5>1).
[2, 3, 1, 5, 7, 8, 10]
[2, 3, 1, 5, 7, 8, 10]
4th pass:
[2, 3, 1, 5, 7, 8, 10] True(3>1).
[2, 1, 3, 5, 7, 8, 10]
[2, 1, 3, 5, 7, 8, 10]
5th pass:
[2, 1, 3, 5, 7, 8, 10] True(2>1).
[1, 2, 3, 5, 7, 8, 10]
[1, 2, 3, 5, 7, 8, 10]
6th pass:
[1, 2, 3, 5, 7, 8, 10]
7th pass:
[1, 2, 3, 5, 7, 8, 10]
def bubbleSort(arr):
n = len(arr)
for i in range(n):
print(arr[i])
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
print(arr[j], arr[j+1])
arr[j], arr[j + 1] = arr[j + 1], arr[j]
print(arr)
arr = [5, 2, 3, 7, 10, 1, 8]
bubbleSort(arr)
print("Sorted array")
print(arr)
The list will be [2, 3, 5, 7, 1, 8, 10] after the for loop completes the first time, but the value of sorted will be False. Since you're still inside the while not sorted loop, everything inside that loop will run again, including another for loop starting at index 0.
This will keep going until the list is sorted when the for loop completes, since that will make the while loop's condition False.

Categories