python 面試題目

#1、數字反轉
i = 12345678
print(int(str(i)[::-1]))

#2、統計一個句子中單詞數量
s = "How old are you "
ls = s.strip().split()
print("There are {} words in this sentence:{}".format(len(ls),s))

#3、統計一個句子中的字母個數
s1 = "My name is lizexu"
alpha_count = 0
for i in s1:
    if i.isalpha():
        alpha_count +=1 
print("There are {} letters in this sentence:{}".format(alpha_count,s1))

#4、在一個二維數組中,查找一個指定數字

def findintinlist(l,integer):
    for i in range(len(l)):
        if l[i].count(integer) != 0:
            return i,l[i].index(integer)

l2 = [[1,2,3,4],[5,6,7,8]]
print(findintinlist(l2,8))

#5、找出一個字符串中只出現一次的字符,並返回他的位置,沒有返回-1
def findsingleletter(s):
    dic = {}
    for i,letter in enumerate(s):
        if letter not in dic:
            dic[letter] = i
        else:
            dic.pop(letter) 
    if dic == {}:
        return -1
    else:
        return dic

s = "abcddcaefbef"
print(findsingleletter(s))
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章