Python函數與資料結構練習題一

來源:互聯網
上載者:User

標籤: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函數與資料結構練習題一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.