[Python] Permutations and Combinations Question, programming homework help

Anonymous
timer Asked: Oct 11th, 2016
account_balance_wallet $40

Question Description

Before you start notes:

  • Enter your answers to these questions in the accompanying Python script file questions.py. Questions 1–3 require only short pieces of code to calculate or generate the answers, so replace the entries that say None with your answers to each part. Question 4 has you completing the implementation of two functions.
  • For questions 1–3, a helper function, fact(n), has been included that provides a simpler interface to SciPy’s factorial function. Note that the comb function will produce real numbers as output instead of integers. The assessment scheme will accept either, as long as the value is correct.
  • (HINT): While developing your code, store the result in a variable so that Spyder doesn’t immediately try to display it (since it may be large).
  • (HINT): Use the len() function to check that your result set is the expected size. If your result, stored in set s, is (necessarily) large, then use code like sorted(s)[:20] to look at only the first 20 elements of s. The use of sorted() will often make it easier for you to confirm that it is generating the right entries.
  • If your script file takes more than ~3 seconds to run then you’ve probably implemented one of the set comprehensions incorrectly.

QUESTIONS (input your answer to these questions to the corresponding parts in the python file provided):

Question 1: Permutations

  1. How many strings of length three are possible using characters from ‘ATGC’ (characters may be reused)? Write Python code tocalculate the number.
  2. Write a set comprehension to generate those strings.
  3. How many four-word sentences can be created from the words ‘cat’, ‘bat’, ‘hat’, ‘sat’, ‘fat’, and ‘rat’, using each word at most once? (Most of the sentences won’t make sense.) Write Python code to calculate the number.
  4. Write a set comprehension to generate tuples (not single strings) of the different arrangements described in part (c).

Question 2: Combinations

You should use the definitions of toy droid names and Star Trek species provided in the assignment script in your answers.

  1. With the resurgence of Star Wars in the cinemas toy companies are keen to invent new toys for children to collect. They have a series of 16 droids (Star Wars robots) and sell them in mixed packs of four (no pack contains a duplicate droid). How many distinct packs do they sell? Write Python code to calculate the number.
  2. Given the droid names defined in the script file, write a set comprehension to generate all compositions of packs of four droids.
  3. The various Star Trek television and film series have introduced a large number of different (humanoid) alien species. One of the common narrative devices is to say that a character’s parents are from two different alien species, and hence that character has a blend of characteristics from the two (yes, science fiction can get away with being both horribly racist and ignorant of biology). For example, the popular character Spock is described as having a Human mother and Vulcan father. Your task:Assuming characters can only have two parents, and ignoring which parent is which species, how many different combinations of such ‘blended’ Star Trek characters are possible given the subset of Star Trek species { ‘Bajoran’, ‘Betazoid’, ‘Cardassian’, ‘Human’, ‘Klingon’, ‘Romulan’, ‘Vulcan’ }? Write Python code to calculate the number. Do not directly call factorial or fact in your solution.
  4. Write a set comprehension to generate those combinations. (The answer will be a set of 2-tuples, or pairs.)

Question 3: You choose which

The following information is used in all parts of this question. Anne has 23 books in her collection at home. They all have short titles: ‘A’, ‘B’, ‘C’, …, ‘W’ (there is a list of these in the assignment script).

  1. Anne is going on holiday, but can only take six books with her. What are her options? Write a set comprehension to generate them all.
  2. All of Anne’s books fit on a single shelf. How many different ways can she arrange them? Write Python code to calculate the number.
  3. Anne sometimes likes to reread books, and may even reread a book she has just finished. What are the possible sequences of book titles of the last three books she has read? Write a set comprehension to generate the alternatives.
  4. Write Python code to calculate how many such sequences there are. (Do not use the len function on your previous answer.)

Question 4: Lovers of anagrams know words are mightier than the sword

An anagram is a word whose letters, when rearranged, form another word. They are a common feature of cryptic crosswords, but you do not need to know how to solve a cryptic crossword to solve this question. An anagram solver finds the alternative arrangements of a word’s letters that represent valid words. Your task is to write the two components of a basic anagram solver: the first part generates the set of all possible permutations of the letters in a given word; the second part restricts this set to contain only those permutations that are valid words.

There are two stub functions for you to complete:

  1. word_perms(word): should generate and return all permutations of the letters in the string represented by word (each permutation will be a single string, not a tuple). It will probably be one line of code.
  2. anagrams(word): should return a set of all the permutations of the letters in word that are valid (that is, that appear in one of the collections of words described below), including the original word. The input can be assumed to be in lower case.
    Not all solutions to this task will receive full marks; an ideal solution will look very short, but still be readable.

The assignment script file contains code to load collections of words of different lengths (between 2 and 10) into two Pythondictionaries, one that holds the words in sorted lists and another that holds them in sets. To use the list of words of length n, useword_lists[n], whereas to use the set of words of length n, use word_sets[n] (you will probably not have a variable called n but an expression instead). You do not necessarily have to use both of these. Select whichever you believe is best-suited to the task.

In order for these words to be loaded in your program you must download and unzip the file containing the words, words.2-10.txt, that accompanies this document.

Testing your anagram solver

To test word_perms, call it with very short words for which you can enumerate all the possible permutations. To test anagrams try it on the word ‘lovers’. Other good words you could try include ‘alerts’, ‘heart’, ‘parsec’ and ‘spare’.


Unformatted Attachment Preview

Tutor Answer

dygitaltech
School: Boston College

Hi, I'm uploading the answer now.  I ...

flag Report DMCA
Review

Anonymous
Top quality work from this guy! I'll be back!

Similar Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors