Last active
June 6, 2021 09:48
-
-
Save jin-x/d77d7468ff677cce0d0f04106a259013 to your computer and use it in GitHub Desktop.
@jinxonik / UniLecs #240
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 <iostream> | |
#include <string> | |
#include <vector> | |
void string_to_int_vector(const std::string s, std::vector<int>& vec) | |
{ | |
for (size_t start = 0, end = 0; end < std::string::npos; start = end + 1) { | |
end = s.find('.', start); | |
size_t len = end - start; | |
vec.push_back(std::stoi(s.substr(start, len))); | |
}; | |
} | |
int version_compare(const std::string a, const std::string b) | |
{ | |
std::vector<int> vec_a, vec_b; | |
string_to_int_vector(a, vec_a); | |
string_to_int_vector(b, vec_b); | |
#ifdef __cpp_lib_three_way_comparison // check for C++20 feature supported by library | |
auto comp = (vec_a <=> vec_b); | |
if (comp == std::strong_ordering::greater) { return 1; } // a > b | |
if (comp == std::strong_ordering::less) { return -1; } // a < b | |
#else | |
size_t min_size = std::min(vec_a.size(), vec_b.size()); | |
for (size_t i = 0; i < min_size; ++i) { | |
if (vec_a[i] > vec_b[i]) { return 1; } // a > b | |
if (vec_a[i] < vec_b[i]) { return -1; } // a < b | |
} | |
if (min_size < vec_a.size()) { return 1; } // a > b | |
if (min_size < vec_b.size()) { return -1; } // a < b | |
#endif | |
return 0; // a == b | |
} | |
int main() | |
{ | |
std::vector<std::pair<std::string,std::string>> version_pairs = { | |
{"5.05", "5.005"}, {"5.005", "5.05"}, | |
{"7.0.1", "7"}, {"7", "7.0.1"}, | |
{"5.1.2.4", "5.1.3"}, {"5.1.3", "5.1.2.4"} | |
}; | |
for (const auto& s : version_pairs) { | |
std::cout << s.first << " <=> " << s.second << " : " << version_compare(s.first, s.second) << std::endl; | |
} | |
} |
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
def compare_version(a, b): | |
aa = list(map(int, a.split('.'))) | |
ab = list(map(int, b.split('.'))) | |
min_len = min(len(aa), len(ab)) | |
for i in range(min_len): | |
if aa[i] > ab[i]: return 1 | |
if aa[i] < ab[i]: return -1 | |
if min_len < len(aa): return 1 | |
if min_len < len(ab): return -1 | |
return 0 | |
for v in ( | |
("5.05", "5.005"), ("5.005", "5.05"), | |
("7.0.1", "7"), ("7", "7.0.1"), | |
("5.1.2.4", "5.1.3"), ("5.1.3", "5.1.2.4")): | |
print(f"{v[0]} <=> {v[1]} : {compare_version(v[0], v[1])}") |
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
def compare_version(a, b) | |
a.split('.').map{ |s| s.to_i } <=> b.split('.').map{ |s| s.to_i } | |
end | |
for v in [ | |
["5.05", "5.005"], ["5.005", "5.05"], | |
["7.0.1", "7"], ["7", "7.0.1"], | |
["5.1.2.4", "5.1.3"], ["5.1.3", "5.1.2.4"]] | |
print "#{v[0]} <=> #{v[1]} : #{compare_version(v[0], v[1])}\n" | |
end |
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 <iostream> | |
#include <string> | |
#include <vector> | |
void string_to_int_vector(const std::string s, std::vector<int>& vec) | |
{ | |
int n = 0; | |
for (const char ch : s) { | |
if (ch == '.') { | |
vec.push_back(n); | |
n = 0; | |
} else if (ch >= '0' && ch <= '9') { | |
n = n * 10 + (ch - '0'); | |
} | |
} | |
vec.push_back(n); | |
} | |
int version_compare(const std::string a, const std::string b) | |
{ | |
std::vector<int> vec_a, vec_b; | |
string_to_int_vector(a, vec_a); | |
string_to_int_vector(b, vec_b); | |
#ifdef __cpp_lib_three_way_comparison // check for C++20 feature supported by library | |
auto comp = (vec_a <=> vec_b); | |
if (comp == std::strong_ordering::greater) { return 1; } // a > b | |
if (comp == std::strong_ordering::less) { return -1; } // a < b | |
#else | |
size_t min_size = std::min(vec_a.size(), vec_b.size()); | |
for (size_t i = 0; i < min_size; ++i) { | |
if (vec_a[i] > vec_b[i]) { return 1; } // a > b | |
if (vec_a[i] < vec_b[i]) { return -1; } // a < b | |
} | |
if (min_size < vec_a.size()) { return 1; } // a > b | |
if (min_size < vec_b.size()) { return -1; } // a < b | |
#endif | |
return 0; // a == b | |
} | |
int main() | |
{ | |
std::vector<std::pair<std::string,std::string>> version_pairs = { | |
{"5.05", "5.005"}, {"5.005", "5.05"}, | |
{"7.0.1", "7"}, {"7", "7.0.1"}, | |
{"5.1.2.4", "5.1.3"}, {"5.1.3", "5.1.2.4"} | |
}; | |
for (const auto& s : version_pairs) { | |
std::cout << s.first << " <=> " << s.second << " : " << version_compare(s.first, s.second) << std::endl; | |
} | |
} |
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
def compare_version(a, b): | |
aa = list(map(int, a.split('.'))) | |
ab = list(map(int, b.split('.'))) | |
for i in count(0): | |
if len(aa) <= i and len(ab) <= i: return 0 | |
if len(aa) <= i: return -1 | |
if len(ab) <= i: return 1 | |
if aa[i] < ab[i]: return -1 | |
if ab[i] < aa[i]: return 1 | |
for v in ( | |
("5.05", "5.005"), ("5.005", "5.05"), | |
("7.0.1", "7"), ("7", "7.0.1"), | |
("5.1.2.4", "5.1.3"), ("5.1.3", "5.1.2.4")): | |
print(f"{v[0]} <=> {v[1]} : {compare_version(v[0], v[1])}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment