Algorithm

最小的K个数

  • 给定一个数组,找出其中最小的K个数。例如数组元素是4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。如果K>数组的长度,那么返回一个空的数组
  • 示例1

输入 [4,5,1,6,2,7,3,8],4
返回值 [1,2,3,4]
image.png

  1. function GetLeastNumbers_Solution(input, k)
  2. {
  3. // write code here
  4. input.sort((a,b)=>a-b); //升序排序
  5. var array=[];
  6. if(k>input.length){
  7. }else{
  8. for(let i=0;i<k;i++){
  9. array.push(input[i])
  10. }
  11. }
  12. return array;
  13. }
  14. module.exports = {
  15. GetLeastNumbers_Solution : GetLeastNumbers_Solution
  16. };

Review

Automation Testing Tutorial: What is Automated Testing?

介绍了自动化测试的概念,作用,使用范围,流程,测试工具,执行和自动化框架。是目前看下来比较全面的介绍,比国内很多技术博客写的详细全面,而且简单易懂。

Tip

多线程编程

“现在的计算机动辄就是多处理器核心的,而每一个线程同一时间只能运行在一个处理器上,那么如果程序采用单线程进行开发,这样就不能充分利用多核处理器带来的优势。所以为了充分利用多核处理器的资源来提高程序的执行性能,多线程编程变得越来越重要。比如对于计算密集型任务,使用一个线程可能需要 100 秒,但是,如果使用十个线程共同完成,那么需要的时间可能只有 10 秒左右。”
协程实现原理
Week 14 - 图2

Java Classes

define the state and behavior of their instances.
State comes from instance fields declared inside the class.
Behavior comes from methods defined in the class.

  1. public class Car {
  2. String color;
  3. // new fields!
  4. boolean isRunning;
  5. int velocity;
  6. // new parameters that correspond to the new fields
  7. public Car(String carColor, boolean carRunning, int milesPerHour) {
  8. color = carColor;
  9. // assign new parameters to the new fields
  10. isRunning = carRunning;
  11. velocity = milesPerHour;
  12. }
  13. public static void main(String[] args) {
  14. // new values passed into the method call
  15. Car ferrari = new Car("red", true, 27);
  16. Car renault = new Car("blue", false, 70);
  17. System.out.println(renault.isRunning);
  18. // false
  19. System.out.println(ferrari.velocity);
  20. // 27
  21. }
  22. }

Method signature is comprised of the method’s name and its parameter type.

  1. public class Store {
  2. // instance fields
  3. String productType;
  4. // constructor method
  5. public Store(String product) {
  6. productType = product;
  7. }
  8. // advertise method
  9. public void advertise() {
  10. System.out.println("Selling " + productType + "!");
  11. System.out.println("Come spend some money!");
  12. }
  13. // main method
  14. public static void main(String[] args) {
  15. Store lemonadeStand = new Store("Lemonade");
  16. lemonadeStand.advertise();
  17. }
  18. }

Reassigning Instance Fields

  1. public class Store {
  2. // instance fields
  3. String productType;
  4. double price;
  5. // constructor method
  6. public Store(String product, double initialPrice) {
  7. productType = product;
  8. price = initialPrice;
  9. }
  10. // increase price method
  11. public void increasePrice(double priceToAdd){
  12. double newPrice=price+priceToAdd;
  13. price=newPrice;
  14. }
  15. // main method
  16. public static void main(String[] args) {
  17. Store lemonadeStand = new Store("Lemonade", 3.75);
  18. lemonadeStand.increasePrice(1.5);
  19. System.out.println(lemonadeStand.price); //output 5.25
  20. }
  21. }

Return

  • We can use a value outside of the method it was created in if we return it from the method.
  • Any code that exists after the return statement in a function is ignored.
  • When creating new methods, we used the keyword void. A non-void method, we are replacing void with int, to signify that the return type is an int.

    1. class CarLot {
    2. Car carInLot;
    3. public CarLot(Car givenCar) {
    4. carInLot = givenCar;
    5. }
    6. public Car returnACar() {
    7. // return Car object
    8. return carInLot;
    9. }
    10. public static void main(String[] args) {
    11. Car myCar = new Car("red", 70);
    12. System.out.println(myCar);
    13. CarLot myCarLot = new CarLot(myCar);
    14. System.out.println(myCarLot.returnACar());
    15. /*outputs the same memory address because myCar and carInLot have the same reference value*/
    16. }
    17. }

    toString Method

    1. public class Store {
    2. // instance fields
    3. String productType;
    4. double price;
    5. // constructor method
    6. public Store(String product, double initialPrice) {
    7. productType = product;
    8. price = initialPrice;
    9. }
    10. // increase price method
    11. public void increasePrice(double priceToAdd){
    12. double newPrice = price + priceToAdd;
    13. price = newPrice;
    14. }
    15. // get price with tax method
    16. public double getPriceWithTax(){
    17. double tax = 0.08;
    18. double totalPrice = price + price*tax;
    19. return totalPrice;
    20. }
    21. // main method
    22. public static void main(String[] args) {
    23. Store lemonadeStand = new Store("Lemonade", 3.75);
    24. Store cookieShop = new Store("Cookies", 5);
    25. System.out.println(lemonadeStand);
    26. System.out.println(cookieShop);
    27. }
    28. public String toString(){
    29. return "This store sells "+productType+" at a price of "+price+".";
    30. }
    31. }

    Review

    ```java public class SavingsAccount {

    int balance;

    public SavingsAccount(int initialBalance){ balance = initialBalance; }

    public void checkBalance(){ System.out.println(“Hello!\n Your balance is “+balance); }

    public int deposit(int amountToDeposit){ balance=balance+amountToDeposit; System.out.println(“You just deposited “+amountToDeposit); return amountToDeposit; }

    public int withdraw(int amountToWithdraw){ balance=balance-amountToWithdraw; System.out.println(“You just withdrew “+amountToWithdraw); return amountToWithdraw; }

    public String toString(){ return “This is a savings account with “ + balance + “ saved.”; }

    public static void main(String[] args){ SavingsAccount savings = new SavingsAccount(2000);

    //Check balance: savings.checkBalance();

    //Withdrawing: savings.withdraw(300);

    //Check balance: savings.checkBalance();

    //Deposit: savings.deposit(2000);

    //Check balance: savings.checkBalance();

    //Deposit: savings.deposit(600);

    //Check balance: System.out.println(“Hello!”); savings.checkBalance(); //test System.out.println(savings); }
    }

  1. Your balance is 2000<br />You just withdrew 300<br />Hello!<br /> Your balance is 1700<br />You just deposited 2000<br />Hello!<br /> Your balance is 3700<br />You just deposited 600<br />Hello!<br />Hello!<br /> Your balance is 4300<br />This is a savings account with 4300 saved.
  2. <a name="j9NBl"></a>
  3. ## Javadoc
  4. javadoc是一种将注释生成HTML文档的技术,是用来生成自己的API文档,易读且清晰明了,对于程序是不可或缺的。
  5. <a name="GJMA3"></a>
  6. ## Java Array
  7. 开一些数组记录不同杂志代号,借阅数量和评分。<br />output:<br />The article 'Humans: Exterminate Or Not?' has now been viewed 14 times!<br />The article 'Humans: Exterminate Or Not?' has now been viewed 15 times!<br />The top article is Oil News<br />The article 'Organic Eye Implants' is now rated 5.0 stars!
  8. ```java
  9. //keep track of trending articles and their associated views and ratings
  10. public class Newsfeed {
  11. String[] trendingArticles;
  12. int[] views;
  13. double[] ratings;
  14. public Newsfeed(String[] initialArticles, int[] initialViews, double[] initialRatings){
  15. trendingArticles = initialArticles;
  16. views = initialViews;
  17. ratings = initialRatings;
  18. }
  19. public String getTopArticle(){
  20. return trendingArticles[0];
  21. }
  22. public void viewArticle(int articleNumber){
  23. views[articleNumber] = views[articleNumber] + 1;
  24. System.out.println("The article '" + trendingArticles[articleNumber] + "' has now been viewed " + views[articleNumber] + " times!");
  25. }
  26. public void changeRating(int articleNumber, double newRating){
  27. if (newRating > 5 || newRating < 0) {
  28. System.out.println("The rating must be between 0 and 5 stars!");
  29. } else {
  30. ratings[articleNumber] = newRating;
  31. System.out.println("The article '" + trendingArticles[articleNumber] + "' is now rated " + ratings[articleNumber] + " stars!");
  32. }
  33. }
  34. public static void main(String[] args){
  35. String[] robotArticles = {"Oil News", "Innovative Motors", "Humans: Exterminate Or Not?", "Organic Eye Implants", "Path Finding in an Unknown World"};
  36. int[] robotViewers = {87, 32, 13, 11, 7};
  37. double[] robotRatings = {2.5, 3.2, 5.0, 1.7, 4.3};
  38. Newsfeed robotTimes = new Newsfeed(robotArticles, robotViewers, robotRatings);
  39. robotTimes.viewArticle(2);
  40. robotTimes.viewArticle(2);
  41. System.out.println("The top article is " + robotTimes.getTopArticle());
  42. robotTimes.changeRating(3, 5);
  43. }
  44. }

Jupyter Notebook更改工作目录

Share

自动化测试介绍

软件测试(功能、接口、性能、自动化)详解

10 个自动化测试框架,测试工程师用起来

Python 自动化测试全攻略:五种自动化测试模型实战详解链接