Page 1 of 7

Valuation

Posted: Fri Nov 14, 2008 3:31 pm
by Andr3w
I tried or better did it with javascript ...

i got really frustrated by this -normally easy- stuff ... costs me 3 hours.

Posted: Wed Nov 26, 2008 12:40 am
by Marv
Ruby is cool:

Code: Select all

string = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'

value = 0
xx = 0
while string.include?('x')
  i = string.index('x')
  xx += string[i-2,1].to_i + string[i-1,1].to_i
  string.sub!('x','')
end

string.each_byte do |s|
  value += s.chr.to_i
end

p value + xx

Did it with perl

Posted: Wed Dec 24, 2008 9:08 pm
by rebelstest
$s='93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx';
while (1)
{
$l= substr($s,$i,1);
last if ($l eq "");
if ($l eq "x")
{ $all=$all+($last+$lastbefore); }
else
{ $all= $all + $l;
$lastbefore=$last;
$last=$l;
}
$i++;
}
print "$all";

Posted: Wed Dec 24, 2008 9:15 pm
by lukas
done it by hand ;) ... 15 minutes

Posted: Thu Dec 25, 2008 12:28 pm
by m!nus
took me 3 approaches before i had all logical errors out. counting principle is the easiest

Code: Select all

<?php

$input = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx';

$num_appear = array(	0 => 0,
						1 => 0,
						2 => 0,
						3 => 0,
						4 => 0,
						5 => 0,
						6 => 0,
						7 => 0,
						8 => 0,
						9 => 0
					);
$curr = 0;
for($i = 0; $i < strlen($input); $i++)
{
	if($input[$i] == 'x')
	{
		$num_appear[$last]++;
		$num_appear[$curr]++;
	}
	else
	{
		$last = $curr;
		$curr = $input[$i];
		$num_appear[$curr]++;
	}
}

$sum = 0;
for($x = 1; $x < 10; $x++)
	$sum += $x * $num_appear[$x];

echo 'The resulting sum is: '.$sum;

?>

Posted: Sat Dec 27, 2008 10:07 am
by Belriel

Code: Select all

<?php
$cipher = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
$sum  = 0;
for($j = 0; $j < strlen($cipher); $j++){
  $x = substr($cipher, $j, 1);
  if($x == "x") {
    $cipher = substr_replace($cipher, "", $j, 1);
    $j -= 3;
  }
  else $sum += $x;
  echo "$x\n";
}
echo "$sum";
?>

Posted: Sat Jan 03, 2009 9:34 am
by ziffi
Javascript for me too :-)

Code: Select all

<script type="text/javascript">
var wort = "93752xxx746...";
var erg = 0;
var tmpwort="";
for(i=0;i<wort.length;i++){
	if(wort.charAt(i)=='x'){
		tmpwort = wort.slice(0,i);
		tmpwort = tmpwort + wort.slice(i+1);
		wort = tmpwort;
		i = i -3;	
	}
	else {
	erg = erg + parseInt(wort.charAt(i));
	}
}
document.write("<p style=\"color:red\">" + erg + "</p>");
</script>
[/code]

VBA of excel

Posted: Tue Jan 06, 2009 2:21 pm
by macdachs
VBA works also fine:


Sub summier()
Dim wert1 As String
Dim wert2 As String
Dim wert3 As String

Dim erg As Long
zahl = 1
wert1 = Workbooks("Mappe1.xls").Worksheets("Tabelle1").Cells(1, 1).Value
laenge = Len(wert1)


Do
akt = Mid(wert1, zahl, 1)
If akt = "x" Then
wert2 = Mid(wert1, 1, zahl - 1)
wert3 = Mid(wert1, zahl + 1, laenge - zahl)
laenge = laenge - 1
zahl = zahl - 3
wert1 = wert2 + wert3
End If

If akt = "L" Then MsgBox (erg)

erg = erg + Val(akt)
zahl = zahl + 1

Loop Until akt = "L"

End Sub




... 5 minutes to work

c++ - ways to complicated!

Posted: Tue Jan 06, 2009 4:36 pm
by Mosher
Hi

I´m relatively new in the field of programming, but i tried to cope with the "Valuation Challange" via c++

I haven´t grown a custom of using elements of <strings> or streams yet, so tried to master it with
regular c-strings and pointers. Here´s my solution, but if i take a look at other people´s programmes, i get a little emberassed :D
There are some german comments, so please don´t feel irritated, if you don´t understand it

Code: Select all

include<iostream>

using namespace std;

int main()
{
  char text[] = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
  int summe = 0;
  unsigned int i = 0;

     while (text[i] != '\0')                    // String wird Zeichen für zeichen durchgegangen
     {
         
         char zahl[2];
         char *ptr = text;
         char *ptr2 = zahl;	
		 
		  if ((text[i] != 'x')) // hier sucht er Zeichen für Zeichen nach Zahlen
		  {                             
		              	 
			 *ptr2 = text[i];
			 summe = summe + atoi(zahl);        // und addiert sie zu der Summe
			 	
			 i++;
		  }
		  
		 else if (text[i] == 'x')            // Sucht String nach 'x' ab und
		  { 
			  int summe2 = 0;
			  int j = 0;
			  while(text[i] == 'x')          
		    {
			   			                     //und durchläuft j schleifen
			   j+=1;
			   
                          *ptr2 = text[i-j];
			   summe2 += atoi(zahl);
			   
			   if(text[i-(j+1)] != 'x')      //prüft, ob 2 plätze links vom 1. x, Zahl oder x ist
			   {
				   *ptr2 = text[i-(j+1)];
			       summe2 += atoi(zahl);
			   	   
			   }
			   else if (text[i-(j+1)] == 'x')
			   {
				   int k=1;
                                  while (text[i-(j+1)-k] == 'x')
				   {
					   k++;
				   }
				   *ptr2 = text[i-(j+1)-k];
				   summe2 += atoi(zahl);
			   }
			    i++;
               
			}
          			  
			cout <<" Gesamtsumme: " <<  summe2 << "\n";  //zur manuellen Nachkontrolle
			cout <<" Anzahl x: " <<  j << "\n";
			summe = summe + summe2;  
		  }  
	 }


 cout << summe << '\n';	 
 return 0 ;
 }

c++ - ways to complicated!

Posted: Tue Jan 06, 2009 4:38 pm
by Mosher
Hi

I´m relatively new in the field of programming, but i tried to cope with the "Valuation Challange" via c++

I haven´t grown a custom of using elements of <strings> or streams yet, so tried to master it with
regular c-strings and pointers. Here´s my solution, but if i take a look at other people´s programmes, i get a little emberassed :D
There are some german comments, so please don´t feel irritated, if you don´t understand it

Code: Select all

include<iostream>

using namespace std;

int main()
{
  char text[] = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
  int summe = 0;
  unsigned int i = 0;

     while (text[i] != '\0')                    // String wird Zeichen für zeichen durchgegangen
     {
         
         char zahl[2];
         char *ptr = text;
         char *ptr2 = zahl;	
		 
		  if ((text[i] != 'x')) // hier sucht er Zeichen für Zeichen nach Zahlen
		  {                             
		              	 
			 *ptr2 = text[i];
			 summe = summe + atoi(zahl);        // und addiert sie zu der Summe
			 	
			 i++;
		  }
		  
		 else if (text[i] == 'x')            // Sucht String nach 'x' ab und
		  { 
			  int summe2 = 0;
			  int j = 0;
			  while(text[i] == 'x')          
		    {
			   			                     //und durchläuft j schleifen
			   j+=1;
			   
                          *ptr2 = text[i-j];
			   summe2 += atoi(zahl);
			   
			   if(text[i-(j+1)] != 'x')      //prüft, ob 2 plätze links vom 1. x, Zahl oder x ist
			   {
				   *ptr2 = text[i-(j+1)];
			       summe2 += atoi(zahl);
			   	   
			   }
			   else if (text[i-(j+1)] == 'x')
			   {
				   int k=1;
                                  while (text[i-(j+1)-k] == 'x')
				   {
					   k++;
				   }
				   *ptr2 = text[i-(j+1)-k];
				   summe2 += atoi(zahl);
			   }
			    i++;
               
			}
          			  
			cout <<" Gesamtsumme: " <<  summe2 << "\n";  //zur manuellen Nachkontrolle
			cout <<" Anzahl x: " <<  j << "\n";
			summe = summe + summe2;  
		  }  
	 }


 cout << summe << '\n';	 
 return 0 ;
 }

Posted: Thu Feb 12, 2009 8:29 am
by rajahten
Wow. Some code-examples here seem to be kinda ... bloated :o

Its so easy with Java (and nice to read as well :D)

Code: Select all

public class Valuation {
	
	public int calculate(String s){
		StringBuffer sb = new StringBuffer(s);
		int result=0, pos=0;
		
		while(pos < sb.length()){
			if(sb.charAt(pos) != 'x'){
				result += Integer.parseInt(sb.substring(pos, pos+1));
				pos++;
			}
			else {
				sb.deleteCharAt(pos);
				pos-=2;
			}
		}	
		return result;
	}
	
	public static void main(String[] args) {
		Valuation v = new Valuation(); System.out.println(v.calculate("93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx"));
	}
}

Posted: Thu Feb 12, 2009 9:30 am
by aurora
my php solver:

Code: Select all

#!/usr/bin/env php
<?php

$a = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx';

$sum = 0;
$idx = 0;

do {
    if ($a[$idx] == 'x') {
        $a = substr_replace($a, '', $idx, 1);
        $idx -= 2;
    } else {
        $sum += (int)$a[$idx++];
    }
} while ($idx < strlen($a));

print "$sum\n";

?>

Posted: Fri Mar 06, 2009 12:02 am
by nomen
Delphi (no kidding, people use that! no error checking either):

Code: Select all

function Challenge153(Input: string): Integer;
var s: string; p: Integer;
begin
  s := Input; Result := 0; p := 1;
  while (p <= Length(s)) do
  begin
    if s[p] in ['0'..'9'] then begin
      Inc(Result, Ord(s[p])-48);
    end else if s[p] = 'x' then begin
      s := Copy(s, 1, p-1)+Copy(s, p+1, Length(s));
      Dec(p, 3);
    end;
    Inc(p);
  end;
end;
JS in convenient bookmarklet-form:

Code: Select all

javascript:
function a(s) {
  s = s.replace(/[^0-9x]/g, '');
  var p = 0, x = 0, c;
  while (p < s.length) {
   c = s.charCodeAt(p);    
   if (c == 120) {
      s = s.slice(0, p) + s.slice(p+1);
      p -= 3;
      if (p < 0) { p = 0; }
    } else if (c > 48 && c < 58) {
      x += c-48;
    }
    p++;
  }
  return x;
}
alert(a(window.getSelection().toString()));

Posted: Sun Mar 08, 2009 11:58 am
by Neelix2000
According to the fact, that this is a typical programming task, I tried manage it without writing code...it took me 10 minutes and I hit the correct result with the first try. It was a good concentration exercise :)

Did it with Delphi^^

Posted: Tue Mar 24, 2009 10:59 pm
by 3.11
muhahahaha 10 min coding with delphi :D

Code: Select all

procedure delx;
var i:integer; st1:tstringlist;
begin
 st1:=tstringlist.Create;
 for i:=1 to length(form1.memo1.Text) do
  begin
   if lowercase(form1.memo1.Text[i])='x' then
    begin
     form1.memo1.Text:=copy(form1.memo1.text,1,i-1)+copy(form1.memo1.text,i-2,2)+copy(form1.memo1.text,i+1,length(form1.Memo1.Text));
    end;
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var i,count:integer; st1:tstringlist;
begin
 while stringcount(memo1.Text,'x')>0 do delx;
 memo1.Text:=stringreplace(memo1.Text,#13#10,'',[rfreplaceall]);
  for i:=1 to length(memo1.Text) do
  begin
   count:=count+strtointdef(memo1.Text[i],0);
  end;
 showmessage(inttostr(count));
end;
greez