I am making new program in Python (Mastermind). I have a problem with references of variables:
def user_turn():
try_counter = 1
user_code = []
guessed_code = random_code()
print("Twoja kolej na zgadywanie!")
while try_counter <= max_tries and user_code != guessed_code:
good_number, good_number_and_position = 0, 0
appearance_types_guessing_code = [0 for i in range(types)]
appearance_types_user_code = [0 for i in range(types)]
user_code = input("Próba nr {}: ".format(try_counter))
user_code = list(map(int, str(user_code)))
count_xos()
print_xos()
try_counter += 1
print_result_user_turn()
Body of the function print_xos():
def print_xos():
for i in range(good_number_and_position):
print("x", end='')
for i in range(good_number):
print("o", end='')
print("")
And my problem is that in function print_xos() variables good_number and good_number_and_position are unknown, despite of fact I declared this variables in the while loop in the body of the function user_turn(). How can I solve this problem? I don't want to send the reference as an argument of the function. In my opinion it isn't elegant. Is it possible to do it in another way?
EDIT:
OK, I changed a code a little bit then:
def user_turn():
try_counter = 1
user_code = []
guessed_code = random_code()
appearance_types_guessed_code = [0] * types
how_many_appearance(guessed_code, appearance_types_guessed_code)
print("Twoja kolej na zgadywanie!")
while try_counter <= max_tries and user_code != guessed_code:
good_number, good_number_and_position = 0, 0
appearance_types_user_code = [0] * types
user_code = input("Próba nr {}: ".format(try_counter))
user_code = list(map(int, str(user_code)))
how_many_appearance(user_code, appearance_types_user_code)
print(appearance_types_guessed_code, appearance_types_user_code)
count_xos(guessed_code, appearance_types_guessed_code, user_code, appearance_types_user_code, good_number, good_number_and_position)
print(good_number_and_position, good_number)
print_xos(good_number_and_position, good_number)
try_counter += 1
print_result_user_turn(guessed_code, user_code)
And the body of function count_xos:
def count_xos(guessed_code, appearance_types_guessed_code, user_code, appearance_types_user_code, good_number, good_number_and_position):
for i in range(len(appearance_types_guessed_code)):
good_number += np.min([appearance_types_guessed_code[i], appearance_types_user_code[i]])
for i in range(code_size):
if guessed_code[i] == user_code[i]:
good_number_and_position += 1
good_number -= 1
print(good_number_and_position, good_number)
And I got this output:
RUNDA 1
Twoja kolej na zgadywanie!
Próba nr 1: 0011
[0, 2, 0, 1, 0, 0, 0, 1, 0, 0] [2, 2, 0, 0, 0, 0, 0, 0, 0, 0]
1 1
0 0
You can be certain that function count_xos counts good_number, good_number_and_position counts properly. It should be 1 1, but I don't know why after running the method count_xos, variables good_number_and_position, good_number are not changed?
Your last attempt does not return the numbers so the provided numbers do not carry out into your calling function.
Your code does the equivalent of:
def one(aa,bb):
aa *= 2
bb *= 3
print("In function", aa, bb)
return aa, bb
a = 5
b = 11
one(a,b) # does not reassign returned values - similar to not return anything like yours
print(a,b)
Output:
In function 10 33
5 11
You need to return and reassign the values:
a,b = one(a,b) # reassign returns
print(a,b)
Output:
In function 10 33
10 33
Have a look at Scoping rules - it it best to keep the scope as small as possible and provide data to the function they need.
If you modify things inside function return its new values and reassign them - this is not deeded if you pass a list, they are mutable references and "autoupdate" because you operate through the ref on the data:
# same function used as above
a = 5
b = [11]
one(a,b)
print(a,b)
Output:
In function 10 [11, 11, 11]
5 [11, 11, 11]
If you take a look at the id()s of the variables you can see that altering aa will repoint the name aa to some other id - but a on the outside still points to the original one. Altering the list does not alter the reference-id - it alters the data the ref "points" to:
def one_ids(aa,bb):
print(id(aa),id(bb))
aa *= 3 # modify an integer
bb *= 3 # modify the list
print(id(aa),id(bb))
a = 5
b = [11]
print(id(a),id(b))
one_ids(a,b)
print(id(a),id(b))
Output:
139647789732288 139647790644808 # id of a,b
139647789732288 139647790644808 # id of aa,bb before changing
139647789732**6**08 139647790644808 # id of aa,bb after changing
139647789732288 139647790644808 # id of a,b
You can read further in Function changes list values and not variable values in Python - see if those explanaitions fit you better.
Related
I would like to do something like this:
x = 0
y = 3
TEST0 = 10
TEST1 = 20
TEST2 = 30
while x < y:
result = exec('TEST{}'.format(x))
print(result)
x += 1
And have the output:
10
20
30
Somehow convert TEST{variable} to the actual variable, or what is the way to do it?
Currently, I have result as:
None
None
None
Welcome to Python! What you need is a list:
x = 0
y = 3
TEST = [10, 20, 30]
while x < y:
result = TEST[x]
print(result)
x += 1
A list is created by putting the values between []. You access a particular element in the list by writing the name of the variable, followed by the index enclosed in []. Read more about lists here in the official tutorial.
Instead of the while loop with explicit indexing, it's nicer to use a for loop instead:
TEST = [10, 20, 30]
for element in TEST:
result = element
print(result)
I would recommend using a dictionary in most cases, however if you don't want to use a dictionary you can always use globals() or vars(). They work as follows:
global_var = 5
>> 5
print(globals()["global_var"])
>> 5
vars() works in the same way but at module scope.
In your case, do the following:
x = 0
y = 3
TEST0 = 10
TEST1 = 20
TEST2 = 30
while x < y:
result = globals()['TEST{}'.format(x)]
print(result)
x += 1
I am not sure of inverse is the proper name, but I think it is.
This example will clarify what I need:
I have a max height, 5 for example, and so height can range from 0 to 4. In this case we're talking integers, so the options are: 0, 1, 2, 3, 4.
What I need, given an input ranging from 0 up to (and including) 4, is to get the inverse number.
Example:
input: 3
output: 1
visual:
0 1 2 3 4
4 3 2 1 0
I know I can do it like this:
position_list = list(range(5))
index_list = position_list[::-1]
index = index_list[3]
But this will probably use unnecessary memory, and probably unnecessary cpu usage creating two lists. The lists will be deleted after these lines of code, and will recreated every time the code is ran (within method). I'd rather find a way not needing the lists at all.
What is an efficient way to achieve the same? (while still keeping the code readable for someone new to the code)
Isn't it just max - in...?
>>> MAX=4
>>> def calc(in_val):
... out_val = MAX - in_val
... print('%s -> %s' % ( in_val, out_val ))
...
>>> calc(3)
3 -> 1
>>> calc(1)
1 -> 3
You just need to subtract from the max:
def return_inverse(n, mx):
return mx - n
For the proposed example:
position_list = list(range(5))
mx = max(position_list)
[return_inverse(i, mx) for i in position_list]
# [4, 3, 2, 1, 0]
You have maximum heigth, let's call it max_h.
Your numbers are counted from 0, so they are in [0; max_h - 1]
You want to find the complementation number that becomes max_h in sum with input number
It is max_h - 1 - your_number:
max_height = 5
input_number = 2
for input_number in range(5):
print('IN:', input_number, 'OUT:', max_height - input_number - 1)
IN: 1 OUT: 3
IN: 2 OUT: 2
IN: 3 OUT: 1
IN: 4 OUT: 0
Simply compute the reverse index and then directly access the corresponding element.
n = 5
inp = 3
position_list = list(range(n))
position_list[n-1-inp]
# 1
You can just derive the index from the list's length and the desired position, to arrive at the "inverse":
position_list = list(range(5))
position = 3
inverse = position_list[len(position_list)-1-position]
And:
for i in position_list:
print(i, position_list[len(position_list)-1-i])
In this case, you can just have the output = 4-input. If it's just increments of 1 up to some number a simple operation like that should be enough. For example, if the max was 10 and the min was 5, then you could just do 9-input+5. The 9 can be replaced by the max-1 and the 5 can be replaced with the min.
So max-1-input+min
I have implemented a genetic algorithm in python 3, and have posted a question on code review with no answers yet, basically because my algorithm is running very slowly. By selectively commenting out different parts of my code, I have narrowed down the bottleneck to this section of code, the crossover algorithm:
def crossover(self, mum, dad):
"""Implements ordered crossover"""
size = len(mum.vertices)
# Choose random start/end position for crossover
alice, bob = [-1] * size, [-1] * size
start, end = sorted([random.randrange(size) for _ in range(2)])
# Replicate mum's sequence for alice, dad's sequence for bob
for i in range(start, end + 1):
alice[i] = mum.vertices[i]
bob[i] = dad.vertices[i]
# # Fill the remaining position with the other parents' entries
# current_dad_position, current_mum_position = 0, 0
#
# for i in chain(range(start), range(end + 1, size)):
#
# while dad.vertices[current_dad_position] in alice:
# current_dad_position += 1
#
# while mum.vertices[current_mum_position] in bob:
# current_mum_position += 1
#
# alice[i] = dad.vertices[current_dad_position]
# bob[i] = mum.vertices[current_mum_position]
#
# # Return twins
# return graph.Tour(self.g, alice), graph.Tour(self.g, bob)
return mum, dad
The part which is commented out makes my program runtime go from ~7 seconds to 5-6 minutes (I am running 5000 iterations of the GA). Is there any way this ordered crossover can be carried out more efficiently?
What the crossover function does
For those unfamiliar, I am implementing an order-based crossover (OX2). Given two arrays of consecutive integers (the parents), two random start/end positions are selected.
mum = 4 9 2 8 3 1 5 7 6
dad = 6 4 1 3 7 2 8 5 9
^ ^
start end
The two children then share the resulting slices:
child 1 = _ _ 2 8 3 1 _ _ _
child 2 = _ _ 1 3 7 2 _ _ _
^ ^
Now the remaining slots are filled in with the entries of the other parents in the order in which they appear, as long as repetitions are avoided. So since child 1 has their slice taken from mum, the rest of the entries are taken from dad. First we take 6, then 4, then next we take 7 (not taking 1 and 3 since they already appear in child 1 from mum), then 5, then 9. So
child 1 = 6 4 2 8 3 1 7 5 9
and similarly,
child 2 = 4 9 1 3 7 2 8 5 6
This is what I am implementing in the function.
I can only guess that your problem lies with the fact that your while-loop and the increment within is not limited to the actual size of the vertices vector, put a hard limit and test again:
while current_dad_position < size and dad.vertices[current_dad_position] in alice:
current_dad_position += 1
while current_mom_position < size and mum.vertices[current_mum_position] in bob:
current_mum_position += 1
I feel compelled to say that this might not necessarily result a unique solution, as I do not know how the how the algorithm, should behave if there are not enough unique unique singular vertices available to chose from because they violate your 'not from the other parent' restriction.
For anybody to test this out, I would recommend to complete your code with an simple example input and not commenting out the code in question, but rather to mark its BEGIN and END with comments.
Okay with the knowledge that the problem is uniquely solvable becuae of construction, here is how it should look like:
import random
import numpy as np
def crossover(mum, dad):
"""Implements ordered crossover"""
size = len(mum.vertices)
# Choose random start/end position for crossover
alice, bob = [-1] * size, [-1] * size
start, end = sorted([random.randrange(size) for _ in range(2)])
# Replicate mum's sequence for alice, dad's sequence for bob
alice_inherited = []
bob_inherited = []
for i in range(start, end + 1):
alice[i] = mum.vertices[i]
bob[i] = dad.vertices[i]
alice_inherited.append(mum.vertices[i])
bob_inherited.append(dad.vertices[i])
print(alice, bob)
#Fill the remaining position with the other parents' entries
current_dad_position, current_mum_position = 0, 0
fixed_pos = list(range(start, end + 1))
i = 0
while i < size:
if i in fixed_pos:
i += 1
continue
test_alice = alice[i]
if test_alice==-1: #to be filled
dad_trait = dad.vertices[current_dad_position]
while dad_trait in alice_inherited:
current_dad_position += 1
dad_trait = dad.vertices[current_dad_position]
alice[i] = dad_trait
alice_inherited.append(dad_trait)
#repeat block for bob and mom
i +=1
return alice, bob
with
class Mum():
def __init__(self):
self.vertices =[ 4, 9, 2, 8, 3, 1, 5, 7, 6 ]
class Dad():
def __init__(self):
self.vertices = [ 6 , 4 , 1 , 3 , 7 , 2 , 8 , 5 , 9 ]
mum = Mum()
dad = Dad()
a, b = crossover(mum, dad)
# a = [6, 4, 2, 8, 3, 1, 5, 7, 9]
line 14 is where my main problem is.i need to cycle through each item in the array and use it's index to determine whether or not it is a multiple of four so i can create proper spacing for binary numbers.
def decimalToBinary(hu):
bits = []
h = []
while hu > 0:
kla = hu%2
bits.append(kla)
hu = int(hu/2)
for i in reversed(bits):
h.append(i)
if len(h) <= 4:
print (''.join(map(str,h)))
else:
for j in range(len(h)):
h.index(1) = h.index(1)+1
if h.index % 4 != 0:
print (''.join(map(str,h)))
elif h.index % 4 == 0:
print (' '.join(map(str,h)))
decimalToBinary( 23 )
If what you're looking for is the index of the list from range(len(h)) in the for loop, then you can change that line to for idx,j in enumerate(range(len(h))): where idx is the index of the range.
This line h.index(1) = h.index(1)+1 is incorrect. Modified your function, so at least it executes and generates an output, but whether it is correct, i dont know. Anyway, hope it helps:
def decimalToBinary(hu):
bits = []
h = []
while hu > 0:
kla = hu%2
bits.append(kla)
hu = int(hu/2)
for i in reversed(bits):
h.append(i)
if len(h) <= 4:
print (''.join(map(str,h)))
else:
for j in range(len(h)):
h_index = h.index(1)+1 # use h_index variable instead of h.index(1)
if h_index % 4 != 0:
print (''.join(map(str,h)))
elif h_index % 4 == 0:
print (' '.join(map(str,h)))
decimalToBinary( 23 )
# get binary version to check your result against.
print(bin(23))
This results:
#outout from decimalToBinary
10111
10111
10111
10111
10111
#output from bin(23)
0b10111
You're trying to join the bits to string and separate them every 4 bits. You could modify your code with Marcin's correction (by replacing the syntax error line and do some other improvements), but I suggest doing it more "Pythonically".
Here's my version:
def decimalToBinary(hu):
bits = []
while hu > 0:
kla = hu%2
bits.append(kla)
hu = int(hu/2)
h = [''.join(map(str, bits[i:i+4])) for i in range(0,len(bits),4)]
bu = ' '.join(h)
print bu[::-1]
Explanation for the h assignment line:
range(0,len(bits),4): a list from 0 to length of bits with step = 4, eg. [0, 4, 8, ...]
[bits[i:i+4] for i in [0, 4, 8]: a list of lists whose element is every four elements from bits
eg. [ [1,0,1,0], [0,1,0,1] ...]
[''.join(map(str, bits[i:i+4])) for i in range(0,len(bits),4)]: convert the inner list to string
bu[::-1]: reverse the string
If you are learning Python, it's good to do your way. As #roippi pointed out,
for index, value in enumerate(h):
will give you access to both index and value of member of h in each loop.
To group 4 digits, I would do like this:
def decimalToBinary(num):
binary = str(bin(num))[2:][::-1]
index = 0
spaced = ''
while index + 4 < len(binary):
spaced += binary[index:index+4]+' '
index += 4
else:
spaced += binary[index:]
return spaced[::-1]
print decimalToBinary(23)
The result is:
1 0111
I want to toggle between two values in Python, that is, between 0 and 1.
For example, when I run a function the first time, it yields the number 0. Next time, it yields 1. Third time it's back to zero, and so on.
Sorry if this doesn't make sense, but does anyone know a way to do this?
Use itertools.cycle():
from itertools import cycle
myIterator = cycle(range(2))
myIterator.next() # or next(myIterator) which works in Python 3.x. Yields 0
myIterator.next() # or next(myIterator) which works in Python 3.x. Yields 1
# etc.
Note that if you need a more complicated cycle than [0, 1], this solution becomes much more attractive than the other ones posted here...
from itertools import cycle
mySmallSquareIterator = cycle(i*i for i in range(10))
# Will yield 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 1, 4, ...
You can accomplish that with a generator like this:
>>> def alternate():
... while True:
... yield 0
... yield 1
...
>>>
>>> alternator = alternate()
>>>
>>> alternator.next()
0
>>> alternator.next()
1
>>> alternator.next()
0
You can use the mod (%) operator.
count = 0 # initialize count once
then
count = (count + 1) % 2
will toggle the value of count between 0 and 1 each time this statement is executed. The advantage of this approach is that you can cycle through a sequence of values (if needed) from 0 - (n-1) where n is the value you use with your % operator. And this technique does not depend on any Python specific features/libraries.
e.g.
count = 0
for i in range(5):
count = (count + 1) % 2
print(count)
gives:
1
0
1
0
1
You may find it useful to create a function alias like so:
import itertools
myfunc = itertools.cycle([0,1]).next
then
myfunc() # -> returns 0
myfunc() # -> returns 1
myfunc() # -> returns 0
myfunc() # -> returns 1
In python, True and False are integers (1 and 0 respectively). You could use a boolean (True or False) and the not operator:
var = not var
Of course, if you want to iterate between other numbers than 0 and 1, this trick becomes a little more difficult.
To pack this into an admittedly ugly function:
def alternate():
alternate.x=not alternate.x
return alternate.x
alternate.x=True #The first call to alternate will return False (0)
mylist=[5,3]
print(mylist[alternate()]) #5
print(mylist[alternate()]) #3
print(mylist[alternate()]) #5
from itertools import cycle
alternator = cycle((0,1))
next(alternator) # yields 0
next(alternator) # yields 1
next(alternator) # yields 0
next(alternator) # yields 1
#... forever
var = 1
var = 1 - var
That's the official tricky way of doing it ;)
Using xor works, and is a good visual way to toggle between two values.
count = 1
count = count ^ 1 # count is now 0
count = count ^ 1 # count is now 1
To toggle variable x between two arbitrary (integer) values,
e.g. a and b, use:
# start with either x == a or x == b
x = (a + b) - x
# case x == a:
# x = (a + b) - a ==> x becomes b
# case x == b:
# x = (a + b) - b ==> x becomes a
Example:
Toggle between 3 and 5
x = 3
x = 8 - x (now x == 5)
x = 8 - x (now x == 3)
x = 8 - x (now x == 5)
This works even with strings (sort of).
YesNo = 'YesNo'
answer = 'Yes'
answer = YesNo.replace(answer,'') (now answer == 'No')
answer = YesNo.replace(answer,'') (now answer == 'Yes')
answer = YesNo.replace(answer,'') (now answer == 'No')
Using the tuple subscript trick:
value = (1, 0)[value]
Using tuple subscripts is one good way to toggle between two values:
toggle_val = 1
toggle_val = (1,0)[toggle_val]
If you wrapped a function around this, you would have a nice alternating switch.
If a variable is previously defined and you want it to toggle between two values, you may use the
a if b else c form:
variable = 'value1'
variable = 'value2' if variable=='value1' else 'value1'
In addition, it works on Python 2.5+ and 3.x
See Expressions in the Python 3 documentation.
Simple and general solution without using any built-in. Just keep the track of current element and print/return the other one then change the current element status.
a, b = map(int, raw_input("Enter both number: ").split())
flag = input("Enter the first value: ")
length = input("Enter Number of iterations: ")
for i in range(length):
print flag
if flag == a:
flag = b;
else:
flag = a
Input:
3 835Output:38383
Means numbers to be toggled are 3 and 8
Second input, is the first value by which you want to start the sequence
And last input indicates the number of times you want to generate
One cool way you can do in any language:
variable = 0
variable = abs(variable - 1) // 1
variable = abs(variable - 1) // 0