Originally posted by TheBlackCat
View Post
Announcement
Collapse
No announcement yet.
HOPE: The Ease Of Python With The Speed Of C++
Collapse
X
-
Originally posted by pal666 View Postbut you need to iterate over string only once. while python-damaged people write code, which iterates over string in for loop and then for each iteration iterates again in s.count()
From what I can tell, your approach would be something like this:
Code:def getmin(s): count = {} for ch in s: if ch in count: count[ch] += 1 else: count[ch] = 1 return len(s) - max(count.values())
Code:def getmin(s): letters = [] count = [] for ch in s: try: ind = letters.index(count) except: letters.append(ch) count.append(1) else: letters[ind] += 1 return len(s) - max(count)
Last edited by TheBlackCat; 20 October 2014, 10:12 AM.
Comment
-
Originally posted by pal666 View Postmy "like that" related to syntax. i hope you aren't going to insist on using variable before introducing it.
http://www.boost.org/doc/libs/1_56_0...roduction.html
Second, I don't think that I immediately understood how that was supposed to work.
Can you please, for clarity, just take the Python example and give that as a C++ + boost version?
Note, I am not being snarky, I am genuinely curious since everything that makes C++ easier to work with is a win in my book.
Comment
-
Originally posted by TheBlackCat View PostYou would either need to iterate over the string twice, or iterate over the string and a sequence of characters that have been found so far. You would need two iterators either way.
Originally posted by TheBlackCat View PostBut the issue is that you still have to iterate over "count" or "letters" to determine if the characters is already present.
if you are using 8 bit strings, you can use fixed size array of 256 counts
if you are using wide strings, you are probably better off with associative array which uses either tree or hash, i.e. O(log N) access or even better
Comment
-
Originally posted by kigurai View PostFirst of all that is boost, and not standard C++.
Second, I don't think that I immediately understood how that was supposed to work.
Can you please, for clarity, just take the Python example and give that as a C++ + boost version?
Note, I am not being snarky, I am genuinely curious since everything that makes C++ easier to work with is a win in my book.
second, i don't think i immediately understood, how python example was supposed to work.Last edited by pal666; 20 October 2014, 11:10 AM.
Comment
-
Originally posted by pal666 View Postif you are using wide strings, you are probably better off with associative array which uses either tree or hash, i.e. O(log N) access or even better
Comment
-
Originally posted by kigurai View PostCan you please, for clarity, just take the Python example and give that as a C++ + boost version?
Comment
-
Originally posted by kigurai View PostFirst of all that is boost, and not standard C++.
Second, I don't think that I immediately understood how that was supposed to work.
Can you please, for clarity, just take the Python example and give that as a C++ + boost version?
Note, I am not being snarky, I am genuinely curious since everything that makes C++ easier to work with is a win in my book.
There are a lot of good things in C++11 (auto, smart pointers, new keywords such as null_ptr to replace macros, etc...).
The one thing I find lacking in C++ is modules, because, let's be serious, #include sucks.
Comment
-
Originally posted by TheBlackCat View PostIf you looked at my examples you would see that is exactly what I did in the first and second case. You are right, technically that is not iterating over a sequence. But it is still not O(n) performance no matter what you do.
Comment
-
Originally posted by pal666 View Postwell, i can't be sure whether you tried to iterate with quadratic complexity in second example or not, because it clearly does not work (you are trying to index one array with another). but it could have compiled without errors in this toy language
But that was my third example, my first was from the earlier post.
Comment
Comment