给定n个同质(拓扑)圆,可组成嵌套图案的情况计算,python代码

给定n个同质(拓扑)圆,可组成嵌套图案的情况计算,python代码

情况描述图:

情况如上图

python代码

#coding=utf-8 
#!/usr/bin/env pythondef order_sum_given(n):'''给定和的非重复排列n尽量不要超过26,否则运算会很慢!len(order_sum_given(26)) = 2436len(order_sum_given(27)) = 3010'''if n < 0:return []if n == 1:return [[1]]if n == 2:return [[1,1],[2]]if n > 26:raise ValueError("n尽量不要超过26,否则运算会很慢!")if n > 2:li_result = []for i in range(1, n, 1):li1 = order_sum_given(n-i)for j in li1:j_temp = j[-1]if j_temp > i:continueelse:a = ja.append(i)li_result.append(a)li_result.append([n])return li_resultli_result = order_sum_given(21)
print(len(li_result))
for i in li_result:print(i)def right_brackets(s:str, brackets = "[]"):n = len(s)n1 = 0n2 = 0flag = Truefor i in range(n):if s[i] == brackets[0]:n1 += 1else:n2 +=1if n2 > n1:flag = Falsebreakreturn flagright_brackets("[][[[]]]]", brackets = "[]")def str2brackts(s:str, brackets = "[]"):s_result = ''n = len(s)for i in range(n):if s[i] == '0':s_result += brackets[0]else:s_result += brackets[1]return s_resultstr2brackts("00001111", brackets = "[]")'''
Anything has its beginning has its end.
'''def raw_circle_pictures(n:int, brackets = "[]")-> list:'''create raw circle pictures'''# brackets or parenthesesif n < 0:return []if n == 1:return [brackets]if n == 2:return [brackets+brackets, brackets[0]+brackets[0]+brackets[1]+brackets[1]]if n > 15:raise ValueError("n尽量不要超过12,否则运算会很慢!")if n > 2:li_result = []for i in range(2**(n-1)-1,2**((n-1)*2),1):s0 = '0'*((n-1)*2 - len(bin(i)[2:])) + bin(i)[2:]if s0.count("0") != (n-1):continueelse:s1 = "0"+s0+"1"if right_brackets(s1, "01"):s_a = str2brackts(s1, brackets = brackets)s_a = s_a + brackets[1] * (n-s_a.count(brackets[1]))li_result.append(s_a)else:continuereturn list(set(li_result))li_result = raw_circle_pictures(9, "<>")
print(len(li_result))for i in li_result:print(i)'''用圆度数的方法可以去重上面的结果'''
import re
def get_circle_degree(s:str, brackets = "[]"):'''计算字符串的圆度'''d_max = len(s)//2  # 圆核的可能最大度数li_degree = [0 for x in range(0, d_max)]  # 各度数的圆的个数存储c_n = len(re.findall(brackets, s)) # 圆核的个数# 圆核的形状 等于 bracketss_temp = sindex_temp = 0for i in range(c_n):d_temp = 0# 计算每个圆核的位置及度数index_temp = s_temp.find(brackets) # 查找字符在字符串中的位置d_temp = index_temp + 1 # 该点度数li_degree[d_temp-1] = li_degree[d_temp-1] + 1 # 往该度数的圆的数量加1s_temp = re.sub(brackets, "", s_temp, count=1, flags=0) # 更新字符串,即去除已计算的核#s_temp = re.sub(brackets, "", s_temp, count=1, flags=0) # 与sub()相同, 但返回一个元组, 其中包含新字符串和替换次数。return li_degrees_test = "<><<><>>"
get_circle_degree(s_test , brackets = "<>")# 最终函数
def circle_pictrues(n: int, sharp = "<>"):'''返回 由n个圆组成的所有套圈的可能情况列表'''li0 = raw_circle_pictures(n, sharp)li_temp = []li_result = []for i in li0:a_temp = get_circle_degree(i , sharp)if a_temp in li_temp:continueelse:li_temp.append(a_temp) # 这个必须存在li_result.append(i)return li_resultn = 7
print("n:", n)
li9 = circle_pictrues(n, sharp = "<>")
print("num:", len(li9))
print("="*20)
for i in li9:print(i)

##部分结果:
部分结果


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部