《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 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
随机推荐
- debian系统包管理工具aptitude
注意:aptitude与 apt-get 一样,是 Debian 及其衍生系统中功能极其强大的包管理工具.与 apt-get 不同的是,aptitude在处理依赖问题上更佳一些.举例来说,aptitu ...
- java对word文档的操作(提取标题和内容等)-直接操作或poi工具包或freemarker+xml或html转word
1,java自带工具包实现对word的排版和写入 import java.awt.Color; import java.io.FileNotFoundException; import java.io ...
- 虚拟机安装mac
没成功,把几篇不错的文章先记录下地址 http://bbs.pcbeta.com/forum.php?mod=viewthread&tid=1437039 http://bbs.pcbeta. ...
- vue定义对象变量并合并成新的对象
背景: 一般情况下,向后台发送数据请求会存在公共的变量,为了避免每一个相同部分的变量都重新定义,则想出以下解决方案: 例如一下:function,version,Authorization是公共请求部 ...
- Ajax原理-重点
Ajax原理 AJAX即“Asynchronous Javascript And XML”,是指一种创建交互式网页应用的网页开发技术.AJAX 是一种用于创建快速动态网页的技术.它可以令开发者只向服务 ...
- Vue中如何在组件内部实现一个双向数据绑定?
假设有一个输入框组件,用户输入时,同步父组件页面中的数据. 具体思路:父组件通过props传值给子组件,子组件通过 $emit 来通知父组件修改相应的props值,具体实现如下: import Vue ...
- windows关于定时执行的php脚本
根据业务需求,需要服务器每天定时执行一些脚本,如后台提交数据,定时处理数据库等. 最初的思路是在某个控制器里写好方法,加入code验证,定期的用计划任务去访问.由于window计划任务这方面比较low ...
- Spring注解+Axis2开发WebService
用Spring注解方式: 配置扫描指定包下的类 <context:component-scan base-package="包名" /> 标识类为spring管理的 ...
- 极路由4pro安装java(Jamvm 2.0.0 + gnu classpath 0.9.8)
首先试了gnu classpath 0.9.9,编译不过后来改成0.9.8 编译环境 OS: 64位 Ubuntu 16.04 LTS(vmware虚拟机) SDK: 用之前讲过的官方SDKmtmip ...
- 10.2.0.4 to 10.2.0.5 Installation of Patch Set Release (Windows)
环境:10.2.0.4集群数据库zlm10g(双节点,zlm10g1,zlm10g2) 系统:Windows 2003 Server 64Bit 内存:2G RAM 存储:ASM 目标:把集群数据库从 ...