valuation

dorahan
Posts: 105
Joined: Tue May 27, 2008 10:36 am
Location: HkRkoz al KuwaiT 2019 HaCkEr 101

valuation

Post by dorahan »

anyone have solved the challenge please help me out... this challenge needs only a simple coding... i've tried it but there's a wrong algorithm in my coding..

Code: Select all

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()	{
   char soal[]= "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
   int a,i;

   a = 0;

   for(i=0;i<strlen(soal);i++) {
   	if(soal[i]='x') {
      	delete soal[i];    //will show error : "Operand of 'delete' must be non-const pointer"
         i = i - 2;
      }
      a = soal[i] + a;
   }

   printf("\n\ntotal = %d",a);

	getch();
}
i'm confuse in how to delete a character in a array string.. anyone can help me? thanks before.
~� dorahan �~
HkRkoz al KuwaiT 2019 HaCkEr 101
User avatar
m!nus
Posts: 202
Joined: Sat Jul 28, 2007 6:49 pm
Location: Germany

Post by m!nus »

you can't delete something out of an char array, not without rewriting the whole string.
i give you a hint: dont modify the string, makes it easier
planke
Posts: 3
Joined: Tue Jan 05, 2010 7:48 pm

Post by planke »

someone may PN me the answer? the test string is right, but at the important one i dont get the right answer :/
--------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char* x = malloc(257);
char* input = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
// char* input = "123x456";
strcpy(x, input);

char* intstr = malloc(2);
intstr[1] = '\0';
int i = 0;
int number = 0;

while( x != '\0')
{
if(x >= '0' && x <= '9')
{
intstr[0] = x;
number = number + atoi(intstr);
}
if(x == 'x')
{
x = 'y';
i = i - 3;
}
i++;
}
printf("%d\n", number);
return 0;
}
michuber
Posts: 57
Joined: Sun Oct 26, 2008 3:30 pm

Post by michuber »

You have to remove the 'x'. Test your program with input '123xx456'.
planke
Posts: 3
Joined: Tue Jan 05, 2010 7:48 pm

Post by planke »

uuh.. y i know what u mean.. thx =D
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

you need to remove the x characters as michuber said, this is because when you have x characters next to each other you end up missing off the numbers before it. On the trivial optimization side of things:
  • no harm will come from not using the strcpy() from input to x, you can use input on the spot as it won't get put into a read only part of the executable
  • you can save yourself counting the length of a string by declaring the string like char mystr[] = "CodeX" and then you can use sizeof(mystr/sizeof(mystr[0])) which will do the counting for you at compile time and will return 6 for the example, you can just use sizeof(mystr) and miss out the second part when working with char arrays as they are a single byte, so you would only divide by 1
  • you don't need the intstr or atoi, being ASCII you can simply subtract '0' or 0x30 from a digit to return its value
  • for speed it's better to use the stack (char array[10]) instead of the heap (char *array = malloc(10))
  • you can simply check for x < 'x' instead of x >= '0' && x <= '9'
  • use else where it's due, such as before the check for x=='x', that way the computer wont check for it when its impossible such as when you've already determined x is a digit
  • you can use valueA += valueB instead of valueA = valueA + valueB, I'm not sure if compilers take advantage of this to make faster code by default

Although the performance of your code isn't an issue these are handy things to consider and put into standard practice for when you have applications that are majorly computationally intensive :D

All you have to do now is figure out how to remove those x characters, hint: you only need 1 strcpy() call :)
XayOn
Posts: 15
Joined: Mon Jul 27, 2009 7:19 pm

Post by XayOn »

Mmmm, I use a different way to it, but doesnt work with the whole string (tough works well with the sample)

It's just... ok, if you get the lastest two digits stored, and when found a X, you sum them. It'll be the same.
I mean:
$_="123x456"; while (/(.)/g) { if ($1 != "x"){ $sum+=$1; $oldest=$old; $old=$1; } else { $sum+= $old + $oldest; }} print $sum

Or something like this, I love perl for this kind of things =D.
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

its not working because it doesn't work if you have two x characters next to each other so you have to remove them or work around them.
XayOn
Posts: 15
Joined: Mon Jul 27, 2009 7:19 pm

Post by XayOn »

Why? It just continues and adds the previous digit. I'll try to explain it:

123xx456 = 1+2+3+[2+3]+[2+3]+4+5+6
345xxx567 = 3+4+5+[4+5]+[4+5]+[4+5]+5+6+7

It's just another way of thinking on it. It's not just about code but thinking different approaches, i just concluded there won't make any difference that to:
345xxx567
3+4+5... x so we return to 4 and delete x
3+4+5+4+5... x so we return to 4 and delete x
3+4+5+4+5+4+5... x so we return to 4 and delete x
3+4+5+4+5+4+5+5+6+7
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

so much for my sneaky misdirection attempt... the problem occurs when you encounter a 0, your using a numerical comparison operator instead of a string one, "0" != "x" evaluates to false. A handy thing to do when you can't see what is going wrong is to place prints around the place to debug things by seeing what's occurring when and where
Masti6
Posts: 55
Joined: Sat May 15, 2010 12:04 pm
Location: Finland, Nurmes

Post by Masti6 »

I already posted my full code to another forum, but I'll post a simplified and shorter one here:

Code: Select all

#Valuation test #1
#x = ''
main = [1,2,3,'x','x',4,5,6]
counter = 0
lenght = len(main)
total = 0
while counter < lenght:
    if main[counter] == "'x'":
        main.remove(counter)
        counter = counter - 2
    else:
        total = total + main[counter]
        counter = counter + 1
if counter == lenght:
    print total
#123xx456
#1,2,3,'x','x',4,5,6
So far it had given an error:

Code: Select all

    main.remove(counter)
ValueError: list.remove(x): x not in list
But I fixed it by putting some extra "" -marks over the "'x'" in while.
But I STILL get an error, this time saying:

Code: Select all

    total = total + main[counter]
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Could anyone please troubleshoot above for me?
Python has a habit of giving confusing errors, and so far I've been told that I can't fuse string value to something something value, thus giving 'int'. But I still can't troubleshoot this one on my own no matter how long I try.
So once again, Thanks in Advance!
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

  1. The remove method removes all occurrences of what you tell it to remove so after the first call it won't have anything to delete plus your set will be messed up.
  2. TypeError: unsupported operand type(s) for +: 'int' and 'str' is quite simple, Python doesn't automatically change the character (a single part of a string) into a number automatically and it doesn't know what + means when you've got an int on one side and a string on the other, use the int() function to change a string into a number.
  3. When you get errors and their error messages mean nothing to you then go and search through documentation, aka RTFM.
Masti6
Posts: 55
Joined: Sat May 15, 2010 12:04 pm
Location: Finland, Nurmes

Post by Masti6 »

I remember getting no manual with the installation. And simply asking from someone who knows saves the trouble of searching for it with invalid keywords for many hours. (Which I've already done.)
Second of all, as I don't know where else to ask/search, I'll seek assistance to various problems in there forums. That's what the "Challenge" forum is usually for. And by judging from the previous topics and replies, I seem to be right on that one, too.
And the question is still open;
What could I do to make that thing work?
...
I've tried to use the int() variable in various places (mostly in while) but I still don't seem to get it right.
Plus, making a "string" into a "number" seems wrong. If it just makes every X into ... 0? then it'd count every 0 the wrong way etc.
Thanks for directions, 'tho. Much appreciated!
Karian
Posts: 75
Joined: Wed Jan 09, 2008 10:21 am

Post by Karian »

while these forums are indeed for asking help, we don't need to give you the solution. We can give hints and point you in the good direction.

These challenges in itself aren't there to be solved. The main reason you should have to do those challenges is to learn new things. You don't get this by being handed the solution.

For the programming challenges, this means also learning to program by yourself. For a lot of program languages, you can find lot's of information (including the manual) online. Use those to see what specific errors mean, learn a way how to debug your program. You can learn a lot of what goes wrong by just following your program step by step.
Masti6
Posts: 55
Joined: Sat May 15, 2010 12:04 pm
Location: Finland, Nurmes

Post by Masti6 »

I know that I'm supposed to learn along the way while solving the Challenges, but if I have no idea what or how to study, I must ask for help. So far the community has been friendly and helpful, which I am glad for.
But something like "Lol you're doing it wrong" ain't getting no one anywhere.
So one must request for additional help, or in more detail.
And with the help of my IRL friend I've finally understood what you were trying to say, but if it wasn't for him I'd still be completely clueless with my broken piece of code.
Thanks to him and all of you, again.
Post Reply