The subject is poisonous but Codewars OJ System (II)

Source: Internet
Author: User
Tags arithmetic new set
Kata

<4kyu>guess the Digits and Expression
Description:
Give you a multiplication arithmetic expression:

ABC
            *   CBA
            -------
            = 39483

Each character represents a diffrent digit (1-9), and your need to find the arithmetic expression This: "123 * 321 = 39483" Solutions

This is my solution:

function Guessexpression (exp) {Let expression = ' Const REG =/\s* (\w+) \n\*\s* (\w+) \n.*\n[\d]* (\d+)/g const UNITARR = REG.EXEC (exp). Slice (1) const [Adigit, Bdigit] = Unitarr Const ADIGITLAST = adigit[adigit.length-1] Const BDIGIT  Last = bdigit[bdigit.length-1] Const DIGITLIST = [... new Set (adigit)] Const Numresult = parseint (unitarr[2)) const isodd = numresult% 2 = 1 function Veach (strs, stat = {}) {const FIRSTSTR = Strs[0] for (Let I of [1, 2, 3, 4, 5, 6, 7, 8, 9]) {if (expression) return stat[firststr] = i if (strs.length > 1) {Veach (s) Trs.slice (1), stat)} else {const REALADIGIT = [].map.call (adigit, v => stat[v]). Join (") const Realbdigit = [].map.call (bdigit, v => stat[v]). Join (') if (realadigit[0] && realbdigit[0] && (
          ~~realadigit * ~~realbdigit = = Numresult)) {expression = ' ${realadigit} * ${realbdigit} = ${numresult} ' if (realadigit.spliT ("). Reverse (). Join (") = = Realbdigit) {Const MIN = math.min (~~realadigit, ~~realbdigit) const 
      max = Math.max (~~realadigit, ~~realbdigit) expression = ' ${min} * ${max} = ${numresult} '}} }} Veach (Digitlist.join (')) return expression}

Filing times Error: Execution Time out 12000ms
It was later thought that if the product is odd, then the two multipliers are odd, and if the product is even, then the two multipliers must have an even number
So there are the following optimizations:

......
Const FIRSTSTR = strs[0] let
    maparr = []
    if (isodd) {
      if (firststr = = Adigitlast | | firststr = = bdigitlast) {
        Maparr = [1, 3, 5, 7, 9]
      } else {
        Maparr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
      }}
    else {
      if (firststr = = = Adigitlast | | Firststr = = bdigitlast) {
        if (adigitlast% 2 = 1 && firststr!== adigitlast) {
          Maparr = [2, 4, 6, 8]< c12/>} else if (bdigitlast% 2 = 1 && firststr!== bdigitlast) {
          Maparr = [2, 4, 6, 8]
        } else {
          m Aparr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        }}
      else {
        Maparr = [1, 2, 3, 4
    , 5, 6, 7, 8, 9]}} for (Let I of Maparr) {
      if (expression) return
    ...

However, there is no egg to use, the execution time is almost the same as the previous algorithm.
So, search for relevant answers on the web and find a solution for a big guy:

function Guessexpression (exp) {let
  m = Exp.match ([\w\d]+)/g);
  Let sym = [m[0], m[1]];
  Let res = +m[2];

  Let Uniq = (Sym[0] + sym[1]). Split (")
    . Filter (EL, I, arr) => Arr.indexof (el, i+1) = = 1);

  Let nums = new Array (uniq.length). Fill (1);

  Brute Force
  while (1) {let
    a = Sym[0].replace (/./g, C => Nums[uniq.indexof (c)]);
    Let B = Sym[1].replace (/./g, C => Nums[uniq.indexof (c)]);
    if ((+a) * (+b) = = Res) {return
      ' ${a} * ${b} = ${res} ';
    }
    for (Let i = 0; i < nums. length; ++i) {
      Nums[i] + = 1;
      if (Nums[i] >=) {
        nums[i] = 1;
      } else {break
        ;
      }

}}}

Using the Console.time test, the big guy's solution is almost half my solution.
Frustrating is: Codewars OJ with the solution of the big guy did not pass the test, I do not know how to optimize my solution. RELATED LINKS Train:guess the Digits and Expression | The solution to codewars Big Brother

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.