about summary refs log tree commit diff
path: root/THT/C/Q-2016
diff options
context:
space:
mode:
authorRaphael McSinyx <vn.mcsinyx@gmail.com>2016-10-31 22:21:50 +0700
committerRaphael McSinyx <vn.mcsinyx@gmail.com>2016-10-31 22:21:50 +0700
commit71a6e456b59171a681bccff7eeba9db7367bca37 (patch)
treee6f517c4b4109e13c9bb31df6c4c40185ad7ddf7 /THT/C/Q-2016
parent250b7d75204bb18311f51d8b67164f9ad4cef9f2 (diff)
downloadcp-71a6e456b59171a681bccff7eeba9db7367bca37.tar.gz
Thêm đề Tin học trẻ Quốc gia 2016 Bảng B
Diffstat (limited to 'THT/C/Q-2016')
-rw-r--r--THT/C/Q-2016/bai2.inp1
-rw-r--r--THT/C/Q-2016/bai2.out4
-rwxr-xr-xTHT/C/Q-2016/bai2.py47
-rwxr-xr-xTHT/C/Q-2016/bai3.py14
4 files changed, 66 insertions, 0 deletions
diff --git a/THT/C/Q-2016/bai2.inp b/THT/C/Q-2016/bai2.inp
new file mode 100644
index 0000000..9a73285
--- /dev/null
+++ b/THT/C/Q-2016/bai2.inp
@@ -0,0 +1 @@
+1-2.(3-4.5)
diff --git a/THT/C/Q-2016/bai2.out b/THT/C/Q-2016/bai2.out
new file mode 100644
index 0000000..b0a3d7f
--- /dev/null
+++ b/THT/C/Q-2016/bai2.out
@@ -0,0 +1,4 @@
+1-2.(3-4.5)
+=1-2.(3-4.5)
+=1-2.(3-20)
+=1-2.-17
diff --git a/THT/C/Q-2016/bai2.py b/THT/C/Q-2016/bai2.py
new file mode 100755
index 0000000..bb04c54
--- /dev/null
+++ b/THT/C/Q-2016/bai2.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+d = {'+': '+', '-': '-', '.': '*', ':': '/', '^': '**'}
+
+def Steps(string):
+    val = int()
+    for char in string:
+        if char in '+-.:^':
+            val += 1
+    return val
+
+def Calc(string):
+    global d
+    for char in '+-.:^':
+        string = string.replace(char, ' {} '.format(char))
+    l = string.split()
+    for char in '^.:+-':
+        if char in l:
+            idx = l.index(char)
+            s = ' '.join(l[idx - 1:idx + 2])
+            return string.replace(s, str(eval(s.replace(char, d[char]))))
+
+def MetaCalc(string):
+    for idx0, char0 in enumerate(string):
+        if char0 == ')':
+            for idx1, char1 in enumerate(reversed(string)):
+                if char1 == '(':
+                    idx1 = len(string) - idx1 - 1
+                    s = '({})'.format(Calc(string[idx1 + 1:idx0]))
+                    if not(Steps(s)) or ((Steps(s) == 1) and (s[:2] == '(-')):
+                        s = s[1:-1]
+                    return string.replace(string[idx1:idx0 + 1], s)
+    return Calc(string)
+
+with open('bai2.inp') as f:
+    s = f.readline()
+lines = Steps(s)
+for i in d:
+    s = s.replace(i, ' ' + i + ' ')
+for i in d:
+    s = s.replace(i + ' - ', i + ' -')
+l = s.split()
+with open('bai2.out', 'w') as f:
+    f.write(''.join(l) + '\n')
+    for _ in range(lines):
+        s = MetaCalc(s).replace(' ', '')
+        f.write('=' + s)
+        print(s)
diff --git a/THT/C/Q-2016/bai3.py b/THT/C/Q-2016/bai3.py
new file mode 100755
index 0000000..f4139a9
--- /dev/null
+++ b/THT/C/Q-2016/bai3.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python3
+d = dict()
+with open('bai3.inp') as f:
+    for i in f.readlines():
+        for j in '.,;!?':
+            i = i.replace(j, ' ')
+        for j in i.split():
+            d[j] = d.get(j, 0) + 1
+l = [(value, key) for (key, value) in d.items()]
+l.sort(reverse=True)
+with open('bai3.out', 'w') as f:
+    f.write(str(len(l)) + '\n')
+    for i, j in l:
+        f.write('{} {}\n'.format(i, j))