Created
March 11, 2016 23:26
-
-
Save alvarezp/be5a8c78cdfe9854f8bc to your computer and use it in GitHub Desktop.
Binary Gap: [Python, C] x [Ibanez, Alvarez]
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
int binary_gap_oa1(int N) { | |
int c=0; | |
int m=0; | |
if (N == 0) | |
return 1; | |
while (N > 1) { | |
if (N % 2 == 0) { | |
c = c + 1; | |
if (c > m) | |
m = c; | |
} else | |
c = 0; | |
N = N / 2; | |
} | |
return m; | |
} |
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
def binary_gap_oa1(N): | |
c=0 | |
m=0 | |
if N == 0: | |
return 1 | |
while N > 1: | |
if N % 2 == 0: | |
c = c + 1 | |
if c > m: | |
m = c | |
else: | |
c = 0 | |
N = N // 2 | |
return m |
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
static inline int max(int a, int b) { | |
if (a > b) | |
return a; | |
return b; | |
} | |
int a_max(int a[], int a_n) { | |
int m = 0; | |
int i = 0; | |
for (i = 0; i < a_n; i++) | |
m = max(m, a[i]); | |
return m; | |
} | |
char *binaryA(int N) { | |
int i=31; | |
char *s = NULL; | |
char r[33] = {0}; | |
if (N == 0) { | |
s = calloc(2, 1); | |
s[0] = '0'; | |
return s; | |
} | |
while (N > 1) { | |
r[i] = (N % 2 == 0) ? '0' : '1'; | |
N = N / 2; | |
i = i - 1; | |
} | |
r[i] = '1'; | |
return strdup(&(r[i])); | |
} | |
int * splitA(char s[], int * a_n, char sep) { | |
int i = 0; | |
int h = 0; | |
*a_n = 0; | |
int *r = NULL; | |
while (1 == 1) { | |
while (s[i] != sep && s[i] != '\0') { | |
i++; | |
} | |
if (h == i) { | |
if (s[i] == '\0') | |
break; | |
h++; | |
i++; | |
continue; | |
} | |
*a_n = *a_n + 1; | |
r = realloc(r, *a_n * sizeof(int *)); | |
r[*a_n - 1] = i - h; | |
if (s[i] == '\0') | |
break; | |
i = i + 1; | |
h = i; | |
} | |
return r; | |
} | |
int binary_gap_oi1(int N) { | |
int m=0; | |
int a_n=0; | |
int* zerolens = NULL; | |
if (N == 0) | |
return 1; | |
char *brep = NULL; | |
brep = binaryA(N); | |
zerolens = splitA(brep, &a_n, '1'); | |
for (int z = 0; z < a_n; z++) { | |
} | |
m = a_max(zerolens, a_n); | |
free(brep); | |
free(zerolens); | |
return m; | |
} |
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
def binary_gap_oi(N): | |
return len(max("{0:b}".format(N).split("1"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment