Lab 6 Using Strings and Lists

advertisement
Lab 6 Using Strings and Lists
Objective:
Gain practical experience using strings and lists.
Problem context:
For analytical purposes, it is often important to tally the numbers of occurrences of the outcomes of an
experimental process. One might, for example, need to know the frequency of certain gene sequences
in attempting to forecast the likelihood of susceptibility to a particular form of cancer. During this lab
you will tackle a simplified version of the gene counting problem by implementing a program to count
the number of occurrences of distinct characters in a string. For example, consider the string “A tale as
tall as time is long”. In order of occurrence, it contains the characters “A talesimong” and they occur
with frequencies:
A1
7 (In this case, a blank space occurs seven times in the string.)
t3
a4
l4
e2
s3
i2
m1
o1
n1
g1
Lab steps:
1. Create a source string named src and store the test string “A tale as tall as time is long” in it.
2. Create a function finddifferentcharacters that accepts a single string parameter named astr and
then uses that parameter to build and return a new string that consists only of the distinct
characters in astr. In finddifferentcharacters, you must:
a. Create an empty string named bstr;
b. For each letter ltr in astr, if ltr is not in bstr, then append it to bstr;
c. Return bstr
3. Call finddifferentcharacters using src as the actual parameter, and store the result in a string
named d (for different characters). Your result should match the string “A talesimong”.
4. Output the string d, appropriately captioned. For the example problem, the output might be
The different letters in src were = A talesimong
Do not worry about generating boldfaced or italicized font.
5. Write a second function named findindex that will accept a string astr and a letter ltr, contained
in the string, and the return the value of the index at the first occurrence of ltr in astr.
a. Start with index value set to zero;
b. While astr[index] is not ltr, increase the index value by one;
c. Return the index value
6. Write a function named countletters that will use two parameters, astr and bstr. When
countletters is called with src and d (see the next problem), the function will use the two strings
to create a list of counts, one for each different letter. Proceed as follows:
a. Create an empty list named counts;
b. For as many times as there are characters in bstr, append a zero to the list counts;
c. For every letter ltr in astr, increment the counts entry that corresponds to the index of
that letter in bstr
counts[ findindex( bstr, ltr ) ]+= 1
d. Return the list counts.
e. NOTE: The list counts will contain the number of occurrences of each distinct character
in the original string. The tallies will be in an order that is parallel to the characters in
the string d.
7. Call the function countletters using src and d, countletters(src , d), and place the result in a list
named lettercountslist.
8. Create a function showcounts that will accept two parameters astr and alist of parallel contents
and then print those contents. This function has no return value!
a. For each item in either astr or alist, display the corresponding character in astr and the
count in alist.
9. Call showcounts using d and lettercountslist, e.g., showcounts(d, lettercountslist). The
resulting output should match the characters, order, and count values shown above.
Download