《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 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
随机推荐
- Vue文件封装日历组件
封装就是要具有灵活性,样式自适应,调用的时候传入props就可以变成自己想要的样式. 效果展示网址:https://1963331542.github.io/ 源代码: <template> ...
- vue2 在methods 中无法获取this对象
在methods中使用箭头函数无法获取this ExamName:()=> { console.log(this);} 这样就行了: ExamName:function() { console. ...
- GitHub两种上传方式的对比----SSH / https
https://www.jianshu.com/p/1ac06bcd8ab5 https://www.cnblogs.com/lqfxyy/p/5740720.html https://blog.cs ...
- JS生成数字加减乘法验证码
给大家分享一个简单的js验证码生成代码 PS:该代码依赖Jquery1.4版本以上 传入元素 如productionVerificationCode(#\(("a")) 反回验证码 ...
- 【codeforces 734F】Anton and School
[题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...
- MyBatis学习总结(5)——实现关联表查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- cmd 下命令
tasklist 查看当前进程 taskkill /? 查看taskkill 的帮助信息 详情 cmd /? 查看cmd详情 color /? 查看颜色详情 比如 color 2 md d:\ji ...
- node--20 moogose demo2
db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库 ...
- SaltStack介绍——SaltStack是一种新的基础设施管理方法开发软件,简单易部署,可伸缩的足以管理成千上万的服务器,和足够快的速度控制,与他们交流
SaltStack介绍和架构解析 简介 SaltStack是一种新的基础设施管理方法开发软件,简单易部署,可伸缩的足以管理成千上万的服务器,和足够快的速度控制,与他们交流,以毫秒为单位.SaltSta ...
- nyoj--18--The Triangle(dp水题)
The Triangle 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure ...