python练习题

简介: 2道外企面试题。1、统计连续字符串出现最大频次#!/usr/bin/env python#-*-coding:utf-8def maxblock(x):l = [ i for i in x ] #或者l=list(x)num=[]n=1for i in range(len(l)-1):if l[i] == l[i+1]:n+=1else:n=1num.append(n)print max(num)maxblock("waerfsddddamssddssssaasda")2、删除字符串中的连续重复的字符,使其唯一。

2道外企面试题。

1、统计连续字符串出现最大频次

#!/usr/bin/env python
#-*-coding:utf-8

def maxblock(x):
l = [ i for i in x ] #或者l=list(x)
num=[]
n=1
for i in range(len(l)-1):
if l[i] == l[i+1]:
n+=1
else:
n=1
num.append(n)
print max(num)
maxblock("waerfsddddamssddssssaasda")

2、删除字符串中的连续重复的字符,使其唯一。

def drop(x):
print x
l=list(x) #将str转换成列表
for i in range(len(l)-1):
if l[i] == l[i+1]:
l[i]=""
#首先想到的是del,结果一直报超出index,因为删除元素后列表动态变短了
print "".join(l) #列表转换为str,必须使用join函数。

drop("aaa234dddd555sd")

目录
相关文章
|
7月前
|
存储 Python
Python经典练习题(三)
Python经典练习题(三)
|
3月前
|
算法 Python
python文件读取操作练习题(统计单词)
python文件读取操作练习题(统计单词)
47 0
|
7月前
|
Python
Python经典练习题(二)
Python经典练习题(二)
|
7月前
|
Python
Python经典练习题(一)
Python经典练习题(一)
|
8月前
python--多态和两个练习题
python--多态和两个练习题
|
10月前
|
Python
Python|PTA练习题
Python|PTA练习题
93 0
|
10月前
|
数据安全/隐私保护 Python
Python——字符串以及分支语句练习题
Python——字符串以及分支语句练习题
|
10月前
|
知识图谱 Python
Python——变量以及基础数据类型练习题
Python——变量以及基础数据类型练习题
|
机器学习/深度学习 Python
Python 数学练习题(二)
Python 数学练习题
349 1
|
Python
Python 数学练习题(一)
Python 数学练习题
230 0

热门文章

最新文章