[PYTHON] Mathematical puzzle to train the programmer's brain Q03 Turn the card over

Problem summary

Turn over 100 cards every other card for the first time, every two cards for the second time, and finally ask for the card that is turned over.

Code

#Q03 Turn the card over

cards0 = [-1]*100 #the first. 1=table, -1=back
cards = [-1, 1]*50  #Second
step = 3 #How many sheets to turn over

#Turn the card over for each step. End when there are no more cards to flip
while cards0 != cards:
    cards0 = cards[:]
    for i in range(step-1, len(cards), step):
        cards[i] *=  -1
    step += 1

#Show how many cards are flipped over
backs = [i+1 for i in range(len(cards)) if cards[i] == -1]
print(backs)

reference

Array generation

list1 = [0] * 3 
print(list1) # [0, 0, 0]

list2 = [0, 1] * 3
print(list2) # [0, 1, 0, 1, 0, 1]

Reference: https://note.nkmk.me/python-list-initialize/

Copy of array

In Python ~~ Since copy of array is passed by reference ~~ List type is ** mutable ** object, ~~ When making a copy ~~ Because simple variable assignment shares an object without making a copy of the object , If you change the list contents from one variable, the list contents of the other variable will also be changed.

a = [1, 2, 3]
b = a
b[2] = 4
print(b) #[1, 2, 4]
print(a) #[1, 2, 4]

~~ When you want to pass by value ~~ Use slices when you want to make an independent copy.

a = [1, 2, 3]
b = a[:]
b[2] = 4
print(b) #[1, 2, 4]
print(a) #[1, 2, 3]

Reference: https://qiita.com/hrs1985/items/4e7bba39a35056de4e73

Mutable and immutable

It is possible to change the value depending on the object. Objects whose values can be changed are called mutables. Objects whose values cannot be changed after they are created are called immutables.

https://docs.python.org/ja/3/reference/datamodel.html

Mutable: Lists, dictionaries, sets, etc. Immutable: numbers, strings, tuples, etc.

What this means is that if you try to change the value of an immutable object, a new object will be created. Mutable objects change the value of existing objects. Therefore, the behavior when copying variables is different.

Immutable example


a=b=1
print(id(a)) # 140716556333904
a=2
print(id(a)) #140716556333936 ★ A new object is created
print(b) #1 ★ Changes in a do not affect b

Mutable example


a=b=['hoge']
print(id(a)) #2667037271944
a[0] = 'fuga'
print(id(a)) #2667037271944 ★ Existing objects are modified
print(b) #['fuga']★ Changes in a affect b

List comprehension

backs = [i+1 for i in range(len(cards)) if cards[i] == -1]
print(backs)

Is the same as

backs=[]
for i in range(len(cards)):
    if cards[i] == -1:
        backs.append(i+1)
print(backs)

Reference: https://note.nkmk.me/python-list-comprehension/

Recommended Posts

Mathematical puzzle to train the programmer's brain Q03 Turn the card over
Mathematical puzzle to train the programmer's brain Q04 Carving a stick
Mathematical puzzle to train the programmer's brain Q01 "Palindrome in decimal"
Mathematical puzzle to train the programmer's brain Q06 (modified version) Collatz's prediction
Mathematical puzzles that train the programmer's brain Q08 Excellent cleaning robot
Mathematical puzzles that train the programmer's brain Q05 Still paying cash?