Python-interview-coding


给定一个长度为 n 的可能有重复值的数组,找出其中不去重的最小的 k 个数。
例如数组元素是4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4(任意顺序皆可)。
def GetLeastNumbers_Solution(tinput, k):
if len(tinput) < k:
return
else:
tinput.sort()
return tinput[:k]
if __name__ == "__main__":
print(GetLeastNumbers_Solution([3,4,5,65,76,7,88,88],5))

输出结果:[3, 4, 5, 7, 65]



#给定一个数组arr,返回arr的最长无重复元素子数组的长度,无重复指的是所有数字都不相同。


子数组是连续的,比如[1,3,5,7,9]的子数组有[1,3],[3,5,7]等等,但是[1,3,7]不是子数组.

def maxLength(arr):
    res = 0
    list1=[]
    for i in arr:
        while i in list1:
            list1.pop(0)
        list1.append(i)
        res = max(res,len(list1))
    return  res
if __name__=="__main__":
    arr= [2,3,5,5,4]
    print(maxLength(arr))   3

 


#已知一个列表,其中的元素都是整数,写出一个函数或者方法,返回列表中重复元素的值,
以及这个元素的第一个索引和最后一个索引,输出形式不限,语言不限。

def findDuplicatedElement4(plist):
    pdict = dict()
    for i in range(len(plist)):

        if plist.count(plist[i]) > 1:
            if plist[i] not in pdict:
                pdict[plist[i]] = (i, len(plist) - plist[::-1].index(plist[i]) - 1)

    return pdict

plist= [1,5,7,8,7,7,7,9]

value =findDuplicatedElement4(plist)
print(value)
{7: (2, 6)}


#已知一个列表,其中的元素都是整数,且按照升序排列,写出一个函数或者方法,返回列表中重复元素的值,
以及这个元素的第一个索引和最后一个索引,输出形式不限,语言不限。
def find(plist):
    dict = {}
    for i in range(len(plist)):
        if plist.count(plist[i])>1:
            if plist[i] not in dict:
                dict[plist[i]]=(i,(i+plist.count(plist[i]))-1)

    return dict

plist= [1,5,7,7,7,8,8,9]
value =find(plist)
print(value) 


输出结果:{7: (2, 4), 8: (5, 6)}

  



一行代码求和,要求返回100-10000以内所有能够被7整除的奇数中去掉其中最大值和最小值之后的总和。

print(sum([i for i in range(100,10001) if i % 7==0 and i%2 ==1][1:-1]))


# 写一个函数,实现通过交换字典中的Key和Value生成新的一个字典

如字典a = {"name":"rotate", "age":29, "score":88},经过交换后变成{"rotate":"name", 29:"age", 88:"score"}。
def pDict():
    a = {"name":"rotate", "age":29, "score":88}
    b = {v:k for k,v in a.items()}
    print(b)
pDict()

输出结果:{'rotate': 'name', 29: 'age', 88: 'score'}

 

 
#列表 plist = [734, 8465, 94, 4345, 653, 266, 665, 5],写一个函数,
按照如下的字符串形式 "94846573466565354345266" 输出。
def listSort(plist):
    list = [str(x) for x in plist]                  ['734', '8465', '94', '4345', '653', '266', '665', '5']
list.sort(reverse=True) #将数值列表转换成字符串列表 ['94', '8465', '734', '665', '653', '5', '4345', '266']
    list = ''.join(list)    #List列表转为Str字符串  94846573466565354345266
    return list

if __name__ == "__main__":
plist = [734, 8465, 94, 4345, 653, 266, 665, 5]
result = listSort(plist)
print(result)
 

#找出字符串中出现的第一个不重复的字符.

#coding=utf-8
def findFirstCharacter(pstr):
    for i in range(len(pstr)):
        if pstr.index(pstr[i]) == pstr.rindex(pstr[i]):
            return pstr[i]
    return None

pstr = "djkljjkjkjlda"
result = findFirstCharacter(pstr)
print(result)

输出结果:a  

 

# 判断字符串是否是一个ip地址(ipv4地址,如 192.168.1.1)


#coding=utf-8
def isIpAddress(ip_address): if ip_address.count('.') != 3: return False else: ip = ip_address.split('.') for i in ip: try: one_ip = int(i) if one_ip >= 0 and one_ip <= 255: pass else: print(one_ip) return False except: print("{0} is not a number.".format(i)) return False return True if __name__ == "__main__": while True: myIp = input("please enter ip: ") if isIpAddress(myIp): print("{0} is a legal ip".format(myIp)) else: print("this is not a legal ip.")

  


#统计字符串中的字符出现的次数。
pstr = "hdkfjowfojlkjal"
pDict = {}
for i in pstr:
pDict[i] = pstr.count(i)
print(pDict)

输出结果:
{'h': 1, 'd': 1, 'k': 2, 'f': 2, 'j': 3, 'o': 2, 'w': 1, 'l': 2, 'a': 1}

 

 

 #删除重复的二维列表

二维列表中,里面元素能用元组则用元组

 

 

 

 

 
posted @ 2020-05-29 14:10  我是一只小小小小鸟~  阅读(530)  评论(0编辑  收藏  举报