python代写 Homework 3 Math 510

Homework 3 Math 510

This homework should be submitted via Canvas no later than 11:59 PM Saturday September 29th. All functions and documentation should be in a single file titled lastname_firstname_hw_3.py (not an .ipynb file) where your last and first name are as they appear on your student ID. The homework is to be completed in Python 3.x. All functions in the homework should be titled and take input as stated in the problem specification. I will be holding office hours Thursday 5:00 – 6:15 PM if you have any questions. The Extra Credit problem will not be graded after the start of class on Thursday.

Don’t worry about error handeling, assume that inputs are given as stated by the problem specification. When possible try and reuse functions from earlier questions (as code is intended to be reusable). Pay attention if the specification asks for a return or a print. Remember to document your code (purpose, input, output), as this will be an important part of your grade. Good luck.

1. Write a class of objects called matrix_2 that act like a 2×2 matrix. Make sure to include: a. __init__ method
b. __add__ method (add two 2×2 matrices)
c. __mul__ method (multiply two 2×2 matrices)

d. __str__ method
e. determinant() a method that finds a matrix’s determinant
f. invert() a method that finds a matrix’s inverse if it has one or returns ‘singular matrix’ if it does not

2. Write a recursive function called binary_search() that accepts as arguments a number (your function should work for float and integer types) and a list of numbers sorted in ascending order. Your function may need to accept more arguments, but all other arguments should be given a default value so that they do not have to be entered. Then use the binary search algorithm (https://en.wikipedia.org/wiki/Binary_search_algorithm (https://en.wikipedia.org/wiki/Binary_search_algorithm)), implemented recursively, to return either an index that contains the number (there may be more than one, but you need only return one) or ‘number not in list’.

3. A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

,
,

, ,

,
,

, ,

Where 0.1(6) means 0.166666…, and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.

Write a function decimal() that accepts as input an integer denominator and returns a string with the decimal representation of that fraction (with no trailing zeros, a leading 0. and any numbers that repeat infinitely in parenthesis as above). Hint: How would you implement long division in python? When do you know that your division process is looping?

1.0 = 01 1

)1(.0 = 9 1

521.0 = 8 1

)758241(.0 = 7 1

)6(1.0 = 6 1

2.0 = 5 1

52.0 = 4 1

)3(.0 = 3 1

5.0 = 2 1

4. An alternade is a word in which its letters, taken alternatively in a strict sequence, and used in the same order as the original word, make up at least two other words. All letters must be used, but the smaller words are not necessarily of the same length. For example, a word with seven letters where every second letter is used will produce a four-letter word and a three-letter word. Here are two examples:

“board”: makes “bad” and “or”. “waists”: makes “wit” and “ass”.

Using the word list unixdict.txt from class, write a function, alternade, that accepts as an argument the file name for the dictionary and then goes through each word in the list and tries to make two smaller words using every second letter. The smaller words must also be members of the list. Print the words to the screen in the above fashion.

5. Write a function called checksum() that accepts as input a filename. The function takes the text of the file and sums the letters (upper and lowercase letters have the same value) for ‘a’ = 1, ‘b’ = 2, ‘c’ = 3, … For example ‘word’ would give ‘w’+ ‘o’ + ‘r’ +’d’ = 23 + 15 + 18 + 4 = 60. The function should return the integer sum of all the letters in the file.

6. Write a function called word_lengths() that accepts a list of words and using a dictionary comprehension returns a dictionary whose keys are integers and values are the set of words from the input list whose length are that of its key. The values should be lower cased. The function should be as follows:

def word_lengths(input_list): “””

documentation here
“””
return {dictionary comprehension here}

7. In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:

1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p

Write a function, make_change() that accepts an integer number of pence and calculates how many different ways one can make change for that amount using any number of coins? What is the answer for 200 pence?

Extra Credit

A certain childrens game involves starting with a word in a particular category. Each participant in turn says a word, but that word must begin with the final letter of the previous word. Once a word has been given, it cannot be repeated. If an opponent cannot give a word in the category, they fall out of the game. For example, with “animals” as the category,

Child 1: dog
Child 2: goldfish
Child 1: hippopotamus
Child 2: snake
… Write a function word_game() that accepts a list of words and returns the longest list of words that can be played in sequence. Your function may accept other input, but default values should be used so that it works if simply passed a list. Your program, while not graded for speed, should solve the following selection of 70 English Pokemon names (extracted from Wikipedia’s list of Pokemon) in less than 3 minutes.

    audino bagon baltoy banette bidoof braviary bronzor carracosta charmeleon
    cresselia croagunk darmanitan deino emboar emolga exeggcute gabite
    girafarig gulpin haxorus heatmor heatran ivysaur jellicent jumpluff kangaskhan
    kricketune landorus ledyba loudred lumineon lunatone machamp magnezone mamoswine
    nosepass petilil pidgeotto pikachu pinsir poliwrath poochyena porygon
    porygonz registeel relicanth remoraid rufflet sableye scolipede scrafty seaking
    sealeo silcoon simisear snivy snorlax spoink starly tirtouga trapinch treecko
    tyrogue vigoroth vulpix wailord wartortle whismur wingull yamask

When you cut and paste this list, then parse it, make sure to check that it has 70 elements.