Python algorithm Exercises

Source: Internet
Author: User

The expression of a function is determined according to a given linear function:

Examples

Get_function ([0,1,2,3,4]) = f (x) =x

Get_function ([1,4,7,10,13]) = f (x) =3x+1

Get_function ([0,3,6,9,12]) = f (x) =3x

Limit the length of the array to 5.

Words not much, first on the code, then do analysis.

1 defget_function (sequence):2m =Sequence[0]3n = sequence[1]-m4     if[N*x+m forXinchRange (5)]! =sequence:5         return "Non-liner Sequence"6     return "f (x) ={}". Format (Get_format (m,n))7 8 defGet_format (M, n):9     if  notN:Ten         returnm OneN_string ="{}{}x". Format ("-" ifN<0Else "", ABS (N)ifABS (n)!=1Else "") AM_string ="{}{}". Format ("+" ifM>0Else "-", ABS (M)ifMElse "") -     return "". Join ([n_string,m_string])

This code is actually looking at the idea of a linear function, which is only represented by Python code.

The coefficients of the linear function δ= (y2-y1)/(X2-X1), if δ>0, the coefficient symbol is "+", and vice versa "-", this is the meaning of 11 lines, for why N and M two numbers, it should be well understood. Since the symbol has been determined, only the absolute value of the coefficients is required, and the increment or decrement function has been determined by the coefficient symbol.

Since the coefficients have been determined, the following need to solve is the intercept problem (according to the geometrical meaning I understand the term), if according to the concept of geometrical intercept is very well understood, if x=0, y>=0, then the symbol of intercept is "+", and vice versa is "-", since the symbol is determined, Then the value can only be obtained by absolute value. This is the meaning of 12 rows.

For row 4, it is a simple way to verify whether a linear function is formatted with the last function output by Get_format.

With June to encourage ................ The life of the run backpackers!

Python algorithm Exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.