python – 递归函数中的计数器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 递归函数中的计数器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2728字,纯文字阅读大概需要4分钟。
内容图文
![python – 递归函数中的计数器](/upload/InfoBanner/zyjiaocheng/748/1dede07d64bd4f23ba09388bd78a677e.jpg)
我是python和编程的新手.我编写了一个函数,它将搜索数组中的相邻元素,并寻找值相互之间0.05的值,就像泛光填充算法一样.唯一的区别是我在计算函数运行的时间时做了一些愚蠢的事情(我想这也会告诉我我发现了多少元素),所以我的计数器值是错误的.代码在找到彼此相差0.05的相邻元素时起作用,只是计数很有趣.
def floodcount (x,y,array,value,count=0): #akin to a bucket fill in paint, finds the area instead
nrows = len(array)-1 #rows of the image
ncols = len(array[0])-1 #columns of the image
diff = array[x][y] - value
if (diff < 0.00) or (diff > 0.05): # the base case, finding a diff more than 0.05 or less than 0 is like finding a boundary
return 0
count = count +1
print 'count1 ',count
array[x][y] = -5 # so we do no calculate this pixel again
#print "[",x,",",y,"]"
if x > 0:
#print '1'# if not the first elemnet then can go back, this makes sure that x is within the array all the time
floodcount (x-1,y,array,value,count)
if y > 0:
#print '2'
floodcount (x,y-1,array,value,count)
if x < nrows:
#print '3'
floodcount (x+1,y,array,value,count)
if y < ncols:
#print '4'
floodcount (x,y+1,array,value,count)
if x > 0 and y > 0:
#print '5'
floodcount (x-1,y-1,array,value,count)
if x < nrows and y < ncols:
#print '6'
floodcount (x+1,y+1,array,value,count)
if x <nrows and y > 0:
#print '7'
floodcount (x+1,y-1,array,value,count)
if x > 0 and y < ncols:
#print '8'
floodcount (x-1,y+1,array,value,count)
print 'count2 ',count
return count
所以对于测试用例
array = [[5,1,1,3,4],[4,5,6,2,5],[5,8,5,5,9]]
x = 0且y = 0
OUTPUT
count1 1
count1 2
count1 3
count1 4
count1 5
count2 5
count2 4
count2 3
count1 3
count2 3
count2 2
count2 1
你可以看到有些东西是可疑的:P
谁能指出我做错了什么?任何帮助,将不胜感激.
解决方法:
除了现在解决的计数问题:
您可以通过每次执行所有递归调用来减少if语句的数量,只需在函数开头检查数组边框,如果x< 0或y <0 0或x> nrows或y> NCOLS.
#akin to a bucket fill in paint, finds the area instead
def floodcount (x,y,array,value,count=0):
nrows = len(array)-1 #rows of the image
ncols = len(array[0])-1 #columns of the image
if x < 0 or y < 0 or x > nrows or y > ncols:
return count
diff = array[x][y] - value
# the base case, finding a diff more than 0.05 or less than 0 is like finding a boundary
if (diff < 0.00) or (diff > 0.05):
return count
count = count +1
print 'count1 ',count
array[x][y] = -5 # so we do no calculate this pixel again
#print "[",x,",",y,"]"
count = floodcount (x-1,y,array,value,count)
count = floodcount (x,y+1,array,value,count)
count = floodcount (x+1,y,array,value,count)
count = floodcount (x,y-1,array,value,count)
count = floodcount (x-1,y-1,array,value,count)
count = floodcount (x+1,y+1,array,value,count)
count = floodcount (x+1,y-1,array,value,count)
count = floodcount (x-1,y+1,array,value,count)
print 'count2 ',count
return count
内容总结
以上是互联网集市为您收集整理的python – 递归函数中的计数器全部内容,希望文章能够帮你解决python – 递归函数中的计数器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。