地址: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的更多相关文章

  1. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  2. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  3. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. [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 ...

  5. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  6. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  7. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  8. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  9. [LeetCode] Compare Version Numbers 版本比较

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

  10. [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 ...

随机推荐

  1. WCF Rest Json

    1.定义ServiceContract及实现 [ServiceContract] public interface IMemberService { [OperationContract] strin ...

  2. 《C和指针》章节后编程练习解答参考——第10章

    10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...

  3. C++中二维数组的动态创建与处理

    C++中用new动态创建二维数组的格式一般是这样: TYPE (*p)[N] = new TYPE [][N]; 其中,TYPE是某种类型,N是二维数组的列数.采用这种格式,列数必须指出,而行数无需指 ...

  4. delphi xe5 android 调用照相机获取拍的照片

    本篇文章我们来看一下delphi xe5 在android程序里怎样启动照相机并获取所拍的照片,本代码取自xe自带打sample,路径为: C:\Users\Public\Documents\RAD ...

  5. Java多线程初学者指南(11):使用Synchronized块同步方法

    synchronized关键字有两种用法.第一种就是在<使用Synchronized关键字同步类方法>一文中所介绍的直接用在方法的定义中.另外一种就是synchronized块.我们不仅可 ...

  6. 【技术贴】关闭CMD错误提示声音

    关掉后,整个世界清静多了. cmd打开后 1. 禁用“嘀嘀”声的设备来源,这是由beep驱动服务所提供,可以将beep驱动的启动类型设置为禁用,可以打开CMD窗口,运行以下命令:永久禁用错误声音 sc ...

  7. 反射实体自动生成EasyUi DataGrid模板 第二版--附项目源码

    之前写过一篇文章,地址 http://www.cnblogs.com/Bond/p/3469798.html   大概说了下怎么通过反射来自动生成对应EasyUi datagrid的模板,然后贴了很多 ...

  8. OSAL多任务资源分配机制

    转自OSAL多任务资源分配机制 一.概述      OSAL (Operating System Abstraction Layer),翻译为"操作系统抽象层". 个应用程序对象. ...

  9. 安装ECMall后报PHP Strict Standards错误,请问如何解决

    Strict Standards: Non-static method ECMall::startup() should not be called statically in /htdocs/ecm ...

  10. 一个简单的DDraw应用程序2

    //------------------------------------------------------------------------- // 文件名 : 6_1.cpp// 创建者 : ...