The Python function and data structure exercises one

Source: Internet
Author: User

Sort the words in alphabetical order
#Change this value for a different result
#思路: Using Sort+hey
My_str ="Hello This is a Example with cased letters"" "This is a very troublesome and difficult solution A = My_str.upper () print (a) b = A.split (") print (b) B.sort () print (b) #your solution here" "#use key to get a handleSorted (My_str.split (), key=str.lower)?#Output Results[' an','cased','Example','Hello',' is','Letters',' This',' with']
Count how many times each vowel appears in a sentence.
#idea: Generate dictionaries, use Fromkey#string of vowelsvowels ='Aeiou'counter={}.fromkeys (vowels,0)#Change this value for a different resultIn_str ='Hello, are 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)#Output Results{'a': 0,'e': 0,'I': 0,'o': 0,'u': 0}
Find the person with the longest name on the list
#idea: It's easy to traverse the list using Len () to find the longestnames = ["Joshua Zhao","Xiaolee","Ze","Josh"]longest=Names[0]#your solution here forNameinchnames:ifLen (name) >Len (longest): Longest=name?Print(Longest)#Output ResultsJoshua Zhao
A singer of a singing contest scores, we design a program to help remove a minimum and a maximum score, and then calculate an average score

For example, the score is: [8,9,5,10,9.5,8,7,9,9.5] minus the lowest score [8,9,5,10,9.5,8,9,9.5]

#idea: Traverse to find the minimum and maximum values respectively, then remove () and then divide the sum of the array by Len (values) .values = [8,9,5,10,5,8,7,9,9.5]" "This approach is implemented in a logical way, but it's a little tricky but you can train your mind to find the minimum small_pos = values[0]for small_grade in Values:if Small_grade < small_pos: Small_pos = Small_gradeprint (' min:%d '% small_pos)? #求最大分high_pos = Values[0]for High_grade in values:if High_grade & Gt High_pos:high_pos = High_gradeprint (' Max points:%d '% high_pos)? Values.remove (Values[small_pos]) Values.remove (Values[high _pos]) print (values)" "#Sorry, Python built-in can be done directlyValues.remove (values) values.remove (min (values)) a= SUM (values)/len (values)Print(a)#Output Results7.928571428571429
Design a function to reverse print the data in the list
 def   print_reversed (values):  #   Traverse the list in reverse order, starting with the last El Ement  #   your solution here  i = len (va lues)-1 reverses  = []  while  i > 0:  print  (Valu Es[i],end= " ,   " ) I  = I-1 print_reversed ([ 3,4,52,3,1, 5,6,78,3])  #   Output  3,78,6,5,1,3,52,4, 

3,78,6,5,1,3,52,4,

The Python function and data structure exercises one

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.