###题目:给定一个字符串,判断其中是否有重复字母
###思路:将重复的字符放入到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函数实现:

  1. def judge_str(str):
  2. str_list=[]
  3. str_repet={}
  4. if str !=None:
  5. for i in str:
  6. if not list is None:
  7. if i in str_list:
  8. if i in str_repet.keys():
  9. str_repet[i]+=1
  10. else:
  11. str_repet[i]=1
  12. else:
  13. str_list.append(i)
  14. else:
  15. str_list.append(i)
  16. print('该字符串中有%s个字符有重复'%len(str_repet.keys()))
  17. for key,value in str_repet.items():
  18. print('重复的字符为【%s】,共重复了%s次'%(key,value))
  19. else:
  20. print('输入的字符串是空!')

###python类的实现:

  1. from functools import wraps
  2. import time
  3. def decorator(func):
  4. @wraps(func)
  5. def wrappres(*args,**kwargs):
  6. start=time.time()
  7. func(*args,**kwargs)
  8. end=time.time()
  9. usetime=end-start
  10. print('查找共花费{:.2f}秒'.format(usetime))
  11. return wrappres
  12. class JudgeStr:
  13. def __init__(self,string):
  14. self.str_tuple=()
  15. self.str_repet={}
  16. self.string=string
  17. self.str=self._filter_handler()
  18. def _judge_handler(self):
  19. for i in self.str:
  20. if self.str_list !=None:
  21. if i in self.str_list:
  22. if i in self.str_repet.keys():
  23. self.str_repet[i]+=1
  24. else:
  25. self.str_repet[i]=1
  26. else:
  27. self.str_list.append(i)
  28. else:
  29. self.str_list.append(i)
  30. def _filter_handler(self):
  31. if self.string !=None:
  32. if type(self.string) ==str:
  33. return self.string
  34. else:
  35. raise ValueError('请传入字符串参数')
  36. else:
  37. raise TypeError('参数为空')
  38. @decorator
  39. def result_handler(self):
  40. self._judge_handler()
  41. print('该字符串中有%s个字符有重复'%len(self.str_repet.keys()))
  42. for key,value in self.str_repet.items():
  43. 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. TCP/IP 三次握手和HTTP过程

    0 引言 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在“无差别”的网络之上. 1 T ...

  2. [置顶] Linux 常用命令集锦

    出处:http://www.vaikan.com/what-are-the-most-useful-swiss-army-knife-one-liners-on-unix/ Linux命令行里的&qu ...

  3. IOS - 6\7下UINavigationBar的颜色的方法改变 ——转载http://www.th7.cn/Program/IOS/201310/155057.shtml

    IOS7下设置UINavigationBar的颜色的方法已经改变(当然如果是用自定义图片的话请忽略---) 首先是区别iOS7和之前版本的方法如下: //如果是iOS7以前的话if (floor(NS ...

  4. [BZOJ1322]Destroying The Graph

    题目大意:有一张有向图,对于每个点,有两种操作:1. 删掉它的所有入边2. 删掉它的所有出边对每个点的每个操作均有不同的价值.求使得图上没有边的最小价值.解题思路:考虑把点拆成入点和出点,然后就是二分 ...

  5. CF833B The Bakery (线段树+DP)

    题目大意:给你一个序列(n<=35000),最多分不大于m块(m<=50),求每个块内不同元素的数量之和的最大值 考试的时候第一眼建图,没建出来,第二眼贪心 ,被自己hack掉了,又随手写 ...

  6. 使用Storyboard实现复杂界面

    http://blog.csdn.net/guchengluoye/article/details/7472771 http://wangjun.easymorse.com/?p=1564 http: ...

  7. java中 flush()方法的作用

    flush() 是清空,而不是刷新啊.一般主要用在IO中,即清空缓冲区数据,就是说你用读写流的时候,其实数据是先被读到了内存中,然后用数据写到文件中,当你数据读完的时候不代表你的数据已经写完了,因为还 ...

  8. l洛谷 P2326 AKN’s PPAP

    P2326 AKN’s PPAP 题目描述 “I have a pen,I have an apple.Eh,Apple-Pen!. I have a pen,I have pineapple.En, ...

  9. 洛谷 P2393 yyy loves Maths II

    P2393 yyy loves Maths II 题目背景 上次蒟蒻redbag可把yyy气坏了,yyy说他只是小学生,蒟蒻redbag这次不坑他了. 题目描述 redbag给了yyy很多个数,要yy ...

  10. 极路由4pro(HC5962)设置阿里云DDNS

    v2ex有个帖子说用Dnspod的API可以一行搞定,不过我既然买的是阿里云的域名还是想尽量用阿里云的API,感觉比较安全,另外修改解析记录后也会自动发邮件通知,所以还是调用阿里云的API吧.阿里云的 ...