Programmer, how many more years can you do this?

雷果国
Release: 2020-06-20 15:11:36
Original
185 people have browsed it

The wave of technology is endless. After the wave recedes, what will be left to us is the methodology? Or outdated technology?

------

We programmers always like to talk about the so-called "35-year-old curse", as if the "35-year-old curse" has become a patent for programmers. However, in fact, the age of 35 is a problem faced by professionals in all walks of life.

Fundamentally, the age of 35 is the turning point when people reach middle age. If our value output at work relies more on physical strength, then we are bound to face the dilemma of career development starting to decline.

Programming is originally a highly creative work that values ​​abstract thinking and logical deduction. However, quite a few of us do programming like transactional work, as dull as water. If this is the case, "35 years old The curse" will haunt us.

In a social group, different people have different choices. I have no intention and cannot change the general environment, but I hope to help programmers who are willing to fight the "35-year-old curse" find some direction. Give it a few more years!

Starting from an interview question

Yang Hui Triangle is a question I often ask in interviews, "Please program and output the first N lines of Yang Hui Triangle according to the specified format."

Programmer, how many more years can you do this?

Most candidates, after getting the question, start coding. First write a two-layer loop, and then start thinking about the termination condition of the loop. After some thinking, Give an answer that you don't even believe. One of the most impressive times for me was when a candidate from WeChat Pay only provided a method to obtain the first N rows of data, but still failed to find a way to print it in the specified format.

Many people will say that they have forgotten the algorithm after leaving school for several years. This kind of question should be asked to the students recruited by the school. I don't think so. What I care about is not a specific algorithm, but the idea of ​​analyzing and solving problems. We can forget some specific algorithms, but while forgetting, we should internalize the problem-solving ideas contained in the algorithms into our own weapons.

Next, let me introduce to you how I view this issue.

Turing Award winner N. Wirth proposed that "program = algorithm data structure", I am more accustomed to saying "program = data process".

Data Perspective

First of all, let’s look at this interview question from the perspective of data. We can find:

1. You can use a two-dimensional array (in PHP language For example) Describe the first N rows of data of Yang Hui's triangle

[
  [1],
  [1, 1],
  [1, 2, 1],
  [1, 3, 3, 1],
]
Copy after login

2. Between the rows of data, there are the following rules:

1) The numbers on both sides of each row are always 1

2) The number in the middle of each row is the sum of the two numbers at the intersection of the previous row

Programmer, how many more years can you do this?

Therefore, a pseudocode can be used to describe this relationship:

F(1) = [1]
F(N) = [
    1,
    F(N-1)[0] + F(N-1)[1],
    F(N-1)[1] + F(N-1)[2],
    ...,
    F(N-1)[N-2] + F(N-1)[N - 1],
    1
]
Copy after login

Process Perspective

Next, let’s look at this interview question from a process perspective. First of all, its process can be divided into two steps: 1) Obtain the data of the first N rows of Yang Hui triangles; 2) Print according to the alignment shown in the diagram.

The first step, based on our previous analysis of the data, we can easily draw the corresponding method design:

function yanghui_datas($n); // 获取前N行杨辉三角数据
function yanghui_line_datas($prev_line_data); // 依据前一行数据获取下一行数据
Copy after login

The second step, print according to the alignment of the diagram. Pure printing is very simple, so the key is "alignment".

The way to solve the alignment problem is to leave the corresponding positions blank. But we found that it was not clear how to leave blank space. Then think further, what are the difficult issues that lead to unclear logic in the blank space?

Through analysis, you can see that if you only need to output the first 5 lines, then each number will be a single digit. If you want to output the 6th line, then there will be tens digits starting. It is therefore the uncertainty about the width of the numbers that creates the difficulty here.

Reverse thinking, if the width of the number is determined, there will be no such difficulty. So:

Programmer, how many more years can you do this?

We put each number into an equal-width grid. In this way, the whole problem becomes clear:

格子宽度 = 最大数字宽度 + 1
行前留白 = (LINE_COUNT - LINE_NO) * 格子宽度 / 2
格子内部: (数字居中)
左留白 = (格子宽度 - 数字宽度)  / 2
右留白 = 格子宽度 - 左留白 - 数字宽度
Copy after login

After the above analysis, the code almost appears on the paper.

// 获取前N行杨辉三角数据
function yanghui_datas($n_line) {
    $datas = []; 
    $prev_line_datas = []; 
    while ($n_line -- > 0) {
        $line_datas = yanghui_line_datas($prev_line_datas);
        array_push($datas, $line_datas);
        $prev_line_datas = $line_datas;
    }   
    return $datas;
}
// 通过前一行数据获取下一行数据
function yanghui_line_datas($prev_line_datas) {
    if (empty($prev_line_datas)) {
        return [1];
    }   
    $line_datas = []; 
    array_push($line_datas, 1); // 行首
    for ($i = 1; $i < count($prev_line_datas); $i ++) {
        array_push($line_datas, $prev_line_datas[$i - 1] + $prev_line_datas[$i]);
    }   
    array_push($line_datas, 1); // 行尾
    return $line_datas;
}
// 打印指定的杨辉三角数据
function yanghui_print($datas) {
    $space         = &#39; &#39;; // 留白字符
    $newline       = &#39;<br />&#39;; // 换行符
    $max_num       = yanghui_max_num($datas); // 最大数字
    $max_num_width = yanghui_num_width($max_num); // 最大宽度
    $unit_width    = $max_num_width + 1; // 格子宽度
    $line_count    = count($datas); // 行数
    foreach ($datas as $idx => $line_datas) {
        $line_no           = $idx + 1; // 行号
        $line_prefix_width = ($line_count - $line_no) * $unit_width / 2; // 行前留白数
        echo str_repeat($space, $line_prefix_width);
        foreach ($line_datas as $num) {
            $num_width   = yanghui_num_width($num); // 数字宽度
            $left_width  = intval(($unit_width - $num_width) / 2); // 格子内左留白宽度
            $right_width = $unit_width - $left_width - $num_width; // 格子内右留白宽度
            echo str_repeat($space, $left_width);
            echo $num;
            echo str_repeat($space, $right_width);
        }
        echo $newline;
    }
}
// 获取最大数
function yanghui_max_num($datas) {
    $max = 0;
    foreach ($datas as $line_datas) {
        foreach ($line_datas as $num) {
            $max = max($max, $num);
        }   
    }   
    return $max;
}
// 获取数字的宽度
function yanghui_num_width($num) {
    return strlen(strval($num));
}
Copy after login

In the end, the two parts of the process are assembled to produce the desired result.

function yanghui_dump($n) {
    $datas = yanghui_datas($n);
    yanghui_print($datas);
}
Copy after login

Supplement

Some people say that the numbers in Yang Hui's triangle can be directly calculated based on the coordinates. It doesn't matter, just replace the corresponding part of the obtained data.

Enlightenment

When we face problems, there are two modes of thinking: experience-based and ability-based.

Experiential thinking, search your own knowledge base, find that Yang Hui's triangle should be a two-layer cycle, and start executing it; after executing one step, you find that you are stuck. The next question is not in your knowledge base. Then I fell into deep thought!

Capability thinking, first analyze the essential components of the problem, decompose the problem, convert the complex big problem into multiple simple small problems, peel off the cocoons, and search for the answers to each small problem in your own knowledge base The answer, ultimately solves the problem.

The advantages and disadvantages of the two modes of thinking are obvious. Experience-based thinking is suitable for solving certain, known, and simple problems, and is more efficient; capability-based thinking is suitable for solving uncertain, unknown, and complex problems, and is adaptable. Wider.

Exercise ability-based thinking through programming

Programming is a highly creative job that focuses on logic and deduction.

Before we start programming, we first need to analyze the target system from the perspective of specific problems, and then we need to analyze the target system from an abstract perspective to perform deductions on the target system. Between the concrete and the abstract, we constantly deliberate to find a reasonable abstract description of reality.

This process of analysis and deduction is the process of finding the essence of things. However, in our daily work, we face a large number of complex demands and do not have much time to think about the design level. Reality has turned us into demand translation machines.

Most programmers are actually unwilling to do this, so I want to use a tutorial to help students who are willing to seek changes and promote the achievement of such changes.

In 2014, I wrote the framework currently used by the company. It has been running stably for more than 5 years and supports more than 600,000 lines of business code, but it only has 6,000 lines of code. In the next period of time, I will use it as a blueprint, from the perspective of rewriting a framework, comprehensively reproduce the entire process of thinking, analysis, design, and implementation of writing a framework, and organize it into a video tutorial, with a view to It can help everyone find the feeling of programming. (Voluntary payment, free to share and disseminate)

The entire tutorial is roughly divided into 12 lectures, each lecture has an independent theme, each lecture lasts 1-3 hours, and is accompanied by a public account article. . The large-level framework is divided into three parts: interaction with the external environment, encapsulation constraints on the business layer, and auxiliary tools such as ORM.

Tutorial

Due to the limited expressiveness of the text, this article is only used as an introduction. For specific analysis of career development, the value that the tutorial wants to convey, and the method used to convey the value, please download the video tutorial Learn more.

[Ten Years of Swords, Understanding Abstract Thinking through Frameworks] is a series of tutorials I recorded to help everyone find programs by recreating the entire process of thinking, analysis, design, and implementation of developing a framework. Design inspiration (voluntary payment, free to spread and share). Tutorials will be released through the public account "Abstract Thinking".

What is released today is the first lecture "The Significance of Recording Tutorials". If you are interested and want to learn in depth, please scan the QR code to download the video tutorial (Address: https://pan.baidu.com /wap/init?surl=IZ24Az5GFpQoQSD4WbfR8g Extract password: rcga)

The above is the detailed content of Programmer, how many more years can you do this?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
1
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template