Valuation

Discussion of challenges you have already solved
Abinmorth
Posts: 4
Joined: Fri Jul 29, 2011 12:32 pm
Location: <u>test</u>

c++

Post by Abinmorth »

Code: Select all

#include <iostream>
#include <string>
using namespace std;

int main()
{
	char value;
	int sum=0;
	string str = ("93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx");
	for(unsigned int i = 0; i < str.length();i++){
	value = str[i];
	if (value == 'x'){
		str.erase(i,1);
		i-=3;
	}
	else{
		sum += (value - 48);
	}
	}
	cout << sum;
    return 0;
}
Taurath
Posts: 5
Joined: Mon Feb 06, 2012 7:15 pm

Post by Taurath »

In Ruby: I needed practice opening files so I made 2 of them, vsource (containing the challenge code) and vsource2 (containing the same but with x's removed).

Code: Select all

file = File.open("vsource", "r")
file2 = File.open("vsource2", "r")
count = 0
total = 0
line = file.gets
line2 = file2.gets

line.each_char { |i|

  if i == "x"
    back = (count - 2)
    total = line2[back].chr.to_i + total
    back = (count - 1)
    total = line2[back].chr.to_i + total
  else
    total = i.chr.to_i + total
    count = count + 1

  end
}
puts total
pedromalta
Posts: 22
Joined: Wed Apr 13, 2011 12:00 am
Location: Vila Velha
Contact:

Java is nice for Strings

Post by pedromalta »

Code: Select all

//Trust in the errors and get your answers!

public class Principal {
	
	
	public static int ValuationChallenge(String string){
		int sum = 0, pivot = 0;
		String algarismo;
		while (true){
			try{
				algarismo = string.substring(pivot, pivot+1);
				sum += Integer.parseInt(algarismo);
				pivot++;
				
			}catch (StringIndexOutOfBoundsException e) {
				return sum;
			}catch (NumberFormatException e) {
				string = string.substring(0, pivot) + string.substring(pivot+1);
				pivot -= 2;				
			}
			
		}
	}

	public static void main(String[] args) {
		
		System.out.println(ValuationChallenge("93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx"));
	}

}
sudo apt-get a life
Sunden
Posts: 1
Joined: Thu Jun 21, 2012 4:35 am
Location: Fort Collins

Used Python

Post by Sunden »

Used python. Took four hours (I'm new to python). Probably should of just done it by hand.

Code: Select all

 mylist =[9,3,7,5,2,'x','x','x',7,4,6,'x',2,7,'x',1,7,5,4,'x','x',9,0,'x',9,3,'x','x','x','x','x',2,3,8,'x',4,4,'x',7,5,'x','x',0,8,7,5,0,9,1,2,7,3,8,'x',8,4,6,1,'x',8,7,5,9,3,8,3,'x','x',3,2,8,'x',4,'x',4,9,3,5,9,0,3,'x',6,'x',5,5,5,0,3,6,0,5,3,5,0,0,4,'x',0,'x','x',9,4,5,9,5,8,9,6,1,2,9,6,'x',2,6,7,'x',8,8,4,2,'x','x','x',5,'x',6,'x','x',6,1,'x',4,'x',4,8,4,8,2,'x',8,0,'x','x','x',8,3,3,1,6,8,4,3,'x',7,'x',4,'x',8,3,'x',9,5,2,1,7,3,1,'x','x','x',2,5,'x',5,1,'x','x',4,5,7,'x',6,'x',5,'x',9,6,9,8,2,2,2,'x',7,7,1,2,3,7,7,4,5,0,3,4,'x',5,1,3,3,5,9,2,'x',2,7,'x','x',8,'x',8,7,'x','x',3,5,2,2,1,'x',3,6,'x',0,'x',5,0,'x',2,3,'x',7,'x',6,3,'x',9,9,8,4,1,8,'x','x'] 
position = 0
lenposition = len(mylist)
total = 0
while position < lenposition - 1:
    if mylist[position] == 'x':
        mylist.remove('x')
        position = position - 2
    else:
        total = total + mylist[position]
        position = position + 1

print total 

  
~Sunden~
voidptr
Posts: 1
Joined: Tue Jun 05, 2012 1:33 pm

Simple in C

Post by voidptr »

I wrote a little command line utility in C:

Code: Select all

#include <stdio.h>

int
main(int argc, char *argv[])
{
    int x, y, n; 
    char *p;

    while (--argc) {
        for (p = *++argv, x=y=n=0; *p; p++)
            if (*p == 'x')
                n += x+y;
            else {
                x = y;
                y = *p-'0';
                n += y;
            }

        printf("%s = %d\n", *argv, n);
    }
    
    return 0;
}
Just pass the desired code(s) on the command line.
User avatar
chaoscrusade
Posts: 6
Joined: Thu Dec 23, 2010 11:55 am

Using Python

Post by chaoscrusade »

Wow Sunden this took you quite a long time

I coded this example in 20 minutes(with debugging i'm not a good programmer :D )

Code: Select all

string='93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4
x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx8331684
3x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36
x0x50x23x7x63x998418xx'
count=0
i=0
while i!= len(string)-1:
    if string[i]=='x':
        i-=2
        string=string.replace('x','',1)
    else:
        count+=int(string[i])
    i+=1
    print string[i]
teh1337z
Posts: 1
Joined: Thu Sep 13, 2012 5:55 pm

Quick Python code

Post by teh1337z »

Took about 15 minutes in Python.

Code: Select all

number = 0
x = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x
4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xx
x83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x
27xx8x87xx35221x36x0x50x23x7x63x998418xx'
count = 0

while (count <= (len(x)-1)):
    if (x[count] == 'x'):
        x = x[:(count)] + x[(count+1):]
        count -= 3
        number -= int(x[count])
    number += int(x[count])
    count += 1
print number
fdln
Posts: 1
Joined: Fri Feb 22, 2013 4:24 pm

Post by fdln »

I did it with Java that (nooby) way:

Code: Select all

		StringBuilder sb = new StringBuilder("93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx");
		int pointer = 0;
		int sum = 0;
		while(pointer <= sb.length()-1) {
			if(sb.charAt(pointer) == 'x') {
				sb.deleteCharAt(pointer);
				pointer-=2;
				if(pointer < 0) pointer = 0;
			} else if(Character.isDigit(sb.charAt(pointer))) {
				sum += sb.charAt(pointer)-48; // How to get the *real* value out of a char?!?!
				pointer++;
			}
		System.out.println(sum);
		}
funny
Posts: 1
Joined: Thu Mar 07, 2013 10:11 pm

C Code

Post by funny »

Coded it fast in C
String handling was a bit tricky but the rest was then pretty easy

Code: Select all

#include <stdio.h>
#include <string.h>
int main() 
{
  char str[] = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
  int i;
  unsigned long res = 0;
  for(i = 0; i < strlen(str); i++) {
    if(str[i] == 'x') {
      memmove( &str[i], &str[i+1], strlen(str) - i);
      i -= 3;
      continue;
    }
    res += (str[i] -48);
  }
  printf("%s\n", str);
  printf("The result is: %ld\n", res);
}
_matmat_
Posts: 2
Joined: Sat Aug 02, 2008 4:55 pm

Post by _matmat_ »

I originally had

Code: Select all

if ($char == 'x') 
which is not the same as

Code: Select all

if ($char eq 'x')
Full code:

Code: Select all

$sum = 0;
$value= "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
#$value="120";

print "value " . $value . "\n";


$pos = 0;


while ($pos != length($value)) {
	$char = substr($value, $pos, 1);

	if ($char eq 'x') {
		print "c=x" . $char . " pos=" . $pos . " sum=" . $sum . "\n";
		
		$value = substr($value, 0, $pos) . substr($value, $pos+1, length($value));
		$pos -= 2;
		print "new value = " . $value . "\n";
	} else {
		$sum += $char;
		print "c=" . $char . " pos=" . $pos . " sum=" . $sum . "\n";
		$pos ++;
	}
}

print "sum " . $sum . "\n";
Toriath
Posts: 1
Joined: Wed Aug 07, 2013 8:35 am

Post by Toriath »

I did it with Java. Unexpierienced as I am I didn't use the StringBuffer instead of a String wich made it a little more complicated.

Code: Select all

public class Main {

    public static void main(String[] args) {

        String text ="93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x87593" +
                        "83xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x" +
                        "4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x7712377" +
                        "45034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
        int sum = 0;
        for (int i = 0; i < text.length();i++) {
           if (text.charAt(i) == 'x') {
               char[] chars = text.toCharArray();
               text = "";
               for (int x = 0; x < chars.length; x++) {
                   if (x == i) {

                   } else {
                       text += chars[x];
                   }
               }
               i -= 3; //I really don't know why I had to put a 3 here instead of a 2.
           } else {
               sum += (Integer.parseInt(text.substring(i,i+1)));
           }
            System.out.println(sum);
            System.out.println(text);
        }
    }
}
User avatar
Grusewolf
Posts: 16
Joined: Sun May 29, 2011 7:58 pm
Location: Munich

did it in groovy

Post by Grusewolf »

Code: Select all

String value = '93752xxx746...'

def index = 0
def sum = 0
while(value.size() > index) {
    def letter = value[index] 
    switch(letter) {
        case ~/[0-9]/:
            sum += Integer.parseInt(letter)
            break
            
        case 'x':
            value = value[0..<index] + value[(index+1)..<value.size()]
            index-=3
            break
    }
    index++
}
println sum
LStrike
Posts: 3
Joined: Tue Nov 18, 2008 7:42 pm

Post by LStrike »

I solved it with java:

Code: Select all

public static void challenge_Valuation(){
//		String input = "123x456";
		String input = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
		Integer result = 0;
		
		for (int i = 0; i < input.length();){
			String tmp = String.valueOf(input.charAt(i));
			if (tmp.equals("x")){
				System.out.println("X");
				input = input.substring(0, i) + input.substring(i+1, input.length());
				i = i - 2;
			}else {
				Integer intTmp = Integer.valueOf(tmp);
				result = result + intTmp;
				i++;
			}
		}
		
		System.out.println("Result: " + result);
	}
User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

This seems like a perfectly good excuse to play with asm/masm32 again:

Code: Select all

.386
.MODEL flat, stdcall
OPTION  CaseMap:None

include     \masm32\macros\macros.asm
include     \masm32\include\masm32.inc
includelib  \masm32\lib\masm32.lib

include     \masm32\include\kernel32.inc
includelib  \masm32\lib\kernel32.lib
.DATA
    InputString db "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x87593"
                db "83xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6"
                db "xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x96982"
                db "22x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx"
                db 0
.CODE

Start:
    xor eax, eax            ; al gets the input characters
    xor ebx, ebx            ; bl = last digit, bh = penultimate digit
    xor ecx, ecx            ; ecx = count
    mov edx, 40h            ; dl has bit only set when x is encountered
    lea esi, InputString    ; esi = pointer to string
    cld                     ; make sure lodsb increments esi

main_loop:                  ; load *esi and branch as needed
    lodsb                   ; al = *esi
    test eax, eax           ; set flags for eax & eax test
    jz main_loop_end        ; go to end if zero
    sub eax, 30h            ; convert '0' to '9' to 0 to 9 and 'x' to 72
    test eax, edx           ; set flags for eax & 0x40 test
    jnz is_x                ; not zero only when eax = 'x'-0x30

is_digit:                   ; put digit into bx and add to count
    add ecx, eax            ; add the value of the digit to the counter
    mov bh, bl              ; move last value to penultimate value
    mov bl, al              ; move current value to last value
    jmp main_loop           ; go back to main loop

is_x:
    mov eax, ebx            ; move the two last values into eax
    add al, ah              ; sum up the two last values into al
    movzx eax, al           ; zero extend al to clear rest of eax
    add ecx, eax            ; add the sum of the last two value to counter
    jmp main_loop           ; go back to main loop

main_loop_end:
    print str$(ecx)         ; print the counter the lazieh way
    exit                    ; TADONE!
    
END Start


User avatar
CodeX
Posts: 350
Joined: Fri Oct 17, 2008 5:28 pm

Post by CodeX »

And here is a backwards Python solution that makes things nice and simple :3

Code: Select all

s = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx"
a = b = 1
c = 0
for X in s[::-1]:
    if X == 'x':
        a += 1
        b += 1
    else:
        c += int(X) * a
        b,a = 1,b
print(c)
Post Reply