Posts

Showing posts from October, 2017

Find the minimum larger node in binary search tree for a giving node.

Question: Find the minimum larger node in binary search tree for a giving node. Answer: struct BSTreeNode { BSTreeNode* pLeft; BSTreeNode* pRight; BSTreeNode* pParent; int value; }; BSTreeNode* findMinLarge(BSTreeNode* node) { if (!node) return 0; if (Node* result = node->pRight) { while (result && result->pLeft) { result = result->pLeft; } return result; } if (node->pParent == 0) { return 0; } if (node->pParent->pLeft == node) return node->pParent; BSTreeNode* parent = node->pParent; BSTreeNode* pparent = parent->pParent; while (1) { if (pparent && pparent->pLeft == parent) { return pparent; } if (!pparent) break; parent = pparent; pparent = parent->pParent; } return 0; }

the devils out of the man

Image
鬼被赶出         耶稣问他说:“你名叫什么?”他说:“我名叫‘群’”;这是因为附着他的鬼多。 鬼就央求耶稣,不要吩咐他们到无底坑里去。那里有一大群猪在山上吃食。鬼央求耶稣,准他们进入猪里去。耶稣准了他们, 鬼就从那人出来,进入猪里去。于是那群猪闯下山崖,投在湖里淹死了。 (路加福音 8:30-33 和合本)         And Jesus asked him, saying, What is thy name?And he said, Legion: because many devils were entered into him.And they besought him that he would not command them to go out into the deep.And there was there an herd of many swine feeding on the mountain: and they besought him that he would suffer them to enter into them. And he suffered them.Then went the devils out of the man, and entered into the swine: and the herd ran violently down a steep place into the lake, and were choked. (Luke 8:30-33 KJV)          耶稣问这人的名字,得到的回答是群,这似乎是在说有一整个军团的鬼进到他里面(一个罗马军团约有六千个士兵)。可见这个人里面住了很多鬼。那些鬼知道它们必须离开这人,就不断恳求耶稣不要吩咐它们到无底坑里去。无底坑是拘禁群鬼、甚至撒但的所在(启二十1及下)。它们央求耶稣准许它们进到正在附近吃食的一大群猪里面去,耶稣准了它们,鬼就离了那人,进入猪里去。那猪群闯下山崖,投在湖里淹死了。这里可能有人会问耶稣竟牺牲猪群的主人来医治这个人?对这个问题,基本的答复应该是:这人的痊愈要比一群猪重要得多。可有任何人当真以为猪应该得救,而这人应该继续留在不得救的情景中吗?同样地,发拉尔指出:“对于全城而言,将邻舍从这野蛮疯子的危险和恐惧中释放出来,这个收益要比

Where is your faith?

Image
这到底是谁? 耶稣对他们说:“你们的信心在哪里呢?”他们又惧怕又希奇,彼此说:“这到底是谁?他吩咐风和水,连风和水也听从他了。” (路加福音 8:25 和合本) And he said unto them, Where is your faith?And they being afraid wondered, saying one to another, What manner of man is this! for he commandeth even the winds and water, and they obey him. (Luke 8:25 KJV) 祂查问,你们的信心在哪里呢?暗示门徒不该惧怕,他们应该信靠祂。可见耶稣希望门徒对祂有信心。他们对这件事的反应,仿佛是见了神的面一样。他们充满了敬畏(惧怕),他们惊奇地问:“这到底是谁?”这是重要的问题,路加不容他的读者忽略它。这件事情,更新了门徒对耶稣的认识,他们更深地认识了祂的奥秘和奇妙。这才是最大的收获。今日的世界,充满艰难试探,很多时候神的计划看起来似乎岌岌可危。基督似乎睡着了。但是,不要叫醒祂。不要以为神想藉着你我的手来保护方舟,来托起小船。祂不需要这样作。

Rebuild Binary Tree from Preorder and Inorder Traversal

Image
Question: struct NODE { NODE* pLeft; NODE* pRight; char chValue; }; e.g. preorder traversal : a b d c e f inorder traversal : d b a e c f Answer: include using namespace std; struct NODE { NODE* pLeft; NODE* pRight; char chValue; }; void PrintPreOrder(NODE* pRoot) { if (!pRoot) return; cout << pRoot->chValue; PrintPreOrder(pRoot->pLeft); PrintPreOrder(pRoot->pRight); } void PrintInOrder(NODE* pRoot) { if (!pRoot) return; PrintInOrder(pRoot->pLeft); cout << pRoot->chValue; PrintInOrder(pRoot->pRight); } void Rebuild(char* pPreOrder, char* pInOrder, int nTreeLen, NODE** pRoot) { if (pPreOrder == 0 || pInOrder == 0) return; NODE* pTemp = new NODE; pTemp->pLeft = 0; pTemp->pRight = 0; pTemp->chValue = *pPreOrder; if (pRoot == 0) pRoot = pTemp; if (nTreeLen == 1) return; char pOrgInOrder = pInOrder; char pLeftEnd = pInOrder; int nTempLen = 0; while (*pPreOrder != *pLeftEnd) { nTempLen++; if (nTem

Array Right Shift K Steps

Question: An array has N numbers, move each elements to right for K steps. time limit : O(N) e.g. abcd1234 move 4 steps turns out 1234abcd Answer: void Reverse(int* arr, int b, int e) { for (; b < e; b++, e--) { int temp = arr[e]; arr[e] = arr[b]; arr[b] = temp; } } void RightShift(int* arr, int N, int K) { K %= N; Reverse(arr, 0, N - K - 1); Reverse(arr, N-K, N-1); Reverse(arr, 0, N-1); }

Therefore do not worry about tomorrow

Image
“所以我告诉你们,不要为生命忧虑吃什么,喝什么;为身体忧虑穿什么。生命不胜于饮食吗?身体不胜于衣裳吗?  (马太福音 6:25 和合本) “Therefore I tell you, do not worry about your life, what you will eat or drink; or about your body, what you will wear. Is not life more than food, and the body more than clothes? (Matthew 6:25 NIV) 你们看那天上的飞鸟,也不种,也不收,也不积蓄在仓里,你们的天父尚且养活它。你们不比飞鸟贵重得多吗?  (马太福音 6:26 和合本) Look at the birds of the air; they do not sow or reap or store away in barns, and yet your heavenly Father feeds them. Are you not much more valuable than they? (Matthew 6:26 NIV) 你们哪一个能用思虑使寿数多加一刻呢(或译:使身量多加一肘呢) ?  (马太福音 6:27 和合本) Can any one of you by worrying add a single hour to your life ? (Matthew 6:27 NIV) 何必为衣裳忧虑呢?你想野地里的百合花怎么长起来;它也不劳苦,也不纺线。  (马太福音 6:28 和合本) “And why do you worry about clothes? See how the flowers of the field grow. They do not labor or spin. (Matthew 6:28 NIV) 然而我告诉你们,就是所罗门极荣华的时候,他所穿戴的,还不如这花一朵呢!  (马太福音 6:29 和合本) Yet I tell you that not even Solomon in all his splendor was dressed like one of these. (Matthew 6:29 NIV) 你们这小信的

Find the smallest k elements

Question: Find the smallest k elements Answer: void findkmin(int *array, int len, int k) {   int* kmin = new int[k];   if (len <= k) {     for (int i = 0; i < len; i++)       kmin[i] = array[i];   }   else {     for (int i = 0; i < len; i++) {       int j;       for (j = 0; j < ((i < k) ? i : k); j++) {         if (kmin[j] > array[i])           break;       }       if (j < k) {         for (int m = k-1; m > j; m--)           kmin[m] = kmin[m-1];         kmin[j] = array[i];       }     }   }   for (int i = 0; i < k; i++)     cout << kmin[i] << " ";   cout << endl;   delete [] kmin; }

Largest subarray max sum problem

Question: Write an efficient C program to find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum. some numbers are negative. Time Complexity: O(n) Answer: void maxsumofsubarray(int* array, int len) { int curMax = 0; int mi = -1; int mj = -1; int max; bool max_init = false; for (int i = 0; i < len; i++) { if (curMax <= 0) { curMax = array[i]; mi = i; mj = i; } else { curMax += array[i]; } if (!max_init || max < curMax) { max = curMax; mj = i; max_init = true; } } for (int i = mi; i <= mj; i++) { cout << array[i] << " "; } cout << endl; cout << " max sub array sum is " << max << endl; }

Jesus's mother and brethren

Image
耶稣的母亲和弟兄         耶稣的母亲和他弟兄来了,因为人多,不得到他跟前。 有人告诉他说:“你母亲和你弟兄站在外边,要见你。” 耶稣回答说:“听了神之道而遵行的人就是我的母亲,我的弟兄了。” (路加福音 8:19-21 和合本)         Then came to him his mother and his brethren, and could not come at him for the press.And it was told him by certain which said, Thy mother and thy brethren stand without, desiring to see thee.And he answered and said unto them, My mother and my brethren are these which hear the word of God, and do it. (Luke 8:19-21 KJV)         耶稣的母亲和弟兄来找耶稣,可是因为人太多,到不了耶稣的跟前。 “有人告诉祂说:‘你母亲和你弟兄站在外边,要见你。’”按照一般人的观念,听说母亲和兄弟来了,应该停下讲道,赶快前往迎接。然而出于人意料的是,“他却回答那人说:‘谁是我的母亲?谁是我的弟兄?’就伸手指着门徒,说:‘看哪,我的母亲,我的弟兄。凡遵行我天父旨意的人,就是我的弟兄姐妹和母亲了。’”(太 12:48-50 ) 即祂的母亲和弟兄是听了神的道而遵行的人。那些接近耶稣的人,乃是那些认真负起他们向着神的职责之人。这并不意味着家庭的维系是不重要的、或可忽略的,耶稣不是在否认祂的家庭,甚至当祂为了完成救赎世人的工作而被挂在十字架上时,祂在痛苦中仍然想到祂的母亲(约十九26-27)。祂的意思是:我们对神的职责必须优于其他一切。

Transform the binary search tree into a sorted two-way linked list

Question: Transform the binary search tree into a sorted two-way linked list. do not use recursion. do not create any new node, only adjust the pointer to point. 10 / \ 6 14 / \ / \ 4 8 12 16 -->> 4=6=8=10=12=14=16 struct BSTreeNode { int m_nValue; // value of node BSTreeNode *m_pLeft; // left child of node BSTreeNode *m_pRight; // right child of node }; Answer: inline BSTreeNode* eleminateMinNode(BSTreeNode** tree) { BSTreeNode* treeHead = tree; if (!treeHead) return 0; BSTreeNode treeHeadParent = 0; while (treeHead->m_pLeft) { treeHeadParent = treeHead; treeHead = treeHead->m_pLeft; } if (treeHeadParent) treeHeadParent->m_pLeft = treeHead->m_pRight; if (treeHead == *tree) *tree = treeHead->m_pRight; return treeHead; } inline BSTreeNode* eleminateMaxNode(BSTreeNode** tree) { BSTreeNode* treeHead = tree; if (!treeHead) return 0; BSTreeNode treeHeadParent = 0; while (treeHead->m_pRight) { treeHeadParent = treeHead; treeH

Take heed therefore how ye hear

Image
恩赐 所以,你们应当小心怎样听;因为凡有的,还要加给他;凡没有的,连他自以为有的,也要夺去。” (路加福音 8:18 和合本) Take heed therefore how ye hear: for whosoever hath, to him shall be given; and whosoever hath not, from him shall be taken even that which he seemeth to have. (Luke 8:18 KJV) 这句话与撒种的比喻和交银的比喻(19:26)连在一起。正确的听是很重要的,不要像比喻中那不结果实的种子。“凡有的还要加给他”,也是在提醒我们同一个功课。这当然不是个鼓励有钱阶级的信息,它是与听神的道有关的。也就是说,当一个人,谦卑地领受主的教导,那么神就会把更多的真理赐给他,让他明白的更多,领受的更多。如果一个人,在神面前骄傲,他不仅没有办法领受到更多真理,甚至连自己已经明白的一些也会失去。这样的观念,也可以用在恩赐的使用上,如果我们认真使用神所赐的,就有添加,如果不用,甚至连我们自以为有的也会失去。

Reset user password for your own Ghost blog

Image
If you do not config mail up for your ghost blog, it maybe a big problem once you lost your user password. however, methods are always more than problems. We can fix it up by reset it. 1. Find out your database name in your ghost config, it is maybe in config.production.json   "database": {     "client": "mysql",     "connection": {       "host": "localhost",       "user": "ghost-452",       "password": "dade1b0565f2585e07c7",       "database": "ghost_production_acm" 2. Use mysql to reset the password. mysql -u root -p Enter the password for root account. mysql> use ghost_production_acm; mysql> select * from users; ghost use bcrypthash for the password. You can get bcrypt hash string from http://bcrypthashgenerator.apphb.com/ mysql>update users set password=' $2b$10$PaL8tKcU4i0CsjRrM6BhROVQMwwLFw2HSx/kWlAUgja23fNllc2Fy' where email=

Set up multi web server on a single VPS via ghost

Image
First please refer to set up ghost on your VPS. Set up ghost(a fully open source, hackable platform for building and running a modern online publication) on your google compute engine.

Set up ghost(a fully open source, hackable platform for building and running a modern online publication) on your google compute engine.

Image
Refers: https://docs.ghost.org/docs https://ghost.org/ Ubuntu 16.04 is recommended as your server OS. If you are first login into your blog server, Please Create a New User and Give Super User Privileges as well. $adduser <user> $usermod -aG sudo <user> $su - <user> Install Pre-requisites: $sudo apt-get update $sudo apt-get upgrade #nginx $sudo apt-get install nginx $sudo ufw allow 'Nginx Full' $sudo apt-get install mysql-server #Warning During mysql installation, you will be prompted to create a root password. Please ensure that you actually specify a value for this, as leaving the root password blank will make Ghost-CLI unable to connect to the database. #Adding swap memory if needed If your system has less than 1GB memory, you may run into errors. To overcome this, configure a larger amount of swap memory: $dd if=/dev/zero of=/var/swap bs=1k count=1024k $mkswap /var/swap $swapon /var/swap $echo '/var/swap swap swap default 0 0' >> /etc/fsta

Set up proxy server on your google compute engine via squid

Image
Install squid     sudo apt install squid3     Install apache utils     sudo apt-get install apache2-utils     This Installs the htpasswd utility that allows for creating password files.     Copy the default configuration file to back it up.     This configuration file is ridiculously huge and has way more than any simple case would need.  We will just back it up and make a new one with only the information that we need.     sudo cp /etc/squid/squid.conf /etc/squid/squid.conf.original         Delete the old configuration file     sudo rm /etc/squid/squid.conf Make a new configuration files     sudo vi /etc/squid/squid.conf     Enter this in the config file     auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwords     auth_param basic realm proxy     acl authenticated proxy_auth REQUIRED     http_access allow authenticated     # Choose the port you want. Default is 3128, but we set it to 7777     http_port 7777     The first bit (4 lines) sets squid to require pass
Image
 四种结果          “这比喻乃是这样:种子就是神的道。 那些在路旁的,就是人听了道,随后魔鬼来,从他们心里把道夺去,恐怕他们信了得救。 那些在磐石上的,就是人听道,欢喜领受,但心中没有根,不过暂时相信,及至遇见试炼就退后了。 那落在荆棘里的,就是人听了道,走开以后,被今生的思虑、钱财、宴乐挤住了,便结不出成熟的子粒来。 那落在好土里的,就是人听了道,持守在诚实善良的心里,并且忍耐着结实。” (路加福音 8:11-15 和合本)         Now the parable is this: The seed is the word of God.Those by the way side are they that hear; then cometh the devil, and taketh away the word out of their hearts, lest they should believe and be saved.They on the rock are they, which, when they hear, receive the word with joy; and these have no root, which for a while believe, and in time of temptation fall away.And that which fell among thorns are they, which, when they have heard, go forth, and are choked with cares and riches and pleasures of this life, and bring no fruit to perfection.But that on the good ground are they, which in an honest and good heart, having heard the word, keep it, and bring forth fruit with patience. (Luke 8:11-15 KJV)         在听道的人群中,有一些人是属于第一种情况,如文士们,他们心里早已有了自己的主张,魔鬼早已把很多错误的
Image
 用比喻的目的         门徒问耶稣说:“这比喻是什么意思呢?” 他说:“ 神国的奥秘只叫你们知道;至于别人,就用比喻,叫他们看也看不见,听也听不明。” (路加福音 8:9-10 和合本)         And his disciples asked him, saying, What might this parable be?And he said, Unto you it is given to know the mysteries of the kingdom of God: but to others in parables; that seeing they might not see, and hearing they might not understand. (Luke 8:9-10 KJV)          耶稣用一些普通的言论作为起头,来答复门徒对这个故事的询问。祂把门徒跟其他人作比较:特别强调你们。神国的奥秘是启示给他们的。真秘(mystēria)是那些我们自己永远无法发现,但神已经将之启示出来的真理;这个字在保罗书信中很常见,但在福音书中却只出现在与此有关的经文中。但至于别人,看却看不见,听也听不明;他们听见了比喻,却不明白它们的意义。比喻启示了真理,同时也把真理隐藏起来:它们把真理向诚心寻求的人启示出来,他们不厌其烦的在表面底下挖掘,发现其意义;但它们也把真理向那只以听听故事为满足的人隐藏起来。这很明显是比喻的结果,但耶稣却说这也是它们的目的(叫……)。对那些认真的人而言,比喻是知识的矿场;但对那些随随便便、漫不经心的人来说,它们是个审判。求主怜悯我们,让我们成为谦卑跟随主的人,因为主向谦卑跟随他的人就显出来,向骄傲的人就藏起来,所以我们要不断地求主保守我们的心,始终有一颗谦卑受教寻求的心。

Hey, ghost my blog, https://errong.win

Hey, ghost my blog! https://errong.win

How to fix up sendgrid-python, BadRequestsError

Traceback (most recent call last):   File "sendmail.py", line 79, in <module>     response = sg.client.mail.send.post(request_body=mail.get())   File "/home/errong_leng/.local/lib/python2.7/site-packages/python_http_client/client.py", line 227, in http_request     return Response(self._make_request(opener, request))   File "/home/errong_leng/.local/lib/python2.7/site-packages/python_http_client/client.py", line 161, in _make_request     raise exc python_http_client.exceptions.BadRequestsError The easy way is to catch this exception and print out the error body: try:     response = sg.client.mail.send.post(request_body=mail.get())     print(response.status_code)     print(response.body)     print(response.headers) except Exception as e:     print (e.body)  #print (e.read())  Then you can know why bad request error reported, such as: {"errors":[{"message":"Following RFC 1341, section 7.2, if either text/html or text/plain

Set up shadowsocks server on your google compute engine(ubuntu os) from shadowsocks-libev sources.

Image
Refers: https://github.com/shadowsocks/shadowsocks-libev