QUESTION: Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.
Notes:
Only lower case letters will be used (a-z). No punctuation or digits will be included. Performance needs to be considered
MY CODE: s1 = 'rkqodlw' s2 = 'world' def scramble(s1, s2): unscrambled_word = [] n = 0 x = 0 s1_list = list(s1) s2_list = list(s2) s1_letter = s1_list[n] s2_letter = s2_list[x] for letters in range(len(s2_list)): while n > len(s1_list): if s1_letter == s2_letter: n = n + 1 x = x + 1 else: n = n + 1 return 'True' if x == len(s2_list) else 'False' I am trying to get my code to dissect each of the two strings. If the word of the first string does not match the word of the second string then n will +1 and move onto the second letter. If it matches then both n and x will + 1. My thought on my code is if x == the length of the second string before the giant loop ends then it will return True. Otherwise False. The problem is with the loop. The loop only seems to run through the first two letters of both strings and then stops. I am a fairly new coder any input is greatly appreciated! Thanks!
https://stackoverflow.com/questions/65730513/how-can-i-fix-my-python-word-descrambler January 15, 2021 at 12:26PM
没有评论:
发表评论