Algorithm
Factorial Numbers阶乘
def FAC(n):
if n == 0:
return 1
return FAC(n - 1) * n
def FAC2(n):
result = 1
while n > 0:
result = result * n
n = n - 1
return result
def test():
print(FAC(5))
print(FAC2(5))
Fibonacci Numbers斐波那契数列
Dynamic programming (also known as dynamic optimization) is a method for solving a complex problem by breaking it down into a collection of simpler subproblems, solving each of those subproblems just once, and storing their solutions – ideally, using a memory-based data structure.
1. Brute Force,O(n exponential)暴力解法,时间复杂度n的指数级
2. Dynamic Programming, storing the leastest two numbers, O(logn)
引入动态规划思想,时间复杂度降低到logn
//the following 3 functions have the same result
def Fib(n):
if n == 0:
return 1
if n == 1:
return 1
return Fib(n - 1) + Fib(n - 2)
def Fib2(n, a=1, b=0):
if n == 0:
return a
return Fib2(n - 1, a + b, a)
def Fib3(n):
a = 1
b = 0
while n > 0:
a = a + b
b = a - b
n = n - 1
return a
def test():
print(Fib(15))
print(Fib2(15))
print(Fib3(15))
The Tower of Hanoi汉诺塔
递归+数学归纳法思想
“对递归的理解的要点主要在于放弃!
放弃你对于理解和跟踪递归全程的企图,只理解递归两层之间的交接,以及递归终结的条件。”
def Hanoi(n, init, aux, fin):
if n > 0:
Hanoi(n - 1, init, fin, aux)//把n-1个从init挪到aux上,借助fin
fin.append(init.pop()) //把第n个盘子直接从init挪到fin
print(init)
print(aux)
print(fin)
print("---------")
Hanoi(n - 1, aux, init, fin)//把n-1个从aux挪到fin上,借助init
def test():
init = [5, 4, 3, 2, 1]
fin = []
aux = []
Hanoi(5, init, aux, fin)
The Coin Change Problem,分治法
import random
def Ways(amount, denominations):
if amount == 0:
return 1
if amount < 0:
return 0
if len(denominations) == 0:
return 0
d = random.sample(denominations, 1)[0] //从denominations产生一个随机数,取第一个
print(denominations);
return Ways(amount - d, denominations) + Ways(amount, denominations.difference([d]))
/*使用d的所有情况+不使用d的所有情况
denominations.difference([d])表示denominations中删除包含d的元素*/
def test():
print(Ways(400, set([5, 10, 20, 50, 100, 200])))
Review
Mock Interview from Leetcode
Tip
Java Basics
Java programs have at least one class and one main()
method.
- Each class represents one real-world idea.
- The
main()
method runs the tasks of the program. - Case-Sensitivity
public
is an access level访问级别 modifier修改器 that allows __other classes to interact with this class.
public class Car {
// scope of Car class starts after curly brace 实参
public static void main(String[] args) { //声明一个字符串数组,叫args
// program tasks 形参
}
}
Output
System
is a built-in Java classout
=outputprintln
=print line, the cursor光标 is moved to the next lineprintln()=
outputs everything on the same lineSystem.out.println("Hello World"); System.out.print("Hello ");
Comment
// calculate customer satisfaction rating /*We chose to store information across multiple databases to minimize the possibility of data loss. We'll need to be careful to make sure it does not go out of sync!*/
Variables
String name = "James Gosling"; int yearCreated = 1995; double price = 8.99; boolean javaIsACupOfCoffee = false; char punctuation = '!'; String greeting = "Hello World"; String salutations = new String("Hello World");
Escape Sequences转义序列
add quotation
- place backslashes反斜杠 \=\
- output a new line \n ```java System.out.println(“\”Hello World\””); // Prints: “Hello World”
System.out.println(“This is the backslash symbol: \“); // Prints: This is the backslash symbol: \
System.out.println(“Hello\nGoodbye”); / Prints: Hello Goodbye /
<a name="LswGv"></a>
### Manipluation
equal
- line1==line2
- line2.equals(line3)
declare a variable with a value that cannot be manipulated
- final int...
```java
int unevenlyDivided = 10 / 4;
//unevenlyDivided holds 2
int songsA = 9;
int songsB = 9;
int albumLengthA = 41;
int albumLengthB = 53;
boolean sameNumberOfSongs=songsA==songsB;
boolean differentLength=albumLengthA!=albumLengthB;
final int yearBorn = 1968;
Class Definition
public class Store {
// instance fields
String productType;
int inventoryCount;
double inventoryPrice;
// constructor method
public Store(String product, int count, double price) {
productType = product;
inventoryCount = count;
inventoryPrice = price;
}
// main method
public static void main(String[] args) {
Store lemonadeStand = new Store("lemonade", 42, .99);
Store cookieShop = new Store("cookies", 12, 3.75);
System.out.println("Our first shop sells " + lemonadeStand.productType + " at " + lemonadeStand.inventoryPrice + " per unit.");
System.out.println("Our second shop has " + cookieShop.inventoryCount + " units remaining.");
}
}
Create An Instance
- the value of ferrari is a reference to an instance’s memory address
- the class Car is used as the ferrari’s type
- invoke the constructor method构造函数:
Car()
, and usenew
to indicate creating an instance. ```java public class Car { public Car() { // instructions for creating a Car instance } public static void main(String[] args) { // Invoke the constructor Car ferrari = new Car(); } }
public class Car { / declare fields inside the class by specifying the type and name / String color;
public Car() { / instance fields available in scope of constructor method / }
public static void main(String[] args) { // body of main method }
}
public class Car { String color; // constructor method with a parameter
public Car(String carColor) { // parameter value assigned to the field color = carColor; } public static void main(String[] args) { // program tasks } }
- We can initialize a reference-type variable without assigning it a reference if we utilize `null`. If we assign `null` to an object, it would have a void reference.
<a name="k9VGI"></a>
### Constructors
- a class can have multiple constructors as long as they have different parameter values.
```java
public class Car {
String color;
int mpg;
boolean isElectric;
// constructor 1
public Car(String carColor, int milesPerGallon) {
color = carColor;
mpg = milesPerGallon;
}
// constructor 2
public Car(boolean electricCar, int milesPerGallon) {
isElectric = electricCar;
mpg = milesPerGallon;
}
}
Instance Fields
public class Store {
// instance fields
String productType;
// constructor method
public Store(String product) {
productType=product;
}
// main method
public static void main(String[] args) {
Store lemonadeStand=new Store("lemonade");
System.out.println(lemonadeStand.productType);
}
}
Share
How to get a Testing Job in New Zealand
Angular、React 和 Vue 三大框架,Web 开发该如何选择?
- jQuery,使得在 HTML 中编写客户端脚本变得更加容易。
- Angular,不需要任何特殊知识,即使你以前从未做过客户端开发,也可以基于以前的 MVC 设计模式使用经验进行构建,这种模式与 MVVM 非常相似。
- React,集成 React 时,不需要更改当前项目的代码,它只负责渲染界面,不会额外带来痛苦。
- Vue,包含一整套程序,包括 TypeScript 编译器、AOT 编译器和 Web 服务器。Angular 的 Web 服务器用于调试使用这个框架开发的站点。它是用同一个 Angular CLI 实用程序启动的,要启动 Angular CLI,你需要在 Windows 命令行中进入项目文件夹,并执行 ng serve 命令。
- Angular 最复杂,有 143KB。React 次之,有 43KB,而 Vue.js 只有 23KB。除非你的应用特别大,并且包含了大量的组件,否则最好使用更小的结构。
- React 和 Vue 都实现了 DOM。在 Web 项目中,性能与 DOM 密切相关:DOM 在浏览器 / 代码中表示 Web 页面。在发生更新时,你可以通过 DOM 控制 Web 页面。
- 社区:React 的 Mental Model 看起来很可靠,其组件让创建用户界面变得更容易,API 灵活且富有表现力
- 劳动力市场需求:大多数职位空缺与 React.js 有关,然后是 Angular,再然后才是 Vue.js。
- 专家建议初学者首先学习 Angular,因为你所需的一切都是“开箱即用”的,这样更不容易犯错(框架会帮你控制)。在学习了 Angular 之后,你可以学习 ReactJS 和 React Native。另外,如果你只需要移动应用,你也可以直接跳到 React Native。事实上,你只需要学习框架的特性,仅此而已。其余的知识都是通用的(OOP、TypeScript、RP)。学习框架本身 1 到 2 周就足够了,你已经可以创建简单的 Web 应用程序了。