Playing card games
Here I have N number of cards, numbered from 1 to N, placed on a round table, so that card 1 is between card 2 and card N. All cards are initially turned over. The goal is to turn all cards face up.
Let's say we touch card i, touching card, I will turn cards i-1, i, i + 1 face up. similarly touching card N, cards N-1, N, 1st card will be facing up. I want to define the minimum number of touches needed to view all the cards.
here is what i tried in python
q = int(raw_input())
if q==1 or q==2:
print "1"
else:
r = q%3
l = q/3
print r+l
q can go up to 10 ^ 20.
What is wrong with the above logic and in case the above logic is completely wrong, which should be correct.
0
source to share
1 answer
It should be something like:
answer = q / 3 (+ 1 if q is not a multiple of 3)
So a simple way for a neat way of coding:
q = int (raw_input()) # This isn't safe since it causes Type Errors easily but... whatever...
print (q / 3) + 1 * (q % 3 > 0) # Because 1 * True = 1, 1 * False = 0
0
source to share