Where is your faith?

这到底是谁?
耶稣对他们说:“你们的信心在哪里呢?”他们又惧怕又希奇,彼此说:“这到底是谁?他吩咐风和水,连风和水也听从他了。” (路加福音 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

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 (nTempLen > nTreeLen)
break;
pLeftEnd++;
}
int nLeftLen = (int)(pLeftEnd - pOrgInOrder);
int nRightLen = nTreeLen - nLeftLen - 1;
if (nLeftLen > 0)
Rebuild(pPreOrder+1, pInOrder, nLeftLen, &(pTemp->pLeft));
if (nRightLen > 0)
Rebuild(pPreOrder+nLeftLen+1, pInOrder + nLeftLen + 1, nRightLen, &(pTemp->pRight));
}

int main() {
char* pPreOrder = "abdcef";
char* pInOrder = "dbaecf";
NODE* pRoot = 0;
int nTreeLen = 6;
Rebuild(pPreOrder, pInOrder, nTreeLen, &pRoot);
PrintPreOrder(pRoot);
cout << endl;
PrintInOrder(pRoot);
cout << endl;
return 0;
}

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

“所以我告诉你们,不要为生命忧虑吃什么,喝什么;为身体忧虑穿什么。生命不胜于饮食吗?身体不胜于衣裳吗?  (马太福音 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)
你们这小信的人哪!野地里的草今天还在,明天就丢在炉里, 神还给它这样的妆饰,何况你们呢!  (马太福音 6:30 和合本)
If that is how God clothes the grass of the field, which is here today and tomorrow is thrown into the fire, will he not much more clothe you—you of little faith? (Matthew 6:30 NIV)
所以,不要忧虑说,吃什么?喝什么?穿什么?  (马太福音 6:31 和合本)
So do not worry, saying, ‘What shall we eat?’ or ‘What shall we drink?’ or ‘What shall we wear?’ (Matthew 6:31 NIV)
这都是外邦人所求的。你们需用的这一切东西,你们的天父是知道的。  (马太福音 6:32 和合本)
For the pagans run after all these things, and your heavenly Father knows that you need them. (Matthew 6:32 NIV)
你们要先求他的国和他的义,这些东西都要加给你们了。  (马太福音 6:33 和合本)
But seek first his kingdom and his righteousness, and all these things will be given to you as well. (Matthew 6:33 NIV)
所以,不要为明天忧虑,因为明天自有明天的忧虑;一天的难处一天当就够了。” (马太福音 6:34 和合本)
Therefore do not worry about tomorrow, for tomorrow will worry about itself. Each day has enough trouble of its own. (Matthew 6:34 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

耶稣的母亲和弟兄

        耶稣的母亲和他弟兄来了,因为人多,不得到他跟前。 有人告诉他说:“你母亲和你弟兄站在外边,要见你。” 耶稣回答说:“听了神之道而遵行的人就是我的母亲,我的弟兄了。” (路加福音 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)。祂的意思是:我们对神的职责必须优于其他一切。

fixed: embedded-redis: Unable to run on macOS Sonoma

Issue you might see below error while trying to run embedded-redis for your testing on your macOS after you upgrade to Sonoma. java.la...