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.
 
  • Post
  • Reply
nintendo65
Oct 16, 2008

Two universes separated by a membrane! It causes gravity! I have a uni degree in SCIENCE!!1!
Those are great suggestions but I'm just going to use geocities pagebuilder and abandon the database in favor of an enormous block of obfuscated text, queries will be performed with ctrl+f.

Really though I think I'll try java, I looked at scala/lift and while it is nifty I'm not sure if I'll have the capabilities to implement it on such short notice, but thanks for the suggestion.

Adbot
ADBOT LOVES YOU

darfur
Nov 1, 2003
Edit: I figured it out.

darfur fucked around with this message at 05:22 on Dec 2, 2009

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Is there a canonical algorithm for choosing an element from a set of elements and weights?

foo:2
bar:1

foo randomly gets chosen twice as often as bar given a call to pick an element.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Triple Tech posted:

Is there a canonical algorithm for choosing an element from a set of elements and weights?

foo:2
bar:1

foo randomly gets chosen twice as often as bar given a call to pick an element.

Choose a random natural number uniformly distributed, strictly less than the sum of weights. If it is strictly less than the weight of the first element in the list, then select the first element. Otherwise, subtract the weight of the first element from your random number, and recurse to the next element of the list.

shrughes
Oct 11, 2008

(call/cc call/cc)

Triple Tech posted:

Is there a canonical algorithm for choosing an element from a set of elements and weights?

foo:2
bar:1

foo randomly gets chosen twice as often as bar given a call to pick an element.

What ShoulderDaemon said, except:

{a:2, b:1, c:3, d:4, e:2, f:5, g:2}

First, precompute the cumulative sum:

{a:2, b:3, c:6, d:10, e:12, f:17, g:19}

Then pick a random number uniformly in the set (0, 19] and use binary search to get the least element greater than or equal to number you've chosen, in O(log(n)) time.

haveblue
Aug 15, 2005



Toilet Rascal
Or, if you're really desperate for an O(1) solution, create an array of length equal to the sum of the (integer) weights where each weighted element is assigned to a number of indexes corresponding to its weight, and randomly pick indexes.

shrughes
Oct 11, 2008

(call/cc call/cc)

haveblue posted:

Or, if you're really desperate for an O(1) solution, create an array of length equal to the sum of the (integer) weights where each weighted element is assigned to a number of indexes corresponding to its weight, and randomly pick indexes.

That's not really O(1) because the random number you pick will still have to have length at least log_2(n). :colbert:

And what if you have floating point weights?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

shrughes posted:

What ShoulderDaemon said, except:

{a:2, b:1, c:3, d:4, e:2, f:5, g:2}

First, precompute the cumulative sum:

{a:2, b:3, c:6, d:10, e:12, f:17, g:19}

Then pick a random number uniformly in the set (0, 19] and use binary search to get the least element greater than or equal to number you've chosen, in O(log(n)) time.

This is better if you're going to be selecting from the same set over and over again. If you only select once from a set, it has the same complexity as the naive method, though, and requires the use of vectors rather than lists because binary search wants cheap random access.

You can also do a variant without precomputing any sum at all in O(n). Remember the first element as the candidate, and its weight as the cumulative sum. For each successive element, add that element's weight to the cumulative sum. Then, generate a random natural strictly less than the cumulative sum. If that number is strictly less than the current element's weight, then use the current element as the new candidate, otherwise preserve the previous candidate. When you run off the end of the list, the candidate is your random selection. Now, for fun, find someone who knows what the Monty Hall problem is, and have a week-long argument about the correctness of this method.

tef
May 30, 2004

-> some l-system crap ->
I use this in buttbot http://prxq.wordpress.com/2006/04/17/the-alias-method/ :3:

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

Could you explain this less tersely? It's a bit hard to follow and it starts introducing variables out of nowhere.

tef
May 30, 2004

-> some l-system crap ->
I didn't implement it in buttbot, but you might find the perl more enlightening.

http://code.google.com/p/buttbot/source/browse/trunk/Butts.pm

tef fucked around with this message at 23:16 on Dec 1, 2009

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
I'm trying to grok it as best as possible, but I still don't get it. The algorithm visually and I think cost-wise looks so much more complicated. And for what? I think to make it less computationally expensive? I'm not convinced... The naive solution doesn't look that expensive to begin with...

Hmm I think maybe it is cost effective on recurring runs... That the naive implementation has a high search cost but the alias method is indexed... I guess that makes much more sense.

Me, I only needed one run, so the naive implementation is cheaper. If you did repeated selections, then the alias method would be cheaper over time.

Edit: It's terribly interesting. I will make the false promise of using my time and my poor foundation in computer science to draft something up that is infinitely more clear and verbose...

Triple Tech fucked around with this message at 00:04 on Dec 2, 2009

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Triple Tech posted:

Hmm I think maybe it is cost effective on recurring runs... That the naive implementation has a high search cost but the alias method is indexed... I guess that makes much more sense.

Note that all optimizations you see for this are only going to optimize repeated selection cases; it's relatively straightforward to prove that O(n) is as good as you can do for the single selection case, as there are n weights, all potentially different, that you have to examine. The alias method happens to be an excellent optimization that is both very space- and very time-efficient for repeated selections.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Yeah, I'm not really sure why you'd care about "canonical algorithms" for something you're only doing once, since at that point, the only factor is really the ease of implementation. It would take a pretty pathological program for it to be bottlenecked by code that only runs once.

Cicero
Dec 17, 2003

Jumpjet, melta, jumpjet. Repeat for ten minutes or until victory is assured.
Does anyone have a good example resume for a CS Major looking for a part-time job/internship?

EDIT: I was looking at this one but I obviously don't have that much experience plus why would anyone care that I know how to animate GIFs or use Powerpoint?

EDIT2: Forget it, my brother gave me his, which he shamelessly stole from yet another person.

Cicero fucked around with this message at 06:40 on Dec 2, 2009

yatagan
Aug 31, 2009

by Ozma

Cicero posted:

Does anyone have a good example resume for a CS Major looking for a part-time job/internship?

EDIT: I was looking at this one but I obviously don't have that much experience plus why would anyone care that I know how to animate GIFs or use Powerpoint?

EDIT2: Forget it, my brother gave me his, which he shamelessly stole from yet another person.

The whole point of a resume is to make you stand out. Make sure you're not using something that started as a word template...

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Cicero posted:

EDIT: I was looking at this one but I obviously don't have that much experience plus why would anyone care that I know how to animate GIFs or use Powerpoint?

This resume is god awful. It took me a couple glances to realize that he even listed programming languages at all. And even then, when HTML is your first on the list, there's a problem. That's aside from grammatical issues ("various print medium") and weasel phrases that even HR can tell mean nothing ("Support the Health Net Health Plan of Oregon Web Site using HTML and MS Front Page.")

Avenging Dentist fucked around with this message at 10:08 on Dec 2, 2009

BizarroAzrael
Apr 6, 2006

"That must weigh heavily on your soul. Let me purge it for you."
Something is going very strange in this batch file:

code:
	for /f "tokens=1-3 delims=/ " %%a in ("%date%") do (
		SET DD=%%a
		SET MM=%%b
		SET YYYY=%%c
	)
SET /A JDate  = 3652425 * %YYYY%
echo %JDATE%
Trying to write a script that establishes the Julian date (so I can then work out the day of the week) but the number output is negative, in spite of the fact the calculation I have so far uses only addition and multiplication, and no negative value. Here's the output for the above:

code:
C:\Test>SET /A JDate  = 3652425 * 2009

C:\Test>echo -1252212767
-1252212767
What the hell?

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
The integers that cmd uses are too small to hold the result of that multiplication, so you got an unexpected result.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
3652425 * 2009 is significantly larger than 2^31, so it's overflowing and happening to end up negative.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
What are the definitions of API and protocol and how do they compare/contrast?

csammis
Aug 26, 2003

Mental Institution
An API is a programming interface to a system, and a protocol is a structured method of communication. On the surface they really don't have anything to do with each other despite what Z-Bo is saying in the DBMS thread:

Z-Bo posted:

The first rule of API design is that your API should be at least as expressive as your protocol.

What is the "expressive power" of a protocol? Without clarification of what Z-Bo means by "protocol," it's a nonsense phrase. A protocol can be as simple as saying "I will send you plaintext data in lines ended by \r\n. When you see two \r\n's in a row you know I am done sending you data, then you can send me data in the same format."

As for the expressiveness of an API, an API should expose methods to allow programmers to manipulate data in ways well-supported by the system in question and that's it.

Now get this weird discussion back in the DBMS thread :)

BizarroAzrael
Apr 6, 2006

"That must weigh heavily on your soul. Let me purge it for you."

Plorkyeran posted:

3652425 * 2009 is significantly larger than 2^31, so it's overflowing and happening to end up negative.

Yeah, found it with some help here. Coupled with cmd not using decimals it's not worth the effort, I bodged it with a C# app.

Incidentally:
code:
if (DOW == "Friday" || DOW == "Saturday" || DOW == "Sunday" )
Is this the proper way to determine if DOW (the day of the week) is one of the listed days? I thought there was a way to express it more like:

if (DOW == Friday || Saturday || Sunday)

Basically without the repetition, since I'm only checking one thing multiple times.

yippee cahier
Mar 28, 2005

BizarroAzrael posted:

Yeah, found it with some help here. Coupled with cmd not using decimals it's not worth the effort, I bodged it with a C# app.

Incidentally:
code:
if (DOW == "Friday" || DOW == "Saturday" || DOW == "Sunday" )
Is this the proper way to determine if DOW (the day of the week) is one of the listed days? I thought there was a way to express it more like:

if (DOW == Friday || Saturday || Sunday)

Basically without the repetition, since I'm only checking one thing multiple times.

According to http://msdn.microsoft.com/en-us/library/system.dayofweek(VS.71).aspx, DaysOfWeek is a normal enum, from 0-6. OR wouldn't work on that.

If it were a bitfield enum, things would be golden. MS uses this exact situation in their example:
http://msdn.microsoft.com/en-us/library/cc138362.aspx

I forget how C# handles bitwise stuff, but you could be a bastard to the next guy by doing something like:
code:
if (1<<DOW & 1<<Friday | 1<<Saturday | 1<<Sunday)
Can you tell I've been doing too much embedded C lately? Does C# have a membership operator? In python you can do something like:
code:
if DOW in (Friday,Saturday,Sunday):

shrughes
Oct 11, 2008

(call/cc call/cc)
code:
if (new[] { Friday, Saturday, Sunday }.Contains(DOW))

Backhand
Sep 25, 2008
I'm putting together a little Access 2007 stuff for my employers, and they want to be able to generate a given report in letter format so that it can be printed and mailed to concerned parties. And no, using a mail merge to Word is not acceptable, apparently, they want the whole thing to stay in Access.

Anyway, I was figuing I'd generate the report programmatically to get around formatting issues, setting up things like the body of the letter as one massive variable that binds to a textbox, with variables inserted where necessary, sorta like so:


Private Sub Report_Load()

Dim body as String
body = "This letter is addressed to " & name & " regarding your current account status..."
Me.BodyTextBox = body

End Sub

Only problem I'm having? Embarassingly enough, I can't seem to find a good resource to tell me how to get at my recordset programmatically. I have the data in a table, I know where it is, I can even create a recordset... but I'm not sure how to get the contents out of it. Everything I've been looking at is just advice on how to set the Recordsource property and let fields auto-generate. That's not my problem. I need to be able to assign variables, not just output stuff without being able to modify it. I do have one small advantage in that I'll only ever be addressing one record per letter, so I don't have to worry about stepping through an array. Can anyone help me out?

Backhand fucked around with this message at 21:20 on Dec 2, 2009

Super Dude
Jan 23, 2005
Do the Jew
I am making a robot for Google Wave in Java, but I'm running into a small problem. After adding a few features that work, I wanted to add language translation functionality using Google's google-api-translate-java-0.91. My issue is that when I add the code to call the translation in the robot, it simply doesn't print anything to the wavelet at all. It printed fine before I added the translation code.

Here is a simplified version of my code that I am using to get this to work.

code:
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

import com.google.wave.api.*;

@SuppressWarnings("serial")
public class ParrotyServlet extends AbstractRobotServlet {
	
	@Override
	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();

		if (bundle.wasSelfAdded()) {
			Blip blip = wavelet.appendBlip();
			TextView textView = blip.getDocument();
			textView.append("Thank you for using Superdude-Bot!\n");
			String translate = "";
                        
                        //translation code
			Translate.setHttpReferrer("http://wave.google.com");
			try {
				translate = Translate.execute("Hello world!", Language.ENGLISH, Language.SPANISH);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
                        textView.append(translate);
		}
        }
}
I know that the translation code works because I made a small test program that ran it perfectly. Can anyone help to get it to work on Wave?

Harry
Jun 13, 2003

I do solemnly swear that in the year 2015 I will theorycraft my wallet as well as my WoW
moved to proper thread

Harry fucked around with this message at 05:05 on Dec 4, 2009

Flamadiddle
May 9, 2004

Okay, a couple of things:

I'm building an application (in Java) which needs a command-line based menu interface and a GUI to perform the same functions. I'm therefore trying to abstract all the methods into a class to sit between the interface and the data so that either interface (along with any future interfaces) can do the same things with minimal logical code within the interface classes themselves. Does this sound sensible? It seems the most efficient way to do it.

I have an add method to insert an object into a hash table. There's a chance that the key will already be in use and so I want to get the user's confirmation to overwrite the current contents.

Should I:
a) give the method a boolean return value or something and only ever call the add method with an if(add(x,y)){} with the return value determining whether a prompt should be shown?
b) throw an exception and use a catch block to prompt the user dependent on interface type?

I guess my understanding of when you should throw an exception versus a return type is a bit cloudy.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Well these helpful folk in this thread have told me... Exceptions are exceptional. If this is a behavior you're likely to encounter, then it's not an exception (hence, go the try/boolean route). But if it's some weird crazy poo poo that you're practically never going to see but might see in a blue moon, then it is an exception.

Or so I understand.

Edit: The above partly motivated by the cost of exception handling, apparently?

tripwire
Nov 19, 2004

        ghost flow

Triple Tech posted:

Well these helpful folk in this thread have told me... Exceptions are exceptional. If this is a behavior you're likely to encounter, then it's not an exception (hence, go the try/boolean route). But if it's some weird crazy poo poo that you're practically never going to see but might see in a blue moon, then it is an exception.

Or so I understand.

Edit: The above partly motivated by the cost of exception handling, apparently?

Well in python they're used often for plain old control flow, any iterator for example signals that its got no more things left to iterate by raising the StopIteration exception. I think in performance minded languages like C it makes more sense to use them as you describe.

Flamadiddle
May 9, 2004

Triple Tech posted:

Well these helpful folk in this thread have told me... Exceptions are exceptional. If this is a behavior you're likely to encounter, then it's not an exception (hence, go the try/boolean route). But if it's some weird crazy poo poo that you're practically never going to see but might see in a blue moon, then it is an exception.

Or so I understand.

Edit: The above partly motivated by the cost of exception handling, apparently?

Great. Thanks for the advice. It makes sense how you describe it.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

Flamadiddle posted:

I have an add method to insert an object into a hash table. There's a chance that the key will already be in use and so I want to get the user's confirmation to overwrite the current contents. Should I give the method a boolean return value or something and only ever call the add method with an if(add(x,y)){} with the return value determining whether a prompt should be shown?
The simplest and easiest for users would be to have a get and a put method like the java.util.HashMap, and the caller can do something like this:
code:
if(table.get(whatever) != null && !confirmed()) return;
table.put(whatever, value);
replacing confirmed() with a prompting method of their choice. Or, if you want to (attempt to) enforce that the check happens (instead of the caller being able to just use put with no preceding check), try something like this:
code:
interface Confirmation{
  boolean confirm();
}

// ...

public boolean add(Key k, Value v, Confirmation c){
  if(c == null) throw IllegalArgumentException("c must be non-null");
  if(table.get(k) != null && !c.confirm()) return false;
  table.put(k, v);
  return true;
}
Then the caller will have to define their own Confirmation, be it a dialog box or lovely curses UI or something that always returns true or false or whatever.

Flamadiddle
May 9, 2004

Ahhh.. Okay. I wasn't sure if Java boolean ands short circuited if the first argument is false as in that first example, so I wouldn't have thought to implement it anything like that. Will have a go at something like the second example. Thanks for the advice.

causticfluids
Dec 25, 2006

Congratulations on not getting fit in 2011!
OCaml question:

I'm trying to write a day 1 newbie module I can load into the interpreter or use later. (For learning.) Basically, I'm applying Heron's method of approximating square roots within an error. (0.01 here) But it doesn't work! After

code:
#use "sroot.ml";;
I try " sroot(4.,1.);; ", and it just ticks away, no answer, even with large error. Anybody see anything?

code:
let next (a,b) = 0.5*.(b +. a/.b);;

let rec sroot (a,b) =
  if abs_float(next(a,b) -. a) < 0.01 then b
  else sroot(a,next(a,b));; 

causticfluids fucked around with this message at 01:10 on Dec 5, 2009

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

causticfluids posted:

Anybody see anything?

You're terminating when b (the root) is sufficiently close to a (the input).

causticfluids
Dec 25, 2006

Congratulations on not getting fit in 2011!
Yeah I screwed up the error checking. Works now!

code:
let next a b = 0.5 *. (b +. (a /. b));;

let err a b = abs_float( ((next a b)**2.0) -. a);;

let rec sroot a b epsilon = 
  if (err a b) <  epsilon then (next a b)
  else (sroot a (next a b) epsilon);; 
Forgive my piss poor butchering of OCaml. Though it IS multi-paradigm...I'm trying to be functional. I did split up the next and err functions for clarity. I just didn't even see my arithmetic screw up. It's time to go to sleep.

causticfluids fucked around with this message at 03:05 on Dec 5, 2009

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
A simpler, more idiomatic solution would be:

code:
let sroot a epsilon =
  let next b = 0.5 *. (b +. a /. b) in
  let rec iter b = if abs_float (a -. b *. b) < epsilon then b else iter (next b) in
  iter (next 1.0)

Fast Luck
Feb 2, 1988

hm i can move this post

Fast Luck fucked around with this message at 17:36 on Dec 5, 2009

Adbot
ADBOT LOVES YOU

atb
Mar 20, 2009
edit: wrong thread

atb fucked around with this message at 08:12 on Dec 5, 2009

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply