0

I'm trying to make a search function, but I'm not sure how I should implement this. I want the function to take the input string and find the string that is closest to the input and return it.

Table:

 {
    "Death": { "Value": 190000, "Demand": 4, "Trend": 1, "Tier": "S" },
    "Legendary Borul (Alternative)": { "Value": 125000, "Demand": 2, "Trend": 1, "Tier": "S" },
    "Club Beast": { "Value": 50000, "Demand": 6, "Trend": 1, "Tier": "S" },
    "Dark Wing": { "Value": 19800, "Demand": 6, "Trend": 1, "Tier": "S" },
    "Zaruto (GRR III)": { "Value": 18800, "Demand": 6, "Trend": 1, "Tier": "S" },
    "First Wood Bender": { "Value": 17000, "Demand": 8, "Trend": 1, "Tier": "S" },
    "Old Will": { "Value": 14500, "Demand": 5, "Trend": 1, "Tier": "S" },
    "Martial Artist": { "Value": 12000, "Demand": 3, "Trend": 6, "Tier": "S" },
    "Expert Sorcerer": { "Value": 11250, "Demand": 5, "Trend": 1, "Tier": "S" },
    "Zaruto (GRR II)": { "Value": 9600, "Demand": 8, "Trend": 7, "Tier": "S" },
    "Sandwhich Leader / Mecha Frieza": { "Value": 1050000, "Demand": 0, "Trend": 4, "Tier": "S" },
    "First Wood Bender (Sage)": { "Value": "N/A", "Demand": 0, "Trend": 4, "Tier": "S" }
}

Example:

search("legen"); // --> Output: 
    "Legendary Borul (Alternative)": { "Value": 125000, "Demand": 2, "Trend": 1, "Tier": "S" },
pygon
  • 3
  • 2

1 Answers1

0

Well your question is a bit vague, you have not cleared what type of match you want so for example from your example it looks a simple sub string match just in case you can try following way to search in your table

Method 1 (Simple)

import json

table = {
    "Death": {"Value": 190000, "Demand": 4, "Trend": 1, "Tier": "S"},
    "Legendary Borul (Alternative)": {"Value": 125000, "Demand": 2, "Trend": 1, "Tier": "S"},
    "Club Beast": {"Value": 50000, "Demand": 6, "Trend": 1, "Tier": "S"},
    "Dark Wing": {"Value": 19800, "Demand": 6, "Trend": 1, "Tier": "S"},
    "Zaruto (GRR III)": {"Value": 18800, "Demand": 6, "Trend": 1, "Tier": "S"},
    "First Wood Bender": {"Value": 17000, "Demand": 8, "Trend": 1, "Tier": "S"},
    "Old Will": {"Value": 14500, "Demand": 5, "Trend": 1, "Tier": "S"},
    "Martial Artist": {"Value": 12000, "Demand": 3, "Trend": 6, "Tier": "S"},
    "Expert Sorcerer": {"Value": 11250, "Demand": 5, "Trend": 1, "Tier": "S"},
    "Zaruto (GRR II)": {"Value": 9600, "Demand": 8, "Trend": 7, "Tier": "S"},
    "Sandwhich Leader / Mecha Frieza": {"Value": 1050000, "Demand": 0, "Trend": 4, "Tier": "S"},
    "First Wood Bender (Sage)": {"Value": "N/A", "Demand": 0, "Trend": 4, "Tier": "S"}
}


def Search(q):
    q = q.lower()
    for key in table.keys():
        if q in key.lower():
            return key + ": " + json.dumps(table[key])
    return False


print(Search('legen'))

Output:

Legendary Borul (Alternative): {"Value": 125000, "Demand": 2, "Trend": 1, "Tier": "S"}

Other Methods

You can try to search using semantics of the string for example test and best have same semantics, hence greater similarity index

This stack post highlights such methods e.g. hamming distance between two strings or smoothing functions for searching

Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29