Valuation

Discussion of challenges you have already solved
InyaGaming
Posts: 2
Joined: Fri Sep 12, 2014 8:53 am
Contact:

Post by InyaGaming »

PHP Solution for me:

Code: Select all

<?php

function reformatKeys($full){
	$return = array();
	foreach($full as $value){
		$return[] = $value;
	}
	return $return;
}

$full = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx';
$full = str_split($full);
$i = 1;
$sum = 0;

while($i <= count($full)){
	if($full[$i-1] == "x"){
		unset($full[$i-1]);
		$full = reformatKeys($full);
		$i -= 2;
	} else {
		$sum += $full[$i-1];
		$i++;
	}
}
echo $sum;
LStrike
Posts: 3
Joined: Tue Nov 18, 2008 7:42 pm

Post by LStrike »

Is this node a leaf, after solving there is no new challenge.....
AMindForeverVoyaging
Forum Admin
Posts: 496
Joined: Sat May 28, 2011 9:14 am
Location: Germany

Post by AMindForeverVoyaging »

LStrike wrote:Is this node a leaf, after solving there is no new challenge.....
Correct. Solving "Valuation" does not open up a new challenge.
Pommes
Posts: 1
Joined: Thu Apr 23, 2015 2:08 am

Post by Pommes »

Javascript ftw =)

took me a bit to learn the commands xD
im just a programmer newb

Code: Select all

var y = "93752xxx74............";
var text = 0;

for(i = 0; i < y.length; i++) {
    if (y[i] < 10) {
        text = text + parseFloat(y[i]);
    } else {
        y = y.replace(y[i],"");
        i = i-3
    }
}
alert(text);
manaregen
Posts: 2
Joined: Sat Jun 13, 2015 2:12 pm
Location: S. Korea
Contact:

Post by manaregen »

C solution

Code: Select all

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

int main() {
	char input[300] = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
	int sum = 0, length, i = 0;

	length = (int)strlen(input);
	while (1) {
		if (i == length) break;

		if (input[i] == 'x') {
			input[i] = '\0';
			{
				int count = 0;
				while (1) {

					if (input[--i] == '\0') continue;
					if (++count == 2) break;
				}
			}
		}
		else if (input[i] == '\0') i++;
		else {
			sum += input[i] - 0x30;
			i++;
		}
	}
	printf("%d\n", sum);

	return 0;
}
adark
Posts: 9
Joined: Fri Nov 20, 2015 2:04 pm
Contact:

Post by adark »

Simple enough.

Lua:

Code: Select all

local str = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'

local testStr = '123x456'

function getRes(str)
	local i = 1
	local total = 0
	while i <= #str do
		local s = str:sub(i,i)
		if s == 'x' then
			str = str:sub(1, i - 1) .. str:sub(i + 1)
			i = i - 2
		else
			total = total + tonumber(s)
			i = i + 1
		end
	end

	return total
end

print(getRes(testStr))
print(getRes(str))
btzxxyy
Posts: 2
Joined: Sat May 21, 2016 3:43 pm

with python

Post by btzxxyy »

It also costs me a lot of time...

Code: Select all

num = []
s = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'
change = list(s)



def func(change):
    n = 0
    while n < (len(change)):
        s = change[n]
        if s.isdigit():
            num.append(int(s))
            n += 1
        elif s == 'x':
            change.pop(n)
            num.append(int(change[n - 1]) + int(change[n - 2]))

    return(sum(num))


print(func(change))

User avatar
xiexun162534
Posts: 4
Joined: Fri Jul 26, 2013 1:26 pm
Contact:

Post by xiexun162534 »

It took me hours to get the answer

Code: Select all

#include <stdio.h>

int main ()
{
  int sum = 0;
  char string[] = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
  int i = 0, j;
  int x = 0;

  while (string[i] != '\0')
    {
      if (string[i] == 'x')
        {
          if (x == 0)
            {
              for (j = 1; j - x < 2 || string[i-j] == 'x'; j ++)
                if (string[i - j] == 'x')
                  x ++;
              x ++;
              i -= j;
            }
          else
            {
              x --;
              i ++;
            }
        }
      else
        {
        sum += string[i] - '0';
        i ++;
        }
    }
  printf ("%d\n", sum);

  return 0;
}
I'm boring.
User avatar
call
Posts: 1
Joined: Fri Nov 27, 2015 4:54 am
Contact:

Post by call »

I did it in an overly complicated way with javascript.

Code: Select all

var string = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'
var stringSplit = string.split('')
var total = 0

function add(index){
	console.log(total)
	if(stringSplit[index]!='x'){
		total = total + parseInt(stringSplit[index])
		if(index<=stringSplit.length){
			add(index+1)
		}
	}
	if(stringSplit[index]=='x'){
		stringSplit.splice(index,1)
		add(index-2)
	}
	if(index==stringSplit.length){
		console.log(total)
	}
}

add(0)
Seplik
Posts: 2
Joined: Wed Jan 25, 2012 5:34 pm

Post by Seplik »

Done in 10 minutes with C#:

Code: Select all

using System;

namespace Valuation
{
    class Program
    {
        static void Main(string[] args)
        {
            string cipher = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
            char readkey;
            int summe = 0;

            for (int i = 0; i < cipher.Length; i++)
            {
                readkey = Convert.ToChar( cipher.Substring(i, 1));

                if (readkey!='x')
                {
                    summe += readkey - '0'; 
                }
                else
                {
                    cipher = cipher.Remove(i, 1);
                    i -= 3;
                }
            }
            Console.WriteLine(summe.ToString());
            Console.ReadKey();
        }
    }
}
[/code]
sebek0990
Posts: 1
Joined: Sat Dec 26, 2015 8:07 pm

DONE

Post by sebek0990 »

bump after almost decade :D
c++

Code: Select all

//char(120) = x
//char(48)   = 0
#include <iostream>
#include <unistd.h>
#include <string>
using namespace std;
int main()
{
	string xxx = "93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx";
	int suma = 0;
	cout << "bazowa dlugosc: " << xxx.length() << endl;
	for(int i=0; i<=xxx.length()-1; i++)
		{
			if(xxx[i] != char(120))
			{
				suma = suma + xxx[i] - '0';
				cout << "suma: " << suma << " dlugosc: " << xxx.length() << endl;
			}
			else
			{
			xxx.erase(i,1);
			i = i-3;
			}
		};
	cout << suma;
	//test.erase(9,1);
	return 0;
}
:D
elmarko
Posts: 4
Joined: Sat Feb 25, 2017 8:31 pm

Post by elmarko »

Easy bit of python:

Code: Select all

string = "*snip*”
l = list(string)
position = 0
sum = 0
while (position < len(l)):
    if (l[position] == "x"):
        del(l[position])
        position -= 2
    else:
        sum += int(l[position])
        position += 1
print sum
teazy
Posts: 1
Joined: Thu Aug 01, 2019 7:04 pm

C# with recursion

Post by teazy »

Works as well in C# with recursion

Code: Select all

        private int Valuate(string text, int index)
        {
            if (index >= text.Length)
            {
                return 0;
            }

            if (text[index].ToString().Equals("x"))
            {
                text = text.Remove(index, 1);
                index = index - 2;
                if (index < 0)
                {
                    index = 0;
                }

                return 0 + Valuate(text, index);
            }
            else
            {
                int number = Convert.ToInt32(text[index].ToString());

                index++;
                return number + Valuate(text, index);
            }
        }
kevin1kevin1k
Posts: 1
Joined: Mon Aug 26, 2019 11:53 am

Python code

Post by kevin1kevin1k »

Code: Select all

s = '93752xxx746x27x1754xx90x93xxxxx238x44x75xx08750912738x8461x8759383xx328x4x4935903x6x5550360535004x0xx945958961296x267x8842xxx5x6xx61x4x48482x80xxx83316843x7x4x83x9521731xxx25x51xx457x6x5x9698222x771237745034x5133592x27xx8x87xx35221x36x0x50x23x7x63x998418xx'
r = []
i = 0
sum_ = 0
while i < len(s):
    if i not in r:
        if s[i].isdigit():
            sum_ += ord(s[i]) - ord('0')
        elif s[i] == 'x':
            r.append(i)
            m = 0
            while m < 2:
                i -= 1
                if s[i] != 'x':
                    m += 1
            continue
    i += 1
print(sum_)
Post Reply