How to compare int with char? - c++

Ok basically, I have a file containing a 9x9 grid (sudoku puzzle). Now this sudoku puzzle is only partially completed, the empty spaces are replaced with "_" (underscores). So here is how the file looks.
5 _ _ _ _ _ 1 7 _
1 _ 6 5 _ 9 _ 4 _
4 7 2 1 _ 6 _ _ _
9 _ _ _ _ _ 5 _ _
_ 1 8 _ 9 5 4 _ _
6 _ _ 4 _ 2 3 8 9
_ 4 _ _ _ _ 9 3 _
_ 9 _ 7 _ 3 _ 5 _
2 6 3 9 5 8 7 1 4
And here is my code:
for(int row=0;row<9;row++)
{
for(int column=0;column<9;column++)
{
fin >> num;
if(num == '\95')
sudokuPuzzle[row][column] = 0;
else
sudokuPuzzle[row][column] = num;
cout << sudokuPuzzle[row][column] << " ";
}
cout << endl;
}
The problem I'm having is on this line:
if(num == '\95')
I looked in an ASCII table for the decimal value of underscore and that was what I found. However, whenever the cout statement in my loop executes, it shows that my array is filled with just the first number in the file, 5. How do I make a proper comparison between integers and characters?
FYI: essentially what I am attempting to do is replace all underscores with 0.

Use
if (num=='_')
It really is that simple.
But I think the problem is in another part of the code - when you read
fin >> num;
You will not get a satisfactory conversion from underscore to int: you need to read things into an intermediate string variable:
fin >> myStringVariable;
Then see whether the character is a number or an underscore, and act on it.
EDIT complete program:
#include <iostream>
#include <fstream>
int main(void)
{
std::ifstream fin ("sudoku.txt");
std::string line;
int pos;
int sudokuPuzzle[9][9];
for(int row=0;row<9;row++)
{
getline(fin, line);
std::cout << "as read in: " << line << std::endl;
while((pos = line.find("_", 0))!=std::string::npos)
{
line[pos] = '0';
}
// show that it worked:
std::cout << "after substitution: " << line << std::endl;
// make a copy of line to process with strtok:
char *dup = strdup(line.c_str());
// and now numerically:
sudokuPuzzle[row][0] = atoi(strtok(dup, " "));
std::cout << "after conversion: " << sudokuPuzzle[row][0] << " ";
for(int column=1;column<9;column++)
{
sudokuPuzzle[row][column] = atoi(strtok(NULL, " "));
std::cout << sudokuPuzzle[row][column] << " ";
}
std::cout << std::endl;
std::cout << std::endl;
free(dup);
}
}
Produces the following output. I think this is self-explanatory.
as read in: 5 _ _ _ _ _ 1 7 _
after substitution: 5 0 0 0 0 0 1 7 0
after conversion: 5 0 0 0 0 0 1 7 0
as read in: 1 _ 6 5 _ 9 _ 4 _
after substitution: 1 0 6 5 0 9 0 4 0
after conversion: 1 0 6 5 0 9 0 4 0
as read in: 4 7 2 1 _ 6 _ _ _
after substitution: 4 7 2 1 0 6 0 0 0
after conversion: 4 7 2 1 0 6 0 0 0
as read in: 9 _ _ _ _ _ 5 _ _
after substitution: 9 0 0 0 0 0 5 0 0
after conversion: 9 0 0 0 0 0 5 0 0
as read in: _ 1 8 _ 9 5 4 _ _
after substitution: 0 1 8 0 9 5 4 0 0
after conversion: 0 1 8 0 9 5 4 0 0
as read in: 6 _ _ 4 _ 2 3 8 9
after substitution: 6 0 0 4 0 2 3 8 9
after conversion: 6 0 0 4 0 2 3 8 9
as read in: _ 4 _ _ _ _ 9 3 _
after substitution: 0 4 0 0 0 0 9 3 0
after conversion: 0 4 0 0 0 0 9 3 0
as read in: _ 9 _ 7 _ 3 _ 5 _
after substitution: 0 9 0 7 0 3 0 5 0
after conversion: 0 9 0 7 0 3 0 5 0
as read in: 2 6 3 9 5 8 7 1 4
after substitution: 2 6 3 9 5 8 7 1 4
after conversion: 2 6 3 9 5 8 7 1 4
BONUS
5 3 9 8 2 4 1 7 6
1 8 6 5 7 9 2 4 3
4 7 2 1 3 6 8 9 5
9 2 4 3 8 7 5 6 1
3 1 8 6 9 5 4 2 7
6 5 7 4 1 2 3 8 9
7 4 5 2 6 1 9 3 8
8 9 1 7 4 3 6 5 2
2 6 3 9 5 8 7 1 4

Subtracting 48 from character will give you numeric equivalent of it.
e.g suppose if you have
char num = '0';
to convert this char '0' into numeric 0 you can use
int num1= num - 48;
You can just define variable 'num' of type 'char' and use following
sudokuPuzzle[row][column] = num - 48;
char num;
for(int row=0;row<9;row++)
{
for(int column=0;column<9;
{
fin >> num;
if(num == '_')
sudokuPuzzle[row][column] = 0;
else
sudokuPuzzle[row][column] = num - 48;
cout << sudokuPuzzle[row][column] << " ";
}
cout << endl;
}

In \nnn, the nnn is in octal. 9 is not an octal digit. I'm pretty sure -Wall would have told you that, but I could be wrong about that.
However, if num is an int, then:
fin >> num;
is unlikely to do what you want, since _ is not an integer. I would think that the streaming input operator would set the error bit on fin in that case.

You say the file looks like this:
5 _ _ _ _ _ 1 7 _
1 _ 6 5 _ 9 _ 4 _
4 7 2 1 _ 6 _ _ _
9 _ _ _ _ _ 5 _ _
_ 1 8 _ 9 5 4 _ _
6 _ _ 4 _ 2 3 8 9
_ 4 _ _ _ _ 9 3 _
_ 9 _ 7 _ 3 _ 5 _
2 6 3 9 5 8 7 1 4
That means that the first line of the file could be expressed as follows:
char line[] = { '5', '_', '_', '_', '_', '_', '1', '7', '_' };
Although this is a char array, it's not a string because I didn't add the trailing nul.
What does this array contain?
The first char, line[0] represents the ASCII character '5'. That's not the same as the integer value, 5, it is the ASCII code for '5'. Looking at an ascii table we can see that '5' is ASCII character 53.
So, line[0], if printed as an ASCII character, will display '5', but if printed as an integer value will display '53'.
#include <iostream>
int main() {
char fourtyTwoPlus11 = 53;
std::cout << fourtyTwoPlus11 << "\n";
std::cout << static_cast<int>(fourtyTwoPlus11) << "\n";
return 0;
}
Live demo: http://ideone.com/JEUcLO
Your file appears to already contain ASCII characters rather than integer values. So for each character, you actually need to convert to numeric values. You could do something like the following:
enum { NumColumns = 9, UnknownValue = -1 };
char line[NumColumns] = { '5', '_', '_', '_', '_', '_', '1', '7', '_' };
int values[NumColumns];
for (size_t i = 0; i < NumColumns; ++i) {
char col = line[i];
if (col == '_')
values[i] = UnknownValue;
else
values[i] = col - '0';
}
The integer values are in the range 0-9, which we obtain using our knowledge that the ASCII table stores digits in the sequence 0, 1, 2 ... 9. So '0' - '0' == 0, and '9' - '0' = 9.
I chose to use -1 to represent the Unknown Value but you could just as easily use 0 here.

For this kind of situation (get an input of either a char or an int), I usually use this trick:
char c;
int n;
cin >> c;
if(c != '_'){
cin.putback(c);
cin >> n;
}
fin is similar.

Related

Split json like string with C++20 ranges/views

I have the following string:
std::string ob = R"({ "U" : 972448093270,"u" : 972448098626,"pu" : 972448093117,"b" : [["54019.38","0.414"],["54016.91","0.002"],["54016.80","0.180"],["54016.51","0.001"],["54012.74","0.001"],["54012.38","0.001"],["54011.65","0.044"],["54011.57","0.005"],["54011.40","0.001"],["54011.27","0.200"],["54011.26","0.200"],["54011.25","0.784"],["54011.12","0.200"],["54011.11","0.312"],["54011.04","1.920"],["54010.67","0.123"],["54010.39","0.025"],["54008.59","0.074"],["54008.58","0.167"],["54008.06","0.010"]]})";
Starting from "b" I want all the numbers split and joined in a vector, so effectively I want my vector to look like this:
54019.38 0.414 54016.91 etc
I have implemented the followint with C++20 ranges/views
#include <iostream>
#include <vector>
#include <ranges>
auto to_vector(auto&& rng)
{
std::vector<std::ranges::range_value_t<decltype(rng)>> v;
if constexpr (std::ranges::sized_range<decltype(rng)>) {
v.reserve(std::ranges::size(rng));
}
std::ranges::copy(rng, std::back_inserter(v));
return v;
}
int main()
{
std::string ob = R"({ "U" : 972448093270,"u" : 972448098626,"pu" : 972448093117,"b" : [["54019.38","0.414"],["54016.91","0.002"],["54016.80","0.180"],["54016.51","0.001"],["54012.74","0.001"],["54012.38","0.001"],["54011.65","0.044"],["54011.57","0.005"],["54011.40","0.001"],["54011.27","0.200"],["54011.26","0.200"],["54011.25","0.784"],["54011.12","0.200"],["54011.11","0.312"],["54011.04","1.920"],["54010.67","0.123"],["54010.39","0.025"],["54008.59","0.074"],["54008.58","0.167"],["54008.06","0.010"]]})";
auto digits = ob | std::views::drop_while([](auto i) {return i != 'b'; }) | std::views::split('"') | std::views::join;
auto vec = to_vector(digits);
for (auto const& item : vec) {
std::cout << item <<" ";
}
}
Unfortunately instead of expected output I got
`b : [ [ 5 4 0 1 9 . 3 8 , 0 . 4 1 4 ] , [ 5 4 0 1 6 . 9 1 , 0 . 0 0 2 ] , [ 5 4 0 1 6 . 8 0 , 0 . 1 8 0 ] , [ 5 4 0 1 6 . 5 1 , 0 . 0 0 1 ] , [ 5 4 0 1 2 . 7 4 , 0 . 0 0 1 ] , [ 5 4 0 1 2 . 3 8 , 0 . 0 0 1 ] , [ 5 4 0 1 1 . 6 5 , 0 . 0 4 4 ] ...etc
So I have each character in a vector individually instead of splitting whatever was between "'s
Can someone please help me fix this, thanks in advance.

Pandas replacing values depending on prior row

I'm pretty new to pandas and would like your input on how to tackle my problem. I've got the following data frame:
df = pd.DataFrame({'A' : ["me","you","you","me","me","me","me"],
'B' : ["Y","X","X","X","X","X","Z"],
'C' : ["1","2","3","4","5","6","7"]
})
I need to transform it based on the row values in column A and B. The logic should be that as soon as values in column A and B are the same on consecutive rows, the first row in this sequence should be maintained but following rows should have an 'A' set in column B.
For example: Values in column A and B are the same in row 1 and 2. Value in column B row 2 should be replaced with A. This is my expected output:
df2= pd.DataFrame({'A' : ["me","you","you","me","me","me","me"],
'B' : ["Y","X","A","X","A","A","Z"],
'C' : ["1","2","3","4","5","6","7"]})
You can first sum columns A and B:
a = df.A + df.B
Then compare with shifted version:
print (a != a.shift())
0 True
1 True
2 False
3 True
4 False
5 False
6 True
dtype: bool
Create unique groups by cumsum:
print ((a != a.shift()).cumsum())
0 1
1 2
2 2
3 3
4 3
5 3
6 4
dtype: int32
Get boolean mask where values are duplicated:
print ((a != a.shift()).cumsum().duplicated())
0 False
1 False
2 True
3 False
4 True
5 True
6 False
dtype: bool
Solutions for replace True values to A:
df.loc[(a != a.shift()).cumsum().duplicated(), 'B'] = 'A'
print (df)
A B C
0 me Y 1
1 you X 2
2 you A 3
3 me X 4
4 me A 5
5 me A 6
6 me Z 7
df.B = df.B.mask((a != a.shift()).cumsum().duplicated(), 'A')
print (df)
A B C
0 me Y 1
1 you X 2
2 you A 3
3 me X 4
4 me A 5
5 me A 6
6 me Z 7
print (df2.equals(df))
True

Identify group with two variables

Suppose I have the following data in Stata:
clear
input id tna ret str2 name
1 2 3 "X"
1 3 2 "X"
1 5 3 "X"
1 6 -1 "X"
2 4 2 "X"
2 6 -1 "X"
2 8 -2 "X"
2 9 3 "P"
2 11 -2 "P"
3 3 1 "Y"
3 4 0 "Y"
3 6 -1 "Y"
3 8 1 "Z"
3 6 1 "Z"
end
I want to make an ID for new groups. These new groups should incorporate the observations with the same name (for example X), but should also incorporate all the observations of the same ID if the name started in that ID. For example:
X is in the data set under two IDs: 1 and 2. The group of X should incorporate all the observations with the name X, but also the two observations of the name P (since X started in ID 2 and the two observations with value P belong to group X)
Y started in ID 3, so the group should incorporate every observation with ID 3.
This is a tricky problem to solve because it may take several pass to completely stabilize identifiers. Fortunately, you can use group_id (from SSC) to solve this. To install group_id, type in Stata's Command window:
ssc install group_id
Here's a more complicated data example where "P" also appears in ID == 4 and that ID also contains "A" as a name:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(id tna ret) str2 name
1 2 3 "X"
1 3 2 "X"
1 5 3 "X"
1 6 -1 "X"
2 4 2 "X"
2 6 -1 "X"
2 8 -2 "X"
2 9 3 "P"
2 11 -2 "P"
3 3 1 "Y"
3 4 0 "Y"
3 6 -1 "Y"
3 8 1 "Z"
3 6 1 "Z"
4 9 3 "P"
4 11 -2 "P"
4 12 0 "A"
end
clonevar newid = id
group_id newid, match(name)
I am not sure that I understand the definitions here (e.g. tna and ret are not explained; conversely, omit them from a question if they are irrelevant; does "start" imply a process in time?), but why not copy first values of name within each id, and then classify on first names? (With your example data, results are the same.)
clear
input id tna ret str2 name
1 2 3 "X"
1 3 2 "X"
1 5 3 "X"
1 6 -1 "X"
2 4 2 "X"
2 6 -1 "X"
2 8 -2 "X"
2 9 3 "P"
2 11 -2 "P"
3 3 1 "Y"
3 4 0 "Y"
3 6 -1 "Y"
3 8 1 "Z"
3 6 1 "Z"
end
sort id, stable
by id: gen first = name[1]
egen group = group(first), label
list, sepby(group)
+---------------------------------------+
| id tna ret name first group |
|---------------------------------------|
1. | 1 2 3 X X X |
2. | 1 3 2 X X X |
3. | 1 5 3 X X X |
4. | 1 6 -1 X X X |
5. | 2 4 2 X X X |
6. | 2 6 -1 X X X |
7. | 2 8 -2 X X X |
8. | 2 9 3 P X X |
9. | 2 11 -2 P X X |
|---------------------------------------|
10. | 3 3 1 Y Y Y |
11. | 3 4 0 Y Y Y |
12. | 3 6 -1 Y Y Y |
13. | 3 8 1 Z Y Y |
14. | 3 6 1 Z Y Y |
+---------------------------------------+

using get() and appending those chars to a string is giving me nonsense

I am supposed to use a program that (one char at a time) reads a GPS log file, and parses it. I'm getting normal responses when I use some .log files, and not with others (especially not long ones).
The code that grabs the information and stores it is below:
class ReadInput{
public:
string massiveString;
};
void ReadInput::assign(string inMassiveString){ //assign the string passed to it to the object
this->massiveString.empty(); //clear whatever might be in the string
this->massiveString=inMassiveString; //copy the string into where it belongs.
}
int main(int argc, const char * argv[]) {
ifstream inputStream;
inputStream.open("gps3.log");
int i = 1;
char temp;
string tempString;
ReadInput *sentence[258];
//this is the part that grabs the characters
while(1){ //
if (!inputStream.eof()){ //
inputStream.get(temp);
if (temp!=' ') {
tempString.append(&temp); //this appends the characters to a temporary string
// cout << i << "\t" << tempString << endl; //testing the input
}
if (temp == '\n') { //if this is the end of the line, pass it on.
sentence[i]=new ReadInput;
sentence[i]->assign(tempString); //the part that copies the temporary string to the object
cout << i << "\t" << sentence[i]->massiveString << endl; //another testing line
i++;
tempString.clear();
}
}
else{
break;
}
}
}
it is very important that I read it character by character.
what it should be reading looks like this:
$GPGGA,155008.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*53
and here is an example of a problematic reading. in the format of
entry#, stringStoredInEntry.
1 $GPGGA,155002.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*58
2 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E
3 $GPRMC,155002.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*48
4 $GPGGA,155003.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*59
5 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E
6 $GPGSV,3,1,12,17,64,038,42,06,63,250,43,28,44,150,45,02,25,239,38*7B
7 $GPGSV,3,2,12,24,17,294,22,10,21,176,22,12,25,316,23,51,36,223,37*71
8 F
9 $ G P R M C , 1 5 5 0 0 3 . 0 0 0 , A , 3 79
10 $
G
P
G
G
A
,
1
5
5
0
0
4
.
0
0
0
,
3
7
3
2
.
7
2
3
9
,
N
,
0
7
7
2
6
.
9
9
5
6
,
W
,
1
,
0
5
,
1
.
7
,
8
9
.
1
,
M
,
-
3
3
.
6
,
M
,
,
0
0
0
0
*
5
E
11 $
G
P
G
S
A
,
A
,
3
,
1
7
,
0
6
,
2
8
,
0
2
,
2
4
,
,
,
,
,
,
,
,
3
.
2
,
1
.
7
,
2
.
7
*
3
E
12 $
G
P
R
M
C
,
1
5
5
0
0
4
.
0
0
0
,
A
,
3
7
3
2
.
7
2
3
9
,
N
,
0
7
7
2
6
.
9
8 $GPGGA,155009.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*52
29 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E
30 $GPRMC,155009.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*43
31 $GPGGA,155010.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*5A
2 $ G P G S A , A , 3 , 1 7 , 0 6 , 2 8 , 0 2 , 2 4 , , , , , , , , 3 . 2 , 1 . 7 , 2 . 7 * 3 E
!33 $!G!P!R!M!C!,!1!5!5!0!1!0!.!0!0!0!,!A!,!3!7!3!2!.!7!2!3!9!,!N!,!0!7!7!2!6!.!9!9!5!6!,!W!,!0!.!0!0!,!8!0!.!3!0!,!1!4!1!0!1!4!,!,!,!A!*!4!B!
"34 $"G"P"G"G"A","1"5"5"0"1"1"."0"0"0","3"7"3"2"."7"2"3"9","N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2*,*2*5*,*2*3*9*,*3*8***7*F*
+43 $+G+P+G+S+V+,+3+,+2+,+1+2+,+2+4+,+1+7+,+2+9+4+,+2+3+,+1+0+,+2+1+,+1+7+6+,+2+2+,+1+2+,+2+5+,+3+1+6+,+2+4+,+5+1+,+3+6+,+2+2+3+,+3+5+*+7+5+
,44 $,G,P,G,S,V,,,3,,,3,,,1,2,,,2,0,,,3,4,,,0,6,2,,,,,0,1,,,0,7,,,0,5,1,,,,,0,4,,,0,6,,,0,5,2,,,,,3,2,,,0,2,,,0,3,3,,,*,7,F,
-45 $-G-P-R-M-C-,-1-5-5-0-1-3-.-0-0-0-,-A-,-3-7-3-2-.-7-2-3-9-,-N-,-0-7-7-2-6-.-9-9-5-6-,-W-,-0-.-0-0-,-8-0-.-3-0-,-1-4-1-0-1-4-,-,-,-A-*-4-8-
.46 $.G.P.G.G.A.,.1.5.5.0.1.4...0.0.0.,.3.7.3.2...7.2.3.9.,.N.,.0.7.7.2.6...9.9.5.6.,.W.,.1.,.0.5.,.1...7.,.8.9...0.,.M.,.-.3.3...6.,.M.,.,.0.0.0.0.*.5.E.
/47 $/G/P/G/S/A/,/A/,/3/,/1/7/,/0/6/,/2/8/,/0/2/,/2/4/,/,/,/,/,/,/,/,/3/./2/,/1/./7/,/2/./7/*/3/E/
048 $0G0P0R0M0C0,0105050001040.0000000,0A0,030703020.070203090,0N0,00070702060.090905060,0W0,000.00000,08000.03000,0104010001040,0,0,0A0*040F0
149 $1G1P1G1G1A1,1115151011151.1010101,131713121.171213191,1N1,10171712161.191915161,1W1,111,10151,111.171,18191.101,1M1,1-13131.161,1M1,1,101010101*151F1
250 $2G2P2G2S2A2,2A2,232,21272,20262,22282,20222,22242,2,2,2,2,2,2,2,232.222,212.272,222.272*232E2
351 $3G3P3R3M3C3,3135353031353.3030303,3A3,333733323.373233393,3N3,30373732363.393935363,3W3,303.30303,38303.33303,3134313031343,3,3,3A3*343E3
452 $4G4P4G4G4A4,4145454041464.4040404,434743424.474243494,4N4,40474742464.494945464,4W4,414,40454,414.474,48484.494,4M4,4-43434.464,4M4,4,404040404*45444
553 $5G5P5G5S5A5,5A5,535,51575,50565,52585,50525,52545,5,5,5,5,5,5,5,535.525,515.575,525.575*535E5
654 $6G6P6R6M6C6,6165656061666.6060606,6A6,636763626.676263696,6N6,60676762666.696965666,6W6,606.60606,68606.63606,6164616061646,6,6,6A6*646D6
755 $7G7P7G7G7A7,7175757071777.7070707,737773727.777273797,7N7,70777772767.797975767,7W7,717,70757,717.777,78787.797,7M7,7-73737.767,7M7,7,707070707*75757
856 $8G8P8G8S8A8,8A8,838,81878,80868,82888,80828,82848,8,8,8,8,8,8,8,838.828,818.878,828.878*838E8
957 $9G9P9R9M9C9,9195959091979.9090909,9A9,939793929.979293999,9N9,90979792969.999995969,9W9,909.90909,98909.93909,9194919091949,9,9,9A9*949C9
:58 $:G:P:G:G:A:,:1:5:5:0:1:8:.:0:0:0:,:3:7:3:2:.:7:2:3:9:,:N:,:0:7:7:2:6:.:9:9:5:6:,:W:,:1:,:0:5:,:1:.:7:,:8:8:.:9:,:M:,:-:3:3:.:6:,:M:,:,:0:0:0:0:*:5:A:
;59 $;G;P;G;S;A;,;A;,;3;,;1;7;,;0;6;,;2;8;,;0;2;,;2;4;,;,;,;,;,;,;,;,;3;.;2;,;1;.;7;,;2;.;7;*;3;E;
<60 $<G<P<G<S<V<,<3<,<1<,<1<2<,<1<7<,<6<4<,<0<3<9<,<4<2<,<0<6<,<6<4<,<2<5<0<,<4<3<,<2<8<,<4<3<,<1<5<1<,<4<2<,<0<2<,<2<5<,<2<3<9<,<3<8<*<7<C<
=61 $=G=P=G=S=V=,=3=,=2=,=1=2=,=2=4=,=1=7=,=2=9=4=,=1=3=,=1=0=,=2=1=,=1=7=6=,=2=2=,=1=2=,=2=5=,=3=1=6=,=2=6=,=5=1=,=3=6=,=2=2=3=,=3=6=*=7=7=
>62 $>G>P>G>S>V>,>3>,>3>,>1>2>,>2>0>,>3>4>,>0>6>1>,>,>0>1>,>0>7>,>0>5>1>,>,>0>4>,>0>5>,>0>5>2>,>,>3>2>,>0>2>,>0>3>2>,>*>7>E>
?63 $?G?P?R?M?C?,?1?5?5?0?1?8?.?0?0?0?,?A?,?3?7?3?2?.?7?2?3?9?,?N?,?0?7?7?2?6?.?9?9?5?6?,?W?,?0?.?0?0?,?8?0?.?3?0?,?1?4?1?0?1?4?,?,?,?A?*?4?3?
#64 $#G#P#G#G#A#,#1#5#5#0#1#9#.#0#0#0#,#3#7#3#2#.#7#2#3#9#,#N#,#0#7#7#2#6#.#9#9#5#6#,#W#,#1#,#0#4#,#2#.#3#,#8#8#.#9#,#M#,#-#3#3#.#6#,#M#,#,#0#0#0#0#*#5#D#
A65 $AGAPAGASAAA,AAA,A3A,A1A7A,A0A6A,A2A8A,A0A2A,A,A,A,A,A,A,A,A,A4A.A8A,A2A.A3A,A4A.A3A*A3A0A
B66 $BGBPBRBMBCB,B1B5B5B0B1B9B.B0B0B0B,BAB,B3B7B3B2B.B7B2B3B9B,BNB,B0B7B7B2B6B.B9B9B5B6B,BWB,B0B.B0B0B,B8B0B.B3B0B,B1B4B1B0B1B4B,B,B,BAB*B4B2B
B67 $GPGGA,155007.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*5C
C1 $GPGGA,155002.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*58
21 $GPRMC,155007.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*4D
22 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E,1.7,89.0,M,-33.6,M,,0000*53
23 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E
34 $GPRMC,155002.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*48
25 $GPGSV,3,2,12,24,17,294,22,10,21,176,22,12,25,316,24,51,36,223,35*74
46 $GPGGA,155003.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*59
27 $
5 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E
6 $GPGSV,3,1,12,17,64,038,42,06,63,250,43,28,44,150,45,02,25,239,38*7B
7 $GPGSV,3,2,12,24,17,294,22,10,21,176,22,12,25,316,23,51,36,223,37*71
8 F
9 $ G P R M C , 1 5 5 0 0 3 . 0 0 0 , A , 3 79
10 $
G
P
G
G
A
,
1
5
5
0
0
4
.
0
0
0
,
3
7
3
2
.
7
2
3
9
,
N
,
0
7
7
2
6
.
9
9
5
6
,
W
,
1
,
0
5
,
1
.
7
,
8
9
.
1
,
M
,
-
3
3
.
6
,
M
,
,
0
0
0
0
*
5
E
11 $
G
P
G
S
A
,
A
,
3
,
1
7
,
0
6
,
2
8
,
0
2
,
2
4
,
,
,
,
,
,
,
,
3
.
2
,
1
.
7
,
2
.
7
*
3
E
12 $
G
P
R
M
C
,
1
5
5
0
0
4
.
0
0
0
,
A
,
3
7
3
2
.
7
2
3
9
,
N
,
0
7
7
2
6
.
9
9
5
6
,
W
,
0
.
0
0
,
8
0
.
3
0
8 $GPGGA,155009.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*52
1
29 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E 4
1
30 $GPRMC,155009.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*43
1
31 $GPGGA,155010.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*5A
,
2 $ G P G S A , A , 3 , 1 7 , 0 6 , 2 8 , 0 2 , 2 4 , , , , , , , , 3 . 2 , 1 . 7 , 2 . 7 * 3 E
,
!3 $!G!P!R!M!C!,!1!5!5!0!1!0!.!0!0!0!,!A!,!3!7!3!2!.!7!2!3!9!,!N!,!0!7!7!2!6!.!9!9!5!6!,!W!,!0!.!0!0!,!8!0!.!3!0!,!1!4!1!0!1!4!,!,!,!A!*!4!B!
! *
"4 $"G"P"G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*1*,*0*6*,*6*3*,*2*5*0*,*4*2*,*2*8*,*4*4*,*1*5*0*,*4*3*,*0*2*,*2*5*,*2*3*9*,*3*8***7*F*
*
+3 $+G+P+G+S+V+,+3+,+2+,+1+2+,+2+4+,+1+7+,+2+9+4+,+2+3+,+1+0+,+2+1+,+1+7+6+,+2+2+,+1+2+,+2+5+,+3+1+6+,+2+4+,+5+1+,+3+6+,+2+2+3+,+3+5+*+7+5+
+
,4 $,G,P,G,S,V,,,3,,,3,,,1,2,,,2,0,,,3,4,,,0,6,2,,,,,0,1,,,0,7,,,0,5,1,,,,,0,4,,,0,6,,,0,5,2,,,,,3,2,,,0,2,,,0,3,3,,,*,7,F,
,
-5 $-G-P-R-M-C-,-1-5-5-0-1-3-.-0-0-0-,-A-,-3-7-3-2-.-7-2-3-9-,-N-,-0-7-7-2-6-.-9-9-5-6-,-W-,-0-.-0-0-,-8-0-.-3-0-,-1-4-1-0-1-4-,-,-,-A-*-4-8-
-
.6 $.G.P.G.G.A.,.1.5.5.0.1.4...0.0.0.,.3.7.3.2...7.2.3.9.,.N.,.0.7.7.2.6...9.9.5.6.,.W.,.1.,.0.5.,.1...7.,.8.9...0.,.M.,.-.3.3...6.,.M.,.,.0.0.0.0.*.5.E.
.
/7 $/G/P/G/S/A/,/A/,/3/,/1/7/,/0/6/,/2/8/,/0/2/,/2/4/,/,/,/,/,/,/,/,/3/./2/,/1/./7/,/2/./7/*/3/E/
/
08 $0G0P0R0M0C0,0105050001040.0000000,0A0,030703020.070203090,0N0,00070702060.090905060,0W0,000.00000,08000.03000,0104010001040,0,0,0A0*040F0
0
19 $1G1P1G1G1A1,1115151011151.1010101,131713121.171213191,1N1,10171712161.191915161,1W1,111,10151,111.171,18191.101,1M1,1-13131.161,1M1,1,101010101*151F1
1
20 $2G2P2G2S2A2,2A2,232,21272,20262,22282,20222,22242,2,2,2,2,2,2,2,232.222,212.272,222.272*232E2
2
31 $3G3P3R3M3C3,3135353031353.3030303,3A3,333733323.373233393,3N3,30373732363.393935363,3W3,303.30303,38303.33303,3134313031343,3,3,3A3*343E3
3
42 $4G4P4G4G4A4,4145454041464.4040404,434743424.474243494,4N4,40474742464.494945464,4W4,414,40454,414.474,48484.494,4M4,4-43434.464,4M4,4,404040404*45444
4
53 $5G5P5G5S5A5,5A5,535,51575,50565,52585,50525,52545,5,5,5,5,5,5,5,535.525,515.575,525.575*535E5
5
64 $6G6P6R6M6C6,6165656061666.6060606,6A6,636763626.676263696,6N6,60676762666.696965666,6W6,606.60606,68606.63606,6164616061646,6,6,6A6*646D6
6
75 $7G7P7G7G7A7,7175757071777.7070707,737773727.777273797,7N7,70777772767.797975767,7W7,717,70757,717.777,78787.797,7M7,7-73737.767,7M7,7,707070707*75757
7
86 $8G8P8G8S8A8,8A8,838,81878,80868,82888,80828,82848,8,8,8,8,8,8,8,838.828,818.878,828.878*838E8
8
97 $9G9P9R9M9C9,9195959091979.9090909,9A9,939793929.979293999,9N9,90979792969.999995969,9W9,909.90909,98909.93909,9194919091949,9,9,9A9*949C9
9
:8 $:G:P:G:G:A:,:1:5:5:0:1:8:.:0:0:0:,:3:7:3:2:.:7:2:3:9:,:N:,:0:7:7:2:6:.:9:9:5:6:,:W:,:1:,:0:5:,:1:.:7:,:8:8:.:9:,:M:,:-:3:3:.:6:,:M:,:,:0:0:0:0:*:5:A:
:
;9 $;G;P;G;S;A;,;A;,;3;,;1;7;,;0;6;,;2;8;,;0;2;,;2;4;,;,;,;,;,;,;,;,;3;.;2;,;1;.;7;,;2;.;7;*;3;E;
;
<0 $<G<P<G<S<V<,<3<,<1<,<1<2<,<1<7<,<6<4<,<0<3<9<,<4<2<,<0<6<,<6<4<,<2<5<0<,<4<3<,<2<8<,<4<3<,<1<5<1<,<4<2<,<0<2<,<2<5<,<2<3<9<,<3<8<*<7<C<
<
=1 $=G=P=G=S=V=,=3=,=2=,=1=2=,=2=4=,=1=7=,=2=9=4=,=1=3=,=1=0=,=2=1=,=1=7=6=,=2=2=,=1=2=,=2=5=,=3=1=6=,=2=6=,=5=1=,=3=6=,=2=2=3=,=3=6=*=7=7=
=
>2 $>G>P>G>S>V>,>3>,>3>,>1>2>,>2>0>,>3>4>,>0>6>1>,>,>0>1>,>0>7>,>0>5>1>,>,>0>4>,>0>5>,>0>5>2>,>,>3>2>,>0>2>,>0>3>2>,>*>7>E>
>
?3 $?G?P?R?M?C?,?1?5?5?0?1?8?.?0?0?0?,?A?,?3?7?3?2?.?7?2?3?9?,?N?,?0?7?7?2?6?.?9?9?5?6?,?W?,?0?.?0?0?,?8?0?.?3?0?,?1?4?1?0?1?4?,?,?,?A?*?4?3?
?
#4 $#G#P#G#G#A#,#1#5#5#0#1#9#.#0#0#0#,#3#7#3#2#.#7#2#3#9#,#N#,#0#7#7#2#6#.#9#9#5#6#,#W#,#1#,#0#4#,#2#.#3#,#8#8#.#9#,#M#,#-#3#3#.#6#,#M#,#,#0#0#0#0#*#5#D#
#
A5 $AGAPAGASAAA,AAA,A3A,A1A7A,A0A6A,A2A8A,A0A2A,A,A,A,A,A,A,A,A,A4A.A8A,A2A.A3A,A4A.A3A*A3A0A
A
B6 $BGBPBRBMBCB,B1B5B5B0B1B9B.B0B0B0B,BAB,B3B7B3B2B.B7B2B3B9B,BNB,B0B7B7B2B6B.B9B9B5B6B,BWB,B0B.B0B0B,B8B0B.B3B0B,B1B4B1B0B1B4B,B,B,BAB*B4B2B
B
1-7 are perfect. great.
then 8 gets weird.
and it never get's normal.
Where is this coming from? is this problems with the .log file? is this my code? where is this nonsense creeping in? and, more importantly, how is it messing with the integers to the left? How can anything consistently make an integer that nonsensical?
using tempString.push_back(temp);
instead of tempString.append(&temp);
fixed everything.

List of lists in Scala? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a grid (that is declared as a List (of Lists)) and each 3*3 block for example is indexed as 0, the next 3*3 block is indexed as 1, etc.
I need to write a method that returns the elements (integers) in a particular block given an index.
For example : b(0) should give 1 0 0 4 1 2 7 0 0
1 0 0 | 4 0 2 | 5 0 7 |
4 1 2 | 0 0 3 | 6 8 0 |
7 0 0 | 0 0 9 | 0 0 1 |
----------------------
1 0 0 | 4 0 2 | 5 0 7 |
9 0 2 | 0 4 3 | 1 8 0 |
7 1 0 | 0 8 9 | 8 0 0 |
----------------------
1 0 0 | 4 0 2 | 5 0 7 |
4 0 2 | 0 1 9 | 6 2 0 |
7 0 0 | 0 0 9 | 0 0 1 |
----------------------
implicit class MatrixOps[T](val l: List[List[T]]) extends AnyVal {
def getBlock(index: Int, size: Int = 3): List[T] = {
l map (_ drop index * size) take size flatMap (_ take size)
}
}
override def main(args: Array[String]): Unit = {
val x = List(
List(1, 2, 3),
List(4, 5, 6)
)
println(x.getBlock(0))
println(x.getBlock(1))
}
Try with following function,
def block(i:Int, grid:List[List[Int]]) = grid.
grouped(3).
toList(i / 3).
map(_.grouped(3).toList(i % 3))
Here's that example with nice print method
def print(q:List[List[Int]]) = q.map(_.mkString("\t")).mkString("\n")
val grid = (1 to 81).toList.grouped(9).toList
scala> print(block(1))
res37: String =
4 5 6
13 14 15
22 23 24
(Sorry, Im too lazy to write a sudoku board as an example :) )
To get the list of integers, try flattern.
scala> block(1).flattern
res38: List[Int] = List(4, 5, 6, 13, 14, 15, 22, 23, 34)