###题目:给定一个字符串,判断其中是否有重复字母
###思路:将重复的字符放入到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. C# 基础复习 四 ADO

    调用存储过程,并接收多个表的查询结果 一个表用一个DataTable,DataSet可以存储多个DataTable DataSet需要SqlDataAdapter来填充数据 SqlConnection ...

  2. 如何设置,获取,删除cookie?

    cookie : 存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来像访问者电脑上存储数据 1.不同的浏览器存放的cookie位置不一样,也是不能通用的 2.cookie的存储 ...

  3. python的父类和子类中关于继承的不同版本的写法

    Python 2.7中的继承 在Python 2.7中,继承语法稍有不同,ElectricCar 类的定义类似于下面这样: class Car(object): def __init__(self, ...

  4. python中try…except的使用,处理程序异常

    通常情况下,在python中运行程序,多多少少会出现程序异常的问题,try……except能很好的解决程序中的异常.以下是其用法,在不同位置时进行什么样的工作和起到什么样的作用. try: 可能出现异 ...

  5. navicat连接SQL Sever数据库出现的问题

    1.navicat 连接sqlserver提示要安装 sql server native client 解决办法:在navicat安装目录下有个sqlncli_x64.msi文件,双击安装一直下一步就 ...

  6. 【 【henuacm2016级暑期训练】动态规划专题 G】 Palindrome pairs

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先用枚举回文串中点的方法. 得到这个字符串中出现的所有的回文. 得到他们的左端点以及右端点. 整理成一个pair<int,in ...

  7. 【Codeforces Round #483 (Div. 2) C】Finite or not?

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 有个性质. 如果p/q是分数的最简形式. 那么p/q能化成有限小数. 当且仅当q的质因数分解形式中只有质因子2和5 (且不能出现其他 ...

  8. fedora linux源代码下载

    yumdownloader --source kernel 如果是下载insight 就是 yumdownloader --source insight 下载到的是当前目录. 然后在用rpm2cpio ...

  9. POJ 2084

    第一题组合数学题.可以使用递推,设1与其他各数分别连边,假设N=3;若1-4,则圆分成两部分计数,此时可以利用乘法原理.(高精度) #include <cstdio> #include & ...

  10. npm API文档

    npm API文档 https://docs.npmjs.com/