String practice of data types

Source: Internet
Author: User

First question: known string a = "Aasmr3idd4bgs7dlsf9eaf", change the uppercase of a string to lowercase and lowercase to uppercase.

1 defHandle_str (Params,*arg,**args):#for the robustness of the function, define *arg,**args extra parameters to be passed here .2result="'3     ifIsinstance (PARAMS,STR):#robustness of functions, judging the type of input parameters4          foreachinchparams:5             ifEach.isalpha ():6                 ifeach.isupper ():7result=result+Each.lower ()8                 Else:9result=result+Each.upper ()Ten             Else: Oneresult=result+ each A         returnresult -     Else: -         return 'This function requires a string type of data' the assertHANDLE_STR ([1,2,3],5) = ='This function requires a string type of data' - PrintHANDLE_STR ([1,2,3],5) - assertHANDLE_STR ('A12CD')=='A12CD'  - PrintHANDLE_STR ('A12CD')         + PrintHandle_str (a)

The function you define yourself must clearly understand what the function is entering, what it outputs, for robustness, the necessary type checking, the exception catching,

Second question: Please take the number of a string out and output it as a new string.

1 defGet_int (Params,*arg,**args): #*arg,**args receives extra arguments2result="'3     ifIsinstance (PARAMS,STR):#parameter type judgment4          foreachinchparams:5             ifeach.isdigit ():6result=result+ each7         returnresult8     Else:9         return 'This function requires a string type of data'Ten assertGet_int ('a1b4c5d67', 2,3,4,5) = ='14567' One PrintGet_int ('a1b4c5d67', 2,3,4,5) A assertGet_int ([1,2],[1,2],[1,2]) = ='This function requires a string type of data' - assertGet_int (a) = ='This function requires a string type of data' - PrintGet_int (A) the PrintGet_int (a)

Question three: Count the occurrences of each letter that appears in a string (ignoring case, A is the same as a letter) and outputting it as a dictionary. Example {' A ': 4, ' B ': 2}

1 defCount_alp_times (Params,*arg,**args):#define *arg,**args to receive redundant parameters2Dict_count={}3     ifisinstance (PARAMS,STR):4          foreachinchparams:5             ifEach.isalpha ():6Loweach=each.lower ()#converts characters in a string to lowercase7                 ifLoweach not inchDict_count.keys ():#determines whether a character exists in the dictionary key8Dict_count[loweach]=19                 Else:TenDict_count[loweach]+=1 One         returnDict_count A     Else: -         return 'This function requires a string type of data'  - PrintCount_alp_times (a)

Question Fourth: Remove the letters that appear multiple times in a string, leaving only the one that appears first. Example ' Abcabb ', after removal, output ' abc '

1 defNo_repeat_str (params,*arg,**args):2str_list=[]3     ifisinstance (PARAMS,STR):4          foreachinchparams:5             ifeach not inchstr_list:6 str_list.append (each)7         return "'. Join (str_list)8     Else:9         return 'This function requires a string type of data' Ten assertNO_REPEAT_STR ('Abcadeys')=='Abcdeys' One PrintNO_REPEAT_STR ('Abcadeys')

Question Fifth: Please invert the A string and output it. Example: ' abc ' reversal is ' CBA ', a = ' aasmr3idd4bgs7dlsf9eaf '

1 " Aasmr3idd4bgs7dlsf9eaf " 2 # Convert a string to a list 3 # use the flip function of a list 4 Print ". Join (list_a)# Join function that uses string

Question sixth: After removing the number within a string, reorder the words in the string (A-Z) and re-export a sorted string. (Case-preserving, a-to-a-order relationship is: A is in front of a.) Example: AaBb)

1 defStr_sorted (Params,*arg,**args):#*,** receiving redundant parameters2result="'3     ifIsinstance (PARAMS,STR):#parameter type judgment4          foreachinchparams:5             ifEach.isalpha ():6result=result+ each7m=sorted (Result)#using sorted to sort strings from small to large8         return "'. Join (sorted (m,key=string.upper))#use the key keyword in the sorted built-in function to implicitly manipulate each item of an iteration object

Question seventh: At this time the word judgment, from ' Boy ' to four, is ' Boy ', ' Girl ', ' bird ', ' dirty ', please determine if each letter in these 4 strings appears in a string.

1 defFind_alp_many (target,sourse):2      foreachinchTarget#iterate through all the objects you want to find3         if  notIsinstance (EACH,STR):#determine the data type of each object to be looked up, filtering the non-string form of the4             return 'Not all items are string type'5         Else:6              forEachalpinchEach :7                 ifEachalp not inchSourse:#each item is searched8                     return 'Find%s not all Alpha was in Sourse'% each9     Else:Ten         return 'All Alpha in Sourse' OneA='ABCDYSOGIRDGTL' A assertFind_alp_many ([' Boy','Girl','Bird','Dirty'],a) = ='All Alpha in Sourse' - PrintFind_alp_many ([' Boy','Girl','Bird','Dirty'],A)

Question eighth: Output a string with the highest frequency of letters

1A ="Aasmr3idd4bgs7dlsf9eaf"2Dict_a={}3  foreachinchA:4     ifEach.isalpha ():5         ifeach not inchDict_a.keys ():6Dict_a[each]=17         Else:8Dict_a[each]+=19             TenData=Dict_a.items () One PrintSorted (data,key=LambdaX:x[1],reverse=true)

String practice of data types

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.