Look and say numbers
地址:http://www.codewars.com/kata/53ea07c9247bc3fcaa00084d/train/python
There exists a sequence of numbers that follows the pattern
1
11
21
1211
111221
312211
13112221
1113213211 . . .
Starting with "1" the following lines are produced by "saying what you see", so that line two is "one one", line three is "two one(s)", line four is "one two one one".
代码如下:
import collections def say(stra):
lenA = len(stra)
dic = collections.OrderedDict()
ans = ""
for i in range(0,lenA):
if dic.has_key(stra[i]):
dic[stra[i]] += 1
else:
dic[stra[i]] = 1 if i > 0 and stra[i] != stra[i-1] :
ans += (str(dic[stra[i-1]]) + stra[i-1])
dic[stra[i-1]] = 0 ans += (str(dic[stra[i]]) + stra[i]) return ans def look_and_say(data='', maxlen=5):
lista = []
lista.append(say(data))
for i in range(1,maxlen):
lista.append(say(lista[i-1]))
return lista
Look and say numbers的更多相关文章
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- Things About 'extern'
Note: All Learned From Here C和Objective-C的function前面都有个隐含的extern,对于function来说,有没有extern都无所谓,但变量不一样. ...
- jquery插件的写法
jquery插件及zepto插件,写法上有些区别. 区别点: 1.自定义事件的命名空间 jq的时间命名空间是用点“.”,而zepto是用冒号“:” 如 //jquery $(this).trigger ...
- css实现网页布局随滚轮变化响应移动
_position:absolute; _top:expression(eval(document.documentElement.scrollTop)); 1.第一句代码 _position:abs ...
- 由tomcat启动想到的
1.batch:批处理文件,表示一批 2.profile:轮廓 3.用户变量和系统变量的关系是什么? 答:点击"我的电脑→属性→高级"标签的"环境变量&quo ...
- 怎么样删除eclipse已经记录svn的地址
eclipse-->window-->show view-->svn选项卡中选中要删除的svn链接,点击右键废弃即可. 1.
- Navicate使用注意事项
2. Navicat如何连接数据库:点击连接——>输入连接名,然后如果是本机不用更改localhost,如果是别的主机,要将对方 的ip地址输入,端口号3306不变,用户名root,密 ...
- android使用tabhost实现导航
参考 http://blog.csdn.net/xixinyan/article/details/6771341 http://blog.sina.com.cn/s/blog_6b04c8eb0101 ...
- java 二叉搜索树
java二叉查找树实现: 二叉查找树,上图:比根节点小者在其左边,比根节点大者在其右边. 抽象数据结构,上代码: /** * 二叉查找树数据结构(非线程安全): * 范型类型须实现Comparable ...
- BZOJ_1202_狡猾的商人_(并查集)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1202 n 个月的账单,共 m 组数据,每一组数据包括 x , y , t ,表示从 x 月到 ...
- Windows Server基础架构云参考架构:硬件之上的设计
作者 王枫 发布于2014年1月27日 综述 毫无疑问,移动互联网.社交网络.大数据和云计算已经成为IT发展的四个大的趋势.其中云计算又为前三个提供了一个理想的平台.今天不仅互联网公司,很多传统行业的 ...