Skip to content

Commit afe3eba

Browse files
author
Jessica Yung
committed
feat: add gcj 2017 r0 init solutions and io template
1 parent f83247a commit afe3eba

File tree

4 files changed

+266
-0
lines changed

4 files changed

+266
-0
lines changed

google-code-jam-2017/io_template.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# input() reads a string with a line of input, stripping the '\n' (newline) at the end.
2+
# This is all you need for most Google Code Jam problems.
3+
t = int(input()) # read a line with a single integer
4+
for i in range(1, t + 1):
5+
n, m = [int(s) for s in input().split(" ")] # read a list of integers, 2 in this case
6+
print("Case #{}: {} {}".format(i, n + m, n * m))
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
2+
def k_to_flipper_int(k):
3+
flipper_int = 0
4+
for i in range(k):
5+
flipper_int += 2**i
6+
# print ("K = ", k, ", flipper_int = ", flipper_int)
7+
return flipper_int
8+
9+
10+
def string_to_pancakes_int(string, to_int=True):
11+
binary_string = ""
12+
for char in string:
13+
if char == "+":
14+
binary_string += "0"
15+
elif char == "-":
16+
binary_string += "1"
17+
else:
18+
return "Error"
19+
# print("String: ", string)
20+
if to_int == True:
21+
num = binary_string_to_int(binary_string)
22+
# print("Pancake Int: ", num)
23+
return num
24+
else:
25+
# print("Binary string: ", binary_string)
26+
return binary_string
27+
28+
def binary_string_to_int(string):
29+
# print("Binary string: ", string)
30+
binary_string_length = len(string)
31+
num = 0
32+
for i in range(1, binary_string_length+1):
33+
digit = string[-i]
34+
if digit == "1":
35+
num += 2**(i-1)
36+
elif string[-i] == "0":
37+
pass
38+
else:
39+
return "Error"
40+
# print("Pancake Sequence Int: ", num)
41+
return num
42+
43+
def largest_power_of_two_smaller_than(n):
44+
# TODO:
45+
# print("Largest power of two smaller than ", n, ":")
46+
power = -1
47+
while n > 0:
48+
n = divmod(n, 2)[0]
49+
power += 1
50+
if power < 0:
51+
return "Error: n <= 0"
52+
power_of_two = 2**power
53+
# print(power_of_two)
54+
return power_of_two
55+
56+
57+
def pancake_flipper(pancakes_string, k):
58+
flipper_int = k_to_flipper_int(k)
59+
number_of_flips = 0
60+
pancakes_int = string_to_pancakes_int(pancakes_string)
61+
if pancakes_int % flipper_int == 0:
62+
# It is possible
63+
while pancakes_int != 0:
64+
pancakes_int -= largest_power_of_two_smaller_than(pancakes_int)
65+
number_of_flips += 1
66+
# print ("Number of flips: ", number_of_flips)
67+
return number_of_flips
68+
else:
69+
# print("Impossible")
70+
return "Impossible"
71+
72+
# k_to_flipper_int(5)
73+
# k_to_flipper_int(10)
74+
75+
def v2_pancake_flipper(pancakes_string, k):
76+
"""O(n2) greedy algorithm.
77+
Happy side up = 0, empty side up = 1."""
78+
binary_list = list(string_to_pancakes_int(pancakes_string, to_int=False))
79+
flips = 0
80+
# for each distinct possible flip
81+
for i in range(len(binary_list) - (k-1)):
82+
# set active as leftmost pancake in flip
83+
active = binary_list[i]
84+
# if active is empty side up, flip
85+
if active == '1':
86+
# flip this pancake
87+
binary_list[i] = '0'
88+
# Flip the next (k-1) pancakes
89+
for j in range(1,k):
90+
binary_list[i+j] = str(abs(int(binary_list[i+j]) - 1))
91+
# print updated pancake list
92+
# print(binary_list)
93+
flips += 1
94+
# After all necessary flips completed
95+
# if there are still empty side up pancakes, print 'IMPOSSIBLE'
96+
for i in range(1, k):
97+
# print("binary_list[-", i, "]: ", binary_list[-i])
98+
if binary_list[-i] == "1":
99+
# print("IMPOSSIBLE")
100+
return "IMPOSSIBLE"
101+
# print("Flips: ", flips)
102+
return flips
103+
104+
def v3_pancake_flipper(pancakes_string, k):
105+
"""O(n) greedy algorithm with memo-isation."""
106+
binary_list = list(string_to_pancakes_int(pancakes_string, to_int=False))
107+
flips = 0
108+
flip_count = 0
109+
n = len(pancakes_string)
110+
unflip_at = [0] * n
111+
# for each leftmost pancake
112+
for i in range(n):
113+
active = binary_list[i]
114+
if unflip_at[i] == 1:
115+
flip_count -= 1
116+
# if active is empty side up
117+
if (active == '1' and flip_count % 2 == 0) \
118+
or (active == '0' and flip_count % 2 == 1):
119+
# if there is still room to flip, flip
120+
if i < (n - (k-1)):
121+
print("Flip ", i)
122+
# flip this pancake
123+
flips += 1
124+
flip_count += 1
125+
if (i + k) <= n - 1:
126+
unflip_at[i + k] += 1
127+
# if we can't flip, return 'IMPOSSIBLE'
128+
else:
129+
print(flip_count, active)
130+
print("IMPOSSIBLE: -", i)
131+
return "IMPOSSIBLE"
132+
# print("Flips: ", flips)
133+
return flips
134+
135+
# Test cases:
136+
print(v3_pancake_flipper("---+-++-", 3))
137+
print(v2_pancake_flipper("---+-++-", 3))
138+
print(pancake_flipper("---+-++-", 3))
139+
"""
140+
import time
141+
142+
start = time.time()
143+
long_string = "++--++--+++++++--+-+++----+++++++-+---+--+-++--+-" * 19
144+
v2_pancake_flipper(long_string, 8)
145+
end = time.time()
146+
print("Time taken: ", end - start "seconds")
147+
"""
148+
149+
""" For Code Jam
150+
# input() reads a string with a line of input, stripping the '\n' (newline) at the end.
151+
# This is all you need for most Google Code Jam problems.
152+
t = int(input()) # read a line with a single integer
153+
for i in range(1, t + 1):
154+
pancake_string, k = [s for s in input().split(" ")] # read a list of integers, 2 in this case
155+
# print("n, k: ", n, k)
156+
k = int(k)
157+
flips = v2_pancake_flipper(pancake_string, k)
158+
print("Case #{}: {}".format(i, flips))
159+
# check out .format's specification for more formatting options
160+
"""
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def largest_tidy_number_smaller_than(n):
2+
# print("n: ", n)
3+
string_n = list(str(n))
4+
length_of_n = len(string_n)
5+
checked_all = False
6+
changes = 0
7+
while checked_all == False:
8+
edited = False
9+
for index in range(length_of_n-1):
10+
if edited == False:
11+
if string_n[index] > string_n[index + 1]:
12+
string_n[index] = str(int(string_n[index]) - 1)
13+
string_n[remaining_digit] = "9"
14+
"""
15+
if string_n[index] >= string_n[index - 1]:
16+
# check applicable
17+
"""
18+
edited = True
19+
changes += 1
20+
# print("New number: ", string_n)
21+
if edited == False:
22+
checked_all = True
23+
# print("Changes:", changes)
24+
string_n = ''.join(string_n)
25+
n = int(string_n)
26+
# print("Final number: ", n)
27+
return n
28+
29+
""" Test cases
30+
largest_tidy_number_smaller_than(132)
31+
largest_tidy_number_smaller_than(1000)
32+
largest_tidy_number_smaller_than(7)
33+
largest_tidy_number_smaller_than(11110)
34+
"""
35+
36+
t = int(input()) # read a line with a single integer
37+
for i in range(1, t + 1):
38+
n = input()
39+
answer = largest_tidy_number_smaller_than(n)
40+
# n, m = [int(s) for s in input().split(" ")] # read a list of integers, 2 in this case
41+
print("Case #{}: {}".format(i, answer))
42+
# check out .format's specification for more formatting options
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
def split(n):
2+
if n % 2 == 0:
3+
return [n/2, n/2 - 1]
4+
elif n % 2 == 1:
5+
width = n // 2
6+
return [width, width]
7+
else:
8+
return "Error"
9+
10+
def bathroom_stalls(n, k):
11+
intervals = [n]
12+
max_min_pair = []
13+
for i in range(k):
14+
intervals = sorted(intervals, reverse=True)
15+
active = intervals[0]
16+
print("Max: ", active)
17+
max_min_pair = split(active)
18+
# TODO: replace active with max_min_pair
19+
intervals = intervals[1:] + max_min_pair
20+
print("max_min: ", max_min_pair)
21+
max_min_pair = [int(j) for j in max_min_pair]
22+
return max_min_pair
23+
24+
def bathroom_stalls_v2(n, k):
25+
intervals = [0]*n + [1]
26+
max_min_pair = []
27+
active = n
28+
active_qty = 1
29+
for i in range(k):
30+
# 1) while last element is zero, eliminate
31+
# print("intervals: ", intervals)
32+
while active_qty == 0:
33+
# add one or something
34+
active -= 1
35+
intervals = intervals[:active+1]
36+
# print("Max: ", active)
37+
max_min_pair = split(active)
38+
# TODO: replace active with max_min_pair
39+
max = int(max_min_pair[0])
40+
min = int(max_min_pair[1])
41+
# Update intervals
42+
intervals[max] += 1
43+
intervals[min] += 1
44+
intervals[active] -= 1
45+
# print("max_min: ", max_min_pair)
46+
active_qty -= 1
47+
max_min_pair = [int(j) for j in max_min_pair]
48+
return max_min_pair
49+
50+
# input() reads a string with a line of input, stripping the '\n' (newline) at the end.
51+
# This is all you need for most Google Code Jam problems.
52+
t = int(input()) # read a line with a single integer
53+
for i in range(1, t + 1):
54+
n, k = [int(s) for s in input().split(" ")] # read a list of integers, 2 in this case
55+
# print("n, k: ", n, k)
56+
max_min_pair = bathroom_stalls_v2(n, k)
57+
print("Case #{}: {} {}".format(i, max_min_pair[0], max_min_pair[1]))
58+
# check out .format's specification for more formatting options

0 commit comments

Comments
 (0)