Reverse - Tom Kleen



String ExercisesThere are built-in functions to accomplish some of the tasks below. You are not to use them. You must write your own.ReverseWrite a function called reverse that will accept a single string and return a copy of the string with its characters reversed. Example:print(reverse('Hello')) will print:olleHYour program should loop repeatedly until the user enters an empty string.RemoveWrite a function called remove that will accept two strings: (1) a character to remove from a string, and (2) the string from which the character should be removed. Return a copy of the second argument with all occurrences of the first argument removed. You may assume that the first string is exactly one character long. Your function does not have to worry about matching a letter that is in a different case. That is, if the letter that is passed in is "x", you do not have to look for "X", just the lowercase "x".Example:print(remove('o', 'four'))print(remove('x', 'hello'))Will print:furhelloYour program should loop repeatedly until the user enters an empty string.Palindromes, part 1Write a function called is_palindrome that recognizes palindromes (a palindrome is a string that reads the same forwards as backwards, e.g. "madam"). It will accept one string and will return True if the string is a palindrome, and False if the string is not a palindrome.Example:print(is_palindrome('madam'))print(is_palindrome('Fred'))will print:TrueFalseYour program should loop repeatedly until the user enters an empty string.Palindromes, part 2Write a more general palindrome recognizer function . A less strict definition of a palindrome is: a string in which the letters are the same forwards as backwards. This ignores spaces, punctuation, capitalization, etc., and looks only at the letters. Call this one is_pal to distinguish it from the previous function. Exampleprint(is_pal('Madam, I'm Adam.'))will print:TrueHint: Consider writing another helper function that will remove all non-alphabetic characters from a string. Note that it is impossible to remove any characters from a string, so you will have to make a new string that consists of the alphabetic characters of the string.Your program should loop repeatedly until the user enters an empty string.get_initials(name)Write a function called get_initials that will accept a single string in the format: firstname, middleinitial/middlename, lastname and return the three initials in upper case. Use find and rfind.Exampleprint(get_initials("John F. Kennedy"))will printJFKYour program should loop repeatedly until the user enters an empty string.format_ssn(ssn)Write a function called format_ssn that will accept a string that holds a Social Security Number (9 digits) and return the SSN with hyphens between the 3rd and 4th characters and also between the 5th and 6th characters. Before returning a result, check to make sure that the string has exactly 9 digits. If leading or trailing blanks are part of the SSN, trim them before proceeding. If the trimmed string is not 9 characters long or has any non-digits, return the string "Invalid SSN" along with a description of the problem..print(format_ssn("123456789"))123-45-6789print(format_ssn(" 123456789 "))123-45-6789print(format_ssn("1234567890"))Invalid SSN. Too long.print(format_ssn("abcdefghi"))Invalid SSN. Non-numeric characters.Your program should loop repeatedly until the user enters an empty string.More email addressesWrite a function called make_email that accepts a single string in the format (firstname lastname studentIDnumber). There will be one blank between firstname and lastname and one blank between lastname and studentIDnumber. Create and return an email for the user that consists of the first three characters of the first name followed by the first three characters of the last name followed by the last three digits of the ID number. It is possible that first or last names will be less than three characters long. In that case, use the entire name. All letters in the email address must be in lowercase letters. Follow this string with "@briarcliff.edu". Call this function from a loop in the main program. Continue to ask the user for input until he enters an empty string ("").Examples:Enter a name and ID: Tom Kleen 1234567The email address is: tomkle567@briarcliff.eduEnter a name and ID: Jo Bo 1234567890The email address is: jobo890@briarcliff.eduYour program should loop repeatedly until the user enters an empty string.Date formatsWrite a function called long_date that will accept a date in the format m/d/y (e.g. "1/1/2020", or " 01/01/20 ") and display the date in the format January 1, 2018.Input: The input will be a string made up only of digits and two slashes. There may be leading or trailing white space. The string will always represent a valid date. You don't have to worry about something like 15/99/2020. However, the year may be two or four digits. If it is two digits, assume that the first two are supposed to be '20'. The month or day may have a leading zero (second example above) and if it does, you must strip it. The month will always be between 1 and 12 (inclusive) and you will have to convert it to "January", "February", etc. You will have to put one space after the month name and a comma and space after the day. Write three helper functions called getMonth, getDay, and getYear. Each will accept the input string and will return the appropriate month, day, and year (as strings, even the day and year can be returned as strings that look like numbers). Call these from long_date. Take the results from the three functions and put them together and print out the date. Your program should loop repeatedly until the user enters an empty string.ExampleEnter a date in the format 'm/d/y': 1/1/18January 1, 2018Enter a date in the format 'm/d/y': 01/01/2018January 1, 2018Enter a date in the format 'm/d/y': 12/31/2017December 31, 2017Enter a date in the format 'm/d/y':End of program.More date formatsWrite a function called short_date that is similar to the previous one, but goes in the opposite direction. It takes a date like January 1, 2020 and turns it into the string 1/1/2020. A call would look like this: short_date("January 1, 2020"), and the function would return "1/1/2020"Exampledate = January 1, 2020Print(short_date(date))Will print:1/1/2020Your program should loop repeatedly until the user enters an empty string.titleCase(aString)Write a Python function to accept a string and return it in title case (with each word capitalized) and all letters except the first in lowercase. You may assume that there is one space between words.ExampleaString = "how to write python PROGRAMS"print(titleCase(aString))will print:How To Write Python ProgramsYour program should loop repeatedly until the user enters an empty string.most_frequent_characterWrite a function called most_frequent_letter t will accept a string and return the letter that occurs most frequently in the string. Pass in the string, return the character that is most common and the number of times it occurred. Count the upper-case and lower-case version of a letter as the same letter. Hint: count how many times the first letter in the string occurs. Then check how many times the second letter in the string occurs, etc. It is legal in Python to return more than one thing from a function. Just separate the things you want to return with a comma. Hint: this will require a nested loop. The outer loop steps through each character in the string and the inner loop also steps through each character in the string. For example if the string is "abca", the outer loop index variable will be set to "a". Then the inner loop will step through the string looking for "a". It will count 2 of them. Save the letter "a" and the number 2. Then the outer loop index variable will be set to "b". Then the inner loop will step through the string looking for "b". It will count 1 of them. Since this is NOT greater than the 2 that you saved, you do nothing. The outer loop index variable gets set to "c", etc.Example:print(mostFrequentLetter("abacadaeaf"))print(mostFrequentLetter("aBacadaeBBbbbbaf"))will print:a (or A)b......(or B)Your program should loop repeatedly until the user enters an empty string.contains_all_alpha(aString)Write a Python function called contains_all_alpha to check if a string contains all 26 letters of the alphabet. The letters may be either upper case or lower case or any combination of the two. The function does NOT print anything.ExampleaString = "abcdefghijklmnopqrsTUVWXYZ"print(contains_all_alphabet(aString))aString = "abcdefghijklmnopqrsTUVWXY"print(contains_all_alphabet(aString))Will print:TrueFalseYour program should loop repeatedly until the user enters an empty string. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download