Valuation - piece of code won't work

Post Reply
matthias00
Posts: 3
Joined: Mon Nov 03, 2008 6:48 pm

Valuation - piece of code won't work

Post by matthias00 »

Hi there,

to solve 'Valuation', I've written a small piece of C#-code.

Code: Select all

    
static int recalc(int dx) {
	return dx - 48;
}

[...]

while(cnt < arrText.Length) {
	if (false == Char.IsDigit(arrText[cnt])) { // if 'x' occurs, set it to zero and move left
			arrText[cnt] = '0'; // write ASCII-'0' as char ('48' in dec)
			cnt -= 2; // go two steps left
					
	}
	
	int x = recalc((int)arrText[cnt]); // casting from char to int delivers ASCII-value in dec. To get the digit itself, recalc() subtracts 48 from each value.
	sum += x;
	cnt++;

}
arrText is an array consisting of 256 char-elements read from a text-file. This data seems to be correct. The output is everytime '1295', but it won't work.
Can someome give me a hint?

Thank you.
rmplpmpl
Posts: 113
Joined: Sun Oct 26, 2008 10:38 am
Location: Germany

Post by rmplpmpl »

Yepp, your error is, you are subsituting an X by zero on your way parsing the cipher. Try to find another solution
matthias00
Posts: 3
Joined: Mon Nov 03, 2008 6:48 pm

Post by matthias00 »

But adding zero to the sum shouldn't be problematic?
//okay, got it.
User avatar
m!nus
Posts: 202
Joined: Sat Jul 28, 2007 6:49 pm
Location: Germany

Post by m!nus »

that was my fault at first aswell. i ended up just doing it with counter variables and not changing the string because it's faster
nahnoe
Posts: 7
Joined: Sun Feb 21, 2010 10:40 pm

Valuation not working

Post by nahnoe »

Is this challenge broken? I can't understand why my solution won't work, I've tested it a few times with data that I manually checked and it works. It doesn't 'remove' the X's.. it just stores the last two numbers read then re-adds them if it hits an X:

Code: Select all

$x = "93752xxx....";

@s = split(//, $x);

$j1=0;
$j2=0;

for ( $i=0; $i<scalar @s; $i++ ) {
	if ($s[$i] == "x") {
		$c += $j1 + $j2;
	} else {
		$j2 = $j1;
		$j1 = $s[$i];
		$c += $s[$i];
	}
}

print $c;
I've checked 123x456 and got 26.
I've checked 123x456x789 and got 61.
I've checked 123xx456 and got 31.
I've checked 123xx456xxx789 and got 88.
I've checked 1234xx5x67x8xx and got 102.

All of which seem consistent with my understanding of the problem.

If someone can see something that I can't, or if this is too spoiler-ish please let me know.
tails
Posts: 191
Joined: Tue Jun 10, 2008 7:51 pm
Location: Tokyo

Post by tails »

You can try $x = "120" and see how the code works.
nahnoe
Posts: 7
Joined: Sun Feb 21, 2010 10:40 pm

Post by nahnoe »

tails wrote:You can try $x = "120" and see how the code works.
Haha! Can't believe I missed that!.. Cheers bud.
Post Reply