Lost Password?

Go Back   CodeCall Programming Forum > Software Development > General Programming

General Programming Non language specific, Assembly, Linux/Unix, Mac and anything not covered in other topics. Talk about Programming Theory here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-14-2008, 04:16 PM
gofer gofer is offline
Newbie
 
Join Date: May 2008
Posts: 3
Rep Power: 0
gofer is on a distinguished road
Default Generating all sequences of 1s and 0s of length 30

I'm trying to generate all possible sequences of 1s and 0s of length 30 where no subsequence is repeated 3 consecutive times (without subsequences like 000 or 101010 or 100100100). I'm not sure how I should approach the problem and when I asked my "mentor" about it he gave me some really vague answers (mostly repeating the phrase "There are different ways to do it").

So any help will be greatly appreciated. Thanks in advance
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-14-2008, 11:52 PM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,940
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to behold
Send a message via MSN to v0id
Default Re: Generating all sequences of 1s and 0s of length 30

What language is it going to be in? If you're using C++, you can use two of the standard algorithm functions to solve the problem. They're called next_permutation and prev_permutation.

If not, you may check Wikipedia's article on permutations.

Last edited by v0id; 05-14-2008 at 11:53 PM. Reason: Stupid smileys
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-15-2008, 01:05 AM
gofer gofer is offline
Newbie
 
Join Date: May 2008
Posts: 3
Rep Power: 0
gofer is on a distinguished road
Default Re: Generating all sequences of 1s and 0s of length 30

Thanks for the reply. The language is irrelevant - I can do it in a language that best suits the task (although I was planning to use C++).

However I still don't understand how am I supposed to use permutations to solve the problem. Permutations simply change the order of the elements in a set. What's the set?

Are you suggesting to generate every possible combination of 1s and 0s of length 30 and then search for consecutive sequences in all of them? Because that seems awfully slow to me...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-15-2008, 06:50 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,940
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to beholdv0id is a splendid one to behold
Send a message via MSN to v0id
Default Re: Generating all sequences of 1s and 0s of length 30

Oh, sorry. I read your question a little too fast. I read it as you just wanted the permutations.

You could search for them, yes, but you're right, that is slow. It's the only solution I can think of right now, but I'm sure somebody will stop by and provide you some better answers.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-15-2008, 11:23 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Mod
 
Join Date: Jul 2006
Age: 35
Posts: 1,813
Last Blog:
Game software (GURPS)
Rep Power: 25
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default Re: Generating all sequences of 1s and 0s of length 30

First issue: you are searching through a space of 2^30 or 1,073,741,824 strings.
Second issue: your "invalid" strings consist of
2^1 3 digit strings
2^2 6 digit strings
2^3 9 digit strings
2^4 12 digit strings
2^5 15 digit strings
...
2^10 30 digit strings
This means you have 2^11-2 potential substrings to check for each of your strings. Of course, some of the longer strings are invalid because of the shorter strings (00 00 00 contains 000, etc).

The two options I can see are:
1) build a minimal list of invalid substrings, and for each candidate, check for the presence of all the invalid substrings. This may be prohibitively large.
2) for each string, loop through it grabbing 1, then 2, then 3, etc characters, checking to see if it is the basis of an invalid string.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-25-2008, 05:30 AM
gofer gofer is offline
Newbie
 
Join Date: May 2008
Posts: 3
Rep Power: 0
gofer is on a distinguished road
Default Re: Generating all sequences of 1s and 0s of length 30

Thanks for the help This is what I came up with:
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>

using namespace std;

struct SortStrings
{
	bool operator()( const string& lhs, const string& rhs ) const
	{
		return lhs.length() < rhs.length();
	}
};

std::string StringFromInt( int n )
{
	if( n == 0 )
		return "0";

	int i = 31;
	while( !( (1 << i) & n ) ) // Finds the most significant bit equal to 1
		--i;

	std::string result;

	for( int j = i; j>=0; --j )
	{
		std::string res = (1<<j) & n ? "1" : "0";
		result.append( res );
	}

	return result;
}

void GenereateInvalidDict( std::vector<std::string>& out )
{
	std::vector<std::string> temp;
	for(int i = 0; i < 1024; ++i)
	{
		std::string s = StringFromInt(i);
		temp.push_back( s );

		if( s.length() < 10 )
			temp.push_back( std::string("0") += s );
		if( s.length() < 9 )
			temp.push_back( std::string("00") += s );
	}

	std::sort( temp.begin(), temp.end(), SortStrings() );

	std::vector<std::string> temp2;
	for( size_t i = 0; i<temp.size(); ++i )
	{
		bool bNeeded = true;

		for( size_t j = 0; j<i; ++j )
		{
			std::string toSearch = temp[j];
			toSearch += temp[j];
			toSearch += temp[j];
			if( temp[i].find( toSearch ) != temp[i].npos )
			{
				bNeeded = false;
				//break;
			}
		}
		if( bNeeded )
			temp2.push_back( temp[i] );
	}

	for( size_t i = 0; i<temp2.size(); ++i )
	{
		std::string s = temp2[i];
		s.append( temp2[i] );
		s.append( temp2[i] );

		out.push_back( s );
	}
}

bool IsValid( const std::vector<std::string>& invalid, const string& s )
{
	for( size_t i = 0; i<invalid.size(); ++i )
		if( s.find( invalid[i] ) != s.npos )
			return false;
	return true;
}
void Generate1sAnd0s( const std::vector<std::string>& invalid, const std::string& s, ostream& out )
{
	if( s.length() == 30 )
	{
		out << s.c_str() << endl;
		return;
	}

	string s0 = s; s0.append( "0" );
	string s1 = s; s1.append( "1" );
	if( IsValid( invalid, s0 ) )
		Generate1sAnd0s( invalid, s0, out );
	if( IsValid( invalid, s1 ) )
		Generate1sAnd0s( invalid, s1, out );
}

int main()
{
	std::vector<std::string> invalid;
	GenereateInvalidDict( invalid );
	Generate1sAnd0s( invalid, "", cout );
	cout << "Done" << endl;
	return 0;
}
GenereateInvalidDict generates the dictionary of invalid strings and Generate1sAnd0s recursively appends a 1 and/or a 0 to the string if it's a valid one, until it reaches the required length of 30
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
sequence



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Added] Length file url field rolandd ionFiles 6 04-28-2008 03:16 PM
Generating random background... bentoh General Programming 5 01-24-2008 07:47 AM
Generating Random Numbers with PHP Paradine PHP Tutorials 4 08-27-2007 07:09 PM
variable length arrays in C rattlepanos C and C++ 3 05-03-2007 11:35 AM
Sequences and Tuples Sionofdarkness Delphi/Python 0 07-22-2006 03:47 PM


All times are GMT -5. The time now is 02:15 AM.

Contest Stats

dargueta ........ 128.00000
John ........ 127.00000
Xav ........ 107.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads