Tag Archive > find

Finding the Longest Consecutive Characters on a String

» 02 December 2010 » In Programming, Python » 1 Comment

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 python.

Again, as the title said, this will identify/find the longest consecutive characters on a string. So for example, in the word:

hello

The characters 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?

Python Code

Let’s say for example, we are given the word iamastriiiiiingwaitwaaaaaaaaaaaaaatttt and we need to determine the longest consecutive characters (which is bunch of a‘s obviously).

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 aaaaaaaaaaaaaa.

More Applications

In a simple string, the result can be easily identified. But for a string with too many characters, this script will come in handy.

Improvements

If you have better ways to do this in Python, feel free to leave a comment, or link to the better version.

Continue reading...

Tags: , , , , ,

Identifying Duplicate Lines in a Text File

» 24 November 2010 » In Open-Source, Perl, Programming » 13 Comments

It was never easy checking if there are duplicate entries in our text files. Although there are simple methods like firing up notepad and try to find the whole line. But what if you need to identify line numbers?

Why and How?

Recently, I coded a duplicate line identifier in Perl. Actually I was planning to do that in Python instead, but for the sake of answering this question, I wrote it in Perl. It took me several minutes to get the general idea on how to completely answer that question, and I guess I just succeeded.

About the code, I really used that new style of mine I mentioned 2 blog posts away (maybe), and it worked well. I’m a bit worried about my variables though, they make me feel like I coded a mess. But still, it’s just me.

The code is pretty simple to understand, considering there are nested loops, I don’t recommend simulation. But for a 2 or 3 line file then go ahead. And what makes this different from others is, this identifies line numbers. Not removing them, or just printing them out. It’s a bit handy with, let’s say, debugging a text file. I don’t know if that exists but it’s probably the correct. Anyway, here’s the code.

The Code

#!/usr/bin/perl

#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#	Copyright (c) 2010 Ruel Pagayon <ruel@ruel.me> - http://ruel.me

use strict;
use warnings;

sub loadf($) {
    my @file = ( );
    open(FILE, '<', $_[0] ) or die("Couldn't Open " . $_[0] . "\n");
    @file = <FILE>;
    close(FILE);
    return @file;
}

{
	my @file = loadf("path-to-file.txt");
	my @inner = @file;
	my @dup = ( );
	my $l0 = 0; my $l1 = 0; my $l2 = 0; my $dc = 0;	my $tc;
	foreach my $line (@file) {
		$l1++;
		$line =~ s/^\s+//;
		$line =~ s/\s+$//;
		foreach my $iline (@inner) {
			$l2++;
			$iline =~ s/^\s+//;
			$iline =~ s/\s+$//;
			next if ($l1 == $l2 || grep { $_ eq $l1} @dup );
			if ($iline eq $line) {
				$dc++;
				if ($dc > 0) {
					if ($l0 == 0) {
						print "Line " . $l1 . ": " . $line . "\n";
						$l0++;
					}
					print "Line " . $l2 . ": " . $iline . "\n";
					push (@dup, $l2);
				}
			}
		}
		print "\n" unless($dc == 0);
		$dc = 0; $l0 = 0; $l2 = 0;
	}
}

__END__

Just in case you have suggestions about this code, or if you want to download it without copy-paste (silly), I posted this code to gist. But please do leave a comment, if you have something in mind for this code.

Continue reading...

Tags: , , , , , , , , ,

Least but Best

» 15 November 2010 » In C/C++, Life, Programming » 1 Comment

This morning, we had a laboratory exercise in my algorithm’s class. There are two (2) sets of problems, and we must draw the flowchart in Visio, and code it in C++. The first problem is tricky, while the second one is very easy (so I won’t mention it here). Here’s what the problem says:

Make the user input 3 integers, and determine which has the least value.

Ok, this sounds very easy. Come to think of it, it is. Well, actually it asks for the variable with the least integer value. So a simple if-else statement won’t do. By the way, my flowchart is already done, and it was so messy. There are 14 pairs of On-Page Connectors (makes it 28 ‘A’-'N’ circles), good thing I didn’t upload it so you can’t see how bad it is. Heh.

Anyway, on a normal if-else statement, its not hard. Here’s what it should look like:

#include <iostream>

template <class T>
void rcin(T &input) {
	std::cin >> input; 
	std::cin.ignore();
}

int main () {
	int a, b, c;
	std::cout << std::endl
		      << "\tEnter 3 numbers:" << std::endl
			  << std::endl
			  << "\t\tA) "; rcin(a);
	std::cout << "\t\tB) "; rcin(b);
	std::cout << "\t\tC) "; rcin(c);
	std::cout << std::endl;
	if (a == b && b == c) {
		std::cout << "\tAll numbers are equal.";
	} else if (a == b) {
		if (c < a) {
			std::cout << "\tC has the integer with the least value: " << c;
		} else {
			std::cout << "\tA and B has the integer with the least value: " << a;
		}
	} else if (b == c) {
		if (a < b) {
			std::cout << "\tA has the integer with the least value: " << a;
		} else {
			std::cout << "\tB and C has the integer with the least value: " << b;
		}
	} else if (a == c) {
		if (b < a) {
			std::cout << "\tB has the integer with the least value: " << b;
		} else {
			std::cout << "\tA and C has the integer with the least value: " << a;
		}
	} else if (a < b) {
		if (c < a) {
			std::cout << "\tC has the integer with the least value: " << c;
		} else {
			std::cout << "\tA has the integer with the least value: " << a;
		}
	} else if (b < c) {
		std::cout << "\tB has the integer with the least value: " << b;
	} else {
		std::cout << "\tC has the integer with the least value: " << c;
	}
	std::cin.get();
	return 0;
}

NOTE: Nevermind my rcin() function for now, I’ll discuss that on my future post.

So yeah, it’s bad for the eyes. But it works fine, though, the answer is simpler and easier to understand. I just need to pull things out of my head, and not focus on the flowchart. We had this rule of Resist the temptation to code. And to be honest, I can’t follow it. My mind works better if I’m in front of an IDE or editor. Anyway here’s my solution:

#include <iostream>
#define SIZE 3

template <class T>
void rcin(T &input) {
	std::cin >> input; 
	std::cin.ignore();
}

int main () {
	int nums[SIZE], smallest, var;
	std::cout << std::endl
		      << "\tEnter 3 numbers:" << std::endl
			  << std::endl;
	for (int i = 0; i < SIZE; i++) {
		std::cout << "\t\t" << i + 1 << ") ";
		rcin(nums[i]);
		smallest = (i == 0) ? nums[i] : (nums[i] < smallest) ? nums[i] : smallest;
	}
	std::cout << std::endl
			  << "\tThe smallest value is " << smallest << std::endl;
	std::cin.get();
	return 0;
}

I used nested shorthand if statements to make it simpler. And about the function, nevermind it and use std::cin instead. It was just my practice.

Anyway, that’s how simple it is. And from that, I will create a flowchart. Heh. That’s how my mind works.

Continue reading...

Tags: , , , ,

Find your Facebook ID

» 07 September 2010 » In Guides, Internet » No Comments

Most of the Facebook profiles today, are using usernames in their URLs. Of course, who would remember a numeric ID that long. But now, especially for Facebook developers, some applications requires that numeric ID. And since you only have a username in your URL, it seems like its nowhere to be found.

Continue reading...

Tags: , , , ,