简体   繁体   中英

Fatal error: Uncaught Error: Class 'Func' not found php

My Questions is Find a 7 letter string of characters that contains only letters from

acegikoprs

such that the gen_hash(the_string) is

675217408078

if hash is defined by the following pseudo-code:

Int64 gen_hash (String s) {
    Int64 h = 7
    String letters = "acegikoprs"
    for(Int32 i = 0; i < s.length; i++) {
        h = (h * 37 + letters.indexOf(s[i]))
    }
    return h
}

For example, if we were trying to find the 7 letter string where gen_hash(the_string) was 677850704066, the answer would be "kppracg".

Solution
test1.php

I did that question solve in php, I am unable to run this code I am in php i don't have that much knowledge regarding php class and their function, Can any one solve this code and describe me. thanks in advance i will be very great full if anyone help me.

<?php

$set = "acdegilmnoprstuw";
$CONST_HASH = 7.0;
$CONST_MULT = 37.0;

$hash = new Func(function($string = null) use (&$CONST_HASH, &$CONST_MULT, &$set) {
  $_hash = $CONST_HASH;
  for ($i = 0.0; $i < get($string, "length"); $i++) {
    $_hash = _plus(to_number($_hash) * to_number($CONST_MULT), call_method($set, "indexOf", get($string, $i)));
  }
  return $_hash;
});

$decode = new Func(function($_hash = null) use (&$CONST_MULT, &$Math, &$set) {
  $decoded = ""; $positionsInSet = new Arr();
  for ($i = 0.0; $_hash > $CONST_MULT; $i++) {
    set($positionsInSet, $i, call_method($Math, "floor", (float)(to_number($_hash) % to_number($CONST_MULT))));
    $_hash /= 37.0;
  }
  for ($i = to_number(get($positionsInSet, "length")) - 1.0; $i >= 0.0; $i--) {
    $decoded = _plus($decoded, get($set, get($positionsInSet, $i)));
  }
  return $decoded;
});

I realize that this question was asked well over a year ago and I'm only just stumbling on it right now but seeing as there hasn't been an answer I figured I'd answer it.


You used a third party JavaScript to PHP converter utility and expected the demo to work out of the box. You should have read up on it's usage as it clearly states in the ReadMe.md that, ...this tool is using esprima JavaScript parser with rocambole to walk the AST and escope to figure out the variable scope, hoist function declarations and so on...

The developer goes on to say, After AST manipulation tools/codegen.js generates the PHP code by walking the tree. Now here's where it gets good. Pay attention now..

Various constructs get wrapped in helper functions, for instance, property access, method calls and + operator. The runtime helpers can be found in php/helper and there are a bunch of classes in php/classes for Array, RegExp and such. All this PHP gets packaged into your output file, or you can save it to a standalone runtime and reference that from your output file like so:

js2php --runtime-only > runtime.php
js2php --runtime runtime.php example.js > example.php

You can also specify the output file using -o or --out and you can compile multiple input files into one output file like so:

js2php -o example.php file1.js file2.js

So as you can see my friend, to get your code to work you merely need to include the runtime helpers functions so your converted script can be interpreted. I'm not sure as to which files it'll take to get your script to parse correctly, however now that I've pointed you in the right direction I'm confident you'll be able work it out yourself.

Happy coding.. =)

Instead of assigning the inline function with new operator to variable, create a separate functions encode and decode where you can do the hashing and matching the hash code.

Let me give you the snippet of how to do it. I am assuming that your using plain PHP.

/* Call to function encode which returns you the encoded value and store in $encode variable */

$encode = encode($par1, $par2);

/* Call to function decode which returns you the decoded value and store in $decode variable */

$decode = decode($par1, $par2);

/* Function to encode your code */
function encode($par1, $par2){
    return $encode_value
}

/* Function to decode your code */
function decode($par1, $par2){
    return $decode_value
}

I have written this in Javascript to generate both Hash String and Number

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>

var gen = gen_hash('kppracg');
document.write("kppracg hash value is ", gen);

document.write('<br><br>');

var gen = gen_hash('iorocks');
document.write("iorocks hash value is ", gen);


var hash = 675217408078;
var gen_rev = gen_hash_rev(hash);

document.write('<br><br>');

document.write("675217408078 hash value is ", gen_rev);


function gen_hash(s) {
    var h = 7;  
    var acegikoprs = "acegikoprs";
    for(var i = 0; i < s.length; i++) {
        h = (h * 37 + acegikoprs.indexOf(s[i]));
    }
    return h;
}


function gen_hash_rev(hash) {

    var arr_values = ['a','c','e','g','i','k','o','p','r','s'];
    var min = 0;
    var max  = 99999999;

        while (min <= max) {
            var final_result = "";
            var tempInt = parseInt(((max - min) / 2) + min);
            var arr_tempInt = num_array(tempInt);
            for(var i = 0; i < arr_tempInt.length; i++) {
                final_result = final_result + arr_values[arr_tempInt[i]];
            }
            var result = gen_hash(final_result);
            if(result < hash) {
                 min =  tempInt + 1;
            }
            else if( result >  hash) {
                 max =  tempInt - 1;
            }
            else {
                return final_result;
            }
        }
}

function num_array(num){

    var  output = [],
    sNumber = num.toString();

    for (var i = 0, len = sNumber.length; i < len; i += 1) {
        output.push(+sNumber.charAt(i));
    }
    return output;

}

</script>

</body>
</html>


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM