Page 2 of 7

Posted: Tue May 12, 2009 5:33 pm
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.

Posted: Wed May 20, 2009 9:47 pm
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

Posted: Sat Jul 04, 2009 4:39 pm
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

Posted: Sun Jul 12, 2009 4:10 pm
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);

	}

Posted: Sun Jul 12, 2009 6:45 pm
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;

?>

Did it with java

Posted: Mon Jul 20, 2009 8:36 am
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);
	}
}

javascript solution

Posted: Fri Oct 30, 2009 5:27 pm
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>

Posted: Mon Nov 02, 2009 10:57 pm
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;   
}

Posted: Tue Nov 03, 2009 1:27 pm
by Triton456
done it by hand ... 15 minutes
Same :P

Greetz,
Triton456

Posted: Fri Nov 06, 2009 7:56 pm
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'

Posted: Mon Nov 30, 2009 8:28 pm
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++;
            }


        }

Posted: Mon Nov 30, 2009 10:50 pm
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

Posted: Fri Dec 04, 2009 7:05 am
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

Posted: Thu Dec 10, 2009 11:17 pm
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)

Posted: Fri Dec 11, 2009 7:33 pm
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 ...