7
1
True Story: Several years ago while interviewing at an investment advisory firm that managed over a billion dollars in client funds, I was asked a collection of reasonable and bizarre questions by a partner in the firm. An example of a reasonable question would be "What can go wrong with a linear fit?".
But then, things devolved....
Interviewer: "Have you ever written code that modifies itself?"
Job Candidate: "No, can't say that I have"
Interviewer: "We have one guy who writes programs that modify themselves, I think he uses awk"
Job Candidate: "Wow. That's pretty incredible. AWK? "
Now it is your turn to be that one guy that financial houses would kill to hire...
Challenge: Write a self-modifying script of some kind that performs a "useful financial function".
Rules:
- Use a scripting language, not a language requiring compilation such as C or Java. Perl, Python and the like that will work from a #! are OK. Of course, heroes use awk, but awk is not a requirement.
- Self-modifying means that the script modifies its own source code. The script must be reusable after it has modified itself. The script might also generate useful user output to STDOUT or some other interface.
- The definition of "useful financial function" is left to the voters and can be as intriguing or dysfunctional as you prefer. If you need examples for inspiration, here are a few ideas: stock picking, stock trading, prediction and/or fitting, future value of account, Ponzi scheme generator, risk analysis of Nigerian email deals, salary bonus calculator for partners vs employees, etc.
- Most upvotes wins.
EDIT: I agree with one participant who notes that it is not obvious how to read the current gawk script from within gawk. Here is a solution for Linux-based systems and other O/S that have a /proc filesystem.
#!/usr/bin/gawk -f
BEGIN {
pid = PROCINFO["pid"]
commfile = "/proc/"pid"/comm"
print "I will look here for the script file name: " commfile
print "Here is what is in that file:"
getline scriptfilename < commfile
print scriptfilename
print "Here is my script:"
while (getline line < scriptfilename){
print line
}
}
Removed code-challenge tag because it would require an objective winning criterion. – Howard – 2013-12-30T12:27:22.600
Wait, rule 2 disallows the self-modify Befunge. :-( – Justin – 2013-12-30T16:58:23.723
@Quincunx How would you modify the rule? – Paul – 2013-12-30T20:04:26.477
@Paul I'd just say that the program must be self-modifying: edits its own source code. – Justin – 2013-12-31T00:57:23.180
@Quincunx Sounds like a fine idea. – Paul – 2013-12-31T09:13:48.230
Wow, I tried to do this in AWK. I gave up after I found out that AWK doesn't support reading the current program. https://gist.github.com/xfix/8201410 - I checked every single variable - nope.
– Konrad Borowski – 2013-12-31T19:49:39.447@GlitchMr My personal preference is to use awk for short data transformation scripts involving STDIN and STDOUT that are trivial enough not to need perl or python. I can't imagine anyone in their right mind writing a serious application in awk for production use that needs to open/close various files or goes beyond minor data munging or calculation in a pipe. – Paul – 2014-01-01T09:24:39.727
@Paul: I agree, but it was somewhat annoying to find out that after writing everything else in the program. – Konrad Borowski – 2014-01-01T09:41:03.703
@GlitchMr I found a way. – Paul – 2014-01-01T09:51:51.103
You'd need to dig out the pid with gawk's
PROCINFO["pid"]
and then use that (on Linux) to read the file/proc/(pid)/comm
, which has the filename (but not the full path) of the script in it. One could be lazy at this point and assume the script is in the current directory, or read some more PROCINFO and ENV vars and /proc filesystem entries until that is pinned down too. – Paul – 2014-01-01T09:52:55.983Hm, ok. That would require me to access
/proc
filesystem that doesn't have to exist (it doesn't exist on FreeBSD I use), but that could work. – Konrad Borowski – 2014-01-01T10:13:25.7101In PERL, special variable
$0
contains the full pathname to the running script. Just goes to show you shouldn't bring gawk to a perl fight. – Paul – 2014-01-01T10:19:27.1831What! You disallow Java eh! Well, I'm going to write a self modifying Java program, complete with compilation. Challenge accepted. – Justin – 2014-01-22T04:30:53.527
Question: Are we allowed to write our own CPU architecture to run this on, because I have the ISA/Specs. for a computer that I designed where self-modification of programming code is frequent/necessary for small code. Down with Harvard architectures, Up with Von Neumann Architectures! Long live the Princeton Architecture! So, I can't see a problem with it, as assembly is technically interpreted by the machine... – Stack Tracer – 2014-03-19T02:38:01.567