hacker.org Forum Index
RegisterSearchFAQMemberlistUsergroupsLog in
Valuation - piece of code won't work

 
Reply to topic    hacker.org Forum Index » Challenges View previous topic
View next topic
Valuation - piece of code won't work
Author Message
matthias00



Joined: 03 Nov 2008
Posts: 3

Post Valuation - piece of code won't work Reply with quote
Hi there,

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

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 View user's profile Send private message
rmplpmpl



Joined: 26 Oct 2008
Posts: 113
Location: Germany

Post Reply with quote
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 View user's profile Send private message
matthias00



Joined: 03 Nov 2008
Posts: 3

Post Reply with quote
But adding zero to the sum shouldn't be problematic?
//okay, got it.
Sun Nov 16, 2008 5:10 pm View user's profile Send private message
m!nus



Joined: 28 Jul 2007
Posts: 202
Location: Germany

Post Reply with quote
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 View user's profile Send private message
nahnoe



Joined: 21 Feb 2010
Posts: 7

Post Valuation not working Reply with quote
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:
$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 View user's profile Send private message
tails



Joined: 10 Jun 2008
Posts: 191
Location: Tokyo

Post Reply with quote
You can try $x = "120" and see how the code works.
Tue Feb 23, 2010 9:53 pm View user's profile Send private message
nahnoe



Joined: 21 Feb 2010
Posts: 7

Post Reply with quote
tails wrote:
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 View user's profile Send private message
Display posts from previous:    
Reply to topic    hacker.org Forum Index » Challenges All times are GMT
Page 1 of 1

 
Jump to: 
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


Powered by phpBB © 2001, 2005 phpBB Group
Design by Freestyle XL / Flowers Online.