卡塔答案与测试答案相似,但测试失败

时间:2020-01-02 14:32:01

标签: javascript

我正在Codewars上制作kata圣诞树:https://www.codewars.com/kata/christmas-tree/train/javascript

即使我的结果和测试结果相似,测试仍会失败。有人可以帮我理解吗?

非常感谢。

    function christmasTree(height) {
      let tree = "";
      let leaf = "*";
      let leafNumber = 3;
      let space = "\xa0";
      let spaceNumber = 0;
      for (var i = 1; i <= height; i++) {
        if (i === 1) {
          spaceNumber = height - 1;
          tree =
            space.repeat(spaceNumber) + leaf + space.repeat(spaceNumber);
          spaceNumber--;
        } else {
          tree +=
            "\n" +
            space.repeat(spaceNumber) +
            leaf.repeat(leafNumber) +
            space.repeat(spaceNumber);
          leafNumber += 2;
          spaceNumber--;
        }
      }
      return tree;
    }

console.log(christmasTree(2));

2 个答案:

答案 0 :(得分:0)

您的测试失败,因为您使用的是不间断空格。 Codewars上的错误代码显示为:

预期:" * \\n***",而不是:" * \\n***"

您可以改用实际空间:

let space = " ";

enter image description here

编辑:"\xa0" == " "false

答案 1 :(得分:0)

此解决方案与我合作。

最好将问题划分为多个功能,以使其更易于阅读和解决。

from xhtml2pdf import pisa             # import python module
from PyPDF2 import PdfFileWriter, PdfFileReader
import string
import random

# Utility function
def convertHtmlToPdf(sourceHtml, outputFilename):
    # open output file for writing (truncated binary)
    resultFile = open(outputFilename, "w+b")

    # convert HTML to PDF
    pisaStatus = pisa.CreatePDF(
            sourceHtml,                # the HTML to convert
            dest=resultFile)           # file handle to recieve result

    # close output file
    resultFile.close()                 # close output file

    # return True on success and False on errors
    return pisaStatus.err

def add_encryption(input_pdf, output_pdf, password):
    pdf_writer = PdfFileWriter()
    pdf_reader = PdfFileReader(input_pdf)

    for page in range(pdf_reader.getNumPages()):
        pdf_writer.addPage(pdf_reader.getPage(page))

    pdf_writer.encrypt(user_pwd=password, owner_pwd=None,
                   use_128bit=True)

    with open(output_pdf, 'wb') as fh:
        pdf_writer.write(fh)

def random_char(y):
       return ''.join(random.choice(string.ascii_letters + string.digits) for x in range(y))