Best Python Assignment Help
Best Python Assignment Help
INTRODUCTION
Part A: Permutations of a string
A permutation is simply a name for a reordering. So the
permutations of the string ‘abc’ are ‘abc’, ‘acb’, ‘bac’, ‘bca’,
‘cab’, and ‘cba’. Note that a sequence is a permutation of itself
(the trivial permutation). For this part of the pset you’ll need to
write a recursive function get_permutations that takes a string
and returns a list of all its permutations. You will find this
function helpful later in the pset for part C.
A couple of notes on the requirements: Recursion MUST be
used , global variables may NOT be used. Additionally, it is
okay to use loops to code the solution. The order of the
returned permutations does not matter. Please also avoid
returning duplicates in your final list.
Suggested Approach
In order to solve any recursive problem, we must have at least
one base case and a recursive case (or cases). We can think of
our base case as the simplest input we could have to this
problem (for which determining the solution is trivial and
requires no recursion) -- for this approach, our base case is if
sequence is a single character (there’s only one way to order a
single character).
If sequence is longer than one character, we need to identify a
simpler
version of the problem that, if solved, will help us easily find
all permutations for sequence . The pseudocode below gives
one approach to recursively solving this problem.
●Recursive case:
○suppose we have a method that can give us a list of all
permutations of all but the first character
in sequence (Hint: think recursion)
○then the permutations of all characters in sequence would
be all the different ways we can insert the first character into
each permutation of the remaining characters
■example: if our word was ‘bust’, we hold out the
character ‘b’ and get the list [‘ust’, ‘sut’, ‘stu’, ‘uts’, ‘tus’,
‘tsu’]
●then ‘ust’ gives us: ‘b ust’, ‘ub st’, ‘us b t’, ‘ust b ’
●‘sut’ gives us: ‘b sut’, ‘s b ut’, ‘su b t’, ‘sut b ’
●and so on …
Implement the get_permutations(sequence) function
found in ps4a.py according to the specifications in the
docstring. Write three test cases for your function in
comments under if __name__ == ‘__main__’ . Each
test case should display the input, expected output,
and actual output. See the if __name__ == ‘__main__’
for an example.
Examples:
Your job will be to fill methods for all three of these classes
according to the specifications given in the docstrings of
ps4b.py . Please remember that you never want to directly
access attributes outside a class - that’s why you have getter
and setter methods.
Don’t overthink this; a getter method should just return an
attribute and a set method should just set an attribute equal to
the argument passed in. Although they seem simple, we need
these methods in order to make sure that we are not
manipulating attributes we shouldn’t be. Directly using class
attributes outside of the class itself instead of using getters
and setters will result in a point deduction – and more
importantly can cause you headaches as you design and
implement object class hierarchies.
Rules
Part 1: Message
●__init__(self, text)
●change_shift(self, shift)
○Hint: think about what other methods you can use to
make this easier. It shouldn’t take more than a couple lines of
code
Part 3: CiphertextMessage
Don’t you want to know what your friends are saying? Given
an encrypted message, if you know the shift used to encode the
message, decoding it is trivial. If message is the encrypted
message, and s is the shift used to encrypt the message, then
apply_shift(message, 26-s) gives you the original plaintext
message. Do you see why?
The problem, of course, is that you don’t know the shift. But
our encryption 5
method only has 26 distinct possible values for the shift! We
know English is the main language of these emails, so if we
can write a program that tries each shift and maximizes the
number of English words in the decoded message, we can
decrypt their cipher!
Part 4: Testing
Part 1: SubMessage
Part 2: EncryptedSubMessage
●__init__(self, text)
○Hint: use the parent class constructor to make your code
more concise. Take a look at Style Guide #7 if you are
confused.
●decrypt_message(self)
Part 3: Testing