2018-01-25

Excel: last character/string match in a string

Đôi khi có nhu cầu phải tìm ký tự hay sub-string cuối cùng xuất hiện trong Excel.
Search thì cũng ra nhanh nhưng mình note lại đây tập trung 1 chỗ.

Ví dụ
https://stackoverflow.com/questions/18617349/excel-last-character-string-match-in-a-string
https://www.excel-university.com/find-the-last-occurrence-of-a-delimiter-to-retrieve-the-lowest-sub-account-from-quickbooks-in-excel/

=FIND("@",SUBSTITUTE(A1,"\","@",(LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))/LEN("\")))

Mình dùng công thức này, ví dụ tìm "\" cuối cùng của cell A1

=SEARCH("@",SUBSTITUTE(A1,"\","@",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))

2017-12-30

How to register a Baibu account without a mainland Chinese mobile number

Đăng ký tài khoản Baidu để làm gì

  1. Để dùng Baidu Wangpan 百度网盘 tức Baidu Web Disk hay Baidu Web Drive, tên trước đó của nó là Baidu Cloud 百度云 https://pan.baidu.com/. Baidu đầu tư cả chục tỷ CNY cho cái đám này :D là ít. Nó cho phép lưu trữ tới 2TB (khởi đầu là 1TB) miễn phí, 2TB nhé quên Dropbox, OneDrive hay Google Drive đi, upload file đến 4GB, tool download/upload, sync. Có tính năng de-duplicate file torrent, có nghĩa là chỉ cần up file torrent lên Wangpan, nếu file torrent này được ghi nhận là đã có trên Wangpan, bạn có thể save về account của mình (resources sharing). Cho phép leech file torrents tức là download torrents online. Có tính năng phân tách "thư mục riêng tư" hay "thư mục ẩn" private zone hay hidden zone "ẩn tàng không gian" 隐藏空间. Nói chung rất rất là TQ và châu Á. Client app thì ngoại trừ tiếng Trung (mình cũng biết đôi chút) là trở ngại còn lại không có gì quá khó khăn để sử dụng. 



  2. Phần thứ 2 là các thứ liên quan đến development http://developer.baidu.com/ không đề cập nhiều ở đây. Baidu có nhiều thứ những cái mình có dùng như Echart https://ecomfe.github.io/echarts-doc/public/en/index.htmlhttp://echarts.baidu.com/ hay push notification ... Gần đây là Baidu AI, mình chưa đụng qua nhưng chắc cũng nên xem. Nó từng mời cả Andrew Ng về làm chief scientist của AI group cơ mà.
  3. Để TQ nó tốn resource chơi :D, để biết người ta ntn...

Đăng ký như thế nào

Nếu không phải từ TQ thì thực hiện đăng ký từ link sau:
https://passport.baidu.com/v2/?reg&overseas=1

Lưu ý là chỉ định từ "hải ngoại" :D overseas=1

Nếu không có chỉ định này tức ngầm định đăng ký từ Trung Quốc đại lục (中国大陆|Mainland China)

Đăng ký phải dùng dùng số di động từ Trung Quốc đại lục. Không có option để chọn nước


Đăng ký từ hải ngoại, có option để chọn nước ví dụ Việt Nam là +84 (+84 越南). Chỉ được đăng ký tài khoản với số di động chưa được đăng ký trước đó.


2017-12-28

Windows 10: show This PC icon on desktop, hide HomeGroup icon, hide OneDrive, hide Folders on This PC...

Khi chuyển qua Windows 10 có nhiều thứ không cần thiết thì lại có, những thứ cần thiết thì không thấy đâu.
Mấy thứ này lâu rồi nhưng không note lại. Mỗi lần làm lại đi search 1 vòng.


  1. Show This PC icon lên desktop: Windows + I  » Theme and related settings »  Scroll xuống Related settings (hoặc trên PC màn hình lớn thì nằm bên tay phải) » Desktop icon settings » Checked Computer » Có thể rename This PC thành My computer bằng cách chọn và edit.



  2. Remove HomeGroup icon trong File Exploere: Windows + I  » tương tự như trên vào Desktop icon settings » Unchecked Network. Bước tiếp theo vào Windows + E  »  View  »  Options hoặc Windows + I  » search File Explorer Options » View » Unchecked Use Sharing Wizard (recommended)
  3. Hide OneDrive icon trong File Explorer: chạy Registry Edit » Windows + R » regedit » search HKEY_CLASSES_ROOT\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6} bằng cách navigate tới HKEY_CLASSES_ROOT\CLSID» Ctrl + F » Find {018D5C66-4533-4307-9B53-224DE2ED1FE6}. Tắt System.IsPinnedToNameSpaceTree bằng cách set giá trị là 0.


  4. Bỏ các icon trong Folders của This PC trong File Explorer: Windows quản lý bật tắt các folder này trong Registry. Thực hiện navigate tới HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions rồi search. Trong đám này chỉ có Desktop và Download là may ra cần giữ còn lại nên cho đi hết. Thực hiện bật tắt tại key ThisPCPolicy, chuyển thành Hide tại:

    Documents Folder:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\{f42ee2d3-909f-4907-8871-4c22fc0bf756}\PropertyBag

    Pictures Folder:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\{0ddd015d-b06c-45d5-8c4c-f59713854639}\PropertyBag

    Videos Folder:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\{35286a68-3c57-41a1-bbb1-0eae73d76c95}\PropertyBag

    Downloads Folder:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\{7d83ee9b-2244-4e70-b1f5-5393042af1e4}\PropertyBag

    Music Folder:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\{a0c69a99-21c8-4671-8703-7934162fcf1d}\PropertyBag

    Desktop Folder:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}\PropertyBag

  5. Bỏ các icon 3D Object trong Folders của This PC trong File Explorer: navigate tới HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\NameSpace và HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\NameSpace rồi xóa {0DB7E03F-FC29-4DC6-9020-FF41B59E513A}. Không cần phải restart máy.

2017-08-01

Error installing polyglot in Python 3

Chuyển code từ Python 2.7 sang 3.x

Gặp lỗi dạng như sau:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 4941: character maps to 

https://stackoverflow.com/questions/41746072/error-installing-polyglot-in-python-3-5-2
https://github.com/aboSamoor/polyglot/issues/93

Nếu chưa cài những packages sau thì cài trước pycld2, PyICU, numpy
Trên Windows download tại http://www.lfd.uci.edu/~gohlke/pythonlibs
pycld2-0.31-cp36-cp36m-win32.whl
PyICU-1.9.7-cp36-cp36m-win32.whl
numpy-1.13.1+mkl-cp36-cp36m-win32.whl

Thực hiện download source từ Github
https://github.com/aboSamoor/polyglot/archive/master.zip
https://codeload.github.com/aboSamoor/polyglot/zip/master

Unzip và chạy tại folder và install bằng source thay vì dùng pip:
python setup.py install

Install một số libraries khác trên Windows.

Install libxml2, libxslt:
pip install lxml

Install SQLAlchemy:
pip install SQLAlchemy


2017-02-16

Notepad++: Remove lines containing/not containing string/word

Đôi khi dùng Notepad++ để remove lines chứa string hay word nào đó có thể sử dụng Regular Expression với negative lookbehind. Dùng nhiều nhưng giờ mới note lại ở đây để nhớ.

Tham khảo link
http://stackoverflow.com/questions/1971738/regex-for-all-strings-not-containing-a-string
http://stackoverflow.com/questions/406230/regular-expression-to-match-a-line-that-doesnt-contain-a-word

Ví dụ muốn remove nhưng line chứa warning hay error thực hiện như sau

^((?!(error|warning)).)*$



Sau đó có thể remove empty lines dùng Edit » Line Operation » Remove Empty Lines hay TextFX » TextFX Edit » Delete Blank Lines.

Nếu ngược lại muốn remove tất cả các lines ngoại trừ các lines chứa string/word. Thực hiện replace RegEx với empty như sau:

(?!^.*sometext.*$)^.+\r?\n
https://superuser.com/questions/290247/delete-all-lines-in-notepad-except-lines-containing-a-word-i-need/1318767#1318767?newreg=552307d560554536922266c54ac56cb7

2017-01-11

Tại sao chiều cao gần như tuân theo phân phối chuẩn ...?

Trong hầu hết các sách về lý thuyết xác xuất thống kê đều lấy ví dụ như vậy.

Đại khái là chiều cao của con người (hay đàn ông/phụ nữ ...) là một biến ngẫu nhiên tuân theo quy luật phân phối chuẩn. Hay khi textbook đề cập hầu hết các hiện tượng sinh học trong tự nhiên (or nhiều hiện tượng trong tự nhiên) gần như tuân theo phân phối chuẩn.

Khi học mình đến đây mình cũng thắc mắc ngay (trong đầu và để đó). Mới đầu mình cũng chưa hình dung vì sao, cứ để tạm đó xem, chắc có một lý do gì thôi. Sau đó chẳng thấy có giải đáp mẹ gì cả, cũng đặt câu hỏi tại sao lại như thế, nhưng cũng chẳng hỏi ai (ít lên lớp, lên cũng nằm ngáp ngáp). Và mình đã ngồi search kiếm câu trả lời. Gần đây có đứa em hỏi tại sao :D. Lâu rồi mình cũng mới thấy có người hỏi như vậy (trước đây khá lâu cũng có vài lần được hỏi...).

Thật ra có nhiều câu trả lời. Trên Quora thôi là đã rất nhiều (rất tiếc chẳng có phiên bản Quora tiếng Việt nào) mà hình như cũng không nhiều người xài Quora. Ví dụ:

Why do quantities in nature tend to be normally distributed, such as students' grade, human's height, size of snowflakes and so on. However, we find the returns of assets (like stocks, bonds, options) don't follow normal distribution (fat tail),How to explain it?

What are some real world examples of normally distributed quantities?


Bạn có thể tham khảo cả hai bài sau của John D. Cook (rất dễ hiểu)

Why hieghts are not normally distributed?


Mình tóm tắt lại bài đầu như sau:

Nếu chiều cao là một đặc tính di truyền đơn giản (simple genetic characteristic) thì sẽ quy định chiều cao là cao hoặc lùn (:D). Ví dụ tác giả nói rằng trong thí nghiệm di truyền của Mendel, Mendel có để là đậu nhăn nheo hoặc mịn chứ không để thuộc tính đậu hơi nhăn nheo. 

Có rất nhiều yếu tố di truyền và cả môi trường ảnh hưởng đến chiều cao. Theo đó nhiều yếu tố độc lập góp thành (sum) sẽ tạo nên một phân phối Gauss (Gaussian distribution) theo định lý giới hạn trung tâm (central limit theorem CLT).

2016-12-26

Castle Windsor Part 2 - Array configuration, dictionary configuration

Xem original

Part 2 – Array Configuration
Part 3 – Dictionary configuration

Array configuration

Đầu tiên là config array.

Ví dụ holiday service có code như sau:
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using System;

namespace ConsoleApp
{
    public class HolidayService
    {
        private DateTime[] holidays;

        public DateTime[] Holidays
        {
            get { return holidays; }
            set { holidays = value; }
        }

        public bool IsHoliday(DateTime date)
        {
            if (holidays != null)
            {
                DateTime matchDate = date.Date;
                foreach (DateTime dt in Holidays)
                {
                    if (dt.Date.Equals(matchDate))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            WindsorContainer container = new WindsorContainer(new XmlInterpreter());

            HolidayService holidayService = container.Resolve<HolidayService>();

            DateTime xmas = new DateTime(2016, 12, 25);
            DateTime newYears = new DateTime(2017, 1, 1);

            if (holidayService.IsHoliday(xmas))
            {
                Console.WriteLine("Merry X'mas!");
            }
            else
            {
                Console.WriteLine("X'mas is only for management!");
            }

            if (holidayService.IsHoliday(newYears))
            {
                Console.WriteLine("Happy new year!");
            }
            else
            {
                Console.WriteLine("New year, you haven't done all the work for last year!");
            }

            Console.ReadLine();
        }
    }
}
Config trong App.config như sau:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
  </configSections>
  <castle>
    <components>
      <component type="ConsoleApp.HolidayService, ConsoleApp">
        <parameters>
          <holidays>
            <array>
              <item>2016-12-24</item>
              <item>2016-12-25</item>
              <item>2017-1-1</item>
            </array>
          </holidays>
        </parameters>
      </component>
    </components>
  </castle>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
  </startup>
</configuration>
Có thể dùng <Holidays> hoặc <holidays> đều được vì Windsor đủ smart để inject. Nếu muốn resolve trực tiếp ra IList (hoặc IList, IEnumerable ... nói chung là generic collection)
static void Main(string[] args)
{
    WindsorContainer container = new WindsorContainer(new XmlInterpreter());
    var holidays = container.Resolve<IList<DateTime>>("holidays");
    Console.WriteLine(string.Join("\r\n", holidays));
    Console.ReadLine();
}
File config tương ứng như sau:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
  </configSections>
  <castle>
    <components>
      <component id="holidays" type="System.Collections.Generic.List`1[System.DateTime]">
        <parameters>
          <collection>
            <array>
              <item>2016-12-24</item>
              <item>2016-12-25</item>
              <item>2017-1-1</item>
            </array>
          </collection>
        </parameters>
      </component>
    </components>
  </castle>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
  </startup>
</configuration>

Cần nhớ ở dạng config này là thực hiện theo <parameters><collection><array><item>. Nếu đầy đủ hơn thì type chỉ định có cả assembly là <component id="holidays" type="System.Collections.Generic.List`1[[System.DateTime, mscorlib]], mscorlib">.

Dictionary configuration

Tương tự như array, dictionary configuration thực hiện với <parameters><dictionary><dictionary><entry>. Ví dụ AliasService như sau:
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using System;
using System.Collections.Generic;

namespace ConsoleApp
{
    public class AliasService
    {
        private Dictionary<string, string> dict;

        public Dictionary<string, string> Aliases
        {
            get { return dict; }
            set { dict = value; }
        }

        public string Evaluate(string term)
        {
            if (dict == null)
            {
                return term;
            }

            while (dict.ContainsKey(term))
            {
                term = dict[term];
            }

            return term;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            WindsorContainer container = new WindsorContainer(new XmlInterpreter());

            AliasService aliasService = container.Resolve<AliasService>();
            string sentence = "A dog ate my homework";

            foreach (string word in sentence.Split(new char[] { ' ' }, 
                StringSplitOptions.RemoveEmptyEntries))
            {
                Console.Write("{0} ", aliasService.Evaluate(word));
            }

            Console.ReadLine();
        }
    }
}
App.config cấu hình dictionary:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
  </configSections>
  <castle>
    <components>
      <component type="ConsoleApp.AliasService, ConsoleApp">
        <parameters>
          <Aliases>
            <dictionary>
              <entry key="dog">duck</entry>
              <entry key="ate">broke</entry>
              <entry key="homework">code</entry>
            </dictionary>
          </Aliases>
        </parameters>
      </component>
    </components>
  </castle>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
  </startup>
</configuration>
Để resolve trực tiếp ra IDictionary
static void Main(string[] args)
{
    WindsorContainer container = new WindsorContainer(new XmlInterpreter());
    var states = container.Resolve<IDictionary<string, string>>("states");
    Console.WriteLine(string.Join("\r\n", states.Keys));
    Console.ReadLine();
}
File config tương ứng như sau:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
  </configSections>
  <castle>
    <components>
      <component id="states" type="System.Collections.Generic.Dictionary`2[System.String, System.String]">
        <parameters>
          <dictionary>
            <dictionary>
              <entry key="VN-CT">Cần Thơ</entry>
              <entry key="VN-DN">Đà Nẵng</entry>
              <entry key="VN-HN">Hà Nội</entry>
              <entry key="VN-HP">Hải Phòng</entry>
              <entry key="VN-SG">Hồ Chí Minh</entry>
            </dictionary>
          </dictionary>
        </parameters>
      </component>
    </components>
  </castle>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
  </startup>
</configuration>
Tới đây là xong part 2. Về type convertor có thể tham khảo thêm phần Configuration with type converters trong series của Mike Hadlow '10 Advanced Windsor tricks'.