###题目:给定一个字符串,判断其中是否有重复字母
###思路:将重复的字符放入到list中,并进行计数统计
###实现:伪代码、函数、类实现

###伪代码:
string=s #给定的字符串
list=[] #放入重复的字符
dict={} #重复的字符进行统计

#判断字符串是否为空,逻辑实现
if str !=None:
for i in s:
if list !=None:
if i in list:
if i in dict.keys():
dict[i]+=1
else:
dict[i]=1
else:
list.append(i)
else:
list.append(i)
else:
print('输入的字符串为空!')

###python函数实现:

def judge_str(str):
str_list=[]
str_repet={}
if str !=None:
for i in str:
if not list is None:
if i in str_list:
if i in str_repet.keys():
str_repet[i]+=1
else:
str_repet[i]=1
else:
str_list.append(i)
else:
str_list.append(i)
print('该字符串中有%s个字符有重复'%len(str_repet.keys()))
for key,value in str_repet.items():
print('重复的字符为【%s】,共重复了%s次'%(key,value))
else:
print('输入的字符串是空!')

###python类的实现:

from  functools import wraps
import time
def decorator(func):
@wraps(func)
def wrappres(*args,**kwargs):
start=time.time()
func(*args,**kwargs)
end=time.time()
usetime=end-start
print('查找共花费{:.2f}秒'.format(usetime))
return wrappres
class JudgeStr:
def __init__(self,string):
self.str_tuple=()
self.str_repet={}
self.string=string
self.str=self._filter_handler() def _judge_handler(self):
for i in self.str:
if self.str_list !=None:
if i in self.str_list:
if i in self.str_repet.keys():
self.str_repet[i]+=1
else:
self.str_repet[i]=1
else:
self.str_list.append(i)
else:
self.str_list.append(i) def _filter_handler(self):
if self.string !=None:
if type(self.string) ==str:
return self.string
else:
raise ValueError('请传入字符串参数')
else:
raise TypeError('参数为空')
@decorator
def result_handler(self):
self._judge_handler()
print('该字符串中有%s个字符有重复'%len(self.str_repet.keys()))
for key,value in self.str_repet.items():
print('重复的字符为【%s】,共重复了%s次'%(key,value))

《Craking the Coding interview》python实现---01的更多相关文章

  1. 《Craking the Coding interview》python实现---02

    ###题目:翻转一个字符串###思路:从字符串的最后一位开始,依次取###实现:伪代码.函数.类实现#伪代码: #01string=sNew_s=""for i in range( ...

  2. Craking the coding interview 面试题:完美随机洗牌

    给定一个序列,随机打乱这个序列,新产生的序列和任意一个序列产生的可能性是一样的,就是所谓的完美随机洗牌. 看下面的运行结果: 上面第一列是原数列,下面一行是新产生的打乱的数列. 基本思想:如果n-1个 ...

  3. Python Coding Interview

    Python Coding Interview Python Advanced Use enumerate() to iterate over both indices and values Debu ...

  4. Python学习--01入门

    Python学习--01入门 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.和PHP一样,它是后端开发语言. 如果有C语言.PHP语言.JAVA语言等其中一种语言的基础,学习Py ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. A few things to remember while coding in Python.

    A few things to remember while coding in Python. - 17 May 2012 - UPDATE: There has been much discuss ...

  7. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  8. 转:Top 10 Algorithms for Coding Interview

    The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...

  9. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

随机推荐

  1. QT中文字的绘制

    为什么要做这次文字的介绍,因为在一般的教材中,还真没有文字的描述: 1.绘制最简单的文字. 我们更改重绘函数如下: void Dialog::paintEvent(QPaintEvent *){QPa ...

  2. Codecademy网站安利 及 javaScript学习

    今天发现一个Code教学网站,号称可以利用零碎时间来学习些代码. codecademy (https://www.codecademy.com)

  3. js数组并集,交集,差集

    js数组并集,交集,差集的计算方式汇总 一. new Set 方式实现 这种方式实现起来比较简单,原理就是参考new Set可以去重的功能 ,关于去重可以点击 https://www.haorooms ...

  4. Mysql学习总结(31)——MySql使用建议,尽量避免这些问题

    做服务器端开发的同学们,相信对于mysql应该是十分熟悉,但是一旦真正出现问题,你是否能够快速的发现问题的起因,并且解决呢?一旦问题涉及到数据库层面,往往不是那么好解决的,通常来说,我们需要提前做应对 ...

  5. URAL 1517 Freedom of Choice

    Freedom of Choice Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on Ural. Orig ...

  6. 菜鸟学Struts——I18N对国际化的支持

    大家肯定都喜欢玩游戏吧. 对于是一个游戏迷的话,肯定玩过不少很棒的经典单机游戏.比方说,国产的<古墓丽影>.<刺客信条>.<鬼泣>国产的仙剑.古剑等.在众多游戏系列 ...

  7. Maven简单介绍(Maven是什么)

    简单介绍 Maven,在意第绪语中意为对知识的积累.Maven最初用来在Jakarta Turbine项目中简化该项目的构建过程. Jakarta Trubine项目有多个project.每一个pro ...

  8. poj3061 Subsequence ,尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  9. POJ 3664 Election Time 题解

    这道题网上非常多人都会说easy,水题之类的话,只是我看了下说这种话的人的程序,能够说他们的程序都不及格! 为什么呢?由于他们的程序都是使用简单的二次排序水过(大概你能搜索到的多是这种程序).那样自然 ...

  10. Java类载入器

    1.   系统载入器简单介绍 Java虚拟机中能够安装多个类载入器,系统默认三个主要类载入器(BootStrap.ExtClassLoader.AppClassLoader).每一个类载入器负责载入特 ...