C# - 178 169 157 characters
This assumes that numbers like 999 are allowed to overflow to 000 and that -+,.E are not part of a number.
class T{static void Main(){var a="".ToCharArray();for(int b=1,c,i=a.Length;i-->0;b=48>c|c>57?7:b>0?c>56?a[i]='0':++a[i]*0:b)c=a[i];System.Console.Write(a);}}
Better readable form:
class T
{
static void Main()
{
var a="7teststring134this 123test string59 100".ToCharArray();
for (int b=3, c, i=a.Length; i-->0;
b=48>c|c>57
?7
:b>2
?c>56?a[i]='0':++a[i]*0
:b
) c=a[i];
System.Console.Write(a);
System.Console.ReadKey();
}
}
I'm new here, never tried code golf before, just gave it a try :)
I wonder if anyone has ideas to get it even shorter...
To participate with C# it would be nice if we could omit all the necessary framework around the actual code - then this would only have 82 chars, and that without calling any powerful system functions.
The same with pointers (182 chars):
class T
{
unsafe static void Main()
{
char[] a="7teststring134this 123test string59 100".ToCharArray();
int b=3;
fixed (char* s=&a[0])
for (var p=s+a.Length; p-->s; )
b=*p<48|*p>57
?7
:b>2
?*p>56?*p='0':++*p*0
:b;
System.Console.Write(a);
System.Console.ReadKey();
}
}
Now without overflowing, this correctly handles the 999 case (223 chars):
class T
{
static void Main()
{
var s=new System.Text.StringBuilder("9999teststring134this 123test string99 100");
for (int b=3, c, i=s.Length; i-->0; )
{
c=s[i];
b=48>c|c>57
?b>8?8:7
:b>2
?c>56?c-(s[i]='0'):++s[i]*0
:b;
if (b>8&i<1|b==8) s.Insert(i+9-b, '1');
}
System.Console.Write(s);
System.Console.ReadKey();
}
}
Another different older one, it reads from standard input and uses recursion:
namespace System {
using C=Console;
class T {
class t {
byte b=1;
string s="";
void R() {
var c=C.Read();
if (c>31) {
R();
if (48>c|c>57) b=1;
else if (b==1) c=c==57?48:++c*b--;
s=(char)c+s;
}
}
public t() {
R();
C.Write(s);
}
}
static void Main() {
new t();
C.ReadKey();
}
}
}
Note: Console.ReadKey();
and the string itself should not be counted.
I improved this already multiple times, see comments. There is still room for more improvements, I would say :)
And sorry for the length, but I think the different versions are interesting enough to keep them...
5
Fun fact: this can be done with 3 pure regex substitutions (no callbacks) http://stackoverflow.com/questions/12941362/is-it-possible-to-increment-numbers-using-regex-substitution/12942634#12942634 (that wouldn't be the golfiest way though)
– Martin Ender – 2014-09-21T11:19:40.6034You specified input but not output. From your input spec I assume both STDOUT and and return value are fine. But can we also store the result in a hardcoded variable (just as we can take input from it)? – Martin Ender – 2014-09-21T11:23:32.207
Your code shouldn't work... Don't you need to declare
NUMBERS
as a global variable insideincrement(s)
or is it different in Python 2? – Beta Decay – 2014-09-21T14:44:21.1171What about carrying? What happens to 999? – fluffy – 2014-09-21T17:13:03.820
3
possible duplicate of Multiply all numbers in a string
– Digital Trauma – 2014-09-21T18:52:00.750Shucks, when I saw the title I thought this was about implementing inflationary language.
– Mike Mertsock – 2014-09-21T18:59:47.6907What about negative numbers? What about numbers with a decimal point? What about numbers with a decimal point and nothing before it (except perhaps for a minus sign)? – Peter Taylor – 2014-09-21T20:45:54.780
@BetaDecay It works fine in Python 2 and 3, you can read variables from outer scopes. – flornquake – 2014-09-23T10:25:54.613
What about numbers with leading zeros?
"A0000"
->"A1"
?"A0001"
? – Kobi – 2014-09-23T12:02:04.237@BetaDecay You need to put an explicit declaration if you want to assign to outer scopes. – Bakuriu – 2014-09-23T16:44:52.233