数学代考|MATH50009-Principles of Programming Exam

你如何编写有效的代码?通过有规律和有目的的方式,这里有5条编程原则,可以让你成为更好的程序员。

  • 保持代码简洁

这是最重要的编码原则之一,你应该尽可能地编写简单的代码,永远不要陷入试图过度聪明或用厚厚的高级代码块来炫耀你的本事。如果你能用一行写一个脚本,就用一行写。

  • 不要重复代码

在编写代码时,要避免数据或逻辑的重复。与其重复行,不如试着找一个使用循环的算法来代替。试一个处理50次重复的循环比调试50个各处理一次重复的代码块要容易。

  • 对拓展开放,对修改关闭

例如,假设你在维护一个GUI框架。你可以发布一个版本供编码者直接修改和整合你发布的代码。但是,当你在四个月后发布一个重要的更新时,会发生什么?你和同伴们的源代码会被破坏;相反,发布的代码要防止直接修改,鼓励扩展。像这样的基本编程原则将核心行为与修改行为分开。这样的代码更稳定,更容易维护。

  • 组合由于继承

组成大于继承原则指出:具有复杂行为的对象应该包含具有单独行为的对象的实例。它们不应该继承一个类并增加新的行为。

  • 单一职责

单一责任原则指出,程序中的每个类或模块都应该只提供一种特定的功能。正如Robert C. Martin所说,”一个类应该只有一个变化的理由”。类和模块常常以这种方式开始。小心不要在类变得越来越复杂时增加太多的责任。重构并将它们分解成更小的类和模块。

  • 注释代码

任何高级开发人员都会强调用适当的注释来记录你的代码的重要性。所有的语言都提供注释;你应该把写注释作为一种习惯。留下注释来解释对象,加强变量定义,并使函数更容易理解。

这是TopMask老师分享的一些小技巧,下面是一个编程原理的数学考试案例:

Question 1 Numerical derivative

The object of this exercise is to create functions which can compute their own numerical derivative. Given a real-valued function f of one real variable, and a small δ>0, the derivative of f at a point x can be approximated by the central difference approximation:

1. (a)

Create a package derivative containing a module derivative.py . Make the packages in this repository installable. You will aid your own completion of the exam by installing this package in editable mode.

1. (b)

(i)

Create a class derivative.derivative.Function whose constructor takes a function fn as an input and stores it. This is the function we will evaluate and whose derivative we will take.

(ii)

Implement the __call__ special method on Function to take a single argument (the value of x), evaluate fn on that input, returning the result.

1 (c)

(i)

Implement a subclass of Function called CentralDifferenceFunction . The constructor of this class should take fn and a scalar parameter delta . This constructor should call the superclass constructor to store fn , and it should store delta on the object instance.

(ii)

Implement a method CentralDifferenceFunction.derivative which takes two parameters, x and delta . The delta parameter should be made optional by giving it a default value of None . The derivative method should return the central difference approximation to the gradient of fn at x . If delta is passed to the method, then this value should be used in the calculation. If the argument is omitted then the value of delta passed in to the constructor should be used.

1 (d)

(i)

Implement a new subclass of Exception named ConvergenceError . The new exception does not need to have any new methods or attributes.

(ii)

Implement a subclass of CentralDifferenceFunction called AdaptiveCentralDifferenceFunction . The constructor of this class should have parameters fn , delta , eps , and max_its . The constructor should call the superclass constructor to store fn and delta , and should store eps and max_its directly on the object.

(iii)

Implement a method AdaptiveCentralDifferenceFunction.derivative which takes x as an argument. This function should call the superclass derivative method for a sequence of values of delta . The first value of delta should be the one passed to the class constructor. The next value of delta should be half the first value. This counts as the first iteration. The method should keep calculating more estimates of the derivative, each time with delta half that of the previous time. This sequence stops when the first of the following conditions is satisfied:

1. The absolute value of the difference between successive derivative approximations falls below eps . In this case the iteration has succeeded and the last calculated approximation should be returned.

2. The number of iterations exceeds max_its . In this case, the iteration has failed and the method should raise ConvergenceError with the message “Failed to converge within the allowed number of iterations.”

1 (e)

(i)

Ensure that your code passes Flake8.

(ii) Ensure that your code otherwise conforms to good programming style as we have learned in this course.

contact

Assignment Exmaple

Recent Case

Service Scope

C|C++|Java|Python|Matlab|Android|Jsp|Prolo
g|MIPS|Haskell|R|Linux|C#|PHP|SQL|.Net|Hand
oop|Processing|JS|Ruby|Scala|Rust|Data Mining|数据库|Oracle|Mysql|Sqlite|IOS|Data Mining|网络编程|多线程编程|Linux编程操作系统|计算机网络|留学生|编程|程序|代写|加急|个人代写|作业代写|Assignment

Wechat:maxxuezhang

wechat