Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 664 665 [666] 667 668 ... 796

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

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9975 on: September 08, 2016, 08:22:19 pm »

Trivially simple for someone with minimal programming experience, but a bit difficult for me.

I mean, I had to think about it for a bit because I, um, wouldn't do that with an array

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9976 on: September 08, 2016, 08:35:06 pm »

Putnam, I think its obvious at this point that what I learn in the second week of a beginner's Java class isn't exactly the best way of doing things.
« Last Edit: September 08, 2016, 08:37:02 pm by DragonDePlatino »
Logged

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9977 on: September 08, 2016, 08:39:04 pm »

Trivially simple for someone with minimal programming experience, but a bit difficult for me.
I mean, I had to think about it for a bit because I, um, wouldn't do that with an array
Well yeah that's because it's horrible efficiency to be constantly removing things from an array and copying over to a slightly smaller array. :P (Seriously, you go from O(n) to something halfway between that and O(n^2), since the first array copy costs you n additional steps, then the next n-1, then n-2, and so on as you shorten the array you need to copy).

I'd probably do it slightly differently though; rather than passing down just the list of elements and having some magic spot at the start of the ArrayList that holds the lowest number I'd probably just chop off the first value each time while keeping the maximum value in a function parameter, i.e. maxElem(ArrayList<Float> elems, Float max), and then then just call either maxElem(smaller_elem, max) or maxElem(smaller_elem, new_max) with each traversal downwards. An even better way to do it would be to not change the array at all but just pass down a current location pointer as well as the max and the array, and then just use that to iterate along as you go, no array changing required (alternatively, use a data structure that responds well to removing elements from the front/back and doesn't require copying the whole thing each time to remove an element).
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9978 on: September 08, 2016, 08:54:20 pm »

Ah, yes. If it were up to me I would've just for-looped through a plain fixed-length array and tracked the largest number thus far until I reached the end. Popping elements and recreating objects is a pretty terrible way to do it. Unfortunately, the question said we *had* to use the signature static Float maxElem(ArrayList<Float> elems) and we *had* to find the answer by modifying the ArrayList with a list of methods given to us.

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9979 on: September 08, 2016, 10:15:07 pm »

Ah, yes. If it were up to me I would've just for-looped through a plain fixed-length array and tracked the largest number thus far until I reached the end. Popping elements and recreating objects is a pretty terrible way to do it. Unfortunately, the question said we *had* to use the signature static Float maxElem(ArrayList<Float> elems) and we *had* to find the answer by modifying the ArrayList with a list of methods given to us.
Eurgh.  Why on earth would they do that to you?!

...Hang on, which language's ArrayList was that, anyway?
« Last Edit: September 08, 2016, 10:17:00 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9980 on: September 09, 2016, 04:46:46 pm »

The intention was to teach recursion by forcing us to use it instead of iteration. It's frustrating, I feel it's teaching us Java students a very bad approach to an otherwise simple problem. In the few months of C++ experience I taught myself, I never had to use recursion. For better or worse, I'm a strong advocate of "learn it as you need it" when it comes to programming. The general consensus online seems to be "Use recursion when its faster/more intuitive than iteration" and I fully intend to hold off recursion until I hit a problem like that.

In unrelated news, the Code::Blocks forums finally processed my email verification after a few days and I got to post the color theme I use. If you're a fan of that IDE, I suggest you check it out!
« Last Edit: September 09, 2016, 04:50:11 pm by DragonDePlatino »
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9981 on: September 09, 2016, 05:04:20 pm »

I want to make Darkling for everything, honestly.

And yeah, learn it as you need it is a thing.
« Last Edit: September 09, 2016, 05:07:07 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9982 on: September 09, 2016, 05:05:48 pm »

Recursion is very nice for some tasks (implementing certain kinds of state machines for example), but you never need it. Everything you can do with recursion can be done with loops and a stack. Recursion is just easier, not better.

If I had that class I would be tearing my hair out because I would instinctively be trying to do the exercises the simple/fast way instead of the learn/stupid way.
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9983 on: September 09, 2016, 05:12:59 pm »

Last time I tried recursion I learned that javascript doesn't have proper tail calls...

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9984 on: September 09, 2016, 05:21:16 pm »

If you need tail calls you really need "continue" instead. Recursion works best when you can branch in lots of different ways to an arbitrary depth (such as in a recursive descent compiler).

Tail calls are kinda dumb, why not just use a loop and a simple state variable? If you are not storing previous state on the stack (which tail calls prevent), why are you using recursion?
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

monkey

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9985 on: September 09, 2016, 05:28:27 pm »

Recursion is used as a teaching tool, it makes you consider all the corner cases of the input, what you return, how you consume all the data and if the thing ever ends, and so on. Skills that a lot of people have problem with.
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9986 on: September 09, 2016, 05:31:38 pm »

Tail calls are kinda dumb

And this is what I learned from the situation.

I almost program in BlooP these days.

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9987 on: September 09, 2016, 05:38:36 pm »

Recursion is used as a teaching tool, it makes you consider all the corner cases of the input, what you return, how you consume all the data and if the thing ever ends, and so on. Skills that a lot of people have problem with.


Good thing I never went to college to learn bad habits then... The best way to teach is to start as you plan to continue, not to get part way in, then say "all that stuff we taught you? Don't do that, it's really slow and inefficient."

I almost program in BlooP these days.

Well, I wouldn't go that far, recursion is nice when you need to knock out a parser in a hurry. Loops are definitely faster though.
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9988 on: September 09, 2016, 05:48:03 pm »

I taught myself to program before going to college.

The first code I saw horrified me, lmao. Instead of the example code using VB's "sleep" function, it used... a for loop that loops ~10,000,000 times to do nothing to simulate a wait. I looked at that, recoiled in horror, replaced it with a proper sleep, then turned it in, heh.

The teachers at my school are very fine with me actually keeping good habits up despite instructions, though... as long as the code passes all their tests, it's fine, I guess.

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9989 on: September 09, 2016, 05:51:40 pm »

Aaaaaayup.  That's horrifying.

*cringes*
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.
Pages: 1 ... 664 665 [666] 667 668 ... 796