Skip to content

Instantly share code, notes, and snippets.

@idfumg
Created September 9, 2024 06:51
Show Gist options
  • Save idfumg/28d474fd1181b804a49516ecb04eda00 to your computer and use it in GitHub Desktop.
Save idfumg/28d474fd1181b804a49516ecb04eda00 to your computer and use it in GitHub Desktop.
import collections
import functools
COMMANDS = {
"clone": lambda: print("clone processed"),
"init": lambda: print("init processed"),
"add": lambda: print("add processed"),
"mv": lambda: print("mv processed"),
"restore": lambda: print("restore processed"),
"rm": lambda: print("rm processed"),
"bisect": lambda: print("bisect processed"),
"diff": lambda: print("diff processed"),
"grep": lambda: print("grep processed"),
"log": lambda: print("log processed"),
"show": lambda: print("show processed"),
"status": lambda: print("status processed"),
"branch": lambda: print("branch processed"),
"commit": lambda: print("commit processed"),
"merge": lambda: print("merge processed"),
"rebase": lambda: print("rebase processed"),
"reset": lambda: print("reset processed"),
"switch": lambda: print("switch processed"),
"tag": lambda: print("tag processed"),
"fetch": lambda: print("fetch processed"),
"pull": lambda: print("pull processed"),
"push": lambda: print("push processed"),
}
def does_handler_exist(user_input):
return user_input in COMMANDS
def processing(query):
COMMANDS[query]()
def get_user_input():
return "pu"
def how_many_common_characters(word1, word2):
cnt1 = collections.defaultdict(int)
for ch in word1:
cnt1[ch] += 1
cnt2 = collections.defaultdict(int)
for ch in word2:
cnt2[ch] += 1
ans = 0
for ch in cnt1.keys():
if ch in cnt2.keys():
ans += min(cnt1[ch], cnt2[ch])
return ans
def filter_commands_by_common_characters(query):
ans = []
for command in COMMANDS:
count = how_many_common_characters(query, command)
if count > 0:
ans.append(command)
return ans
def get_levenshtein_distance(word1, word2):
M = len(word1)
N = len(word2)
@functools.cache
def run(m, n):
if m == -1 and n == -1: return 0
if m != -1 and n == -1: return m + 1
if m == -1 and n != -1: return n + 1
if word1[m] == word2[n]: return run(m - 1, n - 1)
return 1 + min(run(m - 1, n), run(m, n - 1), run(m - 1, n - 1))
return run(M - 1, N - 1)
def get_commands_with_levenshtein_distances(query, commands):
ans = []
for command in commands:
distance = get_levenshtein_distance(query, command)
ans.append((distance, command))
return ans
def get_the_best_commands(commands):
minimum = commands[0][0]
for command in commands:
minimum = min(minimum, command[0])
ans = []
for command in commands:
if command[0] == minimum:
ans.append(command)
return ans
def format_the_output(commands):
return [command[1] for command in commands]
def guessing(query):
filtered_commands = filter_commands_by_common_characters(query)
commands_with_levenshtein_distances = get_commands_with_levenshtein_distances(
query,
filtered_commands
)
best_commands = get_the_best_commands(commands_with_levenshtein_distances)
formatted_commands = format_the_output(best_commands)
return formatted_commands
def main():
user_input = get_user_input()
if does_handler_exist(user_input):
processing(user_input)
else:
commands = guessing(user_input)
print(commands)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment