|
|

 Rank: Completly bathed in our weirdness. Groups: Member
Joined: 10/29/2007 Posts: 103 Points: 330 Location: Palo Alto
|
And it works... Thats 4 nested for loops. And enough IF and else statements to get to mars probably 
|
|

 Rank: Admin Groups: Administration
Joined: 10/20/2007 Posts: 1,525 Points: 3,139
|
where is attrs defined?
|
|

 Rank: Completly bathed in our weirdness. Groups: Member
Joined: 10/29/2007 Posts: 103 Points: 330 Location: Palo Alto
|
oo it a private variable out side of the method.
|
|

 Rank: Just gettin' the hang of the weirdness here. Groups: Member
Joined: 2/1/2008 Posts: 27 Points: 81
|
Poor code usually does things like that. And I seriously wonder what you are trying to accomplish here, and if it couldn't be done 50 times simpler ;)
|
|

 Rank: Completly bathed in our weirdness. Groups: Member
Joined: 11/8/2007 Posts: 82 Points: 252 Location: Atlanta, GA
|
|
|

 Rank: Completly bathed in our weirdness. Groups: Member
Joined: 10/29/2007 Posts: 103 Points: 330 Location: Palo Alto
|
It find the functional dependency of variables for thing like ABDE -> C ACDE -> B EDCA -> B Each letter can be in any of the 5 columns. The majority of the code is just moving elements. if you had ABCD->E and you move A to B's column you need to move B so you move it to were A was orginaly. BUT if you already check that case you need to move B in to C,D,E column. In which case you'll have to move what ever is moved and then check to again. That pretty much all the IF and Else. Ya there is probably a easier way to do this. This way is pretty much manually checks every case. I did get the only 100% on this program as no one else was able to make a algorithm that worked for all cases. But ya not saying this was designed well.
|
|

 Rank: Completly bathed in our weirdness. Groups: Member
Joined: 10/29/2007 Posts: 103 Points: 330 Location: Palo Alto
|
The next program for this class is to see if a you have like 3 or 4 or some number or relations, with a certain number of variables and a certain number of functional dependencies if you will have a lossless decomposition if you combine thous relations. That program not going to have any hideous methods like that.
|
|

 Rank: Just gettin' the hang of the weirdness here. Groups: Member
Joined: 2/1/2008 Posts: 27 Points: 81
|
ogrebears wrote:It find the functional dependency of variables for thing like
ABDE -> C ACDE -> B EDCA -> B
Each letter can be in any of the 5 columns. The majority of the code is just moving elements. if you had
ABCD->E and you move A to B's column you need to move B so you move it to were A was orginaly. BUT if you already check that case you need to move B in to C,D,E column. In which case you'll have to move what ever is moved and then check to again. That pretty much all the IF and Else.
Ya there is probably a easier way to do this. This way is pretty much manually checks every case. I did get the only 100% on this program as no one else was able to make a algorithm that worked for all cases.
But ya not saying this was designed well. Soo, it's basically a "Find the missing variable" question? Code:
std::vector< int > input; //Assign some external input to this int attributeCount; //Asign external attributeCount to this. std::vector< int > missing; for ( int i = 0; i < attributecount; ++i ) { if ( input.find( i ) == input.end() ) missing.push_back( i ); }
printf("Missing dependancies:\n"); for( std::vector< int >::iterator it = missing.begin(); it != missing.end(); ++it ) { printf( "%i", *it ); }
Or something like: Code: std::vector< int > input; //Assign some external input to this int attributeCount; //Asign external attributeCount to this. std::vector< int > missing;
std::sort( input.begin(), input.end() );
std::vector< int >::iterator it = input.begin(); for ( int i = 0; i < attributecount; ++i ) { if ( i != *it ) missing.push_back( *it ); else it++; }
printf("Missing dependancies:\n"); for( it = missing.begin(); it != missing.end(); ++it ) { printf( "%i", *it ); }
|
|

 Rank: One of the Main Weird Groups: Member
Joined: 11/13/2007 Posts: 120 Points: 360
|
First and foremost: Good job being the only student to get this working.
Now for the critique: You are doing much more than you need to here as well. With autoboxing, you don't have to do a lot of casting and morphing from numeric object types to primitive data types. In fact, what you are doing here is creating an Integer object from an int primitive data type; pulling the int from the Integer object and then the JVM is autoboxing the int back to an Integer object. Java collections only store objects.
firstCol.add(Integer.valueOf(temp).intValue());
You should have done this: firstCol.add(Integer.valueOf(temp));
A problem, other than the fact is that it could have been done with less code is that code like this is hard to maintain and reuse. When you get onto the workforce and someone else comes along to enhance or fix a bug in that code, they are going to look at it and start dryheaving. If you have no choice, but to write all that code... you could atleast refactor it into methods and also add comments. If I grabbed that code and tried use it, I would have no idea what you are trying to do. This can present many problems. I have seen lots of new folks coming into the workforce coding like this. Writing clean code takes practice and refactoring takes even more. Check this book out: Refactoring by Fowler. I've learned more about writing good code from this book than I ever did in school. Programming isn't only about making it work.
Kinkle 90 Warden on Antonia Bayle
|
|

 Rank: Admin Groups: Administration
Joined: 10/20/2007 Posts: 1,525 Points: 3,139
|
Refeactoring by Martin Fowler is one of the 5 programming books I would take to a Deserted Island. Also, dependent upon which Langs/IDEs you end up working with, if you go into any .net lang and use Vis Studio, I can't say enough about Refactor Pro. There is a VERY steep learning curve, but once you get going with it, about ~60% of refactoring becomes a couple of keystrokes away (at least in C# for myself).
|
|

 Rank: One of the Main Weird Groups: Member
Joined: 11/13/2007 Posts: 120 Points: 360
|
Cyanbane wrote:Refeactoring by Martin Fowler is one of the 5 programming books I would take to a Deserted Island. Also, dependent upon which Langs/IDEs you end up working with, if you go into any .net lang and use Vis Studio, I can't say enough about Refactor Pro. There is a VERY steep learning curve, but once you get going with it, about ~60% of refactoring becomes a couple of keystrokes away (at least in C# for myself). I have some good tools in the Eclipse IDE , that I use. I use the Move Method and Create Local Variable functions daily. You use to have to do all of your refactoring after you were done coding.. not anymore! Every programmer's bookshelf should include Refactoring by Fowler! I don't think I would be half the programmer that I am today without reading that.
Kinkle 90 Warden on Antonia Bayle
|
|

 Rank: Completly bathed in our weirdness. Groups: Member
Joined: 11/8/2007 Posts: 82 Points: 252 Location: Atlanta, GA
|
|
|

 Rank: Admin Groups: Administration
Joined: 10/20/2007 Posts: 1,525 Points: 3,139
|
wraith808 wrote:Just to make sure, you want to take a string of letters, and make sure that ABCDE are in the string? I was just trying to make sure I understood the problem you were given... don't look at "abde -> c" as an entire equation with a left and right side. "abde" isn't the left side and "c" isn't the right. the entire line is saying abde is functionally dependent on c, I think Ogre's project is giving him a list of these and he has to figure out the most normalized form of all of them. You need to find (given a list of dependences) which dependencies are correct and which are not. It is what a lot of database applications have to do to given X number of tables, you could write an application to get it to it's most normalized form. Say you had a table (T1) with 5 columns abcde and another table with 4 colums fghe (T2) well, e might be a unique identifier in the second table so all the dependencies on the first table (to get it to it's most normalized form) would depend on e (so abcd -> e). so columns a,b,c,d depend on the key of e. BUT there might be another way to look at that table (T1) maybe "abcd -> e" is correct for the first set, but maybe "c" is also a unique, so then "abde -> c", so if you had a whole bunch of these rules and you wanted to get the entire set (of tables) to its most normalized form, this would be the function to do this. (I think that is what it is asking). His solution has so many if, else iterations because you have to check to see for any given equation, if you have already processed it before. I am not saying that Ogre has the most efficient algo for the job (I personally don't know of a better one), but I see the complexity of what it is trying to accomplish.
|
|

 Rank: Completly bathed in our weirdness. Groups: Member
Joined: 11/8/2007 Posts: 82 Points: 252 Location: Atlanta, GA
|
|
|

 Rank: Completly bathed in our weirdness. Groups: Member
Joined: 10/29/2007 Posts: 103 Points: 330 Location: Palo Alto
|
I was just expecting you guys to look at it and cry out in pain as i did when i saw it a few days ago again. this was form last fall from the first part of the database class. But for those who are interested. The program was supports to find all functional dependencies (i know it says minimal cover but he changed it at the last min) in a given relation.  SO first of all this didn't start out that messy as it appears. For a Single variable dependencies it pretty simple code Pretty much you have 2 for loops (to change the letter) and one if/else pair for single dependencies (to make sure it wasn't the same letter). A->B C->A Stuff like that and it looks like that  So now remeber i'm a college student and i procrastinate and stuff like that. So let say at this point it the weekend and i spend my time grinding in eq2 instead of working on this and i return on Sunday to realize i need to do more than single functional dependencies. So for 3 variables AB -> C DC -> A I would need 3 for loops. The orignal if/else pair. And a New if/else pair with an nested IF inside since theres a chance of the the changing letter matching 2 of the current letter on the screen. LIke this  So at this point. Any programmer would realize this is retarded and figure out some other way to do it. A college kid who only has a few hour left and only has to go up to the 4 variable case (ME). Will realize there probably an easier way to do it, but with only a few hours left the choice is finish it this way knowing it works or risk not being able to find a new way and turning in a program that doesn't work fully... SO yes it is bad Code i know that... that why i posted it up here with the title i did. It is by far the most ugliest thing I've ever written.
|
|

 Rank: Just gettin' the hang of the weirdness here. Groups: Member
Joined: 2/1/2008 Posts: 27 Points: 81
|
wraith808 wrote:I was just wondering, because SG's response seemed not to answer the original question, but I wasn't 100% sure. Heh, that's because I didn't get it, mainly. I may almost be a graduated CS student, but I never took database classes. In fact, my whole study focusses on real time and embedded systems, so I'm not 100% on what this is all about.
|
|

 Rank: Completly bathed in our weirdness. Groups: Member
Joined: 10/29/2007 Posts: 103 Points: 330 Location: Palo Alto
|
well functional dependencies look like this A B C D 1 2 3 4 1 2 3 5 1 3 4 4 2 1 3 1 If A-->B then there has to be A one to one relation with B Which in this case there is not 1 goes to 2, and 3 But B-->C is one to one as 2 goes to 3 only, 3 goes to 4 only and 1 goes to 3 only. But C is not dependent on B since 3 goes to 2, and 1. The code you see up there does that. Check every combination of the letters, then check each of the arraylist of numbers compares them to see if they all have one to one relations, print it out.
|
|

 Rank: Just gettin' the hang of the weirdness here. Groups: Member
Joined: 2/1/2008 Posts: 27 Points: 81
|
It's starting to make a creepy kind of sense now. Though I'm still glad I didn't take those classes, perhaps I'll just challenge myself and try to find a better algorithm that does catch all ;)
|
|

 Rank: Just gettin' the hang of the weirdness here. Groups: Member
Joined: 2/1/2008 Posts: 27 Points: 81
|
Edit: Think I fixed the issue. So, this bit of C++ code should give all functional dependencies now. The compiled executable can be downloaded hereCode: #include <iostream> #include <vector> #include <map>
#define DEBUG_OUTPUT 0
struct DependsKey { int column; std::vector< int > dependantColumns;
void Print() const { char buffer[2] = " "; for ( std::vector< int >::const_iterator it = dependantColumns.begin(); it != dependantColumns.end(); ++it ) { buffer[0] = 'A' + *it; std::cout << buffer; } buffer[0] = 'A' + column; std::cout << " --> " << buffer;
} };
void FillDependsTable( std::vector< DependsKey > & dep, int column, int columnCount ) { unsigned long columns = 1; unsigned long target = 0; for ( int i = 0; i < columnCount; ++i ) { target |= 1 << i; } DependsKey k; k.column = column; for ( ; columns < target; ++columns ) { if ( columns & ( 1 << column ) ) { continue; } k.dependantColumns.clear(); for ( int i = 0; i < columnCount; ++i ) { if ( i != column && (columns & ( 1 << i ) ) ) { k.dependantColumns.push_back( i ); } } dep.push_back( k ); } }
bool CheckDependency( DependsKey const & dependency, std::map< std::pair< int, int >, int > const & table, int attributeCount, int tuppleCount ) { #if DEBUG_OUTPUT std::cout << "Checking dependency "; dependency.Print(); std::cout << std::endl; #endif
typedef std::vector< std::pair< int, std::vector< int > > > FoundVector; FoundVector foundPairs; for ( int t = 0; t < tuppleCount; ++t ) { int value = table.find( std::pair< int, int >( t, dependency.column ) )->second; std::vector< int > keys; for ( int i = 0; i < dependency.dependantColumns.size(); ++i ) { keys.push_back( table.find( std::pair< int, int >( t, dependency.dependantColumns[ i ] ) )->second ); }
for ( FoundVector::iterator it = foundPairs.begin(); it != foundPairs.end(); ++it ) { bool isCurrent = true; for ( int i = 0; isCurrent && i < dependency.dependantColumns.size(); ++i ) { isCurrent &= it->second[ i ] == keys[ i ]; } if ( isCurrent && value != it->first ) { #if DEBUG_OUTPUT std::cout << "Dependancy failed\n"; #endif
return false; } } foundPairs.push_back( std::pair< int, std::vector< int > >( value, keys ) ); } #if DEBUG_OUTPUT std::cout << "Dependancy ok\n"; #endif return true; }
std::vector< DependsKey > FindDependants( std::map< std::pair< int, int >, int > const & table, int attributeCount, int tuppleCount ) { std::vector< DependsKey > dependencies; for ( int i = 0; i < attributeCount ; ++i ) { FillDependsTable( dependencies, i, attributeCount ); } for ( int i = 0; i < dependencies.size(); ) { if ( !CheckDependency( dependencies[ i ], table, attributeCount, tuppleCount ) ) { dependencies.erase( dependencies.begin() + i ); } else { ++i; } } return dependencies; }
void main() { std::cout << "Number of attributes: "; int attrs; std::cin >> attrs;
std::cout << "Number of Tupples: "; int tupples; std::cin >> tupples;
std::map< std::pair< int, int >, int > table;
for ( int r = 0; r < tupples; ++r ) { for ( int c = 0; c < attrs; ++c ) { std::cout << "Tupple " << r + 1 << " attribute " << c + 1 << ": "; int buffer; std::cin >> buffer; table.insert( std::pair< std::pair< int, int >, int >( std::pair< int, int >( r, c ), buffer ) ); } }
std::cout << "Is this the correct relation? (1/0)\n"; for ( int r = 0; r < tupples; ++r ) { for ( int c = 0; c < attrs; ++c ) { std::cout << table[ std::pair< int, int >( r, c ) ] << " "; } std::cout << std::endl; } bool correct; std::cin >> correct;
if ( correct ) { std::vector< DependsKey > deps = FindDependants( table, attrs, tupples ); std::cout << "Dependencies:\n"; for ( std::vector< DependsKey >::iterator dep = deps.begin(); dep != deps.end(); ++dep ) { dep->Print(); std::cout << std::endl; } } }
|
|

 Rank: Completly bathed in our weirdness. Groups: Member
Joined: 10/29/2007 Posts: 103 Points: 330 Location: Palo Alto
|
Ya i check it out it looks like it works
|
|
|
Guest |