Skip to content

Commit

Permalink
Refactored solutions 249 - 250
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Jan 30, 2021
1 parent a8b9c96 commit d897fe8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
15 changes: 12 additions & 3 deletions Solutions/249.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@
"""

from sys import maxsize
from typing import List


def get_max_xor(arr):
def get_max_xor(arr: List[int]) -> int:
max_xor = -maxsize
for index, elem1 in enumerate(arr):
for elem2 in arr[index + 1 :]:
max_xor = max(max_xor, elem1 ^ elem2)
return max_xor


# DRIVER CODE
print(get_max_xor([1, 2, 3, 4]))
if __name__ == "__main__":
print(get_max_xor([1, 2, 3, 4]))


"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(1)
"""
48 changes: 33 additions & 15 deletions Solutions/250.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
A cryptarithmetic puzzle is a mathematical game where the digits of some numbers are
represented by letters. Each letter represents a unique digit.
For example, a puzzle of the form:
SEND
+ MORE
--------
MONEY
may have the solution:
{'S': 9, 'E': 5, 'N': 6, 'D': 7, 'M': 1, 'O': 0, 'R': 8, 'Y': 2}
Given a three-word puzzle like the one above, create an algorithm that finds a solution
"""


def get_num_from_string(char_map, string):
# function to generate the value of a number from the charater map and string
from typing import Dict, List, Set


def get_num_from_string(char_map: Dict[str, int], string: str) -> int:
mantissa = 10
total = 0
for char in string[::-1]:
Expand All @@ -24,25 +30,30 @@ def get_num_from_string(char_map, string):
return total


def is_valid_map(exp1, exp2, res, char_map):
# function to check the validity of the expression for the given character map
def is_valid_map(exp1: str, exp2: str, res: str, char_map: Dict[str, int]) -> bool:
num1 = get_num_from_string(char_map, exp1)
num2 = get_num_from_string(char_map, exp2)
num3 = get_num_from_string(char_map, res)
return num1 + num2 == num3


def evaluate_char_maps(exp1, exp2, res, char_maps):
# function to get the valid charater map from a list of character maps
def get_valid_char_map(
exp1: str, exp2: str, res: str, char_maps: List[Dict[str, int]]
) -> Dict[str, int]:
for char_map in char_maps:
if is_valid_map(exp1, exp2, res, char_map):
return char_map


def assign_letters(chars_left, nums_left, restrictions, char_map={}):
def assign_letters(
chars_left: Set[str],
nums_left: Set[int],
restrictions: Dict[str, Set[int]],
char_map: Dict[str, int] = {},
) -> List[Dict[str, int]]:
# function to assign digits to the characters
# brute force approach: all valid (doesn't contradict restictions) combinations
# are generated
# are generated
if not chars_left:
return [char_map]
curr_char = list(chars_left)[0]
Expand All @@ -62,22 +73,29 @@ def assign_letters(chars_left, nums_left, restrictions, char_map={}):
return char_maps


def decode(exp1, exp2, res):
# fuction to convert the expressions to character map
# check if the expression is valid
def decode(exp1: str, exp2: str, res: str) -> Dict[str, int]:
characters = set(exp1) | set(exp2) | set(res)
if len(characters) > 10:
raise ValueError("Number of digits cannot be more than 10")
# generating the valid character map

nums = set(range(0, 10))
restrictions = {}
for char in characters:
restrictions[char] = set()
for word in [exp1, exp2, res]:
restrictions[word[0]].add(0)
char_maps = assign_letters(characters, nums, restrictions)
return evaluate_char_maps(exp1, exp2, res, char_maps)
return get_valid_char_map(exp1, exp2, res, char_maps)


if __name__ == "__main__":
print(decode("SEND", "MORE", "MONEY"))


# DRIVER CODE
print(decode("SEND", "MORE", "MONEY"))
"""
SPECS:
TIME COMPLEXITY: O(2 ^ n)
SPACE COMPLEXITY: O(n)
[n = number of unique characters]
"""

0 comments on commit d897fe8

Please sign in to comment.