Skip to content

Commit 0778e66

Browse files
committed
Add oj python stdin.
1 parent d8b6992 commit 0778e66

File tree

4 files changed

+1392
-0
lines changed

4 files changed

+1392
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Jupyter标准输入sys.stdin\n",
8+
"\n",
9+
"https://www.reddit.com/r/Python/comments/24b3nv/programmatically_providing_input_to/\n",
10+
"\n",
11+
"**使用python io模块的StringIO将字符串替换成sys.stdin**\n",
12+
"\n",
13+
"You can change sys.stdin with any other file like object:"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 1,
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"from io import StringIO \n",
23+
"import sys\n",
24+
"\n",
25+
"doc = '''3\n",
26+
"1 2 3\n",
27+
"4 5 6\n",
28+
"7 8 9\n",
29+
"'''\n",
30+
"\n",
31+
"sys.stdin = StringIO(doc)"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 2,
37+
"metadata": {},
38+
"outputs": [
39+
{
40+
"name": "stdout",
41+
"output_type": "stream",
42+
"text": [
43+
"3\n",
44+
"[1, 2, 3]\n",
45+
"[4, 5, 6]\n",
46+
"[7, 8, 9]\n"
47+
]
48+
}
49+
],
50+
"source": [
51+
"n = int(sys.stdin.readline().strip())\n",
52+
"print(n)\n",
53+
"for i in range(n):\n",
54+
" line_str = sys.stdin.readline().strip()\n",
55+
" line = list(map(int,line_str.split()))\n",
56+
" print(line)"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 3,
62+
"metadata": {},
63+
"outputs": [
64+
{
65+
"name": "stdout",
66+
"output_type": "stream",
67+
"text": [
68+
"3\n",
69+
"7\n"
70+
]
71+
}
72+
],
73+
"source": [
74+
"#coding=utf-8\n",
75+
"\n",
76+
"from io import StringIO \n",
77+
"import sys\n",
78+
"\n",
79+
"doc = '''1 2\n",
80+
"3 4\n",
81+
"'''\n",
82+
"\n",
83+
"sys.stdin = StringIO(doc)\n",
84+
" \n",
85+
"if __name__ == \"__main__\":\n",
86+
" while True:\n",
87+
" try:\n",
88+
" line = sys.stdin.readline().split()\n",
89+
" a,b = list(map(int,line))\n",
90+
" print(a+b)\n",
91+
" except:\n",
92+
" break"
93+
]
94+
}
95+
],
96+
"metadata": {
97+
"kernelspec": {
98+
"display_name": "tf36",
99+
"language": "python",
100+
"name": "tf36"
101+
},
102+
"language_info": {
103+
"codemirror_mode": {
104+
"name": "ipython",
105+
"version": 3
106+
},
107+
"file_extension": ".py",
108+
"mimetype": "text/x-python",
109+
"name": "python",
110+
"nbconvert_exporter": "python",
111+
"pygments_lexer": "ipython3",
112+
"version": "3.6.8"
113+
}
114+
},
115+
"nbformat": 4,
116+
"nbformat_minor": 2
117+
}

0 commit comments

Comments
 (0)