Created
January 13, 2017 10:45
-
-
Save nmabhinandan/e9a5f682db7e575dfde986c0da338fac to your computer and use it in GitHub Desktop.
Implementation of Intersection and Union of integer digits in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
bool anyMatch(int32_t num1, int32_t num2) { | |
while(true) { | |
if ((num2 % 10) == num1) { | |
return true; | |
} | |
if ((num2 / 10) == num2) { | |
break; | |
} | |
num2 /= 10; | |
} | |
return false; | |
} | |
// 112342 /\ 5612124 = 124 | |
int32_t intersect(int32_t num1, int32_t num2) { | |
int32_t matched = 0; | |
while(true) { | |
if (anyMatch((num1 % 10), num2) && !anyMatch((num1 % 10), matched)) { | |
matched = (matched * 10) + (num1 % 10); | |
} | |
if ((num1 % 10) == num1) { | |
break; | |
} | |
num1 /= 10; | |
} | |
return matched; | |
} | |
// 112342 \/ 5612124 = 123456 | |
int32_t yunion(int32_t num1, int32_t num2) { | |
int32_t yun = 0; | |
while(true) { | |
if (!anyMatch((num1 % 10), yun)) { | |
yun = (yun * 10) + (num1 % 10); | |
} | |
if ((num1 % 10) == num1) { | |
break; | |
} | |
num1 /= 10; | |
} | |
while(true) { | |
if (!anyMatch((num2 % 10), yun)) { | |
yun = (yun * 10) + (num2 % 10); | |
} | |
if ((num2 % 10) == num2) { | |
break; | |
} | |
num2 /= 10; | |
} | |
return yun; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
printf("%d\n", intersect(112342, 5612124)); | |
printf("%d\n", yunion(112342, 5612124)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment