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.
Write a program 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 39Spoiler: my code
Write a program 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 average is 7.8Spoiler: my code
Write a program that calculates the factorial, $k!$ of a given integer $k$. The fatorial is the product of the first $k$ positive integers, defined as $$k! = 1 \cdot 2 \cdot \ \cdots \ \cdot k.$$ For example $4! = 1 \cdot 2 \cdot 3 \cdot 4 = 24$.
Enter k: 4 24Spoiler: my code
For full disclosure, there is already a factorial function in Python's math module.
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. While this could require need for a nested loop (if you need another loop to do a factorial), we'll let you use math.factorial in your solution.
Enter n: 10 2.7182815255731922
For what it's worth the more precise approximation
math.e is 2.718281828459045, and if we use $n=18$
terms with our program, we get the same precision.
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: Make a goal of creating the new string internally, and then print it. (The challenge with trying to print a little bit at a time is that the print function will by default print separating spaces or newlines.)
Ask the user for a word and a number (that is assumed to be less than the word length) and then output the following pattern.
Enter word: example Enter k: 3 exa xam amp mpl ple
Hint: each line is produced as j spaces (computed as
Spoiler:
my code
Given a list of strings named words, write a loop
that produces a new list of strings that are the
capitalized versions of the original words. For example, if
the original list were
Spoiler: my code
As a variant of the previous problem, this time you are to
instead write code that mutates the existing list to
replace each element with the capitalized version. Note well,
it is not that we want you to reassign the
identifier words to a new list; you should
actually be changing the contents of the original list.
Spoiler: my code
Given a list of strings, words, compute the cumulative number of characters used by those words. For example, if
words = ['apple', 'banana', 'fig']the total is 14.
Note: please use a for loop to compute this, even though wise Python programers might realize that this could be done more easily by joining the list of strings into a single string.
Spoiler: my code
Given a list of strings, words, compute the length of the longest individual word in the list. For example, if
words = ['apple', 'banana', 'fig']the longest word has length 6.
While this would be a classic example of a problem that might
require a conditional ("if") statement, we can avoid need for
that structure by recognizing that the max function
can be used to effectively compute the running max with a
command that effectively determines
Spoiler: my code