Programming challenge
Andy Bradford
amb-plug at bradfords.org
Fri Sep 28 15:32:23 MDT 2007
Thus said "Sasha Pachev" on Thu, 27 Sep 2007 15:44:45 MDT:
> Given a string that could contain arithmetic expressions, with the
> addition that numeric constants could be potentially expressed as
> times, e.g 1:36 for 96 seconds, or 2:10:08 for 2 hours 10 minutes
> and 8 seconds, also decimal fractions after seconds are allowed,
> e.g 3:45.6 or 3:40:50.67, replace all the time values with their
> equivalent number of seconds.
Ok, there was more left over so I might as well just send the entire
corrected version:
------------------------------------------------------------------------
#!/bin/sh
# \
exec tclsh "$0" ${1+"$@"}
while {[gets stdin line] >= 0} {
set j 0
while {[regexp -indices -start $j {(?x) \d+:\d+(?::\d+)?} $line match]} {
foreach {i j} $match {}
set s [split [string range $line $i $j] :]
set y [llength $s]
set z 0
for {set x 0} {$x < $y} {incr x} {
set z [expr $z * 60 + [scan [lindex $s $x] "%d"]]
}
set line [string replace $line $i $j $z]
incr j [expr [string length $z] + $i - $j + 1]
}
puts stdout $line
}
------------------------------------------------------------------------
More information about the PLUG
mailing list