当前位置:网站首页>(construction notes) learning experience of MIT reading
(construction notes) learning experience of MIT reading
2022-07-03 12:03:00 【Special case】
MIT reading Some learning experiences
List of articles
Static check
Hail sequence
The following example , It is given according to the law of hail sequence , According to the following example , Try to find the law of hail sequence :
2 ,1
3 ,10, 5 ,16, 8, 4, 2, 1
2n, 2n-1 , … , 4, 2, 1
5, 16, 8, 4, 2, 1
7, 22, 11, 34, 17, 52, 26, 13, 40, …? ( Will it stop ?)
Calculate the hail sequence
The following is the calculation of hail sequence java Code :
int n = 3;
while (n != 1) {
System.out.println(n);
if (n % 2 == 0) {
n = n / 2;
} else {
n = 3 * n + 1;
}
}
System.out.println(n);
Experience
The hail sequence is based on the given sequence , From positive integers n Start , If n It's even , Then the next number is n/2, Otherwise, the next number is 3n+1, until n be equal to 1. Due to the existence 3n+1 This change , Therefore, the size of sequence elements may vary from high to low —— This is also “ Hail sequence ” The origin of the name , Hail will go up and down in the clouds before landing . Then all sequences will eventually “ to ground ” Change to 1 Do you , This question is worth pondering .
Static type
Java Is a statically typed language . The types of all variables at compile time ( Before the program runs ) It is known. , Therefore, the compiler can also infer the type of all expressions . for example a and b Be declared int type , Then the compiler will get a+b It's also int Type of conclusion .
Static type is a special kind of static check , You can avoid bug The birth of . Among them, static types prevent a large part of type related bug—— To be exact , Is to use operators on objects of different types . If you write a line of code as follows :
"5"*"6
Then static type checking will find this when you edit the code bug, Instead of waiting until you compile and run the program .
Static check 、 Dynamic check 、 No check
Programming languages usually provide the following three methods of automatic checking :
Static check : bug Found before the program runs
Dynamic check : bug Found in the program running
No check : The programming language itself does not help you find errors , You have to pass certain conditions ( For example, the output results ) Check the correctness of the code .
Obviously , The effect of static inspection is much better than that of dynamic inspection , Better than no inspection .
Now let's analyze what we usually find in the three inspections bug.
Static check
Grammar mistakes : For example, forget punctuation marks or write wrong keywords . stay python in , If in programming , Extra indentation or line feed , Will be checked out .
Wrong name : Such as Math.sine(2). ( Should be sin.)
Number of parameters : Such as Math.sin(30, 20).
Type of parameter : Such as Math.sin(“30”).
Wrong return type : For example, a declaration returns String Type function return 10;
Dynamic check
Illegal variable value : For example, integer variables x、y, expression x/y Only after running y by 0 That's what makes a mistake , Otherwise, it is right .
Unrepresentable return value : For example, the final return value cannot be represented by the declared type .
A cross-border visit : For example, use a negative index in a string . Use one null Object dereference .
Experience
Static checking can detect errors related to variable types . As mentioned above , Grammar mistakes 、 Wrong name 、 Number of parameters, etc . Dynamic type checking tends to be triggered by specific values , For example, divide by zero error and cross-border access , The compiler will not report errors when compiling , An error will be reported only when running .
So when we write code , The first thing to pay attention to is to reduce the occurrence of static errors , Pay attention to the writing specification of the code , And the writing format , Improve our programming efficiency .
practice
Here are some based on java Code example of , Judge which error it is described above .
// Example 1
int sum=0;
for(i=0;i<=100;i++)
{
sum=sum+i
}
System.out.println(sum);
Example 1 Is a static error , Omission when writing code “ ; ”
// Example 2
int sum=10;
int i=0;
int average=sum/i;
Example 2 Dynamic error , When writing code, except 0 error .
// Example 3
int a=100000;
int b=a*a;
System.out.println("a The square of is :"+b);
Example 3 No error reported , But the wrong result .
Array and aggregate types
Now store the results of the hail sequence in the data structure . stay Java There are two commonly used linear storage structures : Arrays and lists .
An array is a structure consisting of a series of elements of the same type , And its length is fixed ( The number of elements is fixed ). for example , We declare one int An array of types :
int a[] = new int[50];
The following is based on arrays , Write hail sequence storage code :
int a[] = new int[50];
int i = 0;
int n = 3;
while (n != 1) {
a[i] = n;
i++;
if (n % 2 == 0) {
n = n / 2;
} else {
n = 3 * n + 1;
}
}
a[i] = n;
i++;
We can declare the list like this :
List<Integer> list = new ArrayList<Integer>();
The following is based on the list , Write hail sequence storage code :
List<Integer> list = new ArrayList<Integer>();
int n = 3;
while (n != 1) {
list.add(n);
if (n % 2 == 0) {
n = n / 2;
} else {
n = 3 * n + 1;
}
}
list.add(n);
Compare the above two storage methods , Thinking about similarities and differences .
Experience
We will find some problems in the storage of hail sequence based on array , We don't know the length of the hail sequence , The storage structure for storing hail sequences is defined as an array , And the length of the array is only 50, If the hail sequence we give is long enough , The system will report an error , Like this, bug It is called cross-border visit .
The list type improves this problem very well , Because the list will automatically expand to meet the newly added elements , So there is no longer worry about cross-border issues .
When we're programming , Choose different storage structures according to different scenarios and requirements , Make the code brief and easy to understand , And try to minimize unnecessary mistakes .
iteration
For in a sequence structure ( For example, lists and arrays ) Traversing elements ,Java and Python It's written in a similar way :
int max = 0;
for (int x : list) {
max = Math.max(x, max);
}
Math.max() It's a Java API Provided function .
Method
stay Java in , Declarations usually have to be in a method , And every method should be in a type , The following is the procedure of hail sequence :
public class Hailstone {
/** * Compute a hailstone sequence. * @param n Starting number for sequence. Assumes n > 0. * @return hailstone sequence starting with n and ending with 1. */
public static List<Integer> hailstoneSequence(int n) {
List<Integer> list = new ArrayList<Integer>();
while (n != 1) {
list.add(n);
if (n % 2 == 0) {
n = n / 2;
} else {
n = 3 * n + 1;
}
}
list.add(n);
return list;
}
}
Experience
MIT reading Part of the Static Checking It mainly describes the static inspection and several storage data structures .
First, introduce the hail sequence , First, the definition of hail sequence and simple output code .
Then pass the static check 、 Comparison between dynamic inspection and no inspection , Let's be more familiar with their similarities and differences . namely :
Static checking can detect errors related to variable types . Such as grammatical errors 、 Wrong name 、 Number of parameters, etc . Dynamic type checking tends to be triggered by specific values , For example, divide by zero error and cross-border access , The compiler will not report errors when compiling , An error will be reported only when running .
So when we write code , The first thing to pay attention to is to reduce the occurrence of static errors , Pay attention to the writing specification of the code , And the writing format , Improve our programming efficiency .
Then compare the difference between array and aggregation , And the difference in writing hail sequence code ,
It should be noted that , Arrays have lengths ( That is, the number of elements ), When storing non quantitative elements , There may be cross-border problems . The list will be automatically expanded to meet the newly added elements , So there is no need to worry about cross-border issues .
When we're programming , Choose different storage structures according to different scenarios and requirements , Make the code brief and easy to understand , And try to minimize unnecessary mistakes .
[ This article references from ]
(http://web.mit.edu/6.031/www/sp20/classes/01-static-checking/)
边栏推荐
- 《剑指offer 04》二维数组查找
- Symlink(): solution to protocol error in PHP artisan storage:link on win10
- Concurrent programming - singleton
- Yintai department store ignites the city's "night economy"
- vulnhub之pyexp
- Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
- Visual studio 2022 downloading and configuring opencv4.5.5
- (构造笔记)GRASP学习心得
- Cacti监控Redis实现过程
- 错排问题 (抽奖,发邮件)
猜你喜欢
牛牛的组队竞赛
Vulnhub narak
Talk about the state management mechanism in Flink framework
Yintai department store ignites the city's "night economy"
量化计算调研
Unicode encoding table download
STL Tutorial 9 deep copy and shallow copy of container elements
[MySQL special] read lock and write lock
vulnhub之presidential
QT OpenGL rotate, pan, zoom
随机推荐
STL Tutorial 9 deep copy and shallow copy of container elements
Vulnhub's Nagini
Vulnhub narak
网络通讯之Socket-Tcp(一)
2022年湖南工学院ACM集训第二次周测题解
Vulnhub's cereal
Raven2 of vulnhub
Vulnhub's Tomato (tomato)
Niuniu's team competition
Optimize interface performance
VS2015的下载地址和安装教程
同事写了一个责任链模式,bug无数...
previous permutation lintcode51
Socket TCP for network communication (I)
Unity3d learning notes 5 - create sub mesh
Colleagues wrote a responsibility chain model, with countless bugs
Basic knowledge of OpenGL (sort it out according to your own understanding)
【mysql官方文档】死锁
PHP導出word方法(一mht)
Quantitative calculation research