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 ...
随机推荐
- POJ 3349 Snowflake Snow Snowflakes Hash
题目链接: http://poj.org/problem?id=3349 #include <stdio.h> #include <string.h> #include < ...
- Array 原型扩展(快速排序,搅乱顺序)
/// 快速快速排序算法Array.prototype.quickSort = function (left, right) { // left = left || 0; // right = rig ...
- Rewrite Path in Asp.Net MVC Project
// Change the current path so that the Routing handler can correctly interpret // the request, then ...
- C++默认参数值函数
1.默认参数值的函数 C++语言允许在定义函数时给其中或某些形式参数(形参)指定默认值,方法就是在相应的形参后面写上“=默认值”,如果省略了对应位置上的实参的值,则在执行被调函数时以该形参的默认值进行 ...
- ReactEurope Conf 参会感想
React 带来的革命性创新是前端世界过去几年最激动人心的变化.自从接触 React 以来,我深信 React 会改变客户端开发者(包括前端.iOS 和 Android)的开发体验.这次在巴黎举办的 ...
- FishEye简介
前言 在项目开发过程中,随着开发的进行,将有大量的代码编写提交到代码仓库,如何能全面准确的了解源代码的变化,提交的频率,代码量的趋势,发现代码的缺陷,将是控制源代码质量的重要指标,这个时候一个 ...
- [主机/oracle/msyql] 监控脚本
为了方便自己测试和监控,写了三个监控的脚本. 分别监控: 主机[cpu,mem,disk,fs,net] oracle mysql 脚本如下: hmon.py: monitor Linux os sy ...
- Json遇到引号需要转义的问题
首先看一个简单的Json格式的数据: {data:{id:1,text:"this is text",src:"abc/abc/abc.png"},succes ...
- Makefile第四讲:include 引用其它makefile文件
main.cpp #include "classes/fun.h" int main() { Test::display("Hello makefile"); ...
- (转载)C++创建对象的两种方法
(转载)http://blog.sina.com.cn/s/blog_586b6c050100dhjg.html 在C++里,有两种方法创建对象: 方法一: ClassName object(para ...