Skip to content

Commit

Permalink
Add sample source code
Browse files Browse the repository at this point in the history
  • Loading branch information
GBS-Skile committed Dec 20, 2019
1 parent 6692ac9 commit bb2a12d
Show file tree
Hide file tree
Showing 5 changed files with 2,249 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@
**Lab 3 "색상 코드 2", "Caesar Cipher" 문제 예제 소스 업데이트**
- [day3/samples/](https://github.com/jtjun/PySamsungDS/tree/master/day3/samples) 폴더에서
관련 소스코드를 확인하실 수 있습니다.
* (2019. 12. 20. 20시)
**Project "Wolfram Beta" 샘플 소스 업데이트**
- [day5/sample_complete.py](https://github.com/jtjun/PySamsungDS/blob/master/day5/sample_complete.py)
실습조교 이정민([email protected]) 예제 소스 코드 입니다.
- [day5/grader.py](https://github.com/jtjun/PySamsungDS/blob/master/day5/grader.py)
채점에 사용한 프로그램입니다. 채점에 사용하는 데이터셋은 1000개입니다.
62 changes: 62 additions & 0 deletions day5/grader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import math
import traceback

# from wolfram_beta import solve
from sample_complete import solve
from sample_complete import parse_equation

input_path = 'text/input.txt'
output_path = 'text/output.txt'
answer_path = 'text/answer.txt'

solve(input_path, output_path)

def is_same(eq1, eq2, eps=1e-6):
try:
t1 = parse_equation(eq1)
except:
traceback.print_exc()
return False

t2 = parse_equation(eq2)

keys = set(t1.keys()).union(t2.keys())
for k in keys:
if not math.isclose(t1.get(k, 0), t2.get(k, 0), rel_tol=eps, abs_tol=eps):
return False

return True

with open(input_path) as f:
inputs = f.readlines()

with open(output_path) as f:
outputs = f.readlines()

with open(answer_path) as f:
answers = f.readlines()

correct = 0
wrong_cases = []


num = 1
for i, o, a in zip(inputs, outputs, answers):
if is_same(o.strip(), a.strip()):
correct += 1
else:
wrong_cases.append((num, i, o, a))

num += 1

print("Score : " + str(correct) + "/" + str(correct + len(wrong_cases)))

if len(wrong_cases):
with open('text/wrong_cases.txt', 'w') as f:
for num, i, o, a in wrong_cases:
f.write(
f'Line #{num}\n' +
f'Input: {i}' +
f'Expected: {a}' +
f'But you : {o}\n'
)
181 changes: 181 additions & 0 deletions day5/sample_complete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import math
import traceback


def print_term(degree, factor):
"""
:param degree: 항의 차수 (int, 음이 아닌 정수)
:param factor: 항의 계수 (int)
:return: (str)
"""
f, d = factor, degree
term = ""

if d == "exp(x)":
return str(factor) + "exp(x)"
elif d == "sin(x)":
return str(factor) + "sin(x)"
elif d == "cos(x)":
return str(factor) + "cos(x)"

if f < 0:
term += "-"
if not (abs(f) == 1 and d != 0):
term += str(abs(f))
if d != 0:
term += "x"
if d != 0 and d != 1:
term += "^" + str(d)
return term


def print_equation(terms):
"""
:param terms: dict (key=degree, value=factor)
:return: str
"""
term_str = []
for d, f in terms.items():
term_str.append(print_term(d, f))

return " + ".join(term_str)


def parse_term(term_str):
"""
:param term_str: str
:return: (degree: int, factor: int)
"""
if term_str.endswith('(x)'):
d = term_str[-6:]
f_str = term_str[:-6]
if f_str == "-" or f_str == "":
f_str += "1"

return d, float(f_str)
elif "x" in term_str:
f_str, d_str = term_str.split("x")
if f_str == "-" or f_str == "":
f_str += "1"

f = float(f_str)
d = float(d_str[1:]) if d_str else 1.0
return d, f
else:
return 0.0, float(term_str)


def parse_equation(equation):
"""
:param equation: str
:return: dict (key=degree, value=factor)
"""
terms = {}
for term_str in equation.split(" + "):
d, f = parse_term(term_str)
terms[d] = terms.get(d, 0) + f

return terms


def d_dx_as_terms(terms):
"""
:param terms: dict (key=degree, value=factor)
:return: terms와 동일한 형식이되,
그 값이 terms의 미분인 것
"""
derivative = {}

for d, f in terms.items():
if d == "sin(x)":
derivative["cos(x)"] = derivative.get("cos(x)", 0) + f
elif d == "cos(x)":
derivative["sin(x)"] = derivative.get("sin(x)", 0) - f
elif d == "exp(x)":
derivative["exp(x)"] = derivative.get("exp(x)", 0) + f
elif d != 0:
derivative[d - 1] = derivative.get(d - 1, 0) + f * d

if not len(derivative):
derivative[0] = 0

return derivative


def d_dx(equation):
"""
:param equation: str
:return: str
"""
terms = parse_equation(equation)
terms = d_dx_as_terms(terms)
return print_equation(terms)


def integral_as_terms(terms, constant):
result = {0: constant}
for d, f in terms.items():
if d == "sin(x)":
result["cos(x)"] = result.get("cos(x)", 0) - f
elif d == "cos(x)":
result["sin(x)"] = result.get("sin(x)", 0) + f
elif d == "exp(x)":
result["exp(x)"] = result.get("exp(x)", 0) + f
else:
result[d + 1] = result.get(d + 1, 0) + f / (d + 1)

return result


def integral(equation, constant):
terms = parse_equation(equation)
terms = integral_as_terms(terms, constant)
return print_equation(terms)


def compute_as_terms(terms, x):
y = 0
for d, f in terms.items():
if d == "sin(x)":
y += f * math.sin(x)
elif d == "cos(x)":
y += f * math.cos(x)
elif d == "exp(x)":
y += f * math.exp(x)
else:
y += f * (x ** d)
return y


def compute(equation, x):
terms = parse_equation(equation)
y = compute_as_terms(terms, x)
return str(y)


def solve_query(line):
try:
tokens = line.split(',')
command = tokens[0]

if command == 'D':
return d_dx(tokens[1])
elif command == 'I':
return integral(tokens[1], float(tokens[2]))
elif command == 'C':
return compute(tokens[1], float(tokens[2]))
except:
traceback.print_exc()
return ''


def solve(input_path, output_path):
input_f = open(input_path)
output_f = open(output_path, 'w')

for line in input_f:
line = solve_query(line.strip())
output_f.write(line + '\n')

input_f.close()
output_f.close()
Loading

0 comments on commit bb2a12d

Please sign in to comment.