Valuation

Discussion of challenges you have already solved
psycore
Posts: 8
Joined: Wed Feb 18, 2009 5:40 am

Post by psycore »

Dude, why nobody told me there's a deleteCharAt(pos) command in java. Well, it was a good challange to get used to java again. My code:

Code: Select all

public class stringtest {

	public static void main(String[] args) {
		
		int sum = 0;
		  String code = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";

		  for(int i = 0; i<code.length(); i++) {

		   String help=String.valueOf(code.charAt(i));

		   if(help.equals("x")) {
		    code = code.substring(0,i) + code.substring(i+1);
		    i -= 3;
		   }

		   else {
		    sum += Integer.valueOf(help).intValue();
		   }

		  }
		 System.out.print(sum);
		
	}
}
Not perfect but I'm still learning.
Mad Hacker
Posts: 7
Joined: Tue May 19, 2009 12:02 am

Post by Mad Hacker »

python

Code: Select all

calcme = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'
total = 0
position = 0
strlen = len(calcme)

while position < strlen:
    if calcme[position] != 'x':
	total = total + int(calcme[position])
	position = position + 1
    elif calcme[position] == 'x':
	calcme = calcme.replace('x', '', 1)
	position = position - 2
    strlen = len(calcme)

print total
~~~~
note: i'm not necessarily a mad hacker, i'ts my screen name
Cobruto
Posts: 3
Joined: Sun Jun 28, 2009 8:26 pm

Post by Cobruto »

Code: Select all

<?php
$sum = 0;
$var = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
for($i = 0; $i <strlen($var); $i++) {
$tmp = substr($var, $i, 1);
	switch($tmp) {
		case '0': $sum = $sum + 0;	break;
		case '1': $sum = $sum + 1;	break;
		case '2': $sum = $sum + 2;	break;
		case '3': $sum = $sum + 3;	break;
		case '4': $sum = $sum + 4;	break;
		case '5': $sum = $sum + 5;	break;
		case '6': $sum = $sum + 6;	break;
		case '7': $sum = $sum + 7;	break;
		case '8': $sum = $sum + 8;	break;
		case '9': $sum = $sum + 9;	break;
		case 'x': $var = substr_replace($var, "", $i, 1);
			  $i = $i -3;	
			  
		break;  
	}

}
echo($var);
echo(" : ");
echo($sum); 

?>
echo($var) was to see if the x's were gone :p.

Cya,
Cob
Aldreyu
Posts: 3
Joined: Tue Jul 07, 2009 8:17 pm

Post by Aldreyu »

another java solution...

Code: Select all

public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>();
		
		// parse String and replace x with Integer.MIN_VALUE
		String s ="93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
		for (int i=0; i< s.length(); i++) {
			String sub = s.substring(i, i+1);
			if (sub.equals("x")) {
				list.add(Integer.MIN_VALUE);
			} else {
				list.add(Integer.valueOf(sub));
			}
		}
		
		int count = 0;
		for (int i = 0; i< list.size(); i++) {
			if (list.get(i)!= Integer.MIN_VALUE){
				count += list.get(i);
			} else {
				list.remove(i);
				i -=3;
			}
		}
		System.out.println(count);

	}
User avatar
livinskull
Posts: 22
Joined: Fri Jun 26, 2009 12:01 pm
Location: /dev/null
Contact:

Post by livinskull »

And another PHP... dunno why they all used switch for the numbers o.O

Code: Select all

<?php

$input = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx';

$pos = 0;
$result = 0;

while ($pos<strlen($input)) {
	if ($input[$pos] == 'x') {
		$input = substr_replace($input, '', $pos, 1);
		$pos = $pos - 2;
	} else {
		$result += $input[$pos];
		$pos++;
	}
}
echo $result;

?>
WingeDD
Posts: 1
Joined: Sun Jul 19, 2009 6:37 pm
Location: Vilnius, Lithuania
Contact:

Did it with java

Post by WingeDD »

It was easy, really.

Code: Select all

public final class QuickSolver {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		StringBuilder sumtext = new StringBuilder("93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx");
		int sum = 0;
		for (int i = 0; i < sumtext.length(); i++) {
			if (sumtext.charAt(i) == 'x') {
				sumtext.deleteCharAt(i);
				i -= 3;
			}
			else
				sum += Integer.parseInt(String.valueOf(sumtext.charAt(i)));
		}
		System.out.println(sum);
	}
}
---
pigasus
Posts: 1
Joined: Fri Oct 30, 2009 1:51 pm

javascript solution

Post by pigasus »

I'm extremely new to javascript but I'm really happy with what I came up with:

Code: Select all

<script type="text/javascript">
var str = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
var sum = 0;
xcount = str.match(/x/g).length;
for (i = 0; i < xcount; i++) {
   rep = str.charAt(str.indexOf("x") - 2) + str.charAt(str.indexOf("x") - 1);
   str = str.replace("x", rep);
}
for (i = 0;i < str.length; i++) {
   sum += +str.charAt(i);
}
document.write(sum);
</script>
xulfer
Posts: 5
Joined: Mon Nov 02, 2009 11:27 am

Post by xulfer »

About five minutes in C. I'm pretty happy with the efficiency at least.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>

int
main(int argc, char **argv) {
    int sum = 0;
    int digit[2];
    char dchar[2];
    char *rawptr;

    if (argc < 2) {
        fprintf(stderr, "Insufficient arguments given\n");
        exit(EXIT_SUCCESS);
    }

    rawptr = argv[1];
    dchar[1] = '\0';
    digit[0] = 0;
    digit[1] = 0;
    
    while (*rawptr != '\0') {
        if (*rawptr == 'x') {
            sum += digit[0];
            sum += digit[1];
            rawptr++;
            continue;      
        }
        dchar[0] = *rawptr;
        digit[1] = digit[0];
        digit[0] = atoi(dchar);
        sum += digit[0];
        rawptr++;
    }

    printf("Output: %d\n", sum);

    return 0;   
}
Triton456
Posts: 11
Joined: Sun Oct 25, 2009 5:01 pm
Location: Between Germany and England

Post by Triton456 »

done it by hand ... 15 minutes
Same :P

Greetz,
Triton456
Play a Windows CD backwards and hear satanic messages. That’s nothing, play it forwards and it installs Windows.

Music = Life

Same Shit - Different Day
User avatar
teebee
Posts: 89
Joined: Mon Nov 10, 2008 3:21 pm
Location: Germany

Post by teebee »

This Perl one liner does the job:

Code: Select all

perl -le '$_="93752xxx746x27x1754xx...";1while s/(..)x/$1$1/;$x+=$_ for/(.)/g;print$x'
chiffre
Posts: 7
Joined: Fri Nov 27, 2009 2:52 pm

Post by chiffre »

solved it in java, maybe anything else than good code or perfomant - but it works :D

also I think you could optimize the code very very much...

Code: Select all

String text = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
        ArrayList<Character> neu = new ArrayList<Character>();
        int i = 0;
        char j;

        for (int a = 0; a < text.length(); a++) {
            neu.add(text.charAt(a));
        }
        int a = 0;
        int b = neu.size();
        while (a < b) {
            if (neu.get(a) == 'x') {

                neu.remove(a);
                b--;
                a--;
                a--;
            } else {
                j = neu.get(a);
                i = i + Character.getNumericValue(j);
                a++;
            }


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

Post by CodeX »

A nice, quick and easy Python solution:

Code: Select all

v=i=0;s=list("93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx")
while i<len(s):
   if s[i].isdigit(): v += int(s[i]);i += 1;
   else: del s[i];i -= 2
print v
Not quite a single line of Perl but does the trick
gaunab
Posts: 1
Joined: Sat Nov 28, 2009 1:55 pm

Post by gaunab »

using Ruby is quite easy:

string="93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx"
sum,tb,ob = 0
string.each_char {|d|
if d!="x"
tb,ob = ob,d.to_i
sum += d.to_i
else
sum += tb+ob
end
}
puts sum
iliis
Posts: 2
Joined: Sun Oct 26, 2008 10:40 pm

Post by iliis »

haskell is imho quite nice too:

Code: Select all

calcValue x = calcValue' ("00"++x)
  where
    calcValue' (a:b:x:xs) =
	  if x == 'x'
		then (calcValue' (a:b:xs))+read([a])+read([b])
	  else
		(calcValue' (b:x:xs))+read([x])
    calcValue' _ = 0
I'm quite new to haskell, so this solution is probably far from perfect... ;)
(oh, and I'm indeed very creative about names XD)
User avatar
teebee
Posts: 89
Joined: Mon Nov 10, 2008 3:21 pm
Location: Germany

Post by teebee »

HVM (the special piece of text that represents the specific value is located in the first memory cells):

Code: Select all

000^<0^58*?68*-11^9::92+?0^3v+2v1+2cd3^3^+2v+1v1+2cddp
number of cycles: 9006
operand stack depth: 184
maximum operand stack depth: 190
call stack depth: 256
maximum call stack depth: 256

It's a fast hack, so it can surely be improved ...
Post Reply