Author |
Message |
matthias00
Joined: 03 Nov 2008 Posts: 3
|
|
Valuation - piece of code won't work |
|
Hi there,
to solve 'Valuation', I've written a small piece of C#-code.
 |
 |
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.
|
|
Sun Nov 16, 2008 3:28 pm |
|
 |
rmplpmpl
Joined: 26 Oct 2008 Posts: 113 Location: Germany |
|
|
|
Yepp, your error is, you are subsituting an X by zero on your way parsing the cipher. Try to find another solution
|
|
Sun Nov 16, 2008 4:48 pm |
|
 |
matthias00
Joined: 03 Nov 2008 Posts: 3
|
|
|
|
But adding zero to the sum shouldn't be problematic?
//okay, got it.
|
|
Sun Nov 16, 2008 5:10 pm |
|
 |
m!nus

Joined: 28 Jul 2007 Posts: 202 Location: Germany |
|
|
|
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
|
|
Sun Nov 16, 2008 6:37 pm |
|
 |
nahnoe
Joined: 21 Feb 2010 Posts: 7
|
|
Valuation not working |
|
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:
 |
 |
$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.
|
|
Tue Feb 23, 2010 9:12 pm |
|
 |
tails
Joined: 10 Jun 2008 Posts: 191 Location: Tokyo |
|
|
|
You can try $x = "120" and see how the code works.
|
|
Tue Feb 23, 2010 9:53 pm |
|
 |
nahnoe
Joined: 21 Feb 2010 Posts: 7
|
|
|
|
 |
 |
You can try $x = "120" and see how the code works. |
Haha! Can't believe I missed that!.. Cheers bud.
|
|
Tue Feb 23, 2010 9:58 pm |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|