Factorial

The 'factorial' of a number is defined as:

n! = n * (n-1) * (n-2) * ... 1

Eg. the 4! is 4x3x2x1=24.

Write a function called factorial(n), to output the factorial of 'n'. Use it to print() the factorials of the numbers in this list: 4,6,10,15,40

Your (formatted) answer should look like so: See image.

Strong hint: your code will look a lot like 'sum of first n numbers' statements

Factorial Part 2

The factorial calculation is quite useful, to "count without counting". Eg. if you had five books A,B,C,D,E and wanted to pick out any two, here are all the possibilities: AB, AC, AD, AE, BC, BD, BE, CD, CE, DE - a total of 10 combinations. What if you had 85 books, and had to choose any 3 - how many would that be? No way you'll list them all, to find out :) Likewise, if you wanted to pick out 6 out of the 50 states to visit (for a random summer vacation), how many choices would you have? Given n items (books, states..), if you had to pick out any 'm' from them, the formula to calculate the # of combinations is

n_choose_m = n!/(m! * (n-m)!)

Using the above formula, you can see that for our book choosing question, the answer is 5!/(2!*3!) = 120/(2*6) = 10. We're able to calculate the # of possibilities, rather than list and count them (count without actually counting) - cool :) Write a few lines of code for the combinations formula shown above (create a function called n_choose_m(n,m)), and use it to print out how many choices we'll have for choosing 6 states out of 50. Hint: MILLIONS (> 10M) of choices!!

For fun, verify that n_choose_m(5,2) returns 10 (as from the above enumeration and also calculation); likewise, you can see that n_choose_m(5,3) is ALSO 10 (why?) - verify this via your function, and in addition, list the 10 combinations, eg. ABC, ABD, ABE...

Alice

We are going to count how many times the word theword 'the' occurs, in the following opening paragraphs of 'Alice in Wonderland'.

# copy and paste this into your program aliceOpening = "Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversations?' So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her. There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge."

How do you START to think about what to do? We need to 'consider each word', and 'compare' with "the" - each time we get a 'yes' for the comparison, we'll increment a counter (which we'll start off with a 0 in it).

# notice the commonly occurring CODE PATTERN (loop-with-condition) below: # for each item # if something is true about the item # do something # # else skip # # go back to examine next item foreach word in my wordlist if word is "the" increment counter print out the result (total count)

In the above, aliceOpening is all one big string. We need to SPLIT that long string into a LIST of individual strings, eg. ['Alice', 'was', 'beginning', 'to', 'get' 'very', 'tired'..]. To do so, the split() method will help:

s = "this is so cool" wList = s.split() print(wList) # ['this', 'is', 'so', 'cool']

To compare two strings, just use the == operator:

word='the' if(word == "the"): print "yes" else: print "no" word='thy' if(word == "the"): print "yes" else: print "no"

Now you have everything you need, to count the 'the's in the provided text :) FYI, counting word occurrences is a useful and common task in linguistics. Such counting, classification etc. form the basis for statisical modeling techniques, such as might be done in this piece of research

Happy Birthday

For this problem, you need to write code that produces the following, given singHB("Tommy") for example:

Happy Birthday to you
Happy Birthday to you
Happy Birthday, dear Tommy
Happy Birthday to you

Create (define) the following functions:

  • HBTY() - prints Happy Birthday to you
  • HBD(s) - given HBD(s), where s is 'Max' for example, prints Happy Birthday, dear Max
  • singHB(s) - given a name s, prints out all four lines, ie. a greeting for s

Calling singHB(s) (eg. singHB("Rachel")) would make the song be printed out (eg. with dear Rachel in the third line) - include such a call as the last line of your program

PI

write a calcPi(n) function that will accept a number, to use as the # of terms in the calculation. Use the G-L formula (see below), code up a loop for it:

Gregory-Leibniz (G-L) series formula for 'pi' pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13....

If we use n = 5 terms, the calculation will be

pi = 4 - 4/3 + 4/5 - 4/7 + 4/9

Note that if we number our 5 terms to be 0,1,2,3,4, then terms 0,2,4 are POSITIVE, whereas terms 1 and 3 are NEGATIVE - in other words, even terms are +ve, odd terms are -ve. Use that (oddness/evenness test) as a condition, to decide whether to add or subtract a term.

Note too that the denomenator keeps increasing regularly: 1,3,5,7.. Again, use the term # (0,1,2,3..) to calculate the denomenator (denomenator can be expressed using term#).

Hint: just like for Q1, your code will "strongly" resemble the 'sum of first n numbers' code.. That's because we accumulate our result in both cases.

Run the calcPi function, for these # of terms (use the following statement in your code), and print() the result:

nTerms = [3,6,9,12,120,1200,12000]

Here is the expected answer (your code needs to output this): See image.

Why do we keep returning to calculating pi? Because it is very useful quantity in science and engineering. Plus it is cool that we can calculate a value via a series of ter rather than use RLrope/string to measure a circle

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.