def question2(x): """ Analyzes input word for a substring palindrome """ if x: x_lowered = x.lower() reversed_word = x_lowered[::-1] matches = [] size = len(x_lowered) for i in range(1, size): for j in range(0, size, i): stop = j+i if stop > size: continue poss_pal = x_lowered[j:stop] if poss_pal in reversed_word: matches.append(poss_pal) if len(poss_pal) == stop: if poss_pal == poss_pal[::-1] and len(poss_pal) > 1: myPrint('`' + x + '`' + \ ' contains the palindrome ' + '`' + poss_pal + '`')
def promptUser(): """ Displays main command prompt used for interacting with program """ headerPrint(welcome_banner) # Welcome user to program for item in text_menu: # Present CLI options to user myPrint(item) print('') user_input = input("Select an option 1-6: ") # Await user command if user_input == '1': subprocess.call(["python3", "question1.py"]) elif user_input == '2': subprocess.call(["python3", "question2.py"]) elif user_input == '3': subprocess.call(["python3", "question3.py"]) elif user_input == '4': subprocess.call(["python3", "question4.py"]) elif user_input == '5': subprocess.call(["python3", "question5.py"]) elif user_input == '6': print('') myPrint("Now exiting program...") time.sleep(1) print("Program exited.") return else: # If user does not enter a valid option, restart myPrint("Not a recognized command, restarting program...") time.sleep(1) promptUser()
def question4(T, root, node1, node2): path1 = getPathToRoot(T, root, node1) path2 = getPathToRoot(T, root, node2) # myPrint("Path1: " + str(path1)) # myPrint("Path2: " + str(path2)) for node in path1: if node in path2: return node return False headerPrint('Question 4 - Find Least Common Ancestor Between Two Nodes') subHeaderPrint('Finding the least common ancestor using the following: ') myPrint('[0, 1, 0, 0, 0],') myPrint('[0, 0, 0, 0, 0],') myPrint('[1, 0, 0, 0, 1],') myPrint('[0, 0, 0, 0, 0,]],') myPrint(' 3,') myPrint(' 1,') myPrint(' 4)') myPrint('') subHeaderPrint('Result is: ') myPrint( question4([[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0]], 3, 1, 4)) print('')
""" Analyzes input word for a substring palindrome """ if x: x_lowered = x.lower() reversed_word = x_lowered[::-1] matches = [] size = len(x_lowered) for i in range(1, size): for j in range(0, size, i): stop = j+i if stop > size: continue poss_pal = x_lowered[j:stop] if poss_pal in reversed_word: matches.append(poss_pal) if len(poss_pal) == stop: if poss_pal == poss_pal[::-1] and len(poss_pal) > 1: myPrint('`' + x + '`' + \ ' contains the palindrome ' + '`' + poss_pal + '`') headerPrint('Question 2 - Substring Palindromes') subHeaderPrint('This is a word analysis for finding a substring palindrome.') myPrint('Words to be analyzed: `kayaking`, `level-headed` and `AnNaPoLiS`') print('') question2('kayaking') question2('level-headed24114414111414') question2('AnNaPoLiS') print('') input('Press the enter key to continue...')
def getFromHead(ll, m): node = ll # For second element `m = 2`, we run this loop once. # If iteration returns `None`, abort query and return `None` for iter in range(1, m): node = node.next if node == None: return None # Node is now the mth element from the start of the linked list return node.data def question5(ll, m): return getFromHead(ll, m) headerPrint('Question 5 - Specific element location in singly linked list') subHeaderPrint('Printing generated linked list with 5 nodes:') print_ll(ll) myPrint('') subHeaderPrint( 'Printing value of 3rd element of a linked list with 5 elements') myPrint(question5(ll, 3)) print('') input('Press the enter key to continue...')
# Sort `edges` by `weight` for (weight, union, vertex) in sorted(edges): # Add `edges` that don't contain `union` to `MST`` if union not in MST.keys() or vertex not in MST.keys(): if union not in MST.keys(): MST[union] = [] if vertex not in MST.keys(): MST[vertex] = [] # Append cleared data MST[union].append((vertex, weight)) MST[vertex].append((union, weight)) return MST headerPrint('Question 3 - Finding Minimum Spanning Tree') subHeaderPrint('This is the full starting graph provided to the program:') myPrint(G) myPrint('') subHeaderPrint('Determining Minimum Spanning Tree (MST)...') myPrint('Result: ') myPrint(question3(G)) print('') input('Press the enter key to continue...')