Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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
      }
}
share|improve this question
 
Removed code-challenge tag because it would require an objective winning criterion. –  Howard Dec 30 '13 at 12:27
 
Wait, rule 2 disallows the self-modify Befunge. :-( –  Quincunx Dec 30 '13 at 16:58
 
@Quincunx How would you modify the rule? –  Paul Dec 30 '13 at 20:04
 
@Paul I'd just say that the program must be self-modifying: edits its own source code. –  Quincunx Dec 31 '13 at 0:57
 
@Quincunx Sounds like a fine idea. –  Paul Dec 31 '13 at 9:13
show 7 more comments

4 Answers

Pseudocode

// Set up a cron job to run this script (only) once a day

yesterdays_stock_price = '46.48'

todays_stock_price = curl_google_finance('VTSMX')

print "Difference between yesterday's stock price and today's: "

print todays_stock_price

self = open('self.file')

self.replace( /yesterdays_stock_price = '[0-9\.]+'/, todays_stock_price)

self.save()
share|improve this answer
1  
Nice. I like how it doesn't work if you use it too much or at the wrong interval. –  Paul Dec 30 '13 at 20:03
 
Maybe it should have a wait and loop in it too so it can self run, I love it. –  alan2here Jan 2 at 1:37
add comment

gawk (1224 characters, as this is not code golf)

One of reasons why awk shouldn't be used for self-modifying scripts.

#!/usr/bin/gawk -f
function download(name,     url, service, host, old_rs, old_ors) {
    old_rs = RS
    old_ors = ORS
    RS = ORS = "\r\n"

    host = "download.finance.yahoo.com"
    url = "http://"host"/d/quotes.csv?s="name"&f=l1"
    service = "/inet/tcp/0/"host"/80"
    print "GET", url |& service
    service |& getline
    close(service)

    RS = old_rs
    ORS = old_ors

    return $0
}

function update(value,       file, self, result) {
    # Do complex stuff in order to find the program itself.
    file = "/proc/"PROCINFO["pid"]"/comm"
    if (getline < file <= 0) {
        print "/proc filesystem not supported." > "/dev/stderr"
        exit
    }
    close(file)
    if ($0 ~ /^g?awk$/) {
        print "You need to run program directly." > "/dev/stderr"
        exit
    }
    self = $0
    result = ""
    while (getline < self > 0) {
        # Avoid finding this line.
        if (/[y]esterday/) gsub(/[0-9]+(\.[0-9]+)?/, value)
        result = result $0"\n"
    }
    printf "%s", result > self
    close(self)
}

BEGIN {
    yesterday = 1120.71
    today = download("GOOG")
    update(today)
    print "Yesterday:", yesterday
    print "Today:", today
    print "Difference:", today - yesterday
}
share|improve this answer
 
Wow, an HTTP client in GAWK. –  Paul Jan 1 at 10:31
add comment

Using @jawns317 psueudocode:

BASH

#!/bin/bash
set -e;
google_price_last=1118.79;
current_google_price=`wget -qO- "http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=l1"|tr -d "\r\n"`;
echo "Current Price: $current_google_price";
echo "Last Price: $google_price_last";
echo "Price change: "`echo $current_google_price - $google_price_last | bc -l`;
sed -ri 's/last=[0-9\.]+\;/last='"$current_google_price"';/g' $0;
share|improve this answer
add comment

Back at uni I discovered (ah-hem) a system which I though would prove fool-proof at the local casino. Bet on red on the roulette wheel, and if you lost double the bet, and so on ...See Martingale System

VBS

Dim objFSO
Dim objTF
Dim strFile
Dim lngBet

lngBet = 5
strFile = "boss.txt"
'spin roulette wheel
Randomize

'18 blacks, 18 reds, 1 green    
intnumber = Int(36 * Rnd + 1)

'mofify code to double-down losing bets
Do While intnumber < 18 
intnumber = Int(36 * Rnd + 1)
bfound = True
Set objFSO = CreateObject("scripting.filesystemobject")
Set objTF = objFSO.createtextfile(strFile, 2)
objTF.writeline "lngbet=lngbet*2"
objTF.Close
Set objTF = objFSO.opentextfile(strFile, 1)
Execute objTF.ReadAll()
objTF.Close
Loop

If bfound Then objFSO.deletefile (strFile)    
wscript.echo "final bet was " & lngBet
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.