Welcome GuestWho's On:   Guest @ 38.107.179.233 Guest @ 123.125.71.49 Guest @ 65.52.104.20 Guest @ 220.181.94.222 Guest @ 123.125.71.105  Search | Active Topics | Members | Register | Login | RSS 2.0

My code can make grown me cry Options
ogrebears
Posted: Friday, February 15, 2008 3:11:48 PM


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



Cyanbane
Posted: Friday, February 15, 2008 8:10:39 PM


Rank: Admin
Groups: Administration

Joined: 10/20/2007
Posts: 1,525
Points: 3,139
where is attrs defined?
ogrebears
Posted: Friday, February 15, 2008 9:57:40 PM


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.

SG_01
Posted: Sunday, February 17, 2008 3:52:10 AM


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 ;)

wraith808
Posted: Sunday, February 17, 2008 10:23:59 AM


Rank: Completly bathed in our weirdness.
Groups: Member

Joined: 11/8/2007
Posts: 82
Points: 252
Location: Atlanta, GA
^ my thoughts exactly. Though I wasn't going to say it... ;-)

My task is done/My song has ceased/My theme has died into an echo/It is fit.





ogrebears
Posted: Tuesday, February 19, 2008 12:39:57 PM


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.

ogrebears
Posted: Tuesday, February 19, 2008 12:42:25 PM


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.

SG_01
Posted: Tuesday, February 19, 2008 1:18:55 PM


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 );
}



Risi_Lishian
Posted: Wednesday, February 20, 2008 10:38:08 AM


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
Cyanbane
Posted: Wednesday, February 20, 2008 10:58:51 AM


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

Risi_Lishian
Posted: Wednesday, February 20, 2008 11:03:54 AM


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
wraith808
Posted: Wednesday, February 20, 2008 12:04:20 PM


Rank: Completly bathed in our weirdness.
Groups: Member

Joined: 11/8/2007
Posts: 82
Points: 252
Location: Atlanta, GA
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...

My task is done/My song has ceased/My theme has died into an echo/It is fit.





Cyanbane
Posted: Wednesday, February 20, 2008 12:59:08 PM


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.


wraith808
Posted: Wednesday, February 20, 2008 1:35:37 PM


Rank: Completly bathed in our weirdness.
Groups: Member

Joined: 11/8/2007
Posts: 82
Points: 252
Location: Atlanta, GA
I was just wondering, because SG's response seemed not to answer the original question, but I wasn't 100% sure.

My task is done/My song has ceased/My theme has died into an echo/It is fit.





ogrebears
Posted: Wednesday, February 20, 2008 5:43:13 PM


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.

SG_01
Posted: Saturday, February 23, 2008 6:28:12 AM


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.

ogrebears
Posted: Saturday, February 23, 2008 12:12:07 PM


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.

SG_01
Posted: Saturday, February 23, 2008 5:34:12 PM


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 ;)

SG_01
Posted: Saturday, February 23, 2008 7:04:05 PM


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 here

Code:

#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;
      }
   }
}


ogrebears
Posted: Tuesday, February 26, 2008 1:04:44 PM


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

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

Powered by Yet Another Forum.net version 1.9.1.2 (NET v2.0) - 9/27/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.

30 queries (0.139 seconds, 22.90%).

yaf_pageload: 0.004
yaf_topic_info: 0.001
yaf_forum_list: 0.002
yaf_category_simplelist: 0.001
yaf_forum_listpath: 0.003
yaf_forum_simplelist: 0.004
yaf_forum_listpath: 0.005
yaf_post_list: 0.007
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.002
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.007
yaf_usergroup_list: 0.010
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.002
yaf_usergroup_list: 0.008
yaf_usergroup_list: 0.002
yaf_usergroup_list: 0.004
yaf_usergroup_list: 0.011
yaf_usergroup_list: 0.002
yaf_usergroup_list: 0.007
yaf_usergroup_list: 0.002
yaf_usergroup_list: 0.001
yaf_usergroup_list: 0.021
yaf_usergroup_list: 0.008
yaf_usergroup_list: 0.002
yaf_active_list: 0.016
yaf_active_listtopic: 0.002

Clicky Web Analytics