Ad
  • Custom User Avatar

    There is a simpler method. First, it is enough to create a list, although a dict can also be created using this method.
    You wrap what you copied from the site in quotes, and then work with the result as a string.
    With the help of regular expressions, you remove all unnecessary things - numbers, hyphenation - leaving only the names of the fruits and the spaces between them.
    And finally, you can use the usual replace, replacing spaces with quotation marks, commas, and spaces.

  • Default User Avatar

    This is a great use of Excel. I'm not quite sure why you populate column B with a : instead of just using ":" in the concat function, though. Since the trailing comma is fine, syntactically, and sometimes even desirable, I wouldn't have bothered with the IF statement at the end.

    Since I'm now critiquing it, I'll also point out that it only works (as written) for numeric keys and string values. (You could have colume D be key:value, E be "key":value and F be key:"value" and just pick the column you want, or you could get really fancy with ifs in the concat statement to allow a mix of types...)

    Anyway, all criticism aside, Excel is a fantastic tool for repetitive programming tasks and thanks for sharing your technique.

  • Default User Avatar

    When creating large dictionaries, I use Excel.

    Example:

    A1 = key, B1 = : , C1 = value

    Copy for each set (next set would be in row 2, then 3 and so on)

    D1 = CONCAT(A1,B1,CHAR(34),C1,CHAR(34),IF(D2="","",","))

    ---Hold the bottom right corner of D1 and drag down to Auto-Fill (in my example, drag to D5)

    ---result is key:"value" if the cell below is empty, otherwise it will be key:"value",

    ---CHAR(34) is for the quotes

    Code=CONCAT(A1,B1,CHAR(34),C1,CHAR(34),IF(D2="","",","))

    Example= key + : + " + value + " + (if cell below is empty: "" else ",") = key:"value",

    E1 = CONCAT("{",D1:D5,"}") #D5 would be my last row

    result = {key:"value",key:"value",key:"value"...}

    Keep the sheet and re-use for other dictionaries. Just update the D1:D5 range.

    Let me show you for this kata:

    A1 = 1, B1 = : C1 = "kiwi" and so on...

    result={1:"kiwi",2:"pear",3:"kiwi",4:"banana",5:"melon"}

  • Default User Avatar

    According to condition (1), this is the desired behavior. I added a sentence to (2) to make this clearer:

    "NOTE: When the array is both not square and the wrong size, (1) applies, so "Array not square" should be returned."

  • Default User Avatar

    If more than one of the above conditions occur, the function should return one of the appropriate messages.

    When the array is both not square and the wrong size, instead of accepting either message it seems to only accept

    "Array not square"

  • Custom User Avatar

    Use spoiler flag next time please. Your post was visible in the homepage.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    How did you create the dictionary, or did you just do it manually?

  • Default User Avatar
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Great, now it works :) I made some final adjustments to the description and naming, but now it is approved and published.

  • Default User Avatar
  • Default User Avatar

    Ah yes, I had intended to do this:

    count = 0
        random_ips = []
        while count < 50:
            net_addr =  random_ip()
            wc_mask =   random_ip()
            ipv4_addr = random_ip()
            if count % 2:
                if reference_solution(net_addr, wc_mask, ipv4_addr):
                    print(True)
                    random_ips.append((net_addr, wc_mask, ipv4_addr))
                    count += 1
            else:
                if not reference_solution(net_addr, wc_mask, ipv4_addr):
                    print(False)
                    random_ips.append((net_addr, wc_mask, ipv4_addr))
                    count += 1
        shuffle(random_ips)
    

    but having run it now see that while it works, it is prohibitively slow, I will limit the random positives to your suggestion and re-submit

  • Loading more items...