標籤:size example word inpu 一段 ase 思維 XA 思路
把單詞按首字母順序排列
# change this value for a different result
#思路:使用sort+hey
my_str = "Hello this Is an Example With cased letters"‘‘‘這是一種很麻煩很難受的解法a = my_str.upper()print(a)b = a.split(‘ ‘)print(b)b.sort()print(b)#your solution here‘‘‘#使用key一條搞定sorted(my_str.split(),key=str.lower)?#輸出結果[‘an‘, ‘cased‘, ‘Example‘, ‘Hello‘, ‘Is‘, ‘letters‘, ‘this‘, ‘With‘]
統計一段話裡面每個母音字母各出現了多少次
#思路:產生字典,使用fromkey# string of vowelsvowels = ‘aeiou‘counter = {}.fromkeys(vowels,0)# change this value for a different resultin_str = ‘Hello, have you tried our turorial section yet?‘# make it suitable for caseless comparisionsin_str = in_str.casefold()# make a dictionary with each vowel a key and value 0# your soultion here count the vowelsprint(counter)#輸出結果{‘a‘: 0, ‘e‘: 0, ‘i‘: 0, ‘o‘: 0, ‘u‘: 0}找出名單中名字最長的人
#思路:很簡單,遍曆列表使用len()求最長names = ["Joshua Zhao", "XiaoLee", "Ze", "Josh"]longest = names[0]# your solution herefor name in names: if len(name) > len(longest): longest = name?print(longest) #輸出結果Joshua Zhao
一個歌唱比賽的歌手打分,我們設計一個程式協助現場去掉一個最低分和一個最高分,再計算一個平均分
例如分數為: [8,9,5,10,9.5,8,7,9,9.5] 則去掉最低分 [8,9,5,10,9.5,8,9,9.5]
#思路:遍曆找出分別找出最小和最大值,然後使用remove()去掉,最後求數組總和再除以len(values)values = [8,9,5,10,5,8,7,9,9.5]‘‘‘這種方法用邏輯的思路實現,麻煩了點但是可以訓練下思維#求最小分small_pos = values[0]for small_grade in values: if small_grade < small_pos: small_pos = small_gradeprint(‘最小分:%d‘ % small_pos)?#求最大分high_pos = values[0]for high_grade in values: if high_grade > high_pos: high_pos = high_gradeprint(‘最大分:%d‘ % high_pos)?values.remove(values[small_pos])values.remove(values[high_pos])print(values)‘‘‘#不好意思,python內建可以直接搞定values.remove(max(values))values.remove(min(values))a = sum(values)/len(values)print(a)#輸出結果7.928571428571429
設計一個函數,反向列印list裡面的資料
def print_reversed(values) : # Traverse the list in reverse order, starting with the last element # your solution here i = len(values) - 1 reverses = [] while i > 0: print(values[i],end=‘,‘) i = i - 1 print_reversed([3,4,52,3,1,5,6,78,3]) #輸出結果3,78,6,5,1,3,52,4,
3,78,6,5,1,3,52,4,
Python函數與資料結構練習題一