[0x01 用Python讲解数据结构与算法] 关于数据结构和算法还有编程

忍耐和坚持虽是痛苦的事情,但却能渐渐地为你带来好处。 ——奥维德

一、学习目标

· 回顾在计算机科学、编程和问题解决过程中的基本知识;

· 理解“抽象”在问题解决过程中的重要作用;

· 理解并实现抽象数据结构;

· 复习Python编程语言

二、写在前面

    自第一台电子计算机使用线路和开关传达人类的指令以来,我们编程的思考方式有了很大的改变,在很多方面,计算机技术的发展为计算机科学家提供了众多的工具和平台去实现他们的想法。高性能理器,高速网络和大内存使得计算机研究者必须掌握在这样复杂的螺旋式通道中的编程能力,尽管计算机发展千变万化,但是一些基本的原则是不会改变的,计算机科学所关注的是如何使用计算机去解决问题。你花了大量的时间去学习一些基本的问题解决方式并且期望在处理问题的能力上信心十足,你也知道学习些程序的困难。大型问题的复杂度和相应的解决方案大大超过了与之相关的基础问题的处理方式。

    本章重点说明两个方面的内容。第一,回顾在计算机科学中研究数据结构和算法所必须遵循的框架,特别是要知道为什么学习和要理解这些主题能帮助我们更好地解决问题。第二,复习一下Python语言。尽管这里只是概要介绍和一些简短的例子,但是这回贯穿整全部的内容。

三、什么是计算机科学(Computer science)

Computer science is the study of problems, problem-solving, and the solutions that come out of the problem-solving process. Given a problem, a computer scientist’s goal is to develop an algorithm, a step-by-step list of instructions for solving any instance of the problem that might arise. Algorithms are finite processes that if followed will solve the problem. Algorithms are solutions.

……

Solutions are considered independent from the machine.

计算机科学是关于研究问题、问题解决过程以及问题解决过程的方案。提出一个问题,计算机科学家的目标就是找到一个算法这个算法用来指导如何一步一步解决这一类问题。算法就是一种特定的处理过程,遵循这个过程就能解决这个问题,也就是说算法就是解决方案

Computer science, as it pertains to the problem-solving process itself, is also the study of abstraction. Abstraction allows us to view the problem and solution in such a way as to separate the so-called logical and physical perspectives.

计算机科学的研究也是抽象的研究,抽象使我们看待问题的方式从具体(physical)中分离出来,从而上升到逻辑(logical)的层面。

举个例子,当你在使用Python中的math模块的,当我们导入这个模块,我们可以进行下面的处理:

image

上面这个例子就是一个过程的抽象(procedural abstraction),我们不需要知道sqrt函数是怎样实现的,我们唯一需要知道的如何使用这个函数。如果我们正确地导入了相关的模块,就可以假设这个函数所提供的结果是正确的,可以确定肯定是有人实现了求平方根的函数,但是我们并不关心这个问题,通常我们将这种情况抽象成一种“黑盒”模型:

We simply describe the interface: the name of the function, what is needed (the parameters), and what will be returned. The details are hidden inside。

我们只对接口进行描述:函数的名字,必要的条件(参数)以及函数的返回值。具体的细节被隐藏在内部。

blackbox[5]

四、什么是编程(Programming

Programming is the process of taking an algorithm and encoding it into a notation, a programming language, so that it can be executed by a computer. Although many programming languages and many different types of computers exist, the important first step is the need to have the solution. Without an algorithm there can be no program.

编程就是讲算法转换为符号(编程语言)以供计算机执行。尽管有众多的编程语言和不同的计算机存在,第一步也是最重要的一步就是找到解决问题的方案,没有算法就不会有程序。

Computer science is not the study of programming. Programming, however, is an important part of what a computer scientist does. Programming is often the way that we create a representation for our solutions. Therefore, this language representation and the process of creating it becomes a fundamental part of the discipline.

计算机科学不是学习编程,编程却是计算机科学家的一项重要的工作。编程是我们建立的解决方案的具体表现,所以编程语言是基础。

在计算机中所有的数据项表现为一种二进制串,为了使这些二进制变得有意义,我们需要相应的数据类型data types)。数据类型就是我们对二进制的交互接口,较底层的内置数据类型为算法的开发建立了基础。

The difficulty that often arises for us is the fact that problems and their solutions are very complex. These simple, language-provided constructs and data types, although certainly sufficient to represent complex solutions, are typically at a disadvantage as we work through the problem-solving process. We need ways to control this complexity and assist with the creation of solutions.

五、为什么要学习数据结构和抽象数据类型

在处理问题的时候为了防止陷入细节,通过在问题空间(problem domain)中创建数据模型可以更高效地处理问题,有更多的经历去关注问题的本身。

An abstract data type, sometimes abbreviated ADT, is a logical description of how we view the data and the operations that are allowed without regard to how they will be implemented.

抽象数据类型简称ADT,是一种对数据的逻辑抽象,对他的操作方法无需关注具体的实现。

为了提供这种级别的抽象,我们需要多数据进行封装encapsulation)。具体就是封装实现的具体细节,确保对用户不可见,这就是信息隐藏information hiding),如下图:

image

如果你是Python的初学者,或者对很多的概念感到陌生,建议你学习一下Python的基本语法和常用的模块,这里提供一些资源以供学习:

Python 官方文档:https://docs.python.org/3/reference/index.html

Python 文档(中文):http://python.usyiyi.cn/

廖雪峰的Python教程[推荐]:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000

笨办法学python:http://download.csdn.net/detail/csulennon/8944755

学习资料不求多,好好看一个精品系列就行,切忌东一榔头西一锤子。

原文地址:https://www.cnblogs.com/csulennon/p/4986346.html