地址: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. cocos2d-x笔记5: 通过jni实现C++调用Java

    Cocos2d-x的跨平台性很强大,但是偶尔也需要平台的原生API结合. C++在Win32平台下简单的很,C++可以直接用MFC或者调用Win32API. Ios在XCode下直接就能C++和OC混 ...

  2. Li Fei-fei写给她学生的一封信,如何做好研究以及写好PAPER

    Li Fei-fei写给她学生的一封信,如何做好研究以及写好PAPER 在微博上看到的,读完还是有些收获的,首先是端正做research的态度. 我是从这里看到的:http://www.vjianke ...

  3. 二维卷积c代码

    二维卷积c代码 二维信号的卷积原理请参考另外一篇文章:http://blog.csdn.net/carson2005/article/details/43702241 这里直接给出参考代码: void ...

  4. csu 10月 月赛 F 题 ZZY and his little friends

    一道字典树异或的题,但是数据比较水,被大家用暴力给干掉了! 以前写过一个类似的题,叫做the longest xor in tree: 两个差不多吧! 好久没写字典树了,复习一下! 代码: #incl ...

  5. Colored Sticks

    poj2513:http://poj.org/problem?id=2513 题意:就是求一个欧拉回路. 题解:本题是判断欧拉通路是否存在,但是如果是用map的话就会超时,这里采用了trie树,有发现 ...

  6. 【BZOJ 2693】jzptab(莫比乌斯+分块)

    2693: jzptab Description Input 一个正整数T表示数据组数 接下来T行 每行两个正整数 表示N.M Output T行 每行一个整数 表示第i组数据的结果 Sample I ...

  7. UVA 1513 Movie collection

    #include<stdio.h> #include<string.h> #include<stdlib.h> #define N 200010 #define l ...

  8. X86汇编快速入门

    http://www.cnblogs.com/YukiJohnson/archive/2012/10/27/2741836.html

  9. 测试用(编写优质嵌入式C程序)

    注:相比于Word,如果使用CSDN自带编辑器写出结构清晰的文档,需要花费更多的时间,所以我尝试将我写的一些word文档转换为图片发布,这样就可以保持原来的结构.字体,可以获得更好的可读性.图片的分辨 ...

  10. 只看Delphi自带的WnAPI帮助似乎不够

    比如,MessageBox在Delphi自带帮助的参数说明中,对其第四个参数的MB_类型说明只有最常见的6种类型,这么多年搞得我天经地义的以为MessageBox就是这么简单.今天看了一位前辈写的老代 ...