Florida Southern Sunset
CSC 2280: Intro Programming
(Fall 2024)

Syllabus

LMS

Teachers

Assignments
Assignments


Other Pages

Project 8:
I'm Stringing You Along


Assigned: Mon Oct 07 2024
Due: 11:59:00 PM on Tue Nov 05 2024
Team Size: 1
Language: Python
Out of: 51 points


In this project, you will write functions that manipulate and iterate over strings.

Part 0, 1 points: Write a function has_character, that takes three arguments: a string, an index, and a character and returns true if character occurs in the string at the given index. E.g.:

>>> has_character('monkey', 3, 'k')
True
>>> has_character('cheese', 3, 'x')
False
You can assume that the index is a legal index for the string. (We'll change that later.) Tester.

Part 1, 0 points: Do the exercise at the end of section 7.3.

Part 2, 1 points: Write the function middle_char from class, which returns the middle character of a string, rounding to the right for even-length strings. E.g.:

>>> middle_char('ball')
'l'
>>> middle_char('soccer ball')
'r'
Tester.

Part 3, 2 points: Write the function first_middle_last, which returns a string consisting of the first, middle, and last characters of the argument. E.g.:

>>> first_middle_last('sails')
'sis'
>>>
Tester.

Part 4, 0 points: Expert Practice: modify first_middle_last so that it only includes each character at most once. E.g.:

>>> middle_char('I')
'I'
>>>

Part 5, 1 points: Write a function, char_two_strings(s1, s2, i) that takes two strings and a (non-negative) integer and returns the appropriate character from the first string if the index is less than the length of the first string, and the appropriate character from the second string if the index is larger. E.g.:

>>> char_two_strings('hi', 'monkey', 2)
'm'
>>> char_two_strings('hi', 'monkey', 1)
'i'
Tester.

Part 6, 1 points: Write a function, index_status(string, index) that takes a string and an index and returns the appropriate character from the string if the index is a legitimate index in that string (either positive or negative). If it isn't, it should return 'too low' or 'too high'. E.g.:

>>> index_status('hi', 0)
'h'
>>> index_status('hi',10)
'too high'
>>> index_status('hi', -10)
'too low'
Tester.

Part 7, 1 points: Write a function, num_legal_indices that takes a string and returns the number of working indices for that string. E.g.:

>>> num_legal_indices('hi')
4
That's because the indices -2, -1, 0, and 1 all work for the string "hi". Tester.

Part 8, 1 points: Improve your function has_character so that it returns False for any index that isn't in the range of the string. You should also protect against non-integer indices! Tester.

Part 9, 1 points: Write a function, print_duckling_names(first_letters), that takes a string of first letters of duckling names and prints out the names each on its own line:

>>> print_duckling_names('JKL')
Jack
Kack
Lack
>>> print_duckling_names('JKLMNOPQ')
Jack
Kack
Lack
Mack
Nack
Ouack
Pack
Quack
>>> 
Tester.

Part 10, 1 points: Write a function, get_all_between_string, that returns a string with all the numbers greater than or equal to the first parameter and less than the second. The string should contain the numbers with one space after each of them. The numbers should be sorted in increasing order. Here are some examples:

>>> get_all_between_string(2, 5)
'2 3 4 '
>>> print(get_all_between_string(2, 5))
2 3 4
>>> get_all_between_string(4, 5)
'4 '
>>> get_all_between_string(4, 3)
''
>>> 
(You don't need to add the single quotes, that's the interpreter telling you the result of those lines is a string.) Tester.

Part 11, 2 points: Write a function, odd_characters_only, that takes a string and returns a string that contains only the odd-indexed characters of the original:

>>> odd_characters_only('monkey')
'oky'
>>> x = odd_characters_only('mouseface')
>>> print(x)
osfc
>>> 
Tester.

Part 12, 1 points: Write a boolean function, has_vowels, that takes a string and returns True if and only if that string contains vowels

>>> has_vowels('monkey')
True
>>> has_vowels('fly')
False
Tester.

Part 13, 1 points: Write a function, no_spaces, that takes a string and returns the same string, except without any spaces:

>>> no_spaces('monkey monkey monkey')
'monkeymonkeymonkey'
>>> x = no_spaces("It's time to fly!")
>>> print(x)
>>> It'stimetofly!
Tester.

Part 14, 1 points: Write a function, vowels_only, that takes a string and returns the same strings, but only using the vowels:

>>> vowels_only('monkey')
'oe'
Tester.

Part 15, 1 points: Write a function, reverse_string, that takes a string and returns the that string reversed:

>>> reverse_string('monkey')
'yeknom'
Tester.

Part 16, 2 points: For any string variable, say x, then the expression x[i] yields the i-th character of x. Examples:

>>> awesome = 'Ana, nab a banana!'
>>> print(awesome[3])
,
>>> print(awesome[0] + awesome[2] + awesome[6])
aaa
>>>
Write a new function, last_characters(string, n), that returns a string with only the last n characters of string. Example:
>>> last_characters('awesomesauce', 8)
'omesauce'
Tester.

Part 17, 3 points: Write a function, extend_string, that takes a string and a number of characters. It returns the string repeated a number of times so that the entire string is the correct number of letters. Examples:

>>> extend_string('monkey', 20)
'monkeymonkeymonkeymo'
>>> extend_string('monkey', 10)
'monkeymonk'
>>> extend_string('monkey', 3)
'mon'
Tester.

Part 18, 0 points: Expert Practice: write a function, pongify, which takes a string and returns that string forward, then backward, then forward again. Examples:

>>> pongify('planar chaos')
'planar chaossoahc ranalpplanar chaos'
>>> pongify('hybridization')
'hybridizationnoitazidirbyhhybridization'
>>> pongify('matrix')
'matrixxirtammatrix'
Tester.

Part 19, 3 points: Write a function starts_with(string, prefix) that returns whether string begins with prefix. Examples:

>>> starts_with('champions of kamigawa', 'betrayers')
False
>>> starts_with('champions of kamigawa', 'champ')
True
>>> starts_with('hamburger', 'ham')
True
>>> starts_with('hamburger', 'hamburger')
True
Tester.

Part 20, 2 points: Write a similar function, ends_with(string, suffix).

>>> ends_with('champions of kamigawa', 'kamigawa')
True
>>> ends_with('kamigawa block', 'betrayers')
False
Tester.

Part 21, 1 points: Implement the find function, as described in the chapter. Tester.

Part 22, 1 points: Implement the count function, as described in the chapter. Tester.

Part 23, 3 points: Implement the find_last function:

>>> find_last('abrakadabra', 'a')
10
>>> find_last('alakazam', 'a')
6
Tester.

Part 24, 2 points: Implement the max_count function:

>>> max_count('abrakadabra')
5
>>> max_count('alakazam')
4
Tester.

Part 25, 3 points: Implement the most_frequent_character function:

>>> most_frequent_character('abrakadabra')
'a'
>>> most_frequent_character('blastoise')
's'
Tester.

Part 26, 2 points: Write a function, smallest_missing_digit, that looks for single-digit integers (0-9) in a string and returns the smallest one (as an integer) not in that string. If all ten digits are in the string, it should return None. Here are some examples:

>>> x = smallest_missing_digit("0123456789")
>>> print(x)
None
>>> x = smallest_missing_digit("I t0ld you, I am some1 important 2 many people!")
>>> x
3
>>> x = smallest_missing_digit("9870")
>>> print(x)
1
Tester.

Part 27, 3 points: Write a function, sum_digits, that takes a non-negative integer and returns the sum of its digits. Here are some examples:

>>> sum_digits(31)
4
>>> sum_digits(0)
0
>>> sum_digits(1234)
10
Tester.

Part 28, 1 points: Now write the function that's similar to the last, except that it keeps going until there's only one digit left, sum_digits_limit. Examples:

>>> sum_digits_limit(31)
4
>>> sum_digits_limit(0)
0
>>> sum_digits_limit(123456789)
9
Tester.

Part 29, 1 points: Write a new function, in_both, which takes two strings and returns a new string containing all characters that are in both:

>>> in_both('cheat', 'strongbad')
'a'
>>> in_both('strongbad', 'strongsad')
'strongad'
>>> in_both('steve', 'marzipan'))
''
Tester.

Part 30, 1 points: Write the function, centered_title that takes a string and returns a Title Case version of that string, centered on a 70-character line and with spaces before and after the supplied characters. Example:

>>> centered_title('monkeys Galore')
'                            Monkeys Galore                            '
Tester.

Part 31, 1 points: Write the function, to_fancy_title that takes a string and returns a Title Case version of that string, centered on a 70-character line and with a space before and after, with tildes before and after those spaces. You may assume that the input string contains only alphabetical characters and spaces. Example:

>>> to_fancy_title('monkeys Galore')
'~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monkeys Galore ~~~~~~~~~~~~~~~~~~~~~~~~~~~'
Tester.

Part 32, 3 points: Write the function, snake_to_camel that takes a string in snake_case and returns the equivalent string in camelCase. Example:

>>> snake_to_camel('our_market_research_shows_that_players_like_really_long_card_names')
'ourMarketResearchShowsThatPlayersLikeReallyLongCardNames'
Tester.

Part 33, 1 points: Write the function, earlier_string, which takes two strings and returns whichever of them comes first lexicographically. Tester.

Part 34, 2 points: Write the function, is_abecedarian(word), which takes a string and returns True if word is in alphabetical order. Examples:

>>> is_abecedarian("fly")
True
>>> is_abecedarian('ant')
True
>>> is_abecedarian('wasp')
False
>>> is_abecedarian('aaaaaaa')
True
Instead of assuming something about the case of the letters in the string, you can use the lower() String method to get the lower case version like this:
lower_case_word = word.lower()
You may assume that every character in the argument string is a letter. How could your code use
letters = "abcdefghijklmnopqrstuvwxyz"
with the find method to figure out which letter comes first? Tester.

Part 35, 0 points: Expert Practice: Exercise 8.5.

Submitting your Project:

Make sure all your code is in a file labelled with your user name (everything before the @ in your school email address) followed by _projects.py all in snake_case. (For example, my file name would be: kburke_projects.py.) It's very important to name your file correctly in order for me to grade it. Make sure your code runs, then upload it to the project on Canvas. (Don't submit code that doesn't run; you won't earn any points!) Your code should include solutions to all non-zero-point problems from Project 0 onwards. If there is already a file up on Canvas, delete that before uploading the new version or make sure your new file replaces that. (Sometimes Canvas adds a number after the file name. Don't worry about that, because it's something (freaking annoying) you don't have control of. I have a script that automatically deletes that.)