Linux迷+Python粉 - 总结//blog.pythonwood.com/2017-11-30T22:12:00+08:00寒假挑战PythonTip(一人一python)总结——算法是程序的灵魂,程序员的心法2014-02-16T14:23:00+08:002017-11-30T22:12:00+08:00pythonwoodtag:blog.pythonwood.com,2014-02-16:/2014/02/寒假挑战PythonTip(一人一python)总结——算法是程序的灵魂,程序员的心法/<p>2014年2月中旬,我上升到挑战python英雄榜第3名。这是我寒假修炼算法的成果之一。来一下总结吧!</p> <p>Linux的创始人Linus&nbsp;Torvalds在一次演讲中有一段涉及“什么才是优秀程序员”的话:</p> <p><strong>“烂程序员关心的是代码。好程序员关心的是数据结构和它们之间的关系。”</strong></p> <p>由这句话的启发开始,加上我越来越觉得自己编程能力不足,码代码经常要重构,逻辑思维不过细腻谨慎。正是过于关注代码,完成率所致。我已经学过C/C++/nasm/java …</p><p>2014年2月中旬,我上升到挑战python英雄榜第3名。这是我寒假修炼算法的成果之一。来一下总结吧!</p> <p>Linux的创始人Linus&nbsp;Torvalds在一次演讲中有一段涉及“什么才是优秀程序员”的话:</p> <p><strong>“烂程序员关心的是代码。好程序员关心的是数据结构和它们之间的关系。”</strong></p> <p>由这句话的启发开始,加上我越来越觉得自己编程能力不足,码代码经常要重构,逻辑思维不过细腻谨慎。正是过于关注代码,完成率所致。我已经学过C/C++/nasm/java/python/html/js/jq/shell等等语言,对计算机世界的工作原理有比较好的认识,但代码能力一直上不去。我想算法,就是我的瓶颈。于是有了以下故事。</p> <h3 id="_1">第一阶段<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3> <p>寒假开始,我便在项目工作之余扫pythontip上的题。十几天里,有时一天7、8道题,有时2、3题,我的排名慢慢从第N页到第4页到第2页,让后到第1页。</p> <p>当我扫完101题后,我成功了42题,刚好排在第一页最后一名。然后我休息了一下,继续有成功几题。这大概就是我的无算法时代极限了。</p> <h3 id="_2">第二阶段<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3> <p>我网搜博客,算法,学习排序。好来买了《编程珠玑》和《编程之美》,边看边攻克pyhtontip上的题目。</p> <p>在PythonTip&nbsp;上的一些水题这博客中学习了很多,尤其是看到问题一句话就解决时,恍然大悟。</p> <p>1、有很多利用lambda, reduce, map, zip, 列表分片/解析, sum, max, min, divmod, round, hex/oct/bin; set,list,dict;&nbsp;一句话解决的。这是我认为python强大原因,非常好用。</p> <p>2、关于二进制‘1’的处理是非常妙的,这些题往往诠释了计算机较人类思维不同的特性,以及如何能让程序员思考得更“计算机”一些。在《编程之美》,上面提到的博客中都有提到。</p> <p>3、独立思考,让后比较,我获得更多。在最大连续子序列,最大非连续子序列,最长回文子字符串中,我收获很经典的算法。同时还想出来与Manacher算法一拼的做法。</p> <div class="highlight"><pre><span></span><span class="nv">L</span> <span class="o">=</span> <span class="s s-Atom">&#39;#&#39;</span><span class="p">.</span><span class="nf">join</span><span class="p">(</span><span class="nf">list</span><span class="p">(</span><span class="nv">L</span><span class="p">))</span> <span class="s s-Atom">#</span> <span class="s s-Atom">len</span> <span class="o">is</span> <span class="s s-Atom">odd</span> <span class="s s-Atom">now</span><span class="p">!</span> <span class="s s-Atom">l</span> <span class="o">=</span> <span class="nf">len</span><span class="p">(</span><span class="nv">L</span><span class="p">)</span> <span class="s s-Atom">o</span><span class="p">,</span><span class="s s-Atom">r</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span> <span class="s s-Atom">#</span> <span class="nf">圆心,半径</span><span class="p">(</span><span class="s s-Atom">不计圆心</span><span class="p">)</span> <span class="s s-Atom">for</span> <span class="s s-Atom">i</span> <span class="s s-Atom">in</span> <span class="nf">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="s s-Atom">l</span><span class="p">)</span><span class="s s-Atom">:</span> <span class="s s-Atom">if</span> <span class="s s-Atom">i</span><span class="o">+</span><span class="s s-Atom">r</span><span class="o">&lt;</span><span class="s s-Atom">l</span> <span class="s s-Atom">and</span> <span class="nv">L</span><span class="p">[</span><span class="s s-Atom">i</span><span class="o">-</span><span class="nn">r</span><span class="p">:</span><span class="s s-Atom">i</span><span class="p">]</span> <span class="o">==</span> <span class="nv">L</span><span class="p">[</span><span class="s s-Atom">i</span><span class="o">+</span><span class="nn">r</span><span class="p">:</span><span class="nn">i</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="s s-Atom">:</span> <span class="s s-Atom">#</span> <span class="s s-Atom">移到:最右方便来计算</span> <span class="s s-Atom">o</span><span class="p">,</span><span class="s s-Atom">r</span><span class="o">=</span><span class="s s-Atom">i</span><span class="p">,</span><span class="s s-Atom">r</span> <span class="s s-Atom">while</span> <span class="s s-Atom">i</span><span class="o">+</span><span class="s s-Atom">r</span><span class="o">+</span><span class="mi">1</span><span class="o">&lt;</span><span class="s s-Atom">l</span> <span class="s s-Atom">and</span> <span class="nv">L</span><span class="p">[</span><span class="s s-Atom">i</span><span class="o">-</span><span class="s s-Atom">r</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="nv">L</span><span class="p">[</span><span class="s s-Atom">i</span><span class="o">+</span><span class="s s-Atom">r</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span><span class="s s-Atom">:</span> <span class="s s-Atom">r</span> <span class="s s-Atom">+=</span> <span class="mi">1</span> <span class="s s-Atom">#</span> <span class="s s-Atom">print</span> <span class="nv">L</span><span class="p">[</span><span class="s s-Atom">o</span><span class="o">-</span><span class="nn">r</span><span class="p">:</span><span class="s s-Atom">o</span><span class="o">+</span><span class="s s-Atom">r</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="nv">L1</span> <span class="o">=</span> <span class="nv">L</span><span class="p">[</span><span class="s s-Atom">o</span><span class="o">-</span><span class="nn">r</span><span class="p">:</span><span class="s s-Atom">o</span><span class="o">+</span><span class="s s-Atom">r</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="s s-Atom">t1</span><span class="p">,</span><span class="s s-Atom">t2</span> <span class="o">=</span> <span class="nv">L1</span><span class="p">[</span><span class="s s-Atom">::</span><span class="mi">2</span><span class="p">],</span><span class="nv">L1</span><span class="p">[</span><span class="mi">1</span><span class="s s-Atom">::</span><span class="mi">2</span><span class="p">]</span> <span class="s s-Atom">print</span> <span class="s s-Atom">t1</span> <span class="s s-Atom">if</span> <span class="nf">any</span><span class="p">(</span><span class="s s-Atom">i</span><span class="p">!</span><span class="s s-Atom">=&#39;#&#39;</span> <span class="s s-Atom">for</span> <span class="s s-Atom">i</span> <span class="s s-Atom">in</span> <span class="s s-Atom">t1</span><span class="p">)</span> <span class="s s-Atom">else</span> <span class="s s-Atom">t2</span> </pre></div> <p>3月13日的&nbsp;格式优化版:</p> <div class="highlight"><pre><span></span><span class="nv">L</span> <span class="o">=</span> <span class="s s-Atom">&#39;#&#39;</span><span class="p">.</span><span class="nf">join</span><span class="p">(</span><span class="nv">L</span><span class="p">)</span> <span class="nv">L</span> <span class="o">=</span> <span class="s s-Atom">&#39;#&#39;</span><span class="o">+</span><span class="nv">L</span><span class="s s-Atom">+&#39;#&#39;</span> <span class="s s-Atom">#</span> <span class="s s-Atom">因为这句,末尾print语句统一了。</span> <span class="s s-Atom">o</span><span class="p">,</span><span class="s s-Atom">r</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span><span class="mi">0</span> <span class="s s-Atom">l</span> <span class="o">=</span> <span class="nf">len</span><span class="p">(</span><span class="nv">L</span><span class="p">)</span> <span class="s s-Atom">for</span> <span class="s s-Atom">i</span> <span class="s s-Atom">in</span> <span class="nf">xrange</span><span class="p">(</span><span class="s s-Atom">l</span><span class="p">)</span><span class="s s-Atom">:</span> <span class="s s-Atom">if</span> <span class="s s-Atom">i</span><span class="o">+</span><span class="s s-Atom">r</span> <span class="o">&gt;=</span> <span class="nn">l</span><span class="p">:</span> <span class="s s-Atom">break</span> <span class="s s-Atom">if</span> <span class="nv">L</span><span class="p">[</span><span class="s s-Atom">i</span><span class="o">-</span><span class="nn">r</span><span class="p">:</span><span class="s s-Atom">i</span><span class="p">]</span><span class="o">==</span><span class="nv">L</span><span class="p">[</span><span class="s s-Atom">i</span><span class="o">+</span><span class="nn">r</span><span class="p">:</span><span class="nn">i</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="s s-Atom">:</span> <span class="s s-Atom">o</span> <span class="o">=</span> <span class="s s-Atom">i</span> <span class="s s-Atom">j</span> <span class="o">=</span> <span class="s s-Atom">r</span><span class="o">+</span><span class="mi">1</span> <span class="s s-Atom">#</span> <span class="s s-Atom">尝试加一</span> <span class="s s-Atom">while</span> <span class="s s-Atom">o</span><span class="o">-</span><span class="s s-Atom">j</span><span class="o">&gt;=</span><span class="mi">0</span> <span class="s s-Atom">and</span> <span class="s s-Atom">o</span><span class="o">+</span><span class="s s-Atom">j</span><span class="o">&lt;</span><span class="s s-Atom">l</span> <span class="s s-Atom">and</span> <span class="nv">L</span><span class="p">[</span><span class="s s-Atom">o</span><span class="o">-</span><span class="s s-Atom">j</span><span class="p">]</span> <span class="o">==</span> <span class="nv">L</span><span class="p">[</span><span class="s s-Atom">o</span><span class="o">+</span><span class="s s-Atom">j</span><span class="p">]</span><span class="s s-Atom">:</span> <span class="s s-Atom">j</span> <span class="s s-Atom">+=</span> <span class="mi">1</span> <span class="s s-Atom">r</span> <span class="o">=</span> <span class="s s-Atom">j</span><span class="o">-</span><span class="mi">1</span> <span class="s s-Atom">print</span> <span class="nv">L</span><span class="p">[</span><span class="s s-Atom">o</span><span class="o">-</span><span class="s s-Atom">r</span><span class="o">+</span><span class="mi">1</span><span class="s s-Atom">:o</span><span class="o">+</span><span class="nn">r</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> </pre></div> <p>又经过10多天,我又扫完一遍题,这次我以72题的成绩排在GodIsCoding和<span class="caps">YOSHINO</span>之后第三名。</p> <h3 id="_3">总结:<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3> <p>做完之后,我的感觉是,python是很美很简单语言,如其名。</p> <h3 id="_4">生活题外话:<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h3> <p>没能及时报考C语言二级,非常遗憾,英语六级不知道报呢还是不报好。</p>大二C++课程期末作业——一个小工程项目总结2013-07-31T08:30:00+08:002017-11-30T22:12:00+08:00pythonwoodtag:blog.pythonwood.com,2013-07-31:/2013/07/大二C++课程期末作业——一个小工程项目总结/<p>第一次独立完成的C++小项目,40小时 + 5小时Update +&nbsp;8小时Linux移植。</p> <h3 id="_1">过程:<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3> <p>过程非常认真,一个星期主要就是忙这个,为了完成某个部分,有时饭都推迟吃,连续对着电脑10几个小时很累,晚上写到很晚才睡,休息也不够,这让我明白到程序员是不好当的!</p> <h3 id="_2">成果:<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3> <p><strong>最终版在Win7+<span class="caps">VS2005</span>、Ubuntu+G++环境都成功 …</strong></p><p>第一次独立完成的C++小项目,40小时 + 5小时Update +&nbsp;8小时Linux移植。</p> <h3 id="_1">过程:<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3> <p>过程非常认真,一个星期主要就是忙这个,为了完成某个部分,有时饭都推迟吃,连续对着电脑10几个小时很累,晚上写到很晚才睡,休息也不够,这让我明白到程序员是不好当的!</p> <h3 id="_2">成果:<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3> <p><strong>最终版在Win7+<span class="caps">VS2005</span>、Ubuntu+G++环境都成功。</strong></p> <h3 id="_3">环境工具的选择:<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3> <p><span class="caps">VS2010</span>随便写个程序在<span class="caps">XP</span>原生态下都不能运行!<span class="caps">VC6</span>太旧,<span class="caps">XP</span>下用得好。<span class="caps">VS2008</span>是Vista时代的魂魄。所以最好还是用<span class="caps">VS2005</span>。下载地址:Visual Studio&nbsp;各版本下载合集vs2005下载vs2008下载vs2010下载vs2012下载</p> <h3 id="_4">得益:<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h3> <p>一开始将main分成四个独立子函数,用的是增量式开发,俗称打桩,用得比较好,好处是模块化。这一点非常有益。</p> <p>记得刚开始学C语言的第21天,几天里,20个小时写出个蹩脚的图书馆综合系统。得益于这个经验,才令我相信我能完成。</p> <p>《Accelerate&nbsp;C++》的确是非常适合学习C++的,以前看了不明白它的好,认真看才知妙!完全借用了它的字符图形函数处理类Picture。</p> <h3 id="_5">改进:<a class="headerlink" href="#_5" title="Permanent link">&para;</a></h3> <p>要善用sprintf和sscanf:atoi、atof、itoa、itow函数是windows平台下实现字符串与数值相互转换的函数。Linux平台下请使用标准库中的sprintf与sscanf函数。</p> <p>调试过程很大部分是在<span class="caps">UI</span>用户中,说明<span class="caps">UI</span>函数多,且易错。</p> <p>房号设计02 - 23 ,0223&nbsp;,整数怎样表示,这些问题一直缠绕,一开始没解决好,就会影响全局。</p> <h3 id="_6">备忘:<a class="headerlink" href="#_6" title="Permanent link">&para;</a></h3> <p>类的定义和实现在项目推进过程中不断修改,逐渐完善。</p> <p>用户交互模块最大,ui()函数的ui.cpp文件有454行,<span class="caps">14KB</span>。有的模块文件只有几十行,这些开始时是没想到的。</p> <p>什么是优雅,注重代码重用,函数最好小而精,一次只做一件事。也是Unix类系统的神髓。</p> <p>老师对我的实验成果~~相当~~比较满意,给分也~~相对地~~高一点。感谢。</p>