Saint Louis University |
Computer Science 1300/5001
|
Computer Science Department |
All of these problems are for practice. You are freely encouraged to work together and you do not need to submit any of this work for a grade.
As a data set for the first set of challenges, we revisit the use of the words list from a previous activity.
Give code that prints all strings that have 6 or more occurrences of the character 'i'.
Spoiler: my code
Give code that prints all palindromes having length 7 or more.
Spoiler: my code
Give code that prints the total number of lowercase 'z' characters that occur in the combined list of words. (I find a total of 17428.)
Spoiler: my code
Give code that prints the total number of list entries that include nonalphabetic characters. (I find a total of 48101.)
Spoiler: my code
We noted that some words (e.g., Missouri) are capitalized in the data set because they are proper nouns or adjectivies. There are some words that appear both in proper and imporper form (e.g. Apple and apple). Because of the way our list is sorted, it is guaranteed that if both of those exist, they will be neighbors with the proper one followed by the improper.
Use list comprehension to produce a list of all such proper/improper pairs that start with the letter 'Z'. Note well that you will need to use an index-based approach in order to evaluate a pair words[k] and words[k+1].
Spoiler: my code
Ask the user for a word and then output a new word which has two copies of each character of the original.
Enter a word: argh aarrgghhHint: use the empty string to perform a join over a generated sequence of strings produced using comprehension syntax.
Ask the user for a word and then output that word with all vowels (aeiou) removed.
Enter a word: university nvrstyHint: use the empty string to perform a join over a generated sequence of the non-vowels.
Ask the user for a word and then output that word with all vowels (aeiou) replaced by an underscore character.
Enter a word: university _n_v_rs_tyHint: rely on a conditional expression for the replacement.
When introducing for loops, we gave the following code fragment to convert from DNA to RNA:
1 dnaCodes = 'ACGT' 2 rnaCodes = 'UGCA' 3 4 dna = input('Enter a DNA sequence: ') 5 rnaList = [] 6 for base in dna: 7 whichPair = dnaCodes.index(base) # index into dnaCodes 8 rnaLetter = rnaCodes[whichPair] # corresponding index into rnaCodes 9 rnaList.append(rnaLetter) 10 rna = ''.join(rnaList) # join on empty string 11 print('Transcribed into RNA:', rna)
Lines 5 through 10 of that program can be replaced with a
single command if using list comprehension. Determine such a line.
Spoiler:
my code
Assume that data is a generic list of values. Create
a new list named uniq that contains only the first
occurrence of any given value from the original list.
For example, if
Hint: You'll likely need an index-based approach
Spoiler:
my code
Write a program (using list comprehension rather than a traditional for loop) that allows the user to enter any number of integers on the same line (separated by spaces), and which prints out the sum of those numbers. A sample execution might appear as
Enter numbers: 3 18 5 2 11 Their sum is 39
Use list comprehension to produce a list of powers of two,
such as
Spoiler: my code
The base, $e$, of the natural log can be computed as an infinite sum $$e = \sum_{k=0}^\infty \frac{1}{k!}.$$ While we cannot actually wait for an infinite loop to finish, we can get a pretty good approximation for $e$ by computing a finite portion of this sum.
Write a program that lets the user pick the number of terms, $n$, to compute, and output the approximation that results. Use math.factorial for computing the factorial, and use the sum function and comprehension syntax to avoid using a traditional loop for the summation.
Enter n: 10 2.7182815255731922
For what it's worth the more precise approximation
math.e is 2.718281828459045, and if we use $k=18$
terms with our program, we get the same precision.
Spoiler:
my code
The precise value of the mathematical constant $\pi$ is equal to the following infinite series: $$\pi = 4 \cdot \left( \frac{1}{1} - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \cdots \right).$$ While we cannot actually wait for an infinite loop to finish, we can get an approximation by computing the first n terms for some n.
Write a program that lets the user pick the number of terms, $n$, to compute, and output the approximation that results.
Enter n: 10 2.7182815255731922
Hint: The alternating nature of the sequence can either be
done with a conditional expression or by computing the term
$-1^k$ for appropriate $k$.
Spoiler:
my code