Assignment 6
Loops and composite types
Submit before 11:30 PM Friday October 27
Overview
You will write four functions that practice the use of loops and lists, including 2-dimensional lists (lists of lists).
Read or review sections 5.2, 5.3 and 5.4 for help and examples using loops.
Python Functions and Scripts
Write and test Python functions for each of the following. When writing a function, include a docstring (see end of section 3.3 for explanation).
- Finish (if you have't yet) and include your censor function from lab 7.
- Write a function x_indices that takes a string as a parameter that consists of a series of words, each separated by a space. It should return a list of positions of where letter 'X' (captital or small case) appears in its word (starting letter in a word is at position 0). If there are no capital letters in any of the words, the empty list should be returned. You are encouraged to first use split to create a list of words. A correct solution to this problem would then have an outer loop to access each word and an inner loop to access each character in each word. The inner loop should access characters by index (i.e. one involving a call to range to get the indices of the string) and not use the index function or equivalent.
- Write a function countBig that takes a list of string lists as a parameter and returns the number of elements in the list that have a length of 5 or more.
- Write a function buildStr that takes a two-dimensional list as a parameter and returns a string built from the strings found in the list. The list can contain any type of item. Recall that the type function can determine what type of object is currently stored in a variable and is helpful for this question. If the list is empty or the list contains no strings, the function should return the empty string.
- Implement the
function printTwoLargest, which should
take no arguments. Instead, it inputs an arbitrary number of
strings from the user, one per line. The input of strings stops
when the first empty line is entered. The function should print
the two longest strings (measured by number of characters),
ignoring all duplicates. If more than one string has the longest
length, the function should print all of those strings (two or
more) and no more. If there is only one string of the longest
length and multiple strings of the second longest length, it
should print all of those strings. If there is only one unique
string, it should just print that string. If there are no
strings, it should print the message: "There are no strings."
Requirement: before you begin, first implement a function call max_length. It should take a list of strings as a parameter and return the length of the longest string in the list. Use this function in your printTwoLargest function.
Suggestion: do a simple version of this function first. - A statement that summarizes your completion of the assignment. It should contain
the following information:
- Any help or resources that you used in completing the assignment.
- A summary of your experience possibly including your approach, difficulties and time you spent.
- For each of your Python scripts or functions:
- A listing of the code
- Running examples that demonstrate that your code works correctly
Sample Runs
Sample runs:
x_indices("The lazy fox was a bit Xtreme")
[2, 0]
>>> countBig([ [ "pig", "sheep", "cow", "camel"], \
["emu", "owl", "eagle", "hawk", "penguin"], \
["trout", "bass", "shark", "cod"] ])
6
>>> countBig([ [ "pig", "dog"] ])
0
>>> buildStr([[4, 6, "hello"], ["monday", 4, 6, 0], [True, False, 'stop']])
'hellomondaystop'
>>> buildStr([[]])
''
>>> buildStr([])
''
printTwoLargest()
Enter string, or just return to stop: bat
Enter string, or just return to stop: sheep
Enter string, or just return to stop: horse
Enter string, or just return to stop: sheep
Enter string, or just return to stop: elephant
Enter string, or just return to stop: cat
Enter string, or just return to stop:
elephant
sheep
horse
printTwoLargest()
Enter string, or just return to stop: bat
Enter string, or just return to stop: horse
Enter string, or just return to stop: sheep
Enter string, or just return to stop: cow
Enter string, or just return to stop: sheep
Enter string, or just return to stop: koala
Enter string, or just return to stop:
horse
sheep
koala
printTwoLargest()
Enter string, or just return to stop:
There are no strings.
Deliverables
Create a text file called assn6.txt that contains the following:
Submit your assn6.txt file to D2L.