《Craking the Coding interview》python实现---01
###题目:给定一个字符串,判断其中是否有重复字母
###思路:将重复的字符放入到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的更多相关文章
- 《Craking the Coding interview》python实现---02
###题目:翻转一个字符串###思路:从字符串的最后一位开始,依次取###实现:伪代码.函数.类实现#伪代码: #01string=sNew_s=""for i in range( ...
- Craking the coding interview 面试题:完美随机洗牌
给定一个序列,随机打乱这个序列,新产生的序列和任意一个序列产生的可能性是一样的,就是所谓的完美随机洗牌. 看下面的运行结果: 上面第一列是原数列,下面一行是新产生的打乱的数列. 基本思想:如果n-1个 ...
- Python Coding Interview
Python Coding Interview Python Advanced Use enumerate() to iterate over both indices and values Debu ...
- Python学习--01入门
Python学习--01入门 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.和PHP一样,它是后端开发语言. 如果有C语言.PHP语言.JAVA语言等其中一种语言的基础,学习Py ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 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 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- 转:Top 10 Algorithms for Coding Interview
The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
随机推荐
- TCP/IP 三次握手和HTTP过程
0 引言 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在“无差别”的网络之上. 1 T ...
- [置顶]
Linux 常用命令集锦
出处:http://www.vaikan.com/what-are-the-most-useful-swiss-army-knife-one-liners-on-unix/ Linux命令行里的&qu ...
- IOS - 6\7下UINavigationBar的颜色的方法改变 ——转载http://www.th7.cn/Program/IOS/201310/155057.shtml
IOS7下设置UINavigationBar的颜色的方法已经改变(当然如果是用自定义图片的话请忽略---) 首先是区别iOS7和之前版本的方法如下: //如果是iOS7以前的话if (floor(NS ...
- [BZOJ1322]Destroying The Graph
题目大意:有一张有向图,对于每个点,有两种操作:1. 删掉它的所有入边2. 删掉它的所有出边对每个点的每个操作均有不同的价值.求使得图上没有边的最小价值.解题思路:考虑把点拆成入点和出点,然后就是二分 ...
- CF833B The Bakery (线段树+DP)
题目大意:给你一个序列(n<=35000),最多分不大于m块(m<=50),求每个块内不同元素的数量之和的最大值 考试的时候第一眼建图,没建出来,第二眼贪心 ,被自己hack掉了,又随手写 ...
- 使用Storyboard实现复杂界面
http://blog.csdn.net/guchengluoye/article/details/7472771 http://wangjun.easymorse.com/?p=1564 http: ...
- java中 flush()方法的作用
flush() 是清空,而不是刷新啊.一般主要用在IO中,即清空缓冲区数据,就是说你用读写流的时候,其实数据是先被读到了内存中,然后用数据写到文件中,当你数据读完的时候不代表你的数据已经写完了,因为还 ...
- 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, ...
- 洛谷 P2393 yyy loves Maths II
P2393 yyy loves Maths II 题目背景 上次蒟蒻redbag可把yyy气坏了,yyy说他只是小学生,蒟蒻redbag这次不坑他了. 题目描述 redbag给了yyy很多个数,要yy ...
- 极路由4pro(HC5962)设置阿里云DDNS
v2ex有个帖子说用Dnspod的API可以一行搞定,不过我既然买的是阿里云的域名还是想尽量用阿里云的API,感觉比较安全,另外修改解析记录后也会自动发邮件通知,所以还是调用阿里云的API吧.阿里云的 ...