玩蛇网提供最新Python编程技术信息以及Python资源下载!
python零基础培训

用python完成计算一个图形中三角形数目

python 培训
用python完成计算一个图形中三角形数目,微信朋友圈的一个游戏,数三角形的数目。自己用Python了一个小程序来处理了一下,简介写在了这儿。
 
 

 

1. [文件] triangle_counter.py ~ 2KB 

#!/usr/bin/python

"""
---------------------------------------------------------------------------------------------------
2015.07.23  Grey  www.iplaypy.com
***************************************************************************************************
Counter all the triangles in a picture
---------------------------------------------------------------------------------------------------
"""

import itertools,string

def CounterTriangle():
	print "All the triangles we can find in the picture are as follows:"
	triangle_number    = 0
	sides_match_list   = ['abh','acgi','adfj','aek','bcde','efgh','hijk']
	dot_list           = []
	i = 0
	while i < 26:
		dot_list.append(string.lowercase[i])
		if string.lowercase[i] == 'k':
			break
		else:
			i += 1
	for each_combination in itertools.combinations(dot_list,3):
		triangle_flag = True
		for each_dot_combination in itertools.combinations(each_combination,2):
			for each_side in sides_match_list:
				if each_dot_combination[0] in each_side and each_dot_combination[1] in each_side:
					triangle_flag = True
					break
				else:
					triangle_flag = False
			else:
				triangle_flag = False
				break
		line_flag = False
		for each_side in sides_match_list:
			if each_combination[0] in each_side and each_combination[1] in each_side and each_combination[2] in each_side:
				line_flag = True
		if triangle_flag == True and line_flag == False:
			triangle_number += 1
			print "%d : %s" % (triangle_number,each_combination)
	print "The number of triangles in the picture is : %d" % triangle_number

CounterTriangle()
		

2. [图片] triangle.jpg


站长推荐阅读相关内容:
linux基础入门教程

玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/code/graphics/gr341.html [复制]



微信扫描下图可直接关注Python公众号

玩蛇网Python QQ群,欢迎加入: ① 279974227 玩蛇网Python新手群
修订日期:2017年01月04日 - 15时13分07秒 发布自玩蛇网

上一篇:没有了
下一篇:python3代码完成抓取某论坛贴图教程
我要分享到:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
站长推荐:
去顶部去底部