<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Stack Problems</title>
    <description>My Personal Stack Problems</description>
    <link>https://chychenhongyi.github.io/</link>
    <atom:link href="https://chychenhongyi.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 19 Apr 2019 15:44:45 +0000</pubDate>
    <lastBuildDate>Fri, 19 Apr 2019 15:44:45 +0000</lastBuildDate>
    <generator>Jekyll v3.7.4</generator>
    
      <item>
        <title>TTE 系统详解之时钟</title>
        <description>&lt;h2 id=&quot;时钟&quot;&gt;时钟&lt;/h2&gt;

&lt;p&gt;在TTE系统中，最重要的就是时间同步，本系列文章将逐步由浅入深介绍相关概念和技术，首先介绍的是时钟。在许多工程模型中，时间作为一个独立的变量决定了系统状态序列的先后顺序。时间的度量以物理秒的度量为参考。&lt;/p&gt;

&lt;p&gt;在一个典型的实时应用中，分布式系统并行的执行不同的功能，例如，监测环境的状态变量，运行控制算法等。这些不同的功能通常由不同的节点执行，另外，分布式系统中还引入了冗余节点来容错。为了保证整个分布式系统的行为一致性，必须确保所有的节点使用相同的顺序来处理所有的事件，最完美情况下，处理顺序与事件发生的顺序保持一致。这时就需要一个全局的基准时钟通过给事件打&lt;strong&gt;时间戳&lt;/strong&gt;的方式来建立这种一致的时间顺序。&lt;/p&gt;

&lt;p&gt;下面将逐步介绍时钟相关的概念、全局时钟以及时间戳，最后对这个时间戳的精度进行分析。&lt;/p&gt;
&lt;h3 id=&quot;与时钟有关的概念&quot;&gt;与时钟有关的概念&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;数字物理时钟&lt;/strong&gt;（digital physical clock） ：它是一种测量时间的装置，包含一个计数器和一种周期性增加计数器技术的物理震荡机制。这个周期性的事件称为时钟的&lt;strong&gt;微拍&lt;/strong&gt;（microtick）。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;时钟粒度&lt;/strong&gt;(granularity)：两个连续的微拍之间的持续时间称为时钟粒度。假设一个时钟的震动频率为$10^{15}$microtick/s, 那么这个时钟的粒度则为1飞秒（fs,$10^{-15}$s）。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;时长&lt;/strong&gt;（duration）：两个事件的发生的间隔时长则通过计算这两个事件之间微拍的次数来测量。那么给定一个时钟，其粒度则可以通过本时钟两个微拍间隔期间参考时钟的微拍计数来度量。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;物理时钟漂移&lt;/strong&gt;（drift）：该时钟与参考时钟的频率比。如下图所示，时钟的震动频率与参考时钟存在偏差，而且偏差随着时间的增加而增加。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://chychenhongyi.github.io/static/img/weixin/20190418105336.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;一个完美的时钟的时钟漂移率为0，而在现实中，时钟会受到环境的影响，比如温度、电压、晶振的老化等，时钟的漂移是不可避免的。通常来说，时钟的漂移率根据晶振的质量而定，一般每秒最大时钟漂移在$10^{-2}$sec和$10^{-7}$sec之间。比如Fox Electronics公司的型号为K135晶振，其精度为20 ppm（part per million），则其每秒的时钟漂移为$2x10^{-5}$s
s；由于晶振的老化，每年会有+/-3 ppm 的漂移；随着温度的变化，也大概会造成0.04 ppm/($\Delta^{\circ}c$) 的漂移。正由于有时间漂移的存在，所以时钟是不可能永久同步的，在运行一段时间后，总会出现偏差。&lt;/p&gt;

&lt;h3 id=&quot;全局时钟&quot;&gt;全局时钟&lt;/h3&gt;
&lt;p&gt;在分布式的系统中，如果每一个节点的时钟都能与参考时钟完全同步，并且为所有事件打上时间戳，则很容易测量任意两个事件之间的间隔或者重建事件的时间顺序。但在每个节点都有自己的本地时钟的松耦合系统中，如此紧密的时钟同步是不可能的。因此，在分布式系统中，引入了一种较弱的全局时钟（Global Time）的概念。&lt;/p&gt;

&lt;p&gt;假设存在一组节点，每个节点都有自己的时钟，所有的时钟都以时钟精度 $\Pi$ 进行同步，即任意两个时钟之间的差值小于 $\Pi$。同步以后，我们则可以选择各自本地时钟的&lt;strong&gt;微拍&lt;/strong&gt;子集来作为各自全局时钟的本地实现。例如，本地时钟在实现全局时钟时，每10个微拍(microtick)选一个微拍作为全局时钟的拍（tick）。如果全局时钟的时钟粒度(granularity)大于时钟同步精度$\Pi$，那么我们认为这个全局时钟是合理的。此全局时钟的合理性选择条件决定了同步误差的上限为一拍(全局时钟的一拍)，即分布式系统中的任一节点的本地时钟认定某一事件发生的时间差小于一拍。这一拍的差距是因为同步误差的存在，系统总会出现这样一种情况：时钟$j$增加了一拍，然后事件$e$发生了，随后时钟$k$才增加了一拍。在这种情况下，时钟$j$和$k$给事件$e$打的时间戳则相差一拍。&lt;/p&gt;

&lt;p&gt;由于有一拍误差的存在，因此若两个事件的时间戳仅仅相差一拍，我们是无法知道其确切的先后顺序的。如果我们要知道两个事件的先后顺序，那么这两个事件的时间戳需要至少相差两拍。当我们测量一个事件持续时长时，其测量误差也会增加到2拍的时长，这是由于开始和结束的误差均为一拍，两者相减后即误差加大为2拍。&lt;/p&gt;

&lt;p&gt;在我们后续的文章中，将继续介绍TTE相关概念以及核心技术。&lt;/p&gt;
</description>
        <pubDate>Thu, 18 Apr 2019 16:50:39 +0000</pubDate>
        <link>https://chychenhongyi.github.io/tte/2019/04/18/TTE-Clock.html</link>
        <guid isPermaLink="true">https://chychenhongyi.github.io/tte/2019/04/18/TTE-Clock.html</guid>
        
        
        <category>TTE</category>
        
      </item>
    
      <item>
        <title>English Tips</title>
        <description>&lt;p&gt;我们应当关注那些，你懂得却不会使用的，技能/单词/语法/句子。&lt;/p&gt;

&lt;h1 id=&quot;spoken-english&quot;&gt;Spoken English&lt;/h1&gt;
&lt;ol&gt;
  &lt;li&gt;American Pronunciation Workshop&lt;/li&gt;
  &lt;li&gt;The American Accent Course&lt;/li&gt;
  &lt;li&gt;Mastering the American Accent&lt;/li&gt;
  &lt;li&gt;American Accent Training&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;english-writting&quot;&gt;English Writting&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;The Elements of Sytle&lt;/li&gt;
  &lt;li&gt;On Writion Well&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;website-to-download-e-books&quot;&gt;Website to Download e-books&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;鸠摩搜书 &lt;a href=&quot;https://www.jiumodiary.com/&quot;&gt;OJiumo E-Book Search&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.漢川草廬.tw/&quot;&gt;汉川草庐&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://gen.lib.rus.ec/&quot;&gt;Library Genesis&lt;/a&gt;，&lt;/li&gt;
  &lt;li&gt;古腾堡计划&lt;a href=&quot;http://www.gutenberg.org/&quot;&gt;Project Gutenberg&lt;/a&gt;，&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://bookzz.org/&quot;&gt;BookZZ&lt;/a&gt;，&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.free-ebooks.net/&quot;&gt;Free-Ebooks&lt;/a&gt;，&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://manybooks.net&quot;&gt;ManyBooks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.bookyards.com/en/welcome#.V6WOeKLodK8&quot;&gt;Bookyards&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;kindle&quot;&gt;kindle&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;send to my kindle –brok-drop in drodbox&lt;/li&gt;
  &lt;li&gt;kindmate to manage the bookmarks&lt;/li&gt;
  &lt;li&gt;clippings.io&lt;/li&gt;
  &lt;li&gt;OmniOutliner (IOS and Mac Only), WorkFlowy&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Sat, 06 Aug 2016 13:50:39 +0000</pubDate>
        <link>https://chychenhongyi.github.io/tools/2016/08/06/English-Writing.html</link>
        <guid isPermaLink="true">https://chychenhongyi.github.io/tools/2016/08/06/English-Writing.html</guid>
        
        
        <category>tools</category>
        
      </item>
    
      <item>
        <title>Latex Tips</title>
        <description>&lt;h1 id=&quot;usefull-website&quot;&gt;Usefull Website&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;https://cn.sharelatex.com/learn/Main_Page&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;破折号 —&lt;/p&gt;

&lt;p&gt;LaTeX的连字符, 用 - , 如”X-ray”;
         连数符, 用 – , 如”1–2”;
         破折号, 用 — ;
  这三者分别从短到长. 这些符号前后一般都无空格.&lt;/p&gt;

&lt;p&gt;单引号: 用 ` 和 ‘ 表示两个方向的单引号.
  双引号: 用``（esc键下方tab上方的那个键，连续输入两个） 和 ‘’ (连续输入两个单引号)表示.
           “ 只产生一个方向的双引号.&lt;/p&gt;

&lt;p&gt;注: 在数学环境中, 任何 - 都表示减号.&lt;/p&gt;
</description>
        <pubDate>Sat, 30 Jul 2016 14:50:39 +0000</pubDate>
        <link>https://chychenhongyi.github.io/tools/2016/07/30/Latex-Tips.html</link>
        <guid isPermaLink="true">https://chychenhongyi.github.io/tools/2016/07/30/Latex-Tips.html</guid>
        
        
        <category>tools</category>
        
      </item>
    
      <item>
        <title>Vim Tips</title>
        <description>&lt;h1 id=&quot;motion&quot;&gt;Motion&lt;sup id=&quot;fnref:1&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;简单的移动
    &lt;ul&gt;
      &lt;li&gt;0 -&amp;gt; 到本行头&lt;/li&gt;
      &lt;li&gt;^ -&amp;gt; 到本行第一个不是空白字符的地方&lt;/li&gt;
      &lt;li&gt;$ -&amp;gt; 到本行结尾&lt;/li&gt;
      &lt;li&gt;g_ -&amp;gt; 到本行最后一个不是blank字符的地方{not in vi}&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;更有效率的移动
    &lt;ul&gt;
      &lt;li&gt;NG -&amp;gt; 移动到第N行&lt;/li&gt;
      &lt;li&gt;
        &lt;table&gt;
          &lt;tbody&gt;
            &lt;tr&gt;
              &lt;td&gt;gg -&amp;gt; 移动到第一行&lt;/td&gt;
              &lt;td&gt;1G or :1&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;/li&gt;
      &lt;li&gt;G -&amp;gt; 到最后一行&lt;/li&gt;
      &lt;li&gt;(w/e/W/E) -&amp;gt; 按照单词移动(开头/结尾/Blank分隔开头/Blank分隔结尾)&lt;/li&gt;
      &lt;li&gt;%   -&amp;gt;  匹配括号移动，包括(,{,[&lt;/li&gt;
      &lt;li&gt;*/# -&amp;gt;  匹配光标所在的单词，移动到下一个/上一个匹配单词&lt;/li&gt;
      &lt;li&gt;fa -&amp;gt;   到下一个为a的字符处，a可以换成其他字符，如fs&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;edit&quot;&gt;Edit&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;start position=&quot;&quot;&gt;&lt;command /&gt;&lt;end position=&quot;&quot;&gt;
&lt;/end&gt;&lt;/start&gt;
    &lt;ul&gt;
      &lt;li&gt;0y$ -&amp;gt; 0 先到行头，拷贝，到本行的最后一个字符&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;ci??&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;copy&quot;&gt;Copy&lt;/h1&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;p -&amp;gt; 粘贴到当前位置之后&lt;/td&gt;
          &lt;td&gt;P -&amp;gt; 粘贴到当前位置之前&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
  &lt;li&gt;u -&amp;gt; undo | &lt;C-r&gt; -&amp;gt; redo
&lt;/C-r&gt;    &lt;h1 id=&quot;search&quot;&gt;Search&lt;/h1&gt;
    &lt;h1 id=&quot;plugin&quot;&gt;Plugin&lt;/h1&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every thing you need about vim. cf.https://github.com/mhinz/vim-galore&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot;&gt;
      &lt;p&gt;http://coolshell.cn/articles/5426.html &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Sat, 30 Jul 2016 13:50:39 +0000</pubDate>
        <link>https://chychenhongyi.github.io/tools/2016/07/30/Vim-Tips.html</link>
        <guid isPermaLink="true">https://chychenhongyi.github.io/tools/2016/07/30/Vim-Tips.html</guid>
        
        
        <category>tools</category>
        
      </item>
    
      <item>
        <title>Makdown Tips</title>
        <description>&lt;h1 id=&quot;善其工利其器&quot;&gt;善其工，利其器&lt;/h1&gt;

&lt;p&gt;(Sublime Text 3 Markdown 插件推荐）&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;OmniMarkupPreviewer&lt;sup id=&quot;fnref:1&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;
    &lt;ul&gt;
      &lt;li&gt;ctrl+alt+o 打开默认浏览器预览&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;MarkdownEditing
    &lt;ul&gt;
      &lt;li&gt;mdl+tab 插入链接&lt;/li&gt;
      &lt;li&gt;mdi+tab 插入图片&lt;/li&gt;
      &lt;li&gt;mdc+tab 插入code&lt;/li&gt;
      &lt;li&gt;mdh{1-6}+tab 插入标题&lt;/li&gt;
      &lt;li&gt;alt+shift+6 插入脚注&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;markdown-语法--&quot;&gt;Markdown 语法  &lt;sup id=&quot;fnref:2&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;#&lt;/h1&gt;

&lt;h2 id=&quot;标题&quot;&gt;标题&lt;/h2&gt;
&lt;div class=&quot;language-md highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;gh&quot;&gt;# 一级标题 #&lt;/span&gt;
&lt;span class=&quot;gu&quot;&gt;## 二级标题 ##&lt;/span&gt;
&lt;span class=&quot;gu&quot;&gt;### 三级标题 ###&lt;/span&gt;
...
&lt;span class=&quot;gu&quot;&gt;###### 六级标题 ######&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;列表&quot;&gt;列表&lt;/h2&gt;

&lt;p&gt;无序列表使用星号、加号或是减号作为列表标记；有序列表则使用数字接着一个英文句点。&lt;/p&gt;

&lt;h2 id=&quot;链接&quot;&gt;链接&lt;/h2&gt;
&lt;p&gt;两种形式的链接，不管是哪一种，链接文字都是用 [方括号] 来标记。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;行内式&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;语法部分 &lt;a href=&quot;http://wowubuntu.com/markdown/&quot; title=&quot;语法说明&quot;&gt;参考文献&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;参考式&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;语法部分 &lt;a href=&quot;http://wowubuntu.com/markdown/&quot;&gt;参考文献&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;code&quot;&gt;code&lt;/h2&gt;
&lt;p&gt;小行代码使用反引号标记 &lt;code class=&quot;highlighter-rouge&quot;&gt;`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;代码块 &lt;code class=&quot;highlighter-rouge&quot;&gt;&lt;/code&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt; &lt;/code&gt;`&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Helloword&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;table&quot;&gt;Table&lt;/h1&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Tables&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Are&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Cool&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;col 3 is&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;right-aligned&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;$1600&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;col 2 is&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;centered&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;$12&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;zebra stripes&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;are neat&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;$1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;| Tables        | Are           | Cool  |
| ------------- |:-------------:| -----:|
| col 3 is      | right-aligned | $1600 |
| col 2 is      | centered      |   $12 |
| zebra stripes | are neat      |    $1 |
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/loadingAnimation.gif&quot; alt=&quot;_config.yml&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/logo.png&quot; alt=&quot;logo&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot;&gt;
      &lt;p&gt;http://blog.leanote.com/post/54bfa17b8404f03097000000 &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot;&gt;
      &lt;p&gt;http://wowubuntu.com/markdown/ &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Wed, 27 Jul 2016 13:50:39 +0000</pubDate>
        <link>https://chychenhongyi.github.io/tools/2016/07/27/Markdown-Tips.html</link>
        <guid isPermaLink="true">https://chychenhongyi.github.io/tools/2016/07/27/Markdown-Tips.html</guid>
        
        
        <category>tools</category>
        
      </item>
    
      <item>
        <title>get Month Name [CF]</title>
        <description>&lt;p&gt;Map the given integer to a month.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;For &lt;code class=&quot;highlighter-rouge&quot;&gt;mo = 1&lt;/code&gt;, the output should be &lt;code class=&quot;highlighter-rouge&quot;&gt;getMonthName(mo) = &quot;Jan&quot;&lt;/code&gt;,&lt;/li&gt;
  &lt;li&gt;For &lt;code class=&quot;highlighter-rouge&quot;&gt;mo = 0&lt;/code&gt;, the output should be &lt;code class=&quot;highlighter-rouge&quot;&gt;getMonthName(mo) = &quot;invalid month&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Input/Output&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;[time limit] 4000ms (py)&lt;/li&gt;
  &lt;li&gt;[input] integer mo (A non-negative integer).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Constraints:&lt;/strong&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;0 ≤ mo ≤ 15&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;[output] string&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;code class=&quot;highlighter-rouge&quot;&gt;3&lt;/code&gt;-letter abbreviation of month number &lt;code class=&quot;highlighter-rouge&quot;&gt;mo&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;invalid month&quot;&lt;/code&gt; if the month doesn’t exist.&lt;/p&gt;

&lt;p&gt;Here are abbreviations of all months:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getMonthName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;months&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Jan&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Feb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Mar&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Apr&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;May&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Jun&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Jul&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Aug&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Sep&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Oct&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Nov&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Dec&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mo&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;months&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;months&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;invalid month&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Result Tests&lt;/strong&gt;:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getMonthName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;Jan&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getMonthName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;invalid month&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Fri, 10 Jun 2016 20:43:45 +0000</pubDate>
        <link>https://chychenhongyi.github.io/python/codefights/2016/06/10/get-month-name-cf.html</link>
        <guid isPermaLink="true">https://chychenhongyi.github.io/python/codefights/2016/06/10/get-month-name-cf.html</guid>
        
        
        <category>python</category>
        
        <category>codefights</category>
        
      </item>
    
      <item>
        <title>Find the number of even digits in the given integer [CF]</title>
        <description>&lt;p&gt;Find the number of even digits in the given integer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;For &lt;code class=&quot;highlighter-rouge&quot;&gt;n = 1010&lt;/code&gt;, the output should be &lt;code class=&quot;highlighter-rouge&quot;&gt;numberOfEvenDigits(n) = 2&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;For &lt;code class=&quot;highlighter-rouge&quot;&gt;n = 123&lt;/code&gt;, the output should be &lt;code class=&quot;highlighter-rouge&quot;&gt;numberOfEvenDigits(n) = 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Input/Output&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;[time limit] 4000ms (py)&lt;/li&gt;
  &lt;li&gt;[input] integer n (A positive integer).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Constraints:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;1 ≤ n ≤ 106.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;[output] integer&lt;/strong&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;My Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;numberOfEvenDigits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isdigit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Rests Tests:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;n: 1010
Output: 2

n: 123
Output: 1

n: 135
Output: 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Fri, 10 Jun 2016 20:39:03 +0000</pubDate>
        <link>https://chychenhongyi.github.io/python/codefights/2016/06/10/find-the-number-of-even-digits-in-the-given-integer-cf.html</link>
        <guid isPermaLink="true">https://chychenhongyi.github.io/python/codefights/2016/06/10/find-the-number-of-even-digits-in-the-given-integer-cf.html</guid>
        
        
        <category>python</category>
        
        <category>codefights</category>
        
      </item>
    
      <item>
        <title>Find Substrings for line Encoding [CF]</title>
        <description>&lt;p&gt;Given a string, return its encoding defined as follows:&lt;/p&gt;

&lt;p&gt;First, the string is divided into the least possible number of disjoint &lt;strong&gt;substrings&lt;/strong&gt; consisting of identical characters
for example, &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;aabbbc&quot;&lt;/code&gt; is divided into &lt;code class=&quot;highlighter-rouge&quot;&gt;[&quot;aa&quot;, &quot;bbb&quot;, &quot;c&quot;]&lt;/code&gt;
Next, each substring with length greater than one is replaced with a concatenation of its length and the repeating character
for example, substring &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;bbb&quot;&lt;/code&gt; is replaced by &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;3b&quot;&lt;/code&gt;
Finally, all the new strings are concatenated together in the same order and a new string is returned.&lt;/p&gt;

&lt;h4 id=&quot;substring&quot;&gt;SUBSTRING&lt;/h4&gt;

&lt;p&gt;A &lt;strong&gt;substring&lt;/strong&gt; of a string &lt;code class=&quot;highlighter-rouge&quot;&gt;S&lt;/code&gt; is another string &lt;code class=&quot;highlighter-rouge&quot;&gt;S'&lt;/code&gt; that occurs in &lt;code class=&quot;highlighter-rouge&quot;&gt;S&lt;/code&gt;. For example, &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;Fights&quot;&lt;/code&gt; is a substring of &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;CodeFights&quot;&lt;/code&gt;, but &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;CoFi&quot;&lt;/code&gt; isn’t.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For &lt;code class=&quot;highlighter-rouge&quot;&gt;s = &quot;aabbbc&quot;&lt;/code&gt;, the output should be &lt;code class=&quot;highlighter-rouge&quot;&gt;lineEncoding(s) = &quot;2a3bc&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input/Output&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;[time limit] 4000ms (py)&lt;/li&gt;
  &lt;li&gt;[input] string s (String consisting of lowercase English letters.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Constraints:&lt;/em&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;4 ≤ s.length ≤ 15.&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;[output] string (Encoded version of s.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;re&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lineEncoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;grub&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;finditer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;r&quot;(\w)\1*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;numb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;grub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;numb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numb&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numb&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Result Tests:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;aabbbc&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineEncoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;2a3bc&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;abbcabb&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineEncoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;a2bca2b&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;abcd&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineEncoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;abcd&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Fri, 10 Jun 2016 20:04:23 +0000</pubDate>
        <link>https://chychenhongyi.github.io/python/codefights/2016/06/10/find-substrings-for-line-encoding-cf.html</link>
        <guid isPermaLink="true">https://chychenhongyi.github.io/python/codefights/2016/06/10/find-substrings-for-line-encoding-cf.html</guid>
        
        
        <category>python</category>
        
        <category>codefights</category>
        
      </item>
    
      <item>
        <title>CensorThis - Censor the words [CF]</title>
        <description>&lt;p&gt;Author Question: &lt;strong&gt;Argaen&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Code Fights Weekly has gained popularity in the past months and is receiving lots of fan letters. Unfortunately, some of the readers use offensive words and the editor wants to keep the magazine family friendly.&lt;/p&gt;

&lt;p&gt;To manage this, you have been asked to implement a censorship algorithm. You will be given the fan letter &lt;code class=&quot;highlighter-rouge&quot;&gt;text&lt;/code&gt; and a list of &lt;code class=&quot;highlighter-rouge&quot;&gt;forbiddenWords&lt;/code&gt;. Your algorithm should replace all occurrences of the forbidden words in the text with sequences of asterisks of the same length.&lt;/p&gt;

&lt;p&gt;Be careful to censor only words, no one want to see &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;classic&quot;&lt;/code&gt; spelled as &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;cl***ic&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For text = “The cat does not like the fire” and
forbiddenWords = [“cat”,”fire”], the output should be
`CensorThis(text, forbiddenWords) = “The &lt;strong&gt;* does not like the **&lt;/strong&gt;”.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;[input] string text&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Text to censor, composed of mixed case English words separated by a single whitespace character each.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;[input] array.string forbiddenWords&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The list of words to censor, all in lowercase.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;[output] string&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The censored text. Its length should be the same as the length of text.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CensorThis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forbiddenWords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;*&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forbiddenWords&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Test 1&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;text: &quot;The cat does not like the fire&quot;
forbiddenWords: [&quot;cat&quot;, &quot;fire&quot;]
Expected Output: &quot;The *** does not like the ****&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Test 2&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;text: &quot;The cat does not like the therapy&quot;
forbiddenWords: [&quot;the&quot;,  &quot;like&quot;]
Expected Output: &quot;*** cat does not **** *** therapy&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Test 3&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;text: &quot;Python is the BEST programming language and LOLCODE is the Worst&quot;
forbiddenWords: [&quot;worst&quot;, &quot;best&quot;]
Expected Output: &quot;Python is the **** programming language and LOLCODE is the *****&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Test 4&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;text: &quot;A bald eagle is a worthy adversary&quot;
forbiddenWords: [&quot;bald&quot;, &quot;a&quot;]
Expected Output: &quot;* **** eagle is * worthy adversary&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Test 5&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;text: &quot;The MAGIC words are fiz buzz and plaf&quot;
forbiddenWords: []
Expected Output: &quot;The MAGIC words are fiz buzz and plaf&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Test 6&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;text: &quot;The MAGIC words are fiz buzz and plaf&quot;
forbiddenWords: [&quot;fluzz&quot;, &quot;z&quot;, &quot;ping&quot;, &quot;narf&quot;, &quot;tedd&quot;, &quot;troz&quot;, &quot;zort&quot;]
Expected Output: &quot;The MAGIC words are fiz buzz and plaf&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Thanks to &lt;strong&gt;GOD&lt;/strong&gt;.&lt;/p&gt;
</description>
        <pubDate>Sat, 21 May 2016 21:17:54 +0000</pubDate>
        <link>https://chychenhongyi.github.io/python/codefights/2016/05/21/censorthis-censor-the-words-cf.html</link>
        <guid isPermaLink="true">https://chychenhongyi.github.io/python/codefights/2016/05/21/censorthis-censor-the-words-cf.html</guid>
        
        
        <category>python</category>
        
        <category>codefights</category>
        
      </item>
    
      <item>
        <title>notPaired [CF]</title>
        <description>&lt;p&gt;Author Question: &lt;strong&gt;Lepluto&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Numberland, every integer living there has a soulmate which is the exact same number. To prevent family disasters, the Numberland mayor made sure that there is no more than two of a certain number. However, he clearly forgot to create a pair for one of the numbers, making it very sad and lonely. Given the array representing Numberland’s citizens, your task is to find which number you need to add to the array so that everyone has a pair.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;notPaired([1, 2, 1]) = 2&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;2 is the only number in the sequence that appears once.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;notPaired([1, 3, 5, 7, 9, 7, 5, 3, 1]) = 9&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;9 is the only number in the sequence that appears once.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Input/Output&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;[input] array.integer numberland&lt;/strong&gt;
  1 ≤ numberland.length ≤ 100&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;[output] integer&lt;/strong&gt;
  The final soulmate you need to add to Numberland in order to make everyone happy.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;notPaired&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numberland&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numberland&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numberland&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;notPaired&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Sat, 21 May 2016 21:10:03 +0000</pubDate>
        <link>https://chychenhongyi.github.io/python/codefights/2016/05/21/notpaired-cf.html</link>
        <guid isPermaLink="true">https://chychenhongyi.github.io/python/codefights/2016/05/21/notpaired-cf.html</guid>
        
        
        <category>python</category>
        
        <category>codefights</category>
        
      </item>
    
  </channel>
</rss>
