Hello and welcome to CertForums.co.uk, here we host free active certification forums with links to the best free resources for Microsoft's MCSA MCSE MCDBA Cisco's CCNA CCDA and CCNP, and CompTIA's A+ Network+ i-NET+ and Security+ certifications in the UK. If you wish to post or use other advanced features you will need to register first. Registration is absolutely free and takes only a few minutes to complete so sign up today!

If you have any problems with the registration process or your account login, please contact support

Go Back   CertForums > Computing Support Forums > Programming & Scripting
Home Forums Register Search Today's Posts Mark Forums Read

Neat little python string trick

Post New ThreadReply
 
Thread Tools Display Modes
  #31  
Old 27-May-2008, 10:17 PM
Mathematix's Avatar
Mathematix Mathematix is offline
Longterm Member
Posts: 795
Points: 1124 Mathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 points
Power: 23
None
Join Date: 09 Mar 2006
Location: London
Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
WIP: Not doing certs. Computer geek.
Quote:
Originally Posted by dmarsh26 View Post
I'm with harry and mgoldwasser, reversing the string should be (and by look of it is) a linear time operation. Think STL iterators or indeed the aforementioned loops. Its a bounded loop with direction, nothing more complex, no need to consider sorting whatsoever.
No mate. I know I said that I would give up, but I have to make a point:

1. The problem would be linear if and only if the letters were first in a completely random order and the requirement were to sort them in reverse alphabetical order.

2. Given that the string was already in alphabetical order, part of the work was already done, so half the effort was required to reverse the order, hence the n/2 complexity that the author disagrees with.


 
Reply With Quote
  #32  
Old 28-May-2008, 09:56 AM
dmarsh26's Avatar
dmarsh26 dmarsh26 is online now
Longterm Member
Posts: 988
Points: 1847 dmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 points
Power: 30
None
Join Date: 24 May 2007
Location: Hampshire
Age: 34
Certifications: One or two...
WIP: Girlfriend+
Quote:
1. The problem would be linear if and only if the letters were first in a completely random order and the requirement were to sort them in reverse alphabetical order.
No you are wrong, the definition of the problem was not to sort, it was to REVERSE. The alphabet string was just used as an example candidate string to demonstrate the reverse. There is no need to sort the string, not because its already 'sorted' but because that is not part of the requirements, the string could of been 'gobledegook' and the reversal would be 'koogedelbog', no sort is needed.

Quote:
2. Given that the string was already in alphabetical order, part of the work was already done, so half the effort was required to reverse the order, hence the n/2 complexity that the author disagrees with.
You are diverting the thread to disscuss O notation of popular sorting algorithms when the algorithm is a reverse algorithm. The linear time complexity is less than the logarithmic complexity of a sorting algorithm.
A simple observation for reverse might be N chars will require N reads, N writes and N termination comparisions so N*3 operations. Worst case for bubble sort would be something like N pow 2 comparisons and swaps (read*2&comp&write*2) so (N pow2)*5. Average case will of course be less and prob some logarithmic function. http://www.nist.gov/dads/HTML/bubblesort.html - Has bubble sort as O(npow2), quicksort is N log N.
http://warpelican.com/?page_id=3


Last edited by dmarsh26 : 28-May-2008 at 10:14 AM.
 
Reply With Quote
  #33  
Old 28-May-2008, 10:32 AM
Mathematix's Avatar
Mathematix Mathematix is offline
Longterm Member
Posts: 795
Points: 1124 Mathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 points
Power: 23
None
Join Date: 09 Mar 2006
Location: London
Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
WIP: Not doing certs. Computer geek.
Quote:
Originally Posted by dmarsh26 View Post
No you are wrong, the definition of the problem was not to sort, it was to REVERSE. The alphabet string was just used as an example candidate string to demonstrate the reverse. There is no need to sort the string, not because its already 'sorted' but because that is not part of the requirements, the string could of been 'gobledegook' and the reversal would be 'koogedelbog', no sort is needed.
Dude, I don't know where you get off. I have proved you wrong on many occasions before and have done so again.

When you rearrange the order of elements according to a set of criteria, and the position of those elements are considered sorted when that criteria is met, then they are sorted. When you look on the web or in a book, those are examples of the fundamental sorting algorithms. these are not the be all and end all of the possibilities of sorting! Jeez...


Quote:
Originally Posted by dmarsh26 View Post
You are diverting the thread to disscuss O notation of popular sorting algorithms when the algorithm is a reverse algorithm. The linear time complexity is less than the logarithmic complexity of a sorting algorithm.
A simple observation for reverse might be N chars will require N reads, N writes and N termination comparisions so N*3 operations. Worst case for bubble sort would be something like N pow 2 comparisons and swaps (read*2&comp&write*2) so (N pow2)*5. Average case will of course be less and prob some logarithmic function. http://www.nist.gov/dads/HTML/bubblesort.html - Has bubble sort as O(npow2), quicksort is N log N.
http://warpelican.com/?page_id=3
I wasn't using any of the sorting algorithms described above. Go back an read what I did! The facts are there! Stop digging a hole for yourself, for heavens sake!


 
Reply With Quote
  #34  
Old 28-May-2008, 12:03 PM
tripwire45's Avatar
tripwire45 tripwire45 is offline
Administrator
Posts: 13,132
Points: 3274 tripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 points
Power: 173
None
Join Date: 29 Jun 2003
Location: Boise, ID, USA
Certifications: A+ and Network+
Ok you two...step back for a sec. From my experience, you are both very intelligent guys who generally know what you're talking about. Take a moment and try to understand where the other guy is coming from. Is it that he's wrong or is there some sort of misunderstanding operating here that's causing you to be at loggerheads. If you are in a disagreement, so be it, but there's absolutely no reason to personalize it. Please keep that in mind. A friendly message from "the management".


You know, I wish my parents played Mozart when I slept because half the time I don't even know what the heck anyone's talking about!
 
Reply With Quote
  #35  
Old 28-May-2008, 12:22 PM
dmarsh26's Avatar
dmarsh26 dmarsh26 is online now
Longterm Member
Posts: 988
Points: 1847 dmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 points
Power: 30
None
Join Date: 24 May 2007
Location: Hampshire
Age: 34
Certifications: One or two...
WIP: Girlfriend+
Quote:
I wasn't using any of the sorting algorithms described above. Go back an read what I did! The facts are there! Stop digging a hole for yourself, for heavens sake!
I was just trying to clarify the thread topic, thats what a forums for is it not ?

Quote:
Dude, I don't know where you get off. I have proved you wrong on many occasions before and have done so again.
Geeeezz take a chill pill man, when I am wrong or mistaken I am more than willing to admit I am wrong, thats what heathly debate is all about no ? "Change your mind, prove you have one..."

Quote:
When you rearrange the order of elements according to a set of criteria, and the position of those elements are considered sorted when that criteria is met, then they are sorted. When you look on the web or in a book, those are examples of the fundamental sorting algorithms. these are not the be all and end all of the possibilities of sorting! Jeez...
Never said they were did I if you bother to read the posts. The point is the fact that the example string happened to be sorted in this case is irrelevant to the reverse algorithm under disscussion.

Quote:
I wasn't using any of the sorting algorithms described above. Go back an read what I did! The facts are there! Stop digging a hole for yourself, for heavens sake!
Quote:
Demo: (swapped elements in bold)

Step 1 - 'dbca'
Step 2 - 'dcba'

So we have four elements reversed in two steps! (O(n/2))
This is a type of sort no ? O notation is generally concerned with worst case,

Step 1 - bdca
Step 2 - adcb
Step 3 - acdb
Step 4 - abdc
Step 5 - abcd

As you can see scanning from the ends with one sweep is not sufficient for truely random data, your algorithm would require multiple passes to succeed and could result in swapping elements that were in the correct location.

A general algorithm will need at best N log N swaps in worst case, best case (sorted) zero swaps, note number comparisons can be a lot larger than the number of swaps (best case log2(n!) ?), therefore its no where near n/2 for worst case and your algortihm is fundamentally broken. You are assuming the data is part sorted or partitioned.

http://en.wikipedia.org/wiki/Sorting_algorithm

No usable sorting algorithm comes close to you level of performance of O(n/2).

N log N is the general lower bound for most sorting algorithms, for 4 elements roughly 2.4 which is more than 2 ie N/2.

You could try some other algorithms to get closer to your measure, however in reality these performance measurements are skewed to large datasets because they discount the cost of the 'minor' operations. For small datasets like in our examples these operations are not minor and the timings for many popular algorithms will be very similar.


Last edited by dmarsh26 : 28-May-2008 at 01:30 PM.
 
Reply With Quote
  #36  
Old 28-May-2008, 01:06 PM
Mathematix's Avatar
Mathematix Mathematix is offline
Longterm Member
Posts: 795
Points: 1124 Mathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 points
Power: 23
None
Join Date: 09 Mar 2006
Location: London
Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
WIP: Not doing certs. Computer geek.
Quote:
Originally Posted by tripwire45 View Post
Ok you two...step back for a sec. From my experience, you are both very intelligent guys who generally know what you're talking about. Take a moment and try to understand where the other guy is coming from. Is it that he's wrong or is there some sort of misunderstanding operating here that's causing you to be at loggerheads. If you are in a disagreement, so be it, but there's absolutely no reason to personalize it. Please keep that in mind. A friendly message from "the management".
Point taken, Trip.

One of my primary responsibilities at work is optimisation/reverse engineering, both in terms of algorithms and resource allocation. They respect my work and knowledge a great deal - there is no reason why it should be any different here. I'm just a little tired of knowing exactly what I'm talking about and people coming along trying to teach an old dog new tricks (although I'm not an 'old dog') which are incorrect in the first place.

Seriously, if the general public on certforums want to thrive on incorrect/incomplete information on computing science then so bid. It's disgraceful that I should be challenged for something that I've especially clearly proven.

Rant over.


 
Reply With Quote
  #37  
Old 28-May-2008, 01:41 PM
dmarsh26's Avatar
dmarsh26 dmarsh26 is online now
Longterm Member
Posts: 988
Points: 1847 dmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 points
Power: 30
None
Join Date: 24 May 2007
Location: Hampshire
Age: 34
Certifications: One or two...
WIP: Girlfriend+
Quote:
It's disgraceful that I should be challenged for something that I've especially clearly proven.
Where have you proven that N/2 is lower limit O notation for a sort ? What exactly have you proven ?

Your opinion or mine alone do not count as fact or even a consensus. Nobody deserves respect they have to earn it, and it has to be earned in each new domain/forum (excuse the pun.).

I'm not trying to teach you anything, you started to try to teach several of your elders (old dogs?) that reversing a string is fundamentally related to sort when it is not.

Quote:
Seriously, if the general public on certforums want to thrive on incorrect/incomplete information
Thats precisely why myself, hbromhall and mgoldwasser have voiced our opinions, because we want certforums readers to have as complete a picture as possible given the nature of a forum.


Last edited by dmarsh26 : 28-May-2008 at 01:42 PM.
 
Reply With Quote
  #38  
Old 28-May-2008, 02:15 PM
Mathematix's Avatar
Mathematix Mathematix is offline
Longterm Member
Posts: 795
Points: 1124 Mathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 points
Power: 23
None
Join Date: 09 Mar 2006
Location: London
Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
WIP: Not doing certs. Computer geek.
Quote:
Originally Posted by dmarsh26 View Post
Where have you proven that N/2 is lower limit O notation for a sort ? What exactly have you proven ?
You guys said that reversing the string is a linear time process in its best case, right? Linear time is O(n). I showed via an alternative algorithm that it is infact not a linear problem, in that elements can be swapped as shown, given the criteria in half the time O(n/2). We even have Knuth's works here at our studio, and I have them at home. I've double-checked my definitions even on the web, asked a colleague who sits directly next to me. None of them disagree.

My colleague actually thought it was quite cool how I reveresed the string.

Quote:
Originally Posted by dmarsh26 View Post
Your opinion or mine alone do not count as fact or even a consensus. Nobody deserves respect they have to earn it, and it has to be earned in each new domain/forum (excuse the pun.).
I completely agree. But then I put to you what is the purpose of proof if you still cannot earn respect for your findings. I'm not asking to be treated like a god, just not to be pressed further for proof when I have already clearly provided one that others have accepted.

Quote:
Originally Posted by dmarsh26 View Post
I'm not trying to teach you anything, you started to try to teach several of your elders (old dogs?) that reversing a string is fundamentally related to sort when it is not.
I was referring to myself when saying 'old dog'. How is reversing a string not fundamentally related to sort?

Quote:
Originally Posted by dmarsh26 View Post
Thats precisely why myself, hbromhall and mgoldwasser have voiced our opinions, because we want certforums readers to have as complete a picture as possible given the nature of a forum.
I agree that eveyone should give their perspective, otherwise there wouldn't be room for healthy discussion. I have admitted in the passed when I've made mistakes on these forums, and it's clear that others have a clear problem doing so. Fromm your last posts I can pick out a number of errors and misunderstandings, but to avoid WW3 I'm steering clear.

If you want me to say you're right, then here you go: "You're right, dmarsh." (Even though I don't believe it for one moment. ;) )



Last edited by Mathematix : 28-May-2008 at 02:45 PM.
 
Reply With Quote
  #39  
Old 28-May-2008, 03:14 PM
dmarsh26's Avatar
dmarsh26 dmarsh26 is online now
Longterm Member
Posts: 988
Points: 1847 dmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 pointsdmarsh26 has over 1500 points
Power: 30
None
Join Date: 24 May 2007
Location: Hampshire
Age: 34
Certifications: One or two...
WIP: Girlfriend+
Quote:
Linear time is O(n).
Maybe in the most purest sense yes, however linear in a more general term means 'proportional', non-exponential, non logarithmic, not a curve, ie it simply means it can be represented as a line on a graph, the are many more possible functions that would generate a line. I'd be the first to admit my math and O notation knowledge is poor.

Quote:
I showed via an alternative algorithm that it is infact not a linear problem, in that elements can be swapped as shown, given the criteria in half the time O(n/2).
This is also linear, just its a linear relationship that 'ramps' at half the rate. People who therefore stated it was a 'linear' relationship are therefore still correct.

Quote:
Believe it or not this is not the best way to sort these items in your linear time. I can propose a solution in O(n/2) just off the top of my head. Let me show you by example:
You maybe can do one swap sweep at O(n/2), you cannot sort though, sort requires comparison and potentialy more swaps. Read any text on sort, it will state N log N as lower bound.

Replace sort with reverse and we are in total agreement. However a swap algorithm will probably still perform worse than a reverse iterator copy. A copy would require N operations vs your alleged N/2 but they would most likely be cheaper operations. You would probably require more conditional logic, plus the original method was framed in terms of 'immutable strings' hence hinting at a copy, why copy and swap when you can just copy ?

Quote:
But then I put to you what is the purpose of proof if you still cannot earn respect for your findings. I'm not asking to be treated like a god, just not to be pressed further for proof when I have already clearly provided one that others have accepted.
I do not believe I have seen any 'proof' ? If you are as good at maths as you say you will understand how hard a good proof is to construct. Who has accepted what exactly ?

Quote:
How is reversing a string not fundamentally related to sort?
Reversing a string is generally changing the order of elements in memory so that character elements at all indexes are swapped around the midpoint. You do not need to even inspect the contents of the elements, there need be no comparison or collation. You need not even swap, a straight copy is enough providing you have a reverse iterator. A copy with no comparision is therefore not anything like a sort, it is however linear and time taken is directly related to the number of elements.

Quote:
Fromm your last posts I can pick out a number of errors and misunderstandings, but to avoid WW3 I'm steering clear.
Feel free to point out my misunderstandings or poor explanations, I never said my understanding of the subject was legendary, neither was I rude or abbrasive in any of my posts.


Last edited by dmarsh26 : 28-May-2008 at 03:42 PM.
 
Reply With Quote
  #40  
Old 28-May-2008, 03:52 PM
Mathematix's Avatar
Mathematix Mathematix is offline
Longterm Member
Posts: 795
Points: 1124 Mathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 points
Power: 23
None
Join Date: 09 Mar 2006
Location: London
Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
WIP: Not doing certs. Computer geek.
As said, dmarsh. You're right. Now go play.


 
Reply With Quote
  #41  
Old 28-May-2008, 05:09 PM
ffreeloader's Avatar
ffreeloader ffreeloader is offline
Lifetime Member
Posts: 3,657
Points: 3030 ffreeloader has over 3000 pointsffreeloader has over 3000 pointsffreeloader has over 3000 pointsffreeloader has over 3000 pointsffreeloader has over 3000 pointsffreeloader has over 3000 pointsffreeloader has over 3000 pointsffreeloader has over 3000 pointsffreeloader has over 3000 pointsffreeloader has over 3000 pointsffreeloader has over 3000 points
Power: 72
None
Join Date: 26 Jul 2005
Location: USA
Age: 54
Certifications: MCSE, MCDBA, CCNA, A+
WIP: LPIC 1
Quote:
Originally Posted by Mathematix View Post
As said, dmarsh. You're right. Now go play.
Wow, do you have "issues".



Behold, the turtle. He makes progress only when he sticks his neck out.

James Bryant Conant
 
Reply With Quote
  #42  
Old 28-May-2008, 08:15 PM
Mathematix's Avatar
Mathematix Mathematix is offline
Longterm Member
Posts: 795
Points: 1124 Mathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 pointsMathematix has over 1000 points
Power: 23
None
Join Date: 09 Mar 2006
Location: London
Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
WIP: Not doing certs. Computer geek.
Quote:
Originally Posted by ffreeloader View Post
Wow, do you have "issues".
Says the man who goes round every forum in a bad mood.

If he wants me to say he's right, so bid. Just want this crap to end.


 
Reply With Quote
  #43  
Old 29-May-2008, 12:38 AM
tripwire45's Avatar
tripwire45 tripwire45 is offline
Administrator
Posts: 13,132
Points: 3274 tripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 pointstripwire45 has over 3000 points