Home > other >  Python control flow and data structure of the problem
Python control flow and data structure of the problem

Time:12-03

question1, 2



question3



question4




I don't know this a few questions to how to write hope leaders can help me!!!!!!! Thank you!!!!!!

CodePudding user response:

Def numCharacters (s) :
The count=0
For I in s:
If i.i salpha () :
Count +=1
Return the count

Def upper_sentence (s) :
First_flag=True
Letter_list=s.s plit (" ")
Letter_list=[x.c apitalize () for x in letter_list]
Ret="". Join (letter_list)
Return ret

CodePudding user response:

 import string 

Def numCharacters (s) :
Return len ([I for I s the if in i.l power () in the string. The ascii_letters])

S='I am 123. @ student'
Res=numCharacters (s)
Print (res)

Def upper_sentence (s) :
Return s.t itle ()

S='for nothing now can (last come to any good.'
Res=upper_sentence (s)
Print (res)

Def sort_point (points) :
Return sorted (points, key=lambda x: x (x [0], [1]))

P=[(1, 2), (5, 6), (7, 8), (2, 3)]
P=[(1, 2), (5, 6), (7, 8), (2, 3), (0, 1)]
Res=sort_point (p)
Print (res)

Def winprize (people, winning_number) :
Lt=[(k, len (set (v) & amp; set (winning_number))) for k, v in people. The items ()]
Return sorted (lt, key=lambda x: x [1], reverse=True)

People={' character ': (7, 6, 5, 4, 3, 2, 1),
'Harry: (7, 2, 8, 3, 10, 6, 1),
'Ron' : (1, 2, 3, 4, 11, 6, 7),
'Voldemort: (14, 17, 4, 24, 25, 3, 29),
'Dumbledore: (1, 2, 3, 4, 14, 6, 30),}
Winning_number=(1, 2, 3, 4, 5, 6, 7)
Res=winprize (people, winning_number)
Dic={}
For the name, num in res:
If num==7:
Key='first_prize'
Se=dic. Get (key, set ())
Se. The add (name)
Dic [key]=se
Elif num & gt;=4 and num & lt;=5:
Key='third_prize'
Se=dic. Get (key, set ())
Se. The add (name)
Dic [key]=se

Print (dic)
  • Related