python代写

Problem 1. Welcome

Homework 1

Write a program that takes as input a string containing a name in the Last_Name, First_Name format and prints a welcome message with the first name first and the last name last and tells the person the length of their first name.

What you need:

input: The input function in Python takes one argument, a string. That string then serves as a prompt that describes what the user needs to enter. The value returned is of type str and should be stored in a variable. If you need to use it as a different type (e.g., float or int), you need to convert it to that type. The prompt can be either a literal or a variable.

the concatenate operator +: The + operator concatenates strings. For example, ‘hello’ + ‘goodbye’ will create a new string ‘hellogoodbye’.

print: The print function in Python takes one or more arguments, all must be of type str or implicitly convertible into str. int and float types are implicitly converted by Python into str. print takes each argument and displays it on the screen with spaces separating each value and a
new line added at the end. For e.g., print(‘hello’,’goodbye’) will be displayed as hello goodbye while print(‘hello’+’goodbye’) will be displayed as hellogoodbye

find: The find function returns the location of a character or substring inside a string. For example, loc = ‘Hello’.find(‘e’) will set the value of loc to 1 (locations in programming languages always start with 0).

substring operator []: The substring operator some_string[start:end:increment] returns a substring that begins at location start, goes up to (but not including) location end and picks every increment-th character. For example, if r=’haeblcldoe fignh ifjrkelnmcnho piqsr smteurvcwi’ then print(r[0:len(r):2]) will display “hello in french is merci” (without the quotes!) make sure you manage the print correctly. No extra spaces, no missing spaces, and don’t forget the exclamation point.

strip: is a function that strips leading and trailing spaces from a string.
len: len is a Python function that tells the program how long a particular string is.

Some examples:
Examples:
“Hamilton, Alexander” should return Alexander Hamilton your first name is 9 letters in length “Kennedy, John” should return John Kennedy your first name is 4 letters in length
” Obama, Barack ” should return Barack Obama your first name is 6 letters in length
Note that the extra spaces in the third name must be removed

Problem 2. Linear Algebra – Orthogonal Decomposition

121 Consider the subspace S spanned by vectors: (2), (3), (1),in 𝑅4

341 451

  1. (i)  What is the dimension of the subspace S?

    3

  2. (ii)  Write a Python code to compute the orthogonal linear projection of the point (1) onto the

    2 5

    subspace S.

Problem 3. Moving Average

Write a function that can compute the moving average of time series using a sliding window over an array. This function should take an array of numbers, say arr_record and a sliding window (default value 50). It should:

  1. (i)  Return a list of dictionaries for all windows.
  2. (ii)  Each dictionary should include the average value, min, max, standard deviation at each

    window?

  3. (iii)  Able to handle missing data in time series by replacing missing data with the most recent

    available data.

Download SPY daily data from Yahoo! as your test data in a .csv file, and write a test programming for calling your function.

Problem 4. US Censor Data

We will use census data from the United States Census Bureau. This dataset contains population data for counties and states in the US from 2010 to 2015. See this document for a description of the variable names.

The census dataset (census.csv) should be loaded as census_df. Answer questions using this as appropriate.

Write a Python programming using pandas to answer following questions.
(i) Which state has the most counties in it? (hint: consider the sumlevel key carefully! You’ll need

this for future questions too…)

(ii) Only looking at the three most populous counties for each state, what are the three most populous states (in order of highest population to lowest population)? Use

CENSUS2010POP.

(iii) Which county has had the largest absolute change in population within the period 2010-2015? (Hint: population values are stored in columns POPESTIMATE2010 through POPESTIMATE2015, you need to consider all six columns.)

e.g. If County Population in the 5 year period is 100, 120, 80, 105, 100, 130, then its largest change in the period would be |130-80| = 50.

(iv) In this datafile, the United States is broken up into four regions using the “REGION” column.

Create a query that finds the counties that belong to regions 1 or 2, whose name starts with ‘Washington’, and whose POPESTIMATE2015 was greater than their POPESTIMATE 2014.

This function should return a 5×2 DataFrame with the columns = [‘STNAME’, ‘CTYNAME’] and the

same index ID as the census_df (sorted ascending by index).