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 ...
随机推荐
- Codeforces Round #321 div2
好像前几场的题解忘记写了, Orz 状态太差, 平均出两题 都不好意思写了 , 连掉4场, 都要哭晕了. 很水的一场, 写完A B C就去睡了 D题其实不难, E题研究Ing(已用一种奇怪的姿势 ...
- UML 类图的关系
1. 关联关系 1.1 单向关联 . public class ClassA { private ClassB bVar; } public class ClassB { //... } 1.2 ...
- hdu 2894
刚刚看到这个题感觉挺复杂的~~~因为它还要输出字典序: 很容易知道对于任意的k,第一个输出总是1<<k; 而对于第二个嘛,不管怎么样,前k个元素总是k个0: 然后取前k-1个数,加上0或者 ...
- ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57
转自: ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57 前不久ARM正式宣布推出新款ARMv8架构的Cortex-A50处理器系列 ...
- SDUT 2352 Run Length Encoding
点我看题目 题意 :将给定的字符串编码,编码的规则根据两条,1.如果字符串里有连续相等的字符,就变为两个字符,一个是这些连续相同的字符的个数,另一个是这个字符,但是如果数量超过了9个,那就输出9再输出 ...
- 李洪强iOS开发Swift篇—05_元组类型
李洪强iOS开发Swift篇—05_元组类型 一.元组类型介绍 1.什么是元组类型 元组类型由 N个 任意类型的数据组成(N >= 0),组成元组类型的数据可以称为“元素” 示例: let po ...
- [wikioi]过河卒
棋盘型动态规划.(PPT:http://wenku.baidu.com/view/56badad850e2524de5187ea3.html)该类动态规划有一个共性,那就是在一个矩阵中(一般是二维矩阵 ...
- 用sql增、修改、删除字段
--给一个表增加一个字段 ); --给一个表修改一个字段的数据类型 ,); --通用sql修改字段的用法 ,); --删除一个字段 alter table wm_goods drop column b ...
- 2.linux下Makefile编写规范
转自陈皓 (CSDN) 概述—— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 profession ...
- MySQL源码 information_schema新增表
information_schema是MySQL下的DB, 存储了数据库的数据字典,但OS系统上,并没有information_schema下表的数据和结构文件. 所以,MySQL在针对informa ...