Write a function modenumlist that takes a single argument


Problem - e-stimation

(1 + 1/x)z < n < (1 + 1/x)x+1

for all positive number x.

One way to estimate e is via the following equation:

e ≈ i=0n 1/i!

where: i! = i x (i-1) x . . . x 1

and 0! Is defined to be 1.

That is, we calculate 1/i! for the values i = 0 up to (and including) i = n, and sum them all up.

Write function e_estimator (n) that takes a non-negative integer n as input and returns a float estimate of e based on the equation above.

Problem - For a While

Let's put this into action, in rewriting awhile loop to a for loop, in the context of the provided function allnum(strlist), which takes a list of strings strlist, and returns the list of strings which are made up exclusively of digits (in the same order the strings occurred in the original).

Note that rewritten function should behave identically to the original, and the only changes you should make are to the while loop and associated variables, to rewrite it as a for loop. Note also that in the rewritten version of the code, you should iterate over the elements of strlist directly, without indexing. Submissions which don't conform with both of these will be rejected, so be sure to follow the requirements of the problem carefully!

Problem - Least Vowel Words

Write a function least_vowel_words (text) that takes one argument text containing a block of text in the form of a str, and returns a list of unique word(s) with the lowest "vowel proportion" as a list. The vowel proportion is defined as the proportion of letters in the word which are vowels (e.g. the vowel proportion of life is 2/4 = 0.5). The returned list will have multiple elements if and only if there is a tie for lowest vowel proportion, and in this case, the elements should be ordered as they (first) appear in text.

"Words" should be assumed to be separated by whitespace (spaces, tabs or newlines). In all cases, all "words" should be converted to lower case and any punctuation should be stripped from the (right) end of the word, in the form of the following punctuation marks ONLY: commas (" , "), fullstops (" . "), colons (" : "), semi-colons (" ; "), exclamation marks (" ! "), question marks ("?"), single quotes (" ' "), double quotes ( ' " ' ), and hyphens (" - ").

Problem - Mode List

Write a function mode(numlist) that takes a single argument numlist (a non-empty list of numbers), and returns the sorted list of numbers which appear with the highest frequency in numlist (i.e. the mode).

Problem - Spiral Word

350_figure.png

In this case, the spiralled sequence of letters is in strictly descending order, as each letter comes before its (spirally) preceding letter in the alphabet. An example of an ascending spiral word (where every letter comes after its (spirally) preceding letter) is flush' ('f', 'h', 'l', 's', 'u'). An example of a non-spiral word, on the other hand, is 'all', as the spiral sequence of letters is not strictly ascending ('l' follows 'l').

Write the function spiral_word (word) that takes a single string argument word and returns a 2-tuple made up of bool values as follows:

  • the first bool indicates whether the word is a spiral word or not
  • the second bool indicates whether a spiral word is ascending or not (indicating either that it is descending, or that it's not a spiral word).

Problem - Symmetric Words

Write a function symmetric_words (wlist) that takes a single argument wlist containing a list of words each in the form of an all-lowercase non-empty str, and returns a sorted list of "symmetric" words. A symmetric word is defined as a word where for all values i, the letter i positions from the start of the word and the letter i positions from the end of the word are equidistant from the respective ends of the alphabet.

Problem - Top-5 Frequent Words

Write a function top5_words (text) that takes a single argument text (a non-empty string), tokenises text into words based on whitespace (once again, without any stripping of punctuation or case normalisation), and returns the top-5 words as a list of strings, in descending order of frequency. If there is a tie in frequency at any point, the words with the same frequency should be sub-sorted alphabetically. If there are less than five distinct words in text, the function should return the words in descending order of frequency (with the same tie-breaking mechanism).

Problem -While a For

OK, time now to rewrite a for loop as a while loop.

Mr Frodo is having second thoughts about the trip to Mordor. Being both a superstitious little chap and a Dungeons and Dragons fan, he carries a 20-sided dice wherever he goes. He decides that he will roll the dice a fixed number of times, and if his favourite number comes up, he will go to Mordor, and if not, he will return to the Shire. We will simulate the 20-sided dice through the use of the randint function from the random library (a topic that we will cover properly in Worksheet 13; for now, just accept that from random import randint gives you access to the function, which returns a random integer between the values of the first and second arguments, inclusive).

Given the provided function luck_tester, (lucky_number, max_rolls, di ce_si ze), which takes as arguments: (1) a lucky number lucky_number (3 in Mr Frodo's case: the number of trolls his uncle encountered in the Trollshaws); (2) the maximum number of dice rolls max_rolls; and (3) the dice size dice_si ze (in terms of the number of sides the dice has; 20 in Mr Frodo's case); all can be assumed to be integers. The function should return a string, of value depending on whether the lucky number comes up in the provided number of rolls of the dice or not; the precise strings are provided in the original code.

Note that rewritten function should behave identically to the original, and the only changes you should make are to the for loop and associated variables, to rewrite it as a while loop. Submissions which don't do this will be rejected, so be sure to follow the requirements of the problem carefully!

Problem - Authorship Attribution (Part 1)

The final part of Project 1 takes the form of an "authorship attribution" system, that is a system which attempts to determine who wrote a given document, based on analysis of the language used and style of that document. We will break the system down into multiple parts, to make it clearer what the different moving parts are, and make it easier for you to test your system.

The first step in our authorship attribution system will be to take a document, separate it out into its component words, and construct/return a dictionary of word frequencies. As we are focused on the English language, we will assume that "words" are separated by whitespace, in the form of spaces ('  '), tabs ('\ t ') and newline characters ( ' \ n ').

We will also do something slightly unconventional in considering each "standalone" non-alphabetic character (i.e. any character other than whitespace, or upper- or lower-case alphabetic characters) to be a single word.

Write a function authattr_worddict (doc) that takes a single string argument doc and returns a dictionary (dict) of words contained in doc (as defined above), with the frequency of each word as an int. Note that, as the output is a dict, the order of those words may not correspond exactly to that indicated below, and that the testing will accept any word ordering within the dictionary.

Problem - Authorship Attribution (Part 2)

The next step in our authorship attribution system will be to take two dictionaries of word counts and count the similarity between them. We will do this by:

1. ranking the two sets of words in descending order of frequency, and:

2. for corresponding word pairs, calculate the absolute difference in rank between the two.

If a word is found in one ranking but not the other, we will set the ranking for the second to the value max rank (provided as part of the function call). In the case of a tie in the word frequency ranking (due to multiple words having the same frequency), we will assign all items the same value, calculated as follows:

tied ranking + (number of tied items - 1)/2

Write a function authattr_oop(dictfreq1, dictfreq2, maxrank) that takes three arguments:

  • dictfreq1: a dictionary of words, with the (positive integer) frequency of each
  • dictfreq2: a second dictionary of words, with the (positive integer) frequency of each
  • maxrank: the positive int value to set the ranking to in the case that the word isn't in the dictionary of words in question

and returns a float out-of-place distance between the two (where the smaller the number, the more similar the two rankings are).

Problem - Authorship Attribution (Part 3)

The final step in our authorship attribution system will be to perform authorship attribution based on a selection of sample documents from a range of authors, and a document of unknown origin.

You will be given a selection of sample documents from a range of authors (from which we will learn our word frequency dictionaries), and a document of unknown origin. Given these, you need to return a list of authors in ascending order of out-of-place distance between the document of unknown origin and the combined set of documents from each of the authors. You should do this according to the following steps:

1. Compute a single dictionary of word frequencies for each author based on the combined set of documents from that author (provided in the form of a list of strings)

2. Compute a dictionary of word frequencies for the document of unknown origin

3. Compare the document of unknown origin with the combined works of each author, based on the out¬of-place distance metric

4. Calculate and return a ranking of authors, from most similar (smallest distance) to least similar (greatest distance), resolving any ties in the ranking based on an alphabetic sort

You have been provided with reference implementations of the functions authattr_worddict and authattr_oop from the preceding questions in order to complete this question, and should make use of these in your solution. These are provided via the from hidden_lib import authattr_worddict, authattr_oop statement, which must not removed from the header of your code for these functions to work.

Write a function authattr_authorpred (authordict, unknown, maxrank) that takes three arguments:

  • authordict: a dictionary of authors (each of which is a str), associated with a non-empty list of documents (each of which is a str)
  • unknown: a str contained the document of unknown origin
  • maxrank: the positive int value to set max rank to in the call to authattr_oop

and returns a list of (author, oop) tuples, where author is the name of an author from authordict, and oop is the out-of-place distance between unknown and the combined works of author, in the form of a float.

Attachment:- Assignment Files.rar

Solution Preview :

Prepared by a verified Expert
Computer Engineering: Write a function modenumlist that takes a single argument
Reference No:- TGS02723145

Now Priced at $60 (50% Discount)

Recommended (93%)

Rated (4.5/5)