Date: 09/11/2023
How do you merge two Git repositories?Ref If you want to merge project-a into project-b
    cd path/to/project-a
    git filter-repo --to-subdirectory-filter project-a
    
    cd path/to/project-b
    git remote add project-a /path/to/project-a
    git fetch project-a --tags
    git merge --allow-unrelated-histories project-a/master # or whichever branch you want to merge
    git remote remove project-a
    

Date: 05/11/2022
Zigzag conversion - Leetcode
    /*
    Example 1:
    String: "PAYPALISHIRING"
    Number of rows: 3
    Result grid:
    P   A   H   N
    A P L S I I G
    Y   I   R
    
    Output: "PAHNAPLSIIGYIR"
    
    Example 2:
    String: "PAYPALISHIRING"
    Number of rows: 4
    Result grid:
    P     I     N
    A   L S   I G
    Y A   H R
    P     I
    
    Output: "PINALSIGYAHRPI"
    */
    char*
    convert (char *s, int numRows)
    {
      char *ret = NULL;
    
      // Check if NULL/empty string/zero rows
      if (!s || s[0] == '\0')
        return ret;
    
      if (numRows == 1)
        {
          ret = calloc (strlen (s) + 1, sizeof(char));
          memcpy (ret, s, strlen (s));
          return ret;
        }
    
      /*
       * Calculate number of columns required in map
       * We can fit (numRows + numRows - 2) characters in (1 + numRows - 2) columns
       *
       * zig_set_chars --> zig_set_cols
       * strlen(s)     -->  X
       * X = strlen (s) * zig_set_cols / zig_set_chars
      */
      int zig_set_chars = numRows * 2 - 2;
      int zig_set_cols = numRows - 1;
      int mapCols =  (strlen (s) * zig_set_cols / zig_set_chars);
      mapCols = (mapCols > 0) ? mapCols : 1;
    
      // Array to hold characters
      char *map[numRows];
      for (int i = 0; i < numRows; i++)
        map[i] = calloc (mapCols, sizeof (char));
    
      int s_index = 0, rows_iter = 0, cols_iter = 0;
      int rows_increment = 0, cols_increment = 0;
    
      while (s_index < strlen (s))
        {
          // Rows increment & columns stay same when at top row
          if (rows_iter == 0)
            {
              rows_increment = 1;
              cols_increment = 0;
            }
    
          // Rows decrement & columns increments when at bottom row
          if (rows_iter == numRows - 1)
            {
              rows_increment = -1;
              cols_increment = 1;
            }
    
          // Add character to map
          map[rows_iter][cols_iter] = s[s_index++];
    
          // Increment/decrement row & column in map
          rows_iter += rows_increment;
          cols_iter += cols_increment;
        }
    
      ret = calloc (strlen (s) + 1, sizeof(char));
      int ret_index = 0;
    
      for (int i = 0; i < numRows; i++)
        for (int j = 0; j < mapCols; j++)
          if (map[i][j] != '\0')
            ret[ret_index++] = map[i][j];
    
      for (int i = 0; i < numRows; i++)
        free (map[i]);
    
      return ret;
    }
    

Date: 05/10/2022
Median of two sorted arrays - Leetcode
    double
    findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){
      double res = 0;
    
      if (nums1Size == 0 && nums2Size == 0)
        return res;
    
      // Merge arrays
      int nums1Index = 0, nums2Index = 0, mergeIndex = 0;
      int mergeSize = nums1Size + nums2Size;
      int *merge = calloc (mergeSize, sizeof (int));
    
      // Merge given arrays into merge[]
      while (nums1Index < nums1Size && nums2Index < nums2Size)
        {
          if (nums1[nums1Index] <= nums2[nums2Index])
            merge[mergeIndex] = nums1[nums1Index++];
          else
            merge[mergeIndex] = nums2[nums2Index++];
    
          mergeIndex++;
        }
    
      // Copy the remaining elements of nums1[], if any
      while (nums1Index < nums1Size)
        merge[mergeIndex++] = nums1[nums1Index++];
    
      // Copy the remaining elements of nums2[], if any
      while (nums2Index < nums2Size)
        merge[mergeIndex++] = nums2[nums2Index++];
    
      // Calculate median for merge[]
      int mid = mergeSize / 2;
      if (mergeSize % 2)
        res = (double) (merge[mid]);
      else
        res = ((double) merge[mid] + (double) merge[mid - 1]) / 2;
    
      free (merge);
      return res;
    }
    

Date: 05/08/2022
Longest Palindrome - Leetcode
      char*
      longestPalindrome (char *s)
      {
        char *ret = NULL;
      
        // If NULL pointer or empty string return NULL
        if (!s || strlen (s) < 1)
          return ret;
      
        // If all characters are same, return in O(n)
        bool all_chars_same = true;
        size_t i = 1;
        while (all_chars_same && i < strlen (s))
          {
            if (s[i] != s[0])
              all_chars_same = false;
      
            i++;
          }
      
        if (all_chars_same)
          {
            ret = calloc (strlen (s) + 1, sizeof (char));
            memcpy(ret, s, strlen (s));
            return ret;
          }
      
        int start_index = 0, end_index = 0;
        for (size_t i = 0; i < strlen (s); i++)
          {
            // Odd length
            int left = i;
            int right = i;
            while (left >= 0 && right < strlen (s) && s[left] == s[right])
              {
                left--;
                right++;
              }
            int len1 = right - left - 1;
      
            // Even length
            left = i;
            right = i + 1;
            while (left >= 0 && right < strlen (s) && s[left] == s[right])
              {
                left--;
                right++;
              }
            int len2 = right - left - 1;
      
            int len = (len1 > len2) ? len1 : len2;
            if (len > end_index - start_index)
              {
                start_index = i - ((len - 1) / 2);
                end_index = i + (len / 2);
              }
          }
      
        ret = calloc (start_index + end_index + 2, sizeof (char));
        for (int j = 0, i = start_index; i <= end_index; i++, j++)
          ret[j] = s[i];
      
        return ret;
      }
    

Date: 05/03/2022
Two Sum - Leetcode
      int*
      twoSum (int *nums, int numsSize, int target, int *returnSize)
      {
        int *ret = NULL;
      
        if (!nums || !returnSize)
          return ret;
      
        for (int i = 0; i < numsSize; i++)
          {
            for (int j = i + 1; j < numsSize; j++)
              {
                if (nums[j] == target - nums[i])
                  {
                    *returnSize = 2;
                    ret = calloc (1, *returnSize * sizeof(int));
                    assert(ret);
                    ret[0] = i;
                    ret[1] = j;
                    break;
                  }
              }
      
            if (ret)
              break;
          }
      
        return ret;
      }
    
Add Two Numbers - Leetcode
      struct ListNode
      {
        int val;
        struct ListNode *next;
      };
      struct ListNode*
      addTwoNumbers (struct ListNode *l1, struct ListNode *l2)
      {
        struct ListNode *ret = calloc (1, sizeof(struct ListNode));
        assert(ret);
        struct ListNode *current = ret;
      
        int carry = 0;
        while (true)
          {
            int sum = carry;
            if (l1)
              {
                sum += l1->val;
                l1 = l1->next;
              }
            if (l2)
              {
                sum += l2->val;
                l2 = l2->next;
              }
      
            current->val = sum % 10;
            carry = sum / 10;
      
            if (l1 || l2)
              {
                current->next = calloc (1, sizeof(struct ListNode));
                assert(current->next);
                current = current->next;
              }
            else
              break;
          }
      
        if (carry)
          {
            current->next = calloc (1, sizeof(struct ListNode));
            assert(current->next);
            current->next->val = carry;
          }
        return ret;
      }
    
Longest Substring - Leetcode
      int
      lengthOfLongestSubstring (char *s)
      {
        if (!s || s[0] == '\0')
          return 0;
      
        int result = 0, start = 0, end = 0, n = strlen (s);
        int map[SCHAR_MAX];
        memset (map, -1, sizeof (map));
      
        for (; end < n; end ++)
          {
            // Check if current character is a duplicate
            if (map[(int) s[end]] != -1)
              {
                result = ((end - start) > result) ? (end - start) : result;
                int prev_start = start;
      
                // Since the current character is a duplicate, we update start, as
                // (first occurrence of duplicate in map + 1).
                start = map[(int) s[end]] + 1;
      
                // Remove all characters from the previous window from map
                for (int i = prev_start; i < start; i++)
                  map[(int) s[i]] = -1;
              }
            map[(int) s[end]] = end;
          }
      
        return ((n - start) > result) ? (n - start) : result;
      }
    

Date: 02/02/2022
C swap int without temp
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
    

Date: 01/30/2022
C show progress
        printf ("00%%");
        fflush(stdout);
        for (int i=10; i<101; i+=10)
          {
            printf ("\b\b\b%d%%",i);
            fflush(stdout);
            sleep (1);
          }
        printf ("\n");
      

Date: 01/27/2022
Latency Latency

Date: 01/26/2022
Take an online journey through the history of math
      “History of Mathematics” an online exhibit 
      developed by the National Museum of Mathematics in New York City and Wolfram Research, a 
      computational technology company. Bringing together the Sumerian tablet and more than 70 other 
      artifacts, the exhibit demonstrates how math has been a universal language across cultures and 
      throughout time.
    

Date: 01/22/2022
Search file recursively in a directory tree
      /*
       ==============================================================================
       Function:     search_file()
       Arguments:
       1. char* - Directory name
       2. const char* - File name to search
       3. char ** - Pointer to copy the absolute path to
       Return type:  int - 0 if executable found in directory tree, else -1
       Description:  Perform recirsive search on directory tree for executable
       ==============================================================================
      */
      int
      search_file (char *dir_name, const char *file_name, char **absolute_path)
      {
        // Initialize variables
        DIR *dir;
        int exec_found = -1;
        struct dirent *entry;
      
        dir = opendir (dir_name);
        while ((entry = readdir (dir)))
          {
            // Skip '.' & '..' entries
            if (strcmp (entry->d_name, ".") == 0 || strcmp (entry->d_name, "..") == 0)
              continue;
      
            // Build absolute path of file to search
            int length = strlen (dir_name) + strlen (entry->d_name) + 2;
            char *newpath = (char *) calloc (length, sizeof (char));
            
            if (dir_name[strlen (dir_name) - 1] != '/')
              snprintf (newpath, length, "%s/%s", dir_name, entry->d_name);
            else
              snprintf (newpath, length, "%s%s", dir_name, entry->d_name);
      
            // If file found & is executable, set absolute path and return
            if (strcmp (entry->d_name, file_name) == 0)
              {
                *absolute_path = strdup (newpath);
                exec_found = EXIT_SUCCESS;
                break;
              }
      
            // Perform recursive search, if file not found in current directory
            if (entry->d_type == DT_DIR)
              exec_found = search_file (newpath, file_name, absolute_path);
            else
              {
                free (newpath);
                continue;
              }
      
            free (newpath);
      
            if (exec_found == EXIT_SUCCESS)
              break;
          }
      
        closedir (dir);
        return exec_found;
      }
    

Date: 01/19/2022
JS-Chess in your browser
Needs features & bug-fixes

Last update: 01/17/2022
Book I am currently reading
      War and Peace
      Author: Leo Tolstoy
      Translators: Louise and Aylmer Maude
      Text: The Project Gutenberg
      
      
    

Date: 01/17/2022
Flip 1/0
      
        var = 1 - var
      
    
Flip 1/-1
      
        var = -(var)
      
    
Trim left and right whitespaces from given string
      /*
       ==============================================================================
       Function:     trim()
       Arguments:    char* - String to clean
       Return type:  void
       Description:  Remove leading / trailing whitespaces from given string
       ==============================================================================
       */
      void
      trim_lr (char *str)
      {
        int index = 0;
        int i = 0;
      
        // Trim leading white spaces
        index = 0;
        while (str[index] == ' ' || str[index] == '\t' || str[index] == '\n'
            || str[index] == EOF)
          index++;
      
        // Shift all trailing characters to its left
        i = 0;
        while (str[i + index] != '\0')
          {
            str[i] = str[i + index];
            i++;
          }
      
        // Terminate with NULL character
        str[i] = '\0';
      
        // Trim trailing white spaces
        i = 0;
        index = -1;
        while (str[i] != '\0')
          {
            if (str[i] != ' ' && str[i] != '\t' && str[i] != '\n')
              index = i;
      
            i++;
          }
      
        // Mark the next character to last non white space character as NULL
        str[++index] = '\0';
      }
    
Concatenate strings in array with an optional separator
      /*
       ==============================================================================
       Function:     strcatsep()
       Arguments:    char* - Destination, char* - Source, const char - Separator char
       Return type:  void
       Description:  Append src to dest with separator if given.
       ==============================================================================
       */
      void
      strcatsep (char *dest, const char *src, const char sep)
      {
        // Allocate memory to hold existing & new string with separator
        size_t dest_size = 0;
        dest_size += (dest) ? strlen (dest) : 0;
        dest_size += (src) ? strlen (src) : 0;
        dest_size += (sep) ? 1 : 0;
        
        dest = realloc (dest, dest_size);
      
        // If dest has contents, add given separator
        if (strlen (dest) > 0 && sep)
          dest[strlen (dest)] = sep;
      
        // Append src to dest
        if (src)
          strcat (dest, src);
      }