螺旋数字的python实现
螺旋数字的算法简单实现。
示例 5
01 02 03 04 05
16 17 18 19 06
15 24 25 20 07
14 23 22 21 08
13 12 11 10 09
通过观察,外部数字进行环绕一圈后向内收拢。

从程序出发,只要递归处理好4条边即可。

同时为了避免顶点重复赋值,最后一个点让后续的边处理。
说明:处理暂时存储在一个list对象中。
实现代码:
def getlocIndex(l_x,l_y,steps):
return l_x + l_y*steps def increaseSeedAndSteps(curSeed,cur_steps):
return (curSeed +1,cur_steps+1) def setTargetItem(targetlst,l_cur_x,l_cur_y,steps,curSeed):
loc_index = getlocIndex(l_cur_x, l_cur_y, steps)
targetlst[loc_index] = curSeed def calc(targetlst,seed,l_x,l_y,nextsteps,steps): current_seed = seed
loop_steps = nextsteps-1 if( nextsteps < 1 ):
setTargetItem(targetlst, l_x, l_y,steps, current_seed)
return each_steps = 0
while(each_steps <= loop_steps):
setTargetItem(targetlst, l_x+each_steps, l_y,steps, current_seed)
current_seed,each_steps = increaseSeedAndSteps(current_seed ,each_steps) each_steps = 0
while(each_steps <= loop_steps):
setTargetItem(targetlst, l_x+nextsteps, (l_y+each_steps), steps, current_seed)
current_seed,each_steps = increaseSeedAndSteps(current_seed ,each_steps) each_steps = 0
while(each_steps <= loop_steps):
setTargetItem(targetlst, l_x+nextsteps-each_steps, l_y+nextsteps, steps, current_seed)
current_seed,each_steps = increaseSeedAndSteps(current_seed ,each_steps) each_steps = 0
while(each_steps <= loop_steps):
setTargetItem(targetlst, l_x, l_y+nextsteps-each_steps, steps, current_seed)
current_seed,each_steps = increaseSeedAndSteps(current_seed ,each_steps) if(nextsteps-2 >= 0):
calc(targetlst,current_seed,l_x+1,l_y+1,nextsteps-2,steps)
测试代码:
def outputResult(targetlst,steps):
outBuffer = ''
for rowIndex in range(0, steps* steps):
if(rowIndex % steps == 0 and len(outBuffer) >0):
print('%s\n' % (outBuffer))
outBuffer = ''
outBuffer = outBuffer + '%02d ' %(targetlst[rowIndex])
print('%s\n' % (outBuffer)) import traceback
try:
steps =5 targetlst = list()
[ targetlst.append(0) for nTry in range(0,steps* steps)] calc(targetlst, 1,0,0,steps-1,steps)
outputResult(targetlst, steps) except Exception as exc:
print("app catch: %s\n" % ( exc));
info = traceback.format_exc()
print(info)
print("done")
螺旋数字的python实现的更多相关文章
- 猜数字游戏-python
题目: 用python写一个猜数字的游戏,游戏规则如下: 1.由一个人随机写一个整数1-99(如:21) 2.一群小伙伴轮流猜数字,如第一个人猜一个数(如:48),则缩小范围至(1-48) 3.如第二 ...
- 一起来刷《剑指Offer》-- 题目一:找出数组中重复的数字(Python多种方法实现)
数组中重复的数字 最近在复习算法和数据结构(基于Python实现),然后看了Python的各种"序列"--比如列表List.元组Tuple和字符串String,后期会写一篇博客介绍 ...
- KNN识别图像上的数字及python实现
领导让我每天手工录入BI系统中的数据并判断数据是否存在异常,若有异常点,则检测是系统问题还是业务问题.为了解放双手,我决定写个程序完成每天录入管理驾驶舱数据的任务.首先用按键精灵录了一套脚本把系统中的 ...
- 一串数字中,只有一个数字出现一次,其他数字都出现两次,查找出这个数字(python)(原创)
背景: 电话面试&手撕代码 2019.03.22 Mufasa 问题: 一串数字中,只有一个数字出现一次,其他数字都出现两次,查找出这个数字 条件: 这串数字是有序数 解决方法: 核心代码只有 ...
- Leetcode 268.缺失数字 By Python
给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9,6,4,2 ...
- 猜数字问题 python
猜数字问题,要求如下: ① 随机生成一个整数 ② 猜一个数字并输入 ③ 判断是大是小,直到猜正确 ④ 判断时间提示:需要用time模块.random模块该题目不需要创建函数 import random ...
- 剑指offer-数组中重复的数字-数组-python
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 剑指offer-数组中只出现一次的数字-数组-python
题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. # -*- coding:utf-8 -*- class Solution: # 返回[a, ...
- leetcode 374. 猜数字大小(python)
我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字.每次你猜错了,我会告诉你这个数字是大了还是小了.你调用一个预先定义好的接口 guess(int n ...
随机推荐
- FPU同步(翻译)
本篇翻译的原英文在:http://mauve.mizuumi.net/2013/06/16/desyncs-and-fpu-synchronization/#more-725(可能要FQ) 如果你曾经 ...
- function.py
#文档字符串 def square(x): 'calculates the square of the number x' return x*x square.__doc__ help(square) ...
- python中为什么需要使用“if __name__ == '__main__'”语句
首先用最简洁的语言来说明一下 if __name__ == '__main__': 的作用:防止在被其他文件导入时显示多余的程序主体部分. 先举个例子,如果不用 if __name__ == '__m ...
- Educational Codeforces Round 24 CF 818 A-G 补题
6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子, ...
- 微信小程序在线支付功能使用总结
最近需要在微信小程序中用到在线支付功能,于是看了一下官方的文档,发现要在小程序里实现微信支付还是很方便的,如果你以前开发过服务号下的微信支付,那么你会发现其实小程序里的微信支付和服务号里的开发过程如出 ...
- 一梦浮生2012 IOS高级进阶目录
一梦浮生2012 精通iphone的UI开发,能熟练操作复杂表视图,熟练使用图层技术, 可以自定义UI控件,使用类别扩展系统控件功能; 擅长通讯技术,熟悉各种通信协议,精通xml, json, 二进制 ...
- unittest参数化parameterized
参考文章: https://www.cnblogs.com/royfans/p/7226360.html https://blog.csdn.net/zha6476003/article/detail ...
- html 样式之style属性的使用
转自:https://www.ggbiji.com/html-style.html html中的style属性是用来改变html元素的样式的,样式是 在html 4 引入的,它是改变 html元素样式 ...
- Table View Programming Guide for iOS---(四)---Navigating a Data Hierarchy with Table Views
Navigating a Data Hierarchy with Table Views 导航数据表视图层次 A common use of table views—and one to which ...
- django上课笔记2-视图CBV-ORM补充-Django的自带分页-Django的自定义分页
一.视图CBV 1.urls url(r'^login.html$', views.Login.as_view()), 2.views from django.views import View cl ...