@cboseak/

AuthenticCraftyGrouse

Python 2.7

No description

fork
loading
Files
  • main.py
  • input.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import sys
sys.setrecursionlimit(1000000)

def checkIfAllHappy(cakes):
	allUp = True
	for i in cakes:
		if i == '-':
			allUp = False
	return allUp

def recursiveFlip(cakes,n):
	print "test"

def opposite(side):
	if side == '+':
		return '-'
	elif side == '-':
		return '+'
	else:
		return '?'

def flipBasedOnArr(cakes,arr):
	temp = []
	for i in cakes:
		temp.append(i)
	for i in range(len(arr)):
		temp[arr[i]] = opposite(cakes[arr[i]])
	return ''.join(temp)

def getPositionArray(num, flip):
	arr = []
	a = 0
	while a < flip:
		arr.append(num + a)
		a += 1
	return arr

def getNegativePositionArray(num, flip):
	arr = []
	a = 0
	while a < flip:
		arr.append(num - a)
		a += 1
	return arr

def flipper(cakes,flip,n):
	#print cakes
	for i in range(len(cakes)):
		if cakes[i] == '-':
			if (i + flip)  <= len(cakes):
				cakes = flipBasedOnArr(cakes,getPositionArray(i,flip))
			else:
				cakes = flipBasedOnArr(cakes,getNegativePositionArray(i,flip))
			n += 1
			break
	
	if checkIfAllHappy(cakes):
		return n
	if n == (len(cakes) * flip):
		return "IMPOSSIBLE"
	
	return flipper(cakes,flip,n)

def flipperNonRecursive(cakes,flip,n):
	#print cakes
	for i in range(len(cakes)):
		if cakes[i] == '-':
			if (i + flip)  <= len(cakes):
				cakes = flipBasedOnArr(cakes,getPositionArray(i,flip))
			else:
				cakes = flipBasedOnArr(cakes,getNegativePositionArray(i,flip))
			n += 1
			i = 0
	
	if checkIfAllHappy(cakes):
		return n
	else:
		return "IMPOSSIBLE"


			
lines = open('input.txt').readlines()
q = 1
for line in lines:
	lineArr = line.split()
	if len(lineArr)  == 2:
		print "case #" + str(q)  + ": " + str(flipperNonRecursive(lineArr[0], int(lineArr[1]),0))
		q += 1