var imageList = [100,200,300,400,500];
var index = imageList.indexOf(200); // 1
You will get -1 if it cannot find a value in the array.
Programing Coderfunda
December 17, 2020
No comments
Programing Coderfunda
December 17, 2020
No comments
Programing Coderfunda
December 17, 2020
Array
No comments
Programing Coderfunda
December 17, 2020
Array
No comments
Programing Coderfunda
December 17, 2020
Array
No comments
The problem seems to be here:
for (int i=0; i<200; i++){
fscanf(CFG, "%c", &stringCFG[i]);
howManyItems++;
fprintf(output,"%c", stringCFG[i]);
}
This loop always executes 200 times regardless of what is in the file. In other words - the value of howManyItems will be 200 when the loop is done.
You can check that simply by printing howManyItems after the loop, i.e.
printf("After loop, howManyItems=%d\n", howManyItems);
Since you have:
char stringCFG[200];
then
stringCFG[howManyItems]= ','; // bad.. writing to stringCFG[200]
howManyItems++;
stringCFG[howManyItems]= '0'; // bad.. writing to stringCFG[201]
howManyItems++;
...
will write outside of the array. That is undefined behavior.
You need to stop the first loop once the whole file has been read. Something like:
for (int i=0; i<200; i++){
if (fscanf(CFG, "%c", &stringCFG[i]) != 1)
{
// No more data
break;
}
howManyItems++;
fprintf(output,"%c", stringCFG[i]);
}
and all the following loops must then use howManyItems as the upper limit.
Like
for(int i=0; i<howManyItems; i++){
if(stringCFG[i] == '>'){
RHS= 1;
}
...
}
BTW: Since you want to be able to add 4 extra chars, you probably should do:
char stringCFG[200]; --> char stringCFG[200 + 4];
BTW: Hard-coding the value 200 over and over again is bad practice. Instead use a define like:
#define MAX_CHARS 200
and replace all the hard-coded 200 with MAX_CHARS. Then you can adjust the maximum simply by editing one line instead of multiple lines.
Programing Coderfunda
December 17, 2020
Array
No comments
The problem seems to be here:
for (int i=0; i<200; i++){
fscanf(CFG, "%c", &stringCFG[i]);
howManyItems++;
fprintf(output,"%c", stringCFG[i]);
}
This loop always executes 200 times regardless of what is in the file. In other words - the value of howManyItems will be 200 when the loop is done.
You can check that simply by printing howManyItems after the loop, i.e.
printf("After loop, howManyItems=%d\n", howManyItems);
Since you have:
char stringCFG[200];
then
stringCFG[howManyItems]= ','; // bad.. writing to stringCFG[200]
howManyItems++;
stringCFG[howManyItems]= '0'; // bad.. writing to stringCFG[201]
howManyItems++;
...
will write outside of the array. That is undefined behavior.
You need to stop the first loop once the whole file has been read. Something like:
for (int i=0; i<200; i++){
if (fscanf(CFG, "%c", &stringCFG[i]) != 1)
{
// No more data
break;
}
howManyItems++;
fprintf(output,"%c", stringCFG[i]);
}
and all the following loops must then use howManyItems as the upper limit.
Like
for(int i=0; i<howManyItems; i++){
if(stringCFG[i] == '>'){
RHS= 1;
}
...
}
BTW: Since you want to be able to add 4 extra chars, you probably should do:
char stringCFG[200]; --> char stringCFG[200 + 4];
BTW: Hard-coding the value 200 over and over again is bad practice. Instead use a define like:
#define MAX_CHARS 200
and replace all the hard-coded 200 with MAX_CHARS. Then you can adjust the maximum simply by editing one line instead of multiple lines.
Programing Coderfunda
December 17, 2020
Array
No comments
I was given this problem during a g00gle interview last year (hint hint). I didn't pass that round, but I recently coded up my solution and was wondering if this is right, or if there's a better solution? Thanks!
Question:
Input: fixed-size array of characters, containing:
Output: the same array where spaces are re-distributed between the words in such a way that they are:
Restriction: in-place algorithm. Do not use additional space for copying the characters.
Example:
My solution: (I do notice that since string is immutable in python, my solution is not in-place?)
# remove leading and trailing spaces
s = raw_input()
orig_len = len(s)
s = s.strip(" ")
new_len = len(s)
# find indices of non-space chars
indices = [i for i in range(new_len) if s[i] != ' ']
num_spaces = orig_len - len(indices)
# find indices of start and end of words
start_indices = [indices[i] for i in range(1, len(indices)) if indices[i] != (indices[i-1] + 1)]
start_indices = [0] + start_indices
end_indices = [indices[i] for i in range(len(indices)-1) if indices[i+1] != (indices[i] + 1)]
end_indices = end_indices + [indices[-1]]
intervals = zip(start_indices, end_indices)
num_words = len(intervals)
spaces_each = num_spaces / (num_words - 1)
extra = num_spaces % (num_words - 1)
adjust = 0
for i in range(num_words-1):
end_of_prev = intervals[i][1]
start_of_next = intervals[i+1][0]
gap = start_of_next - end_of_prev - 1
if extra > 0:
if gap != spaces_each + 1:
s = s[:end_of_prev+1+adjust] + (spaces_each + 1) * ' ' + s[start_of_next+adjust:]
adjust = adjust + spaces_each + 1 - gap
extra = extra - 1
Programing Coderfunda
December 17, 2020
Array
No comments
I was given this problem during a g00gle interview last year (hint hint). I didn't pass that round, but I recently coded up my solution and was wondering if this is right, or if there's a better solution? Thanks!
Question:
Input: fixed-size array of characters, containing:
Output: the same array where spaces are re-distributed between the words in such a way that they are:
Restriction: in-place algorithm. Do not use additional space for copying the characters.
Example:
My solution: (I do notice that since string is immutable in python, my solution is not in-place?)
# remove leading and trailing spaces
s = raw_input()
orig_len = len(s)
s = s.strip(" ")
new_len = len(s)
# find indices of non-space chars
indices = [i for i in range(new_len) if s[i] != ' ']
num_spaces = orig_len - len(indices)
# find indices of start and end of words
start_indices = [indices[i] for i in range(1, len(indices)) if indices[i] != (indices[i-1] + 1)]
start_indices = [0] + start_indices
end_indices = [indices[i] for i in range(len(indices)-1) if indices[i+1] != (indices[i] + 1)]
end_indices = end_indices + [indices[-1]]
intervals = zip(start_indices, end_indices)
num_words = len(intervals)
spaces_each = num_spaces / (num_words - 1)
extra = num_spaces % (num_words - 1)
adjust = 0
for i in range(num_words-1):
end_of_prev = intervals[i][1]
start_of_next = intervals[i+1][0]
gap = start_of_next - end_of_prev - 1
if extra > 0:
if gap != spaces_each + 1:
s = s[:end_of_prev+1+adjust] + (spaces_each + 1) * ' ' + s[start_of_next+adjust:]
adjust = adjust + spaces_each + 1 - gap
extra = extra - 1