Python语言——面试编程题

python面试题

1、用python实现,查找字符串中的重复字母的个数

#coding:utf-8
def distinct_str(s):
    count ={}
    for i in set(s):
        count[i]=s.count(i)
        print i
    print(count)

s="aabbccddxxxxffffAAABBB111222"
distinct_str(s)

2、查找字符串中重复的字母个数,不区分大小写,字符串中还包括数字。

#coding:utf-8
tmpStr = "oweiiAJ3453DHHAAAaaaewi8829993711"
results = {}
for c in tmpStr.lower():
    if c.isalpha():
        if c not in results.keys():
            results[c]=0
        results[c] += 1
print results

3、查询10~100中能被3或5整除的数的和

#coding:utf-8

count = 0
for i in range(10,101):
    if i%3 == 0 or i%5 ==0:
        print i
        count = i +count
print count

4、有n个数,求任意n-1个数的和大于第n个数的情况,比如:有三个数,求任意两个数之和大于第三个数;

#coding:utf-8
def pr_triangle(mylist):
    count = sum(mylist)
    length = len(mylist)
    temp = 0
    for i in range(length):
        if count/2 > mylist[i]:
            temp +=1
        else:
            continue
    if temp == length:
        return '任意数小于其他数之和'
    else:
        return '不是任意数小于其他数之和'

mylist = [3,4,5,7,8,10]
print pr_triangle(mylist)

5、求数组中连续和最大的数

6、列表逆序获取

reverse

7、求字符串“abbabac”中最先重复最多次数的字符

8、字典,按字典序排序

9、校验括号配对情况,正确

# -*- coding: utf8 -*-
# 符号表
SYMBOLS = {'}': '{',']':'[',')':'(', '>':'<'}
SYMBOLS_L,SYMBOLS_R = SYMBOLS.values(),SYMBOLS.keys()
def check(s):
    arr = []
    for c in s:
        if c in SYMBOLS_L:
        # 左符号入栈
            arr.append(c)
        elif c in SYMBOLS_R:
            # 右符号要么出栈,要么匹配失败
            if arr and arr[-1] == SYMBOLS[c]:
                arr.pop()
            else:
                return False
            return True

print(check("3 * {3 +[(2 -3) * (4+5)]}"))
print(check("3 * {3+ [4 - 6}]}"))

你可能感兴趣的:(python代码遇到的问题)