No regular expressions included. Yes, this fair method I’m going to show don’t include regular expressions. Actually there’s nothing wrong with regular expressions, but I think I’m over-using it. If you can browse my Perl section, almost all of my codes use regular expressions.
What’s this all about?
This is a simple algorithm coded in Python. Yes, I’m moving on from Perl. Not actually moving on, as in I’m never gonna use it again, but I think Python is better on this kind of tasks. Well anyway, I recommend you just use the python command line. In windows, there’s a GUI called IDLE and if you want the command line, just fire up cmd (or terminal in UNIX like OS) and run
Again, as the title said, this will identify/find the longest consecutive characters on a string. So for example, in the word:
ll are the longest consecutive characters. This is not hard to accomplish, especially with a scripting language as powerful as Python. Let’s take a look at the code, shall we?
Let’s say for example, we are given the word
iamastriiiiiingwaitwaaaaaaaaaaaaaatttt and we need to determine the longest consecutive characters (which is bunch of
string = 'iamastriiiiiingwaitwaaaaaaaaaaaaaatttt' lastchar = '' charcount = 0 tmpcount = 1 for i in string: if lastchar == i: tmpcount += 1 else: tmpcount = 1 if tmpcount > charcount: charcount = tmpcount char = i lastchar = i result = '' for x in range(charcount): result += char print result
The result will be
In a simple string, the result can be easily identified. But for a string with too many characters, this script will come in handy.
If you have better ways to do this in Python, feel free to leave a comment, or link to the better version.