Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 617 618 [619] 620 621 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 908977 times)

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9270 on: March 23, 2016, 12:08:01 pm »

I've been working on the same program for 6+ hours now. I've gotten to the point where I'm so tired that I don't even remember why half of the things are the way they are. The script looked like it was working with two inputs, but that just turned out to be a cruel illusion as it promtly shat itself and died when I tried with five inputs. As it is now, it won't even run and I'm too tired and demoralized to know for sure why.

Spoiler: texmerge.py (click to show/hide)


Don't ask me how it works, because I'm so tired that I don't even remember right now.

The most infuriating thing is, I'm 90% sure I would have had it if I'd went to bed less than an hour ago and worked on it after I'd gotten some sleep. I'm sure that a lot of easy-to-spot mistakes have added up into a huge unfixable mess, because I thought this was going to be so easy ::)
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Avis-Mergulus

  • Bay Watcher
  • This adorable animal can't work.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9271 on: March 24, 2016, 10:14:47 am »

Does anybody know a good Python library for working with XSD Schema files? I basically have a schema and a bunch of XML files, and I have to create another bunch of XML files that would conform to the schema, from the data in the first bunch. Previously, I just used BeautifulSoup, but it's become really obtuse.

I've tried PyXB and GenerateDS, but they don't want to work with Cyrillic tag names for some reason. Plus their documentation is arse, and I'm not really sure they're what I need.
Logged
“See this Payam!” cried the gods, “He deceives us! He cruelly abuses our lustful hearts!”

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9272 on: March 24, 2016, 02:49:31 pm »

So a C++ program I'm writing for a college assignment crashes at the end of the program. It brings up windows' "program has stopped responding blah blah blah" message. Not sure why. Never had this issue in a previous program. I've determined that the issue is somewhere in the last three lines of main(), involving an object, since if I comment those lines out the program runs fine and returns 0.

Spoiler (click to show/hide)

Anyone know what the problem might be? It doesn't seem to have any impact on how the program runs, but crashing looks bad and I wouldn't want to turn in a program that crashes at the end for grading.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9273 on: March 24, 2016, 03:03:34 pm »

<crashy code>

Not having looked at C++ seriously in several years, can you tell me what this does?

Code: [Select]
arr[length];

My assumption would be accessing memory that contains junk data.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9274 on: March 24, 2016, 03:08:30 pm »

This line in the constructor arr[length]; isn't a valid way to size an array, it's just trying to access memory address "arr+length", which is invalid since arr hasn't been initialized yet. What you want here is:

arr = new double[length];

And then you need a destructor with a matching deallocation call:

DataStorage::~DataStorage()
{
    delete [] arr;
}

Actually, if you want to be extra smart, you put an if statement in the destructor:

DataStorage::~DataStorage()
{
    if(arr != 0)
    {
        delete [] arr;
    }
}

This avoids possible crashes in cases where the dynamic memory isn't actually allocated in the constructor, and there is an init function that does that instead. trying to delete a null pointer causes a crash.
« Last Edit: March 24, 2016, 03:14:12 pm by Reelya »
Logged

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9275 on: March 24, 2016, 03:38:12 pm »

snip
snip

I see. Fixed it. The programming class went over using pointers, new, and dynamic memory for stuff like arrays recently but I did not think of using it. Not in the habit of it.

Thanks for the in-depth answer, Reelya. Really cleared up what I did wrong.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #9276 on: March 24, 2016, 06:51:08 pm »

So, I'm trying to get more practice maintaining SQL databases by making a database of personal info for myself. So far I have a few useful things, like people's birthdays and the dates I bought certain things. Anyone have any other ideas for things I could keep track of with a relational database?
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9277 on: March 24, 2016, 07:00:24 pm »

dates that you added entries to the database, including the dates of those entries :P

Seriously though: recipes. People ALWAYS lose recipes. They forget what version of a recipe from what website they liked, and if they write it down then they lose the paper.
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9278 on: March 24, 2016, 07:21:29 pm »

Not knowing your personal details or interests it would be hard to know what to recommend. The point of relational databases is the relational part: i.e. just storing some tables of data isn't utilizing what makes relational databases work. To get better with relational databases you really need to do joins on multiple tables.

If you shared your basic database schema I could probably recommend some stuff to try. But I can kinda guess that this would help:

Spoiler (click to show/hide)

Seriously though: recipes. People ALWAYS lose recipes. They forget what version of a recipe from what website they liked, and if they write it down then they lose the paper.

Maybe that's a personal thing. Also, by itself "recipes" is not an interesting thing to store in a relational database, unless you do something "relational" with them, such as make a recipe database that had links to all the ingredients, and a separate table of current stocks. Then you could write a procedure to select recipes that could be made with the minimal purchases.
« Last Edit: March 24, 2016, 07:38:21 pm by Reelya »
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9279 on: March 24, 2016, 08:55:15 pm »

This avoids possible crashes in cases where the dynamic memory isn't actually allocated in the constructor, and there is an init function that does that instead. trying to delete a null pointer causes a crash.

It surprised me to learn a while back that you actually can delete NULL pointers safely.  I'd written an awful lot of code that checked for NULL before deleting prior to me reading up on it by chance.

Of course, you still need the check unless you initialize the pointer to NULL somewhere before the delete.  Calling delete on an uninitialized pointer is definitely going to cause crashes.
Logged
Through pain, I find wisdom.

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9280 on: March 26, 2016, 04:34:17 am »

So, I'm trying to get more practice maintaining SQL databases by making a database of personal info for myself. So far I have a few useful things, like people's birthdays and the dates I bought certain things. Anyone have any other ideas for things I could keep track of with a relational database?
Songs you like? Video games/movies/shows/books you have yet to play/watch/read?

I keep large text files of songs (mostly on youtube) I might like to listen to again. I've been meaning to put them into an RDB so I can sort by artist, prevent redundancy, etc., but haven't gotten around to it.
« Last Edit: March 26, 2016, 04:40:17 am by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #9281 on: March 26, 2016, 03:08:21 pm »

All good suggestions. Obviously the data doesn't all have to utilize RDB features specifically, but I am doing it partially to learn those features, so it helps.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9282 on: March 27, 2016, 07:24:56 am »

Hey, could someone confident in Python review my program (which is a hometask I have been given by the teacher in university where I'm studying) for encoding/decoding messages using Huffman code? I wonder how efficient, readable, correct etc it is? Sorry I really haven't a person to ask.

Here's the link: http://pastebin.com/2JaqFCn2
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9283 on: March 27, 2016, 08:11:46 am »

Hey, could someone confident in Python review my program (which is a hometask I have been given by the teacher in university where I'm studying) for encoding/decoding messages using Huffman code? I wonder how efficient, readable, correct etc it is? Sorry I really haven't a person to ask.

Here's the link: http://pastebin.com/2JaqFCn2
It seems correct, but the runtime efficiency has a lot of room for improvement:

Let n be the number of symbols in your alphabet.

First off, you're using a gnomesort on the queue every iteration of the tree building loop, and the gnomesort takes O(n^2) in the first iteration and O(n) in subsequent iterations. You can improve this by sorting the queue with a faster algorithm first once (making it O(n log n)), and then in subsequent iterations starting the gnomesort right at the last element (i = len(queue)) instead of attempting to sort the (already sorted) beginning of the queue every time. If you want to be really smart about it, you could just make the queue a heapq, that's exactly the data structure you need for best runtime.

Next, you're building the code table symbol by symbol by using expensive "in" calls in a potentially unbalanced tree, making the algorithm O(n^2) best case, O(n^3) worst case. This could be improved by a factor of n/log n by starting out with a dict((sym, "") for sym in alphabet), then iterating over the tree breadth-first instead of iterating over the alphabet, and appending the code symbol of each traversed node to the code of each symbol in that node's symbol list.

Finally and most importantly, your decoding algorithm doesn't actually make any use of the shiny new decision tree you just built! If you use the decision tree, you can speed things up from O(n log˛ n) to O(log n) best case, and from O(n^3) to O(n) worst case!

Here's how to use the decision tree: Start at the root of the tree. Follow the branch indicated by the first bit of the coded message. Follow the branch indicated by the second bit of the coded message. Repeat until you're at a leaf node. There's your first character. Now go back to the root of the tree. Repeat until you're out of coded message.

Also there's a typo: "Dencoding".
« Last Edit: March 27, 2016, 08:14:13 am by MagmaMcFry »
Logged

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9284 on: March 27, 2016, 08:40:49 am »

Hey, could someone confident in Python review my program (which is a hometask I have been given by the teacher in university where I'm studying) for encoding/decoding messages using Huffman code? I wonder how efficient, readable, correct etc it is? Sorry I really haven't a person to ask.

Here's the link: http://pastebin.com/2JaqFCn2
It seems correct, but the runtime efficiency has a lot of room for improvement:

Let n be the number of symbols in your alphabet.

First off, you're using a gnomesort on the queue every iteration of the tree building loop, and the gnomesort takes O(n^2) in the first iteration and O(n) in subsequent iterations. You can improve this by sorting the queue with a faster algorithm first once (making it O(n log n)), and then in subsequent iterations starting the gnomesort right at the last element (i = len(queue)) instead of attempting to sort the (already sorted) beginning of the queue every time. If you want to be really smart about it, you could just make the queue a heapq, that's exactly the data structure you need for best runtime.

Next, you're building the code table symbol by symbol by using expensive "in" calls in a potentially unbalanced tree, making the algorithm O(n^2) best case, O(n^3) worst case. This could be improved by a factor of n/log n by starting out with a dict((sym, "") for sym in alphabet), then iterating over the tree breadth-first instead of iterating over the alphabet, and appending the code symbol of each traversed node to the code of each symbol in that node's symbol list.

Finally and most importantly, your decoding algorithm doesn't actually make any use of the shiny new decision tree you just built! If you use the decision tree, you can speed things up from O(n log˛ n) to O(log n) best case, and from O(n^3) to O(n) worst case!

Here's how to use the decision tree: Start at the root of the tree. Follow the branch indicated by the first bit of the coded message. Follow the branch indicated by the second bit of the coded message. Repeat until you're at a leaf node. There's your first character. Now go back to the root of the tree. Repeat until you're out of coded message.

Also there's a typo: "Dencoding".

Thank you for the reply, it was really helpful! Where can I read about the computational complexity of various data structures and functions implemented in python? And how do you determine how (and to what degree) it's possible to improve performance of a given operation?

« Last Edit: March 27, 2016, 11:12:06 am by RoguelikeRazuka »
Logged
Pages: 1 ... 617 618 [619] 620 621 ... 796