Skip to content

Commit 9f4592b

Browse files
committed
Update the clash of code example
1 parent 1a8801d commit 9f4592b

File tree

1 file changed

+144
-6
lines changed

1 file changed

+144
-6
lines changed

examples/example_clash_of_code.py

Lines changed: 144 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from time import sleep
2+
13
import codingame
24

35
client = codingame.Client()
@@ -7,9 +9,145 @@
79
# or get a clash of code from its public handle
810
coc = client.get_clash_of_code("clash of code handle here")
911

10-
print(coc)
11-
print(coc.join_url)
12-
print(coc.modes)
13-
print(coc.programming_languages)
14-
print(coc.public_handle)
15-
print(coc.players)
12+
# or if you want to create and play a private clash of code
13+
client.login(remember_me_cookie="your cookie here")
14+
coc = client.create_private_clash_of_code(
15+
language_ids=["Python3"], modes=["SHORTEST"]
16+
)
17+
print("Join here :", coc.join_url)
18+
19+
# wait for at least 2 players
20+
while len(coc.players) < 2:
21+
# refetch the data every second
22+
sleep(1)
23+
coc.fetch()
24+
print("Players :", ", ".join([p.pseudo for p in coc.players]), end="\r")
25+
26+
coc.start(refetch=True)
27+
28+
# coc.start just starts a 5s countdown so we have to wait
29+
while not coc.started:
30+
sleep(1)
31+
coc.fetch()
32+
33+
print("Started with players :", ", ".join([p.pseudo for p in coc.players]))
34+
35+
# get the prompt and update the CoC object
36+
question = coc.get_question()
37+
38+
39+
# parse the statement if BeautifulSoup4 is installed
40+
try:
41+
import html
42+
43+
from bs4 import BeautifulSoup, Tag
44+
except ModuleNotFoundError:
45+
print(question.raw_statement)
46+
else:
47+
48+
def readable(tag: Tag) -> str:
49+
return (
50+
html.unescape(tag.decode_contents())
51+
.replace("<var>", "`")
52+
.replace("</var>", "`")
53+
.replace("<const>", "")
54+
.replace("</const>", "")
55+
.replace("<strong>", "**")
56+
.replace("</strong>", "**")
57+
.replace("<br/>", "\n")
58+
)
59+
60+
statement_soup = BeautifulSoup(question.raw_statement, "html.parser")
61+
statement = readable(statement_soup.find(class_="question-statement"))
62+
statement_input = readable(
63+
statement_soup.find(class_="question-statement-input")
64+
)
65+
statement_output = readable(
66+
statement_soup.find(class_="question-statement-output")
67+
)
68+
statement_constraints = readable(
69+
statement_soup.find(class_="question-statement-constraints")
70+
)
71+
example_in = readable(
72+
statement_soup.find(class_="question-statement-example-in")
73+
)
74+
example_out = readable(
75+
statement_soup.find(class_="question-statement-example-out")
76+
)
77+
78+
print(
79+
"Goal",
80+
statement,
81+
"",
82+
"Input",
83+
statement_input,
84+
"",
85+
"Output",
86+
statement_output,
87+
"",
88+
"Constraints",
89+
statement_constraints,
90+
"",
91+
"Example",
92+
"Input",
93+
example_in,
94+
"Output",
95+
example_out,
96+
"",
97+
sep="\n",
98+
)
99+
100+
# choose a programming language among the allowed ones
101+
print("Languages :", ", ".join(coc.programming_languages))
102+
lang = (
103+
input("Your language : ")
104+
if len(coc.programming_languages) > 1
105+
else coc.programming_languages[0]
106+
)
107+
while lang not in coc.programming_languages:
108+
print(
109+
"Wrong language, allowed languages :",
110+
", ".join(coc.programming_languages),
111+
)
112+
lang = input("Your language : ")
113+
114+
# write the code for the statement
115+
print("\nEnter your code, enter \\q at the end")
116+
code = input()
117+
while "\\q" not in code:
118+
code += "\n" + input()
119+
code = code.replace("\\q", "")
120+
121+
# test the code against the test cases
122+
test_case_results = coc.play_test_cases(lang, code, refetch=True)
123+
print("\nTest cases")
124+
for index, result in test_case_results.items():
125+
print("Test case", index, "passed" if result.success else "failed")
126+
if not result.success:
127+
print("Expected :", result.expected)
128+
print("Found :", result.found)
129+
130+
# submit the code
131+
solution = coc.submit(lang, code, refetch=True)
132+
# share the code
133+
solution.share()
134+
print("Code submitted and shared")
135+
136+
# wait for the clash of code to be finished
137+
while not coc.finished:
138+
sleep(5)
139+
coc.fetch()
140+
141+
# show the rankings with the code if shared
142+
ranked_players = sorted(coc.players, key=lambda p: p.rank)
143+
for player in ranked_players:
144+
print(
145+
"\n{0.rank}. {0.pseudo}, {0.score}%{1}, "
146+
"{0.duration.seconds}s, {0.language_id}".format(
147+
player,
148+
f", {player.code_length} char" if coc.mode == "SHORTEST" else "",
149+
)
150+
)
151+
if player.solution_shared:
152+
solution = player.get_solution()
153+
print(solution.code)

0 commit comments

Comments
 (0)