diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..941746a73439eee017424a063829f5ebed86fd87 Binary files /dev/null and b/.DS_Store differ diff --git a/magic_3x3.py b/magic_3x3.py index cebc0752ad7427a79cae7dda7542f6c1366f769a..5436523bc3dda7db8c66ab2f91a15df504268134 100644 --- a/magic_3x3.py +++ b/magic_3x3.py @@ -7,20 +7,15 @@ 2 1 3 2 1 4 2 3 5 stderr에 다음과 예와 같이 출력한다. 총 1 개의 답이 있습니다. 계산시간은 총 3.45 초 입니다. ''' -import time -import sys -# 어짜피 한줄의 합은 무조건 15가 나와야함 그렇지 않으면 나머지를 어떻게 맞춰 채워도 마방진이 안만들어짐. +import copy,time,sys -print("Typing Your Number split by space : < ex : 1 2 3 4 5 6 7 8 9 >") -str=input() -try:## 스페이스로 구분해서 입력한경우 - arr1=[int(a) for a in str.split()] -except ValueError: ## , 컴마로 구분해서 입력한경우 - arr1=[int(a) for a in str.split(',')] +def newlistremove(src,a): #list를 받아서, 요소 a 를 삭제한 새 리스트를 반환하는 함수 + new_list=copy.copy(src) + new_list.remove(a) + return new_list -startTime = time.time() -sys.stdout = open("one2nine.txt",'w') #one2nine.txt 라는 파일에 print되는 글들이 작성 -def magic_3x3(arr): + +def magic_3x3_old(arr): #이전버전 count=0 linesum=sum(arr)/3 for a in arr: @@ -47,11 +42,58 @@ def magic_3x3(arr): return count +def magic_3x3(arr): #새로운 버전 + match = 0 + avg = sum(arr1) / 3 + templist=[] + + for a in arr1: + blist = newlistremove(arr1, a) + for b in blist: + clist = newlistremove(blist, b) + for c in clist: + if (a + b + c != avg): continue # 1번째 가로줄 검사 + dlist = newlistremove(clist, c) + for d in dlist: + elist = newlistremove(dlist, d) + for e in elist: + flist = newlistremove(elist, e) + for f in flist: + if (d + e + f != avg): continue # 2번째 가로줄 + glist = newlistremove(flist, f) + for g in glist: + if (a + d + g != avg ): continue # 1번째 세로줄 검사 + hlist = newlistremove(glist, g) + for h in hlist: + if (b + e + h != avg): continue # 2번째 세로줄 검사 + ilist = newlistremove(hlist, h) + for i in ilist: + if (g+h+i != avg) : continue # 세번째 가로줄 + if (c+f+i != avg) : continue # 3번째 세로줄 검사 + if (a + e + i != avg or c + e + g != avg): continue # 대각선 방향 검사 + templist.append((a,b,c,d,e,f,g,h,i)) + tempset = set(templist) + if (len(templist) != len(tempset)):#모두 동일한 숫자가 입력한된 경우에 대한 Hot Fix + return match + match+=1 + print(a,b,c,d,e,f,g,h,i) + + + return match + +print("Typing Your Number split by space : < ex : 1 2 3 4 5 6 7 8 9 >") +str=input() +startTime = time.time() +try:## 스페이스로 구분해서 입력한경우 + arr1=[int(a) for a in str.split()] +except ValueError: ## , 컴마로 구분해서 입력한경우 + arr1=[int(a) for a in str.split(',')] +sys.stdout = open('one2nine.txt', 'w') #one2nine.txt 라는 파일에 print되는 글들이 작성 count1 = magic_3x3(arr1) t=round(time.time() - startTime , 2) #수행시간을 출력 -#sys.stderr.write("총 {} 개의 답이 있습니다. 계산시간은 총 {} 초 입니다.".format(len(out1), t)) sys.stderr.write("총 {} 개의 답이 있습니다. 계산시간은 총 {} 초 입니다.".format(count1, t)) +#sys.stderr.write("총 {} 개의 답이 있습니다. 계산시간은 총 {} 초 입니다.".format(len(out1), t)) #print(len(out1)) #print(out1) #t=print(time.time() - startTime)#수행시간을 출력 \ No newline at end of file diff --git a/magic_4x4.py b/magic_4x4.py index 2f53b75cd8af5398e3fd2cb237004a44d396295f..6e86e0b380309f4d7142152ce47e2d9de41f74f3 100644 --- a/magic_4x4.py +++ b/magic_4x4.py @@ -13,17 +13,18 @@ import time , math import sys , copy -startTime = time.time() -print("Typing Your Number split by space : < ex : 1 2 3 4 5 6 7 8 9 10 11 ... 15>") -str=input() +'''startTime = time.time() +#print("Typing Your Number split by space : < ex : 1 2 3 4 5 6 7 8 9 10 11 ... 15>") +#str=input() +str='1 14 14 4 11 7 6 9 8 10 10 5 13 2 3 15' try:#스페이스로 구분해서 입력한경우 arr1=[int(a) for a in str.split()] except ValueError: # , 컴마로 구분해서 입력한경우 arr1=[int(a) for a in str.split(',')] finally: - arr1.sort() - num=int(math.sqrt(len(arr1))) - table=[[0]*num for i in range(num)]#n차 테이블 + #arr1.sort() + num=int(math.sqrt(len(arr1)))''' + def newlistremove(src,a): #list를 받아서, 요소 a 를 삭제한 새 리스트를 반환하는 함수 new_list=copy.copy(src) @@ -35,6 +36,7 @@ def newlistremove(src,a): #list를 받아서, 요소 a 를 삭제한 새 리스 def mkQuadrantTable(arr1): # 사분면기준 마방진 만들기 match = 0 avg = sum(arr1) / 4 + templist = [] for a in arr1: blist = newlistremove(arr1, a) for b in blist: @@ -74,9 +76,12 @@ def mkQuadrantTable(arr1): # 사분면기준 마방진 만들기 plist = newlistremove(olist, o) for p in plist: if (a+f+k+p!= avg or m+n+o+p!=avg or d+h+l+p!=avg or k+l+o+p!=avg): continue - print(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, - p) + templist.append((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)) + tempset = set(templist) + if (len(templist) != len(tempset)):#모두 동일한 숫자가 입력된 경우에 대한 Hot Fix + return match match += 1 + print(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) return match def mkMagicTable(arr1):# 일반적인 마방진형식 @@ -127,7 +132,14 @@ def mkMagicTable(arr1):# 일반적인 마방진형식 match+=1 return match -# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 +#print("Typing Your Number split by space : < ex : 1 2 3 4 5 6 7 8 9 10 11 ... 15>") +#str=input() +startTime = time.time() +str='1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16' +try:#스페이스로 구분해서 입력한경우 + arr1=[int(a) for a in str.split()] +except ValueError: # , 컴마로 구분해서 입력한경우 + arr1=[int(a) for a in str.split(',')] sys.stdout = open("4x4.txt",'w') count=mkQuadrantTable(arr1) t=round(time.time() - startTime , 2) #수행시간을 출력 diff --git a/magix_5x5.py b/magix_5x5.py index 9d1d2a0396b790472b73749b392da39a47e6d1f9..3d6eca5230268c8f1e6c2da8340187b290061416 100644 --- a/magix_5x5.py +++ b/magix_5x5.py @@ -15,7 +15,7 @@ import sys import math -print("Sorry, it is only for consecutive number..") +print("Sorry, it is only for consecutive number...") print("Typing Your Number split by space '중복숫자는 불가합니다': < ex : 1 2 3 4 5 6 7 8 9 10 11 ... 24 25>") str=input() try:#스페이스로 구분해서 입력한경우 diff --git a/test_nxn.py b/test_nxn.py index 1bb39094584fb1df8630ab18f1cb6ecec27a6b32..a97c96079325df2fe869a761d5e8e2b4f724ee80 100644 --- a/test_nxn.py +++ b/test_nxn.py @@ -8,6 +8,7 @@ input으로 입력된 n x n의 숫자를 str.split()으로 분리하고 int()로 출력은 stdout으로 결과를 다음예와 같이 출력한다. 2 1 3 2 1 4 2 3 5 - False ''' import math +import sys def isOddExpo(n):##일단 제곱수여야지 마방진을 만들수있음 if n%2 != 0 : return False #홀수인지 @@ -81,14 +82,18 @@ if input == 1 : for i in lines: endd=isOddMagic(mktable(i)) print(i[0:len(lines[0])-1],'-',endd) + sys.stderr.write("결과 파일 내 {}개의 중복이 존재합니다".format(len(lines) - len(myset))) + else : with open("4x4.txt",'r') as f: lines = f.readlines() - myset=set(lines) - if (len(lines) - len(myset) == 0):print("결과 파일 내 중복이 존재하지 않습니다.") - else :print("결과 파일 내 {}개의 중복이 존재합니다".format(len(lines) - len(myset))) + tempset=set(lines) + if (len(lines) == len(tempset)):print("결과 파일 내 중복이 존재하지 않습니다.") + else :print("결과 파일 내 {}개의 중복이 존재합니다".format(len(lines) - len(tempset))) + for i in lines: arr = [int(a) for a in i.split()] #4x4.txt 파일의 한줄은 각각 type : str 이므로, 이를 list에 int 로 split 해서 처리함. endd=isEvenMagic(arr) print(i[0:len(lines[0]) - 1], '-', endd) + sys.stderr.write("결과 파일 내 {}개의 중복이 존재합니다".format(len(lines) - len(tempset)))