Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
the bsd boys
Aug 8, 2011
Probation
Can't post for 378 days!
snypes

Adbot
ADBOT LOVES YOU

z0rlandi viSSer
Nov 5, 2013


Banned.

JewKiller 3000
Nov 28, 2006

by Lowtax
javascript is seriously awful

code:
var a = [];
for (var i = 0; i < 10; i++) {
    a[i] = function() { return i; }
}
alert(a[0]());
don't run this yet, just look at the code. what do you think the alert box should show? now compare to this one:

code:
var a = [];
function f(x) { return function() { return x; } };
for (var i = 0; i < 10; i++) {
    a[i] = f(i);
}
alert(a[0]());
what do you think should be the result here?





ok, now run them in jsfiddle or something. notice that the first result is 10, what the gently caress while the last result is 0, as a sane person would expect. but the only difference between the second program and the first is that instead of simply assigning to a[i] an anonymous function that returns i, there is the extra step of calling this new function f(i), which returns... an anonymous function that returns i. shouldn't these programs have identical results? especially since the formal name for what i did in the second program is "eta expansion", which is supposed to be equivalent to the original?





i say that a sane person would expect 0 because the anonymous function assigned to a[i] is supposed to capture the current state of its environment like a proper closure, and at that time the value of i was 0. then a[1]() should return 1, etc. what happens is, the function does capture its environment, in which i is a mutable variable (a pointer to something), whose state can and does later change out from under us after the other iterations of the loop. ok, that's awful, but then why does the second program do the right thing? because the process of passing i into the parameter x of f makes javascript copy the current value of i into x, and because there is a different x for each invocation of f, mutation is no longer an issue

oh no blimp issue
Feb 23, 2011

does anyone know a good book on algorithms/algorithmics or whatever its called
im trying to become a less bad programmer

suffix
Jul 27, 2013

Wheeee!

lots of languages work like this. the only exceptions i can think of are when variables are immutable, or when you explicitly choose whether to copy or reference

if you want per-closure state, declare your variable inside the for scope

qntm
Jun 17, 2009
javascript only gets blamed for that behaviour because it's the first place most people run into it

Python code:
>>> a = []
>>> for i in range(10):
...     a.append(lambda: i)
...
>>> a[0]()
9
Python code:
>>> a = []
>>> def get_lambda(x):
...     return lambda: x
...
>>> for i in range(10):
...     a.append(get_lambda(i))
...
>>> a[0]()
0
i agree it's not easy behaviour to defend

qntm fucked around with this message at 12:50 on Feb 7, 2014

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
this is what a sane language looks like

code:
> ((car (for/list ([i (in-range 10)]) (lambda () i))))
0

MononcQc
May 29, 2007

Awia posted:

does anyone know a good book on algorithms/algorithmics or whatever its called
im trying to become a less bad programmer

I liked The Algorithm Design Manual by Steven Skiena (PDF). It reads easily and will be a good intro to basic algorithms in many categories, and can be used as a stepping stone if you need to dig in deeper later on.

oh no blimp issue
Feb 23, 2011

thank you very much
it doesn't load at work but i'll take a look later

FamDav
Mar 29, 2008
that's because js is a poo poo language where you implicitly take references to local mutable values and the only way to construct copies is through function scoping like a goddamn dog.

Zaxxon
Feb 14, 2004

Wir Tanzen Mekanik

JewKiller 3000 posted:

javascript is seriously awful

code:
var a = [];
for (var i = 0; i < 10; i++) {
    a[i] = function() { return i; }
}
alert(a[0]());

The actual issue here is js' variable hoisting. All variables are only scoped to the nearest function. an equivalent program would be

code:
var a = [];
var i;
for (i = 0; i < 10; i++) {
    a[i] = function() { return i; }
}
alert(a[0]());
in which it would make slightly more sense that all the closures capture the same mutating variable.


however, the language is still bad.

qntm
Jun 17, 2009

Zaxxon posted:

The actual issue here is js' variable hoisting. All variables are only scoped to the nearest function. an equivalent program would be

code:
var a = [];
var i;
for (i = 0; i < 10; i++) {
    a[i] = function() { return i; }
}
alert(a[0]());
in which it would make slightly more sense that all the closures capture the same mutating variable.

Hoisting doesn't explain this at all. Even if JavaScript had block scope and i only existed for the interior of the loop, you would expect to see the same behaviour. You can see that i is being modified ("i++") for each successive iteration, not created anew. All the closures therefore refer to the same i, which, at the end of the loop, has value 10.

Miley Virus
Apr 9, 2010

is there any better python ocr library than pytesser? it's suiting my needs atm and is super simple but i may as well use something better if it exists

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
okay I've learned how to implement the following data structures and algos today:

- Linked List/Double Linked List/Stack/Queue

- Binary Tree

- Directed/Undirected Graphs

- Merge Sort/Quicksort/Bubblesort. Binary Tree Search. Breadth/Width searches on graphs/trees

What am I missing?

I can point at things and say "yeah this is quadratic time and this is log time and this is linear" but I can't really get into more detail than that. I haven't done hashmaps.

I can only fit so much more in my head in the next couple days. I really want to nail this interview so I can go from a hobbiest terrible programmer to an official terrible programmer.

Notorious b.s.d.
Jan 25, 2003

by Reene
this is already as much as you will ever be asked by a sane interviewer

with your big 'O' basics knocked out, i would concentrate on language features/wrinkles in whatever you are putting on your resume

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

USSMICHELLEBACHMAN posted:

okay I've learned how to implement the following data structures and algos today:

- Linked List/Double Linked List/Stack/Queue

- Binary Tree

- Directed/Undirected Graphs

- Merge Sort/Quicksort/Bubblesort. Binary Tree Search. Breadth/Width searches on graphs/trees

What am I missing?

answering your question directly without commentin' on the worth of learnin these things vs other skills, off the top of my head the next ones to look at (in priority order) would be
  • hashmaps
  • sets
  • heaps (priority queues)
  • tries
  • b-trees
  • idk, sparse matrix representations? k-d tree?
im playin fast n loose w/ abstract vs concrete datastructures here, but you get the gist

coffeetable fucked around with this message at 00:05 on Feb 10, 2014

Notorious b.s.d.
Jan 25, 2003

by Reene

coffeetable posted:

answering your question directly without commentin' on the worth of learnin these things vs other skills, off the top of my head the next ones to look at (in priority order) would be
  • hashmaps
  • bit vectors
  • heaps (priority queues)
  • bloom filter
  • tries
  • b-trees
  • k-d tree

these are all useful and important if you are going to be implementing data structures, but are they important to interview for a boring business applications job?






p.s. i have no idea what a k-d tree is and i am not ashamed to admit that

Pittsburgh Fentanyl Cloud
Apr 7, 2003


Notorious b.s.d. posted:

these are all useful and important if you are going to be implementing data structures, but are they important to interview for a boring business applications job?






p.s. i have no idea what a k-d tree is and i am not ashamed to admit that

It's a fancy way to measure your Blops score. True gamers unite.

FamDav
Mar 29, 2008
learn a hashmap/table/whatever so you can say "well if i just used a hashmap intsead it'd be faster but then i'd use more space i dunno man what do you want from me let me suck your dick ill do it no dont act gay about it just let me do it come on man slap that dick in my hand yeah feels good dont it poo poo look at it squirm all warm and poo poo no dont worry im cool yo you got any disesase tho"

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY
k-d tree is your first port of call when you're dealin w/ geometric information, as are tries when you're dealin with strings and b-trees when you're accessing external data. you're right though, they're not on par w/ the rest.

she should definitely know her some hashmaps n set implementations though

FamDav
Mar 29, 2008
dont make it weird

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

MononcQc posted:

I liked The Algorithm Design Manual by Steven Skiena (PDF). It reads easily and will be a good intro to basic algorithms in many categories, and can be used as a stepping stone if you need to dig in deeper later on.

seconding this is a v good real world design approach. CLRS teaches the standard elements but not really how and when to use them

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

coffeetable posted:

k-d tree is your first port of call when you're dealin w/ geometric information, as are tries when you're dealin with strings and b-trees when you're accessing external data. you're right though, they're not on par w/ the rest.

she should definitely know her some hashmaps n set implementations though

r-trees would like a word (postgis = rtrees over GiST more or less)

realtalk the best algorithm is one written and tested by someone else so "use a package manager" is probs the best approach

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

coffeetable posted:

answering your question directly without commentin' on the worth of learnin these things vs other skills, off the top of my head the next ones to look at (in priority order) would be
  • hashmaps
  • sets
  • heaps (priority queues)
  • tries
  • b-trees
  • idk, sparse matrix representations? k-d tree?
im playin fast n loose w/ abstract vs concrete datastructures here, but you get the gist

yes, yes, yes, yes,lmao no, nope

b-trees and such are great if u wanna learn how modern filesystems and databases work but are a very specific structure (and v complex)

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

USSMICHELLEBACHMAN posted:

Breadth/Width searches on graphs/trees

you mean breadth/depth first search? (I'm being pedantic to help you)

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

Malcolm XML posted:

r-trees would like a word (postgis = rtrees over GiST more or less)
geometric data structures are a bottomless fukkin pit but k-d are the simplest of the lot. ain't arguin that you should preferentially use a library implementation though.

Malcolm XML posted:

yes, yes, yes, yes,lmao no, nope

b-trees and such are great if u wanna learn how modern filesystems and databases work but are a very specific structure (and v complex)
babby's first b-tree ain't no worse than red-black trees (because they are b-trees), and those - as ugly as they are - turn up in most data structures coursesprobably because of the ubiquity of clrs.

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY
for standard interview fodder though yeah you're right both are pushing it. shoulda stopped tryin to think of common ones after tries

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
Thanks i will ummm take all of this into consideration

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
im just gonna do a bunch of eulers

pointers
Sep 4, 2008

codility will better prepare you for interviews than euler, i think

tef
May 30, 2004

-> some l-system crap ->

USSMICHELLEBACHMAN posted:

okay I've learned how to implement the following data structures and algos today:

What am I missing?

if someone's been reading their algorithmic puzzle book, you're always in for a arduous interview. i am pretty much convinced this is normally to justify the interviewer's computer science degree, because they have rarely had any opportunity to use this stuff in production.

classic things are reversing a linked list without overhead, so write just a reverse first, and then optimize it.

there will be some linear time algorithm where the trick is doing two scans over the collection, once to find a number, value, depth, item, and a second pass

occasionally you'll get a divide and conquer thing, like quickselect.

always write the simplest algorithm that works. and by simple i mean, slow, verbose and in parts. break it down into tiny, tiny pieces and assemble it slowly. don't try and write the whole thing top to bottom.

as for when you get asked to write binary search, remember this one weird old tip: are you writing a lower bound or an upper bound: http://canhazcode.blogspot.co.uk/2012/02/we-need-to-talk-about-binary-search.html


quote:

I haven't done hashmaps.

Knowing the basics of hash tables might help you a bit, especially on collisions.

quote:

I can only fit so much more in my head in the next couple days. I really want to nail this interview so I can go from a hobbiest terrible programmer to an official terrible programmer.

some of the crap up there is worth repeating about coding and algopuzzles in interviews:

- read this: http://canhazcode.blogspot.co.uk/2012/02/we-need-to-talk-about-binary-search.html

- be patient and methodical: write highly explanatory code, deconstruct it into plenty of functions and methods

- don't solve it all at once, either. if it is to be fast, linear and correct, get it correct *first*

- algorithms always have a trick to them. sometimes it's divide and conquer, sometimes it's that linear time can mean multiple passes over the data.


finally, when you get an algopuzzle, don't ask the interviewer "when was the last time you used this in production code", because they don't like saying "oh, we never use anything like this" because it makes them look and feel stupid. if you want to rub salt on their wounds, just say "I just want to know if this interview reflects the sort of work I may be hired to do"

actually don't do that, but it's really funny and I do it every time.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

tef posted:

finally, when you get an algopuzzle, don't ask the interviewer "when was the last time you used this in production code", because they don't like saying "oh, we never use anything like this" because it makes them look and feel stupid. if you want to rub salt on their wounds, just say "I just want to know if this interview reflects the sort of work I may be hired to do"

actually don't do that, but it's really funny and I do it every time.

heh my last interview was stuff like "you have a pool of unreliable servers, how do you decide which one to try first? what if one's had higher-than-normal error rates? what if they've all had higher-than-normal error rates? should you fail loudly or just not succeed very often?"

important stuff

Notorious b.s.d.
Jan 25, 2003

by Reene

Cocoa Crispies posted:

heh my last interview was stuff like "you have a pool of unreliable servers, how do you decide which one to try first? what if one's had higher-than-normal error rates? what if they've all had higher-than-normal error rates? should you fail loudly or just not succeed very often?"

important stuff

were they fishing for a discussion of the byzantine generals problem?

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Notorious b.s.d. posted:

were they fishing for a discussion of the byzantine generals problem?

not really, i was already familiar with CRDTs and the basics of distributed poo poo

e: like the cap theorem

Necc0
Jun 30, 2005

by exmarx
Broken Cake
if you want just good algo practice the UVa online judge is pretty great. i used it a lot when prepping for the ibm acpc

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
I did a thing to play around with the Maven release plugin and gently caress around some more with logback.

https://github.com/hiro2k/logback-twitter

Notorious b.s.d.
Jan 25, 2003

by Reene

Janitor Prime posted:

I did a thing to play around with the Maven release plugin and gently caress around some more with logback.

https://github.com/hiro2k/logback-twitter

my startup is so open source that our app logs to twitter

Zaxxon
Feb 14, 2004

Wir Tanzen Mekanik

qntm posted:

Hoisting doesn't explain this at all. Even if JavaScript had block scope and i only existed for the interior of the loop, you would expect to see the same behaviour. You can see that i is being modified ("i++") for each successive iteration, not created anew. All the closures therefore refer to the same i, which, at the end of the loop, has value 10.

yeah you are right. My mistake

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
my feeling on scoping is that there are scopes. if you stick to general usage they will behave predictably. if you do weird stuff have fun with your scopes not making suns




also if you are a fan of scoping dont use javascript

Adbot
ADBOT LOVES YOU

qntm
Jun 17, 2009

Cocoa Crispies posted:

what if they've all had higher-than-normal error rates?

then higher-than-normal is the new normal, pick a server at random :toot:

  • Locked thread