(1)找出字符串s=”aaabbbccceeefff111144444″中,字符出现次数最多的字符

考虑去重,首先将字符串进行过滤去重,这样在根据这些字符进行循环查询时,将会减少循环次数,提升效率。但是本人写的代码较为臃肿,有更好的希望留言评论

str = 'a1fsfs111bbbcccccvvvvvnnnnboooooosssnb'
class Countvalue():
def countvalue(self, str1)
  • 1.
  • 2.
  • 3.

利用set自身的去重功能
param str1: 对传进来的字符串过滤
return: 返回一个不含重复字符的list

list1 = [] result = [] for x in str1:
list1.append(x)
result = set(list1)
return result
def count(self, str1)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

对已经去重的list进行遍历,因已去重

从而减少循环次数,提高检索效率

list = Countvalue().countvalue(str1)
a = 0
tump = {}
for x in list:
test = str1.count(x)
if test > a:
tump.clear()
a = test
tump[x] = a
elif test == a:
a = test
tump[x] = a
return tump
if __name__ == '__main__':
print(Countvalue().count(str))
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

不考虑去重

s = "xssdddeeeeeeeffff"
max_times = 0
result = {}
for i in s:
if s.count(i) > max_times:
result.clear()
result[i] = s.count(i)
max_times = s.count(i)
elif s.count(i) == max_times:
result[i] = s.count(i)
print result
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

(2)有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

按题意直接写出来

arr = [] for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
num = 100*i+10*j+k
if i!=j and j!=k and i!=k and num not in arr:# 互不相同且无重复数字的三位数
arr.append(num)
print(len(arr),arr)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

其实python自带排列组合模块,可以直接调用。

也知道这个写法,只是函数记不住,还是百度一下才能写出来。如果这是面试题,能写出后一种当然好,不能的话还是老老实实的按照上面的思路来吧。

import itertools
temp_arr = list(itertools.permutations([1, 2, 3, 4], 3)) # 排列 # A_4^3 = (4)!/(4-3)! = (4*3*2*1)/1 = 24
arr = [100*t[0]+10*t[1]+t[2] for t in temp_arr] print(len(arr),arr)
  • 1.
  • 2.
  • 3.

(3)企业发放的奖金根据利润(I)的多少来提成:

低于或等于10万元时,奖金可提10%;

利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;

20万到40万之间时,高于20万元的部分,可提成5%;

40万到60万之间时高于40万元的部分,可提成3%;

60万到100万之间时,高于60万元的部分,可提成1.5%;

高于100万元时,超过100万元的部分按1%提成。

从键盘输入当月利润I,求应发放奖金总数?

'''

def tm002():

'''

程序分析:请利用数轴来分界,定位。

【思路】:这种处理数轴问题的写法,值得参考。比elif的写法,简洁方便的多。

money = int(input('净利润:'))
arr = [1000000,600000,400000,200000,100000,0] rat = [0.01,0.015,0.03,0.05,0.075,0.1] bonus = 0
for i in range(len(arr)):
if money>arr[i]: # 对于处于区间的部分
bonus+=(money-arr[i])*rat[i] # 计算并累加奖励
money=arr[i] # 剩余部分
print(bonus)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

(4)一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

【思路】:网站上是求了一下方程,没细看。
python又不是没有开方函数,直接按字面意思解了。

import math
for i in range(1000):
x = math.sqrt(i+100)
y = math.sqrt(i+100+168)
if x%1==0 and y%1==0:
print(i)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

(5)输入某年某月某日,判断这一天是这一年的第几天?

【思路】:知道python有时间元组这一概念,这道题完全不需要计算。

时间元组包含九个属性

tm_year 年

tm_mon 月(1~12)

tm_mday 日(1~31)

tm_hour 时(0~23)

tm_min 分(0~59)

tm_sec 秒(0~61, 60或61是闰秒)

tm_wday 星期(0~6, 0是周一)

tm_yday 第几天(1~366, 366是儒略历)

tm_isdst 夏令时(平时用不到)

import time
date = input('输入时间(例如2018-01-23):')
st = time.strptime(date,'%Y-%m-%d') # 时间文本转化成时间元祖
num = st.tm_yday
print(num)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.