Git Product home page Git Product logo

dsa's People

Contributors

zhan2016 avatar

Watchers

 avatar

dsa's Issues

python编程

python中的类

1 python中的对象包括两个数据成员(类变量和实例变量)和方法。

class Employee:
   '所有员工的基类'
   empCount = 0 #类对象,类的静态变量
 
   def __init__(self, name, salary):
      self.name = name
      self.salary = salary
      Employee.empCount += 1
   
   def displayCount(self):
     print "Total Employee %d" % Employee.empCount
 
   def displayEmployee(self):
      print "Name : ", self.name,  ", Salary: ", self.salary

2 类中的所有函数第一个变量都是一个代表对象实例的变量,一般定义为self,他代表的是调用该方法的实例的地址。

python的垃圾回收

在python中,一切都是对象,包括常量!
Python 使用了引用计数这一简单技术来跟踪和回收垃圾。
在 Python 内部记录着所有使用中的对象各有多少引用。
一个内部跟踪变量,称为一个引用计数器。
当对象被创建时, 就创建了一个引用计数, 当这个对象不再需要时, 也就是说, 这个对象的引用计数变为0 时, 它被垃圾回收。但是回收不是"立即"的, 由解释器在适当的时机,将垃圾对象占用的内存空间回收。

a = 40      # 创建对象  <40>
b = a       # 增加引用, <40> 的计数
c = [b]     # 增加引用.  <40> 的计数

del a       # 减少引用 <40> 的计数
b = 100     # 减少引用 <40> 的计数
c[0] = -1   # 减少引用 <40> 的计数

垃圾回收机制不仅针对引用计数为0的对象,同样也可以处理循环引用的情况。循环引用指的是,两个对象相互引用,但是没有其他变量引用他们。这种情况下,仅使用引用计数是不够的。Python 的垃圾收集器实际上是一个引用计数器和一个循环垃圾收集器。作为引用计数的补充, 垃圾收集器也会留心被分配的总量很大(及未通过引用计数销毁的那些)的对象。 在这种情况下, 解释器会暂停下来, 试图清理所有未引用的循环。

类的继承

在python中继承中的一些特点:
1:在继承中基类的构造(init()方法)不会被自动调用,它需要在其派生类的构造中亲自专门调用。
2:在调用基类的方法时,需要加上基类的类名前缀,且需要带上self参数变量。区别在于类中调用普通函数时并不需要带上self参数
3:Python总是首先查找对应类型的方法,如果它不能在派生类中找到对应的方法,它才开始到基类中逐个查找。(先在本类中查找调用的方法,找不到才去基类中找)。

class Parent: #定义父类
    parentAttr=100
    def __init__(self):
        print("调用父类构造函数")
    def parentMethod(self):
        print("调用父类方法")
    def setAttr(self, attr):
        Parent.parentAttr = attr
    def getAttr(self):
        print("父类属性:%s" % Parent.parentAttr)

class Child(Parent):#定义子类
    def  __init__(self):
        print("调用子类的构造函数")
        Parent.__init__(self)    #显示调用父类的构造
    def  childMethod(self):
        print("调用子类方法")
    def parentMethod(self):
        print("override parent")
c = Child()
c.childMethod()
c.parentMethod()
c.setAttr(200)
c.getAttr()

调用子类的构造函数
调用父类构造函数
调用子类方法
override parent
父类属性:200

类属性与方法

类的私有属性
__private_attrs:两个下划线开头,声明该属性为私有,不能在类的外部被使用或直接访问。在类内部的方法中使用时 self.__private_attrs。
类的方法
在类的内部,使用 def 关键字可以为类定义一个方法,与一般函数定义不同,类方法必须包含参数 self,且为第一个参数
类的私有方法
__private_method:两个下划线开头,声明该方法为私有方法,不能在类地外部调用。在类的内部调用 self.__private_methods

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class JustCounter:
    __secretCount = 0  # 私有变量
    publicCount = 0    # 公开变量
 
    def count(self):
        self.__secretCount += 1
        self.publicCount += 1
        print self.__secretCount
 
counter = JustCounter()
counter.count()
counter.count()
print counter.publicCount
print counter.__secretCount  # 报错,实例不能访问私有变量

算法

引言

在对神奇的计算机结构进行了一番了解之后,即bit+NAND Gate之后。计算机实际上能够做的事情很简单,ALU只有简单的几种操作,即shift,and,or,xor,add, cmp等等,其最大的特点就是快。这么简单的一个计算机怎么能够"聪明"的做很多事情呢? 这就是算法。

什么是算法

算法和编程什么的没有任何关系啊. 算法是在特定模型下,完成一个特定事件的一系列指令。比方说在古埃及人求垂线的例子中,计算模型就是绳子,在**古时候算账的过程中,计算模型就是算盘,在做蛋糕的过程中,计算模型就是微波炉或烤箱。一系列的指令,在求垂线的过程中就是各个可重复的小步骤,在做蛋糕的过程中,也指的是一个个的步骤,指令。
一个有意思的观点:

So algorithm does not necessarily mean a "computer algorithm". It simply means a set of instructions that we follow to solve a problem at hand efficiently.

算法导论中对于算法的定义:

"an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values as output."

算法

各行各业,做所有的事情都有算法,即使现在这篇blog,也是使用了算法写出来的,因为跟随了如下的指令和步骤来实现的:

1. 开启一个issue
2.查阅资料
3.截取,理解相关资料
4.键入文字
5.修改,保存

你回家这件事,可以使用如下算法:

1.叫一辆出租车
2.告诉出租车司机,你的目的地址
3.付款给出租车司机
4.下车到家

我们写的每个功能函数,一系列代码,也是算法.只有具备了对算法的理解和思维,才能知道怎么使用计算机来做事情,高效的做事情,以及什么是好的,优秀的代码!
下面,我们先把算法限定在计算机算法里面.

算法支配的世界

实际上,当今世界,人们每天的生活已经离不开一些算法. 例如:

排序算法(海量数据查找)
傅里叶边换和快速傅里叶变换
Dijkstra’s algorithm
RSA algorithm(加密算法)
Secure Hash Algorithm
Integer factorization
Link Analysis
Proportional Integral Derivative Algorithm
Data compression algorithms
Random Number Generation

reference:

https://medium.com/@_marcos_otero/the-real-10-algorithms-that-dominate-our-world-e95fa9f16c04
https://www.quora.com/Why-do-we-use-algorithms

c++ 模板类

模板参数

模板参数能够将类型作为一种普通参数的样子传递

(just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type.)

模板函数

模板函数定义的两种语法,没有区别:

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

identifier可以写出任意的标识符,但是一般写作T,简短有力.
定义一个模板函数:

// function template
#include <iostream>
using namespace std;

template <class T>
T GetMax (T a, T b) {
  T result;
  result = (a>b)? a : b;
  return (result);
}

int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax<int>(i,j);
  n=GetMax<long>(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}

注意上面在代码区生成了两个getMax函数哦!因为两个参数的类型相同,可以做如下简写:

k=GetMax<int>(i,j);  --> k=GetMax(i,j); 
  n=GetMax<long>(l,m); -->k=GetMax(l,m);

同时使用多个模板参数

template <class T, class U>
T GetMin (T a, U b) {
  return (a<b?a:b);
}

int i,j;
long l;
i = GetMin(j,l)

模板类

// class templates
#include <iostream>
using namespace std;

template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}

int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}

当把模板类的函数实现可以直接写作内联形式,如果不写作内联形式,就是上面getMax的写法。

template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}

这里的意思是,定义一个模板参数T,getmax的返回值是T类型,同时给mypair的模板参数传值T!

模板特殊化(Template specialization)

有时候,大多数的类型都可以用这个模板类型处理,有些特殊的类型用特殊的实现:

// template specialization
#include <iostream>
using namespace std;

// class template:
template <class T>
class mycontainer {
    T element;
  public:
    mycontainer (T arg) {element=arg;}
    T increase () {return ++element;}
};

// class template specialization:
template <>
class mycontainer <char> {
    char element;
  public:
    mycontainer (char arg) {element=arg;}
    char uppercase ()
    {
      if ((element>='a')&&(element<='z'))
      element+='A'-'a';
      return element;
    }
};

int main () {
  mycontainer<int> myint (7);
  mycontainer<char> mychar ('j');
  cout << myint.increase() << endl;
  cout << mychar.uppercase() << endl;
  return 0;
}

注意,模板特殊化的时候,所有的函数都要实现一遍,因为这时候不能从模板结构中继承!

非类型(type)作为模板参数,与普通类型参数一样使用

// sequence template
#include <iostream>
using namespace std;

template <class T, int N>
class mysequence {
    T memblock [N];
  public:
    void setmember (int x, T value);
    T getmember (int x);
};

template <class T, int N>
void mysequence<T,N>::setmember (int x, T value) {
  memblock[x]=value;
}

template <class T, int N>
T mysequence<T,N>::getmember (int x) {
  return memblock[x];
}

int main () {
  mysequence <int,5> myints;
  mysequence <double,5> myfloats;
  myints.setmember (0,100);
  myfloats.setmember (3,3.1416);
  cout << myints.getmember(0) << '\n';
  cout << myfloats.getmember(3) << '\n';
  return 0;
}

在这里template <class T, int N>, 第二个参数就相当于在该类中有一个成员变量N可以用了!
甚至还可以给这些参数默认值:

template <class T=char, int N=10> class mysequence {..};
如果引用:

mysequence<> myseq;  等同于:mysequence<char,10> myseq;

模板类的声明和实现必须放在同一个文件中

在编译器的角度来看,模板并不是正常的函数或者类。它们是按需编译的,通俗的解释就是,模板函数的代码是在明确的使用某个模板参数实例化了模拟化函数以后,编译器才编译模板函数的代码。编译完这段代码以后,把代码放在可执行文件的代码区某个地方,后面引用该函数的地方就会换上这个代码区的入口地址!
假设我们把声明和实现分别放在.h和.cpp文件中,在编译器编译看到.cpp的文件的时候,他会扫描到模板的实现,但是并不会编译这些代码,直接忽略了这个cpp文件. 等到某个类引用了template的.h文件,这时候再去找template的实现时,找不到了,连接错误!

引用

http://www.cplusplus.com/doc/oldtutorial/templates/
https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl

什么是数据结构

把现实的世界搬到计算机中间的时候,如何存储这些信息?

答案完全是懵逼的,或者说这里就体现了不同的问题,不同人的不同选择,不同的人的不同的处理!这时候就体现了功力。选择了不同的数据结构来组织,存储信息,这就影响了后面对数据的一系列操作实现的算法选择,以及算法效率。

一个现实的例子

一个party,如何记录每个参加party的人的信息(姓名,性别,)。假设使用array,那访问速度很快,但是其存储大小在最初就确定了,比方说初始大小为为50,第51个来的时候,我们需要怎么办,因为使用了array的数据结构,我们需要生成一个新的array,把旧的数据复制到新的array中!如果使用链表结构,只需要在后面attach一个node。如果我们需要修改某个节点的信息,array的话,直接索引到节点修改,而对于链表,还需要遍历,循环到该处。至于求最短路径,搜索,排序之类的,对于不同的数据结构,效率可能有千差万别!
另一个例子,如果在几千万条的数据中找出中间的某一条?用啥数据结构?或者是排序呢,用啥数据结构,区别多大。
海量数据的出现,让算法和数据结构日趋重要起来。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.