/*
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;
}
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;
}
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;
}
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: 01/27/2022LatencyDate: 01/26/2022Take 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/2022Search 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/2022JS-Chess in your browser Needs features & bug-fixesLast update: 01/17/2022Book I am currently reading
War and Peace
Author: Leo Tolstoy
Translators: Louise and Aylmer Maude
Text: The Project Gutenberg
Date: 01/17/2022Flip 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);
}