Last active
July 11, 2020 13:57
-
-
Save oneEyedSunday/d5e3c7b050162c41f3f8552c969e83e9 to your computer and use it in GitHub Desktop.
C++ practice
This file contains hidden or 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 <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
std::tuple<string, string> get_chars(const string&); | |
int main() { | |
/* Enter your code here. Read input from STDIN. Print output to STDOUT */ | |
short num_lines = 0, line = 0; | |
cin >> num_lines; | |
string lines[num_lines]; | |
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); | |
while (line < num_lines) { | |
getline(cin, lines[line++]); | |
} | |
for (string& _line : lines) | |
{ | |
std::tuple<string, string> parsed = get_chars(_line); | |
cout << std::get<0>(parsed)<< " " << std::get<1>(parsed) << std::endl; | |
} | |
return 0; | |
} | |
std::tuple<string, string> get_chars(const string& item) | |
{ | |
string odd = ""; | |
string even = ""; | |
for (short index = 0; index < item.length(); index++) | |
{ | |
[&item, &odd, &even, &index](){ | |
if (index % 2 == 0) odd += item[index]; | |
else even += item[index]; | |
}(); | |
} | |
return std::tuple<string, string>(odd, even); | |
}; |
This file contains hidden or 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 <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
int main() { | |
/* Enter your code here. Read input from STDIN. Print output to STDOUT */ | |
// get how many arrays, and queries | |
int num_arrays, num_queries; | |
scanf("%d %d", &num_arrays, &num_queries); | |
vector<vector<int>> all_arrays(num_arrays, vector<int>()); | |
int var_arrays_index = 0; | |
vector<int> answers_to_queries (num_queries); | |
while (var_arrays_index < num_arrays) | |
{ | |
int num_in_child; | |
scanf("%d", &num_in_child); | |
unsigned int * config = NULL; | |
// should use an array instead | |
config = static_cast<unsigned int*>(malloc((size_t)num_in_child * sizeof(int))); | |
unsigned int input_index = 0; | |
while(input_index < num_in_child && scanf(" %u", (config + input_index))) | |
{ | |
input_index++; | |
} | |
// easier to use an array to init a vector | |
for (unsigned int _index = 0; _index < num_in_child; _index++) | |
{ | |
all_arrays[var_arrays_index].push_back(*(config + _index)); | |
} | |
var_arrays_index++; | |
} | |
int query_index = 0; | |
while (query_index < num_queries) | |
{ | |
int _array_to_inspect, _array_pos; | |
scanf("%d %d", &_array_to_inspect, &_array_pos); | |
answers_to_queries[query_index] = (all_arrays[_array_to_inspect][_array_pos]); | |
query_index++; | |
} | |
for (unsigned int answer: answers_to_queries) | |
cout << answer << std::endl; | |
return 0; | |
} |
This file contains hidden or 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 <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
string * get_chars(const string&, string *); | |
int main() { | |
/* Enter your code here. Read input from STDIN. Print output to STDOUT */ | |
short num_lines = 0, line = 0; | |
cin >> num_lines; | |
string lines[num_lines]; | |
string results[num_lines]; | |
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); | |
while (line < num_lines) { | |
string _input = ""; | |
getline(cin, _input); | |
// function supposed to be called here get_chars | |
string odd = ""; | |
string even = ""; | |
for (unsigned short index = 0; index < _input.length(); index++) | |
{ | |
if (index % 2 == 0) odd += _input[index]; | |
else even += _input[index]; | |
} | |
results[line] = odd + " " + even; | |
line++; | |
} | |
for (short int index = 0; index < num_lines; index++) | |
{ | |
cout << results[index] << std::endl; | |
} | |
return 0; | |
} | |
string * get_chars(const string& item, string * result) | |
{ | |
string odd = ""; | |
string even = ""; | |
for (unsigned short index = 0; index < item.length(); index++) | |
{ | |
if (index % 2 == 0) odd += item[index]; | |
else even += item[index]; | |
} | |
// cant do this, why? | |
// im taking pointer to the first string in the string array | |
// not pointer to the string array | |
// then again, they should be the same thing right? | |
// result = static_cast<string *>(malloc(sizeof(odd))); | |
result[0] = odd; | |
result[1] = even; | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tuples aren't stored in heap unlike dynamic stuff like arrays. I can return a tuple.
Makes for simpler code.
My mem management isn't so strong, so I had bugs when i tried this with a function populating a dynamic array.
Had to skip functions for < c++11