I have the following list of integers:
[[0, 2, 3, 1, 3, 2, 0, 1],
[0, 3, 2, 1, 2, 3, 0, 1],
[1, 2, 3, 0, 3, 2, 1, 0],
[2, 1, 3, 0, 3, 1, 2, 0]]
Taking this whole list as a population and each sublist inside as an individual, like this example:
Population scheme
I need to create a function that will read the individuals and randomly mutate one of the chromosomes with a certain probability, taking into consideration that the numbers in the list can be only in this 0-3 range.
Would anyone know any method, or whatever approach to start developing this? I am completely lost and don't know where to begin, everything I tried has failed, so I am looking for suggestion how would one do it.
from random import randint, uniform;
def mutateIndividual(ind):
if uniform(0,1) < prob: # random probability of mutation
mutationIndex = randint(0, len(ind)) # select one chromosome
ind[mutationIndex] = randint(0,3) # mutate
return ind;
for i in range(0, len(population)): # outer loop on each individual
population[i] = mutateIndividual(population[i]);
Exercise: You may want to modify the program to mutate a chromosome to something different than what it already is.
Related
I'm trying to understand what I'm missing in these two different methods. In the code below, I'm defining a list of lists, and calling it two ways. The first way is to call it in a print statement as one line, separating the index positions of the lists with commas, the second is to print them all separately.
list_of_lists = [[2, 2, 0], [2, 1, 0], [2, 1, 1]]
print(list_of_lists[0][0],[1][0],[2][0])
print('\n')
The output of this code is 2,1,2
print(winner_is_2[0][0])
print(winner_is_2[1][0])
print(winner_is_2[2][0])
The output of this code is 2,2,2
Why, in the first case, does the call output the wrong value for the second set of index positions?
The use case here is trying to check if a set of index positions in a list of lists is all equal to the same value (like you'd see in tic-tac-toe where you'd check if all the values in row x or column y are the same)
NOTE: I know I can use list extraction to get all the values at X position in these sublists, but I'd rather not use that method if possible.
In this snippet here:
list_of_lists = [[2, 2, 0], [2, 1, 0], [2, 1, 1]]
print(list_of_lists[0][0],[1][0],[2][0])
print('\n')
This is actually equivalent to:
list_of_lists = [[2, 2, 0], [2, 1, 0], [2, 1, 1]]
print(list_of_lists[0][0])
print([1][0])
print([2][0])
Python is interpreting the comma separated inputs as separate arrays [1] and [2] indexed by their first value [1][0] and [2][0] which leads to them being printed as 1,2
To see further evidence of this, try changing the values of 1 and 2 and seeing what you get by doing:
list_of_lists = [[2, 2, 0], [2, 1, 0], [2, 1, 1]]
print(list_of_lists[0][0],[5555][0],[262626][0])
print('\n')
Here you should get (2, 5555, 262626)
list_of_lists = [[2, 2, 0], [2, 1, 0], [2, 1, 1]]
print(list_of_lists[0][0], list_of_lists[1][0], list_of_lists[2][0])
print('\n')
Whats wrong with this?
What you trying is equivalent to:
print(list_of_lists[0][0], 1, 2)
So after a little more digging, thanks to Spencer Bard for explaining the syntax mistake, I was able to come up with code that works for precisely what I was trying to do. As I'm attempting to test a marker against each item at specific index positions within a list, the code that worked for me was as follows.
def win_check(marker_list,mark):
if (marker_list[0][0] == mark and marker_list[1][0] == mark and marker_list[2][0] == mark):
print(f'Winner is player {mark}!')
There are a lot more elif statements underneath that to account for the rest of the possibilities, but this is the general format.
I have multiple sets of numbers, like so:
Set 1: 1,2,3,5,4,3,2,1
Set 2: 1,2,1,1
Set 3: 1,1,1,0
Set 4: 1,9
The number of sets will be specified by the user, and also how many numbers each set may contain. Then, the program will randomly populate the values.
Right now, I am using a 2D array with dimensions
[maximum(set-sizes)][maximum(elements_in_set)]
and setting the unused cells with a large negative number to indicate that they are not of use.
Although this serves my purpose, I was wondering if there is a better data structure that I can use.
You can simply create a list of lists:
>>> numbers = [
... [1, 2, 3, 5, 4, 3, 2, 1],
... [1, 2, 1, 1],
... [1, 1, 1, 0],
... [1, 9]
... ]
>>> numbers[3]
[1, 9]
Note that what you describe are not sets, because they have no order and can contain every element only once.
I know this seems like such a simple question, but when I go to remove the last element of my vector it reorders my vector and won't keep 0 as the last element.
vec = [1, 1, 0, 1]
vec.remove(vec[3])
The remaining vec is [1, 0, 1] when I wanted it to stay in order as [1, 1, 0]
Thanks!
vec = [1, 1, 0, 1]
vec.remove(vec[3])
vec[3] is 1, so you are removing the first element whose value is 1, i.e., the first element. The remaining elements are [1, 0, 1]. No reordering has been done. This would have been obvious if you had used a wider variety of values in your list.
You want one of these:
vec.pop(3)
del vec[3]
This question already has answers here:
recursively implementing 'minimum number of coins' in python
(6 answers)
Closed 7 years ago.
I was given a set of problems by my instructor to get us thinking about recursion. In one of them, we are given a value (the cost of postage) and a list of available denominations of stamps. The exercise involves writing a recursive function that returns a minimum length list of stamps whose total is equal to the value sum of postage.
Is the correct way to solve this just to have the a program that compares every single possibility and return the one with the smallest length? If that's the case I'm not exactly sure how to write a program for it, much less one that utilizes recursion as I am new to both python and programming in general. Based on the hints the instructor provided I came up with something like this:
stampList=[1,4,7]
postage=10
def pickStamps(postage):
if postage==0: #base case 1
return ""
if postage<0: #base case 2
return none
else:
x=postage
for i in range(len(stampList)-1):
x=x-stampList[i]
return pickStamps(x)
I was attempting to have python start with the value of postage and subtract each denomination in combination to get to zero,but I'm not sure how to make each possibility into a list. It was suggested in the problem that it might be wise to write another function that takes a parameter that is a list of lists and returns the index of the minimum length element in that list, but I'm not sure how to implement that. Can someone either show me how to write such a code or explain the best way to approach such a problem? Thanks!
def ways(wallet, stamp_values, postage):
amount = sum(wallet)
if amount == postage:
return [wallet]
elif amount > postage:
return []
else:
next_stamp = wallet[-1] if wallet else max(stamp_values)
new_stamps = stamp_values[stamp_values.index(next_stamp):]
gen = (ways(wallet + [c], new_stamps, postage=postage) for c in new_stamps)
return sum(gen, [])
Test drive:
>>> combos = ways([], stamp_values=(7,4,1), postage=10)
>>> combos
[[7, 1, 1, 1],
[4, 4, 1, 1],
[4, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
>>> min(combos, key=len)
[7, 1, 1, 1]
Note the solution is actually non-unique in your example, i.e. 7,1,1,1 and 4,4,1,1 are both the same length.
Let's imagine we have a new stamp of value 3, we should expect to see a unique solution then (7 + 3 == 10 with two stamps).
>>> ways([], stamp_values=(7,4,3,1), postage=10)
[[7, 3],
[7, 1, 1, 1],
[4, 4, 1, 1],
[4, 3, 3],
[4, 3, 1, 1, 1],
[4, 1, 1, 1, 1, 1, 1],
[3, 3, 3, 1],
[3, 3, 1, 1, 1, 1],
[3, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
Imagine you have a function which already works:
def pick_stamps(postage):
'''Returns list of stamps needed to cover postage'''
Given you have this function, you can use it to implement your version:
def my_pick_stamps(postage):
if postage == 0:
return []
else:
stamp = max(stamp for stamp in stamps
if stamp <= postage)
return [stamp] + pick_stamps(postage - stamp)
Of course, once you've implemented your version you can use that instead and replace pick_stamps in your implementation with my_pick_stamps.
I am not looking to get an list inside list instead I just want to add zeros but do not know how. see code
def myconv(x,h):
q=[]
print(h)
for i in range(len_h):
q.append(h[len_h-1-i])
print(q)
q.insert(0,([0]*len_h)) #I want this to add just plain zeros to the array... not an array insdie an
#like it is doing here
q.insert(len(q)+1,(0]*len_h))
print(q)
print(myconv([6,7,8,9],[1,2,3,4,5,6,7,8,9,0]))
You want to use +, e.g. [0, 0, 0] + [2, 1, 3] == [0, 0, 0, 2, 1, 3], to concatenate an array onto the first. Otherwise, you'll need to insert (or append) items one at a time.
To demonstrate how list multiplication works:
>>> [0]*2
[0, 0]
>>> [0]*5
[0, 0, 0, 0, 0]
I prefer the in-place extend operation. Just don't assign to it, because like most all Python in-place methods, it returns None.
>>> l2.extend([0]*5)
>>> l2
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0]
You have two ways of doing this:
q = [0]*len_h + q
or
q[0:0] = [0]*len_h
q.insert(0,([0]*len_h))
This creates a list within a list because [0]*len_h creates a new list. This array is then inserted into q. Instead, insert each element into q without creating a new list.
for i in range(len_h):
q.insert(0, 0)