안녕하세요 @tmkor 입니다. 오늘은 프로그래밍 언어와 논리학에 기반이 되는 명제에 대하여 포스팅 해보려합니다. 사실 명제는 중학교 수학에서부터 찬찬히 배워오는 개념이라 어디선가 많이 본 내용입니다. ^^; 기본이 되는 만큼 너무나 많은 응용이 있기 때문에 한번 짚고 넘어가 보려 합니다. 기반이 되는 자료는 맛있는 해석학 책 입니다.
p & qp | q보기 1.1.1
from functools import reduce
def isPrime(num):
return reduce(lambda x,y:x&y, map(lambda x:num/x != int(num/x), range(2,num)))
p = 15 % 2 # 15는 홀수이다
q = isPrime(15) # 15는 소수이다.
print(p & q) # 15는 홀수인 소수이다.
print(p | q) # 15는 홀수이거나 소수이다
print(p & (not q)) # 15는 홀수이지만 소수가 아니다.
결과
0 # False
1 # True
1 # True
x is intif p : \n return q보기 1.1.2
def TruthTable(p,q):
p = p
q = q
Kpq = p & q # 논리곱
Apq = p | q # 논리합
Cpq = {True:q, False:True}[p] # 조건문
print("%10s %10s %10s %10s %10s" % (p,q,Kpq,Apq,Cpq))
def PrintTT():
print("%10s %10s %10s %10s %10s" % ("p","q","p&q","p|q","p->q"))
TP = [True, False]
for (p,q) in [(p,q) for p in TP for q in TP]:
TruthTable(p,q)
결과
p q p&q p|q p->q
True True True True True
True False False True False
False True False True True
False False False False True