This is the skeleton on the assignment. We are supposed to build our code within this skeleton. Essentially we are supposed to use the first two functions(once we create them) to write the last function. I got the first two out of the way but I can't figure out the "def random_write()" function.
random_seed
:
import random
from urllib.request import urlopen
def random_seed(level, text):
'''Returns a random seed string that is the same length as level from text.
If level >= the length of text an error is raised (hint see assert).
Example:
random_seed(2, 'carp') would return ONE of the following: 'ca', 'ar', or 'rp'
'''
# TODO: replace pass below with your code
pass
get_chars_following
:
def get_chars_following(seed, text):
'''Given a seed and the text, return a list of all the characters that directly
follow the seed in text. If there are none, then an empty list is returned.
If using regular expressions for this be careful about characters in the seed
that are special in regular expressions.
Example usage:
get_chars_following('th', 'We hold these truths to be self-evident: that all men are created equal.') returns ['e', 's', 'a']
'''
# TODO: replace pass below with your code
pass
random_write
:
def random_write(level, text, max_num_chars=40):
'''Returns (does NOT print) max_num_chars number of randomly generated
characters using the specified level analysis from text as follows:
1) Create a result string that is the empty string
2) Your program should pick level consecutive characters at random
from text and use them as the initial seed.
3) Repeat the following max_num_chars times:
3.1) Make a list of every character that follows the seed in the text
If that list is empty, then pick another random seed as described
in Step 2 above
3.2) Randomly pick a character c from the text
3.3) Add c to the result string
3.4) Remove the first character from the seed and append c to seed
'''
# TODO: replace pass below with your code
pass
Aucun commentaire:
Enregistrer un commentaire