Skip to content

Instantly share code, notes, and snippets.

@awesmubarak
Last active December 13, 2024 10:37
Show Gist options
  • Save awesmubarak/f48abdb9848ded4a1032a7fc94d37cd8 to your computer and use it in GitHub Desktop.
Save awesmubarak/f48abdb9848ded4a1032a7fc94d37cd8 to your computer and use it in GitHub Desktop.

Word Combination Challenge

You are given an input string in the format "target_word: subword1, subword2, ... subwordN" where:

  1. The target word appears before the colon
  2. After the colon is a comma-separated string of subwords

Write a function that determines if the subwords can be combined in any order to create the target word. If possible, return the combination with all vowels removed. If not possible, return not possible.

Notes:

  • Subwords can only be used once
  • The order of subwords in the input doesn't matter
  • Remove all vowels (a,e,i,o,u) from the final output
  • The combination should be printed with subwords in the order they form the target word
  • Input subwords are separated by commas and may contain leading/trailing spaces

Examples

Input: "basketball: basket, ball, net"
Output: "bskt bll"

Input: "python: py, ton, java"
Output: "not possible"

Input: "hello: help, lo, el"
Output: "not possible"

Base code

Copy and paste this code into your IDE, and put your code where the comment is:

def can_form_word(input_string):
    ## Your code goes here
    return None # Change this to return the final string

assert can_form_word("basketball", "basket, ball, net") == "bskt bll"
assert can_form_word("python", "py, ton, java") == "not possible"
assert can_form_word("hello", "help, lo, el") == "not possible"
assert can_form_word("football", "foot, ball, bas") == "ft bll"
assert can_form_word("computer", "puter, put, com") == "cm ptr"

Spoilers

Step 0 Draw a flowchart of the data: what are the inputs, outputs, and processes the data goes through?
Step 1 Turn the input into seperate variables: target = the word you're trying to get, and words = a list of the available sub-words.
Step 2 Do a loop around each word, combining it with every other word and see if it equals the target.
Step 3 Remove the vowels.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment