Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We'll say that 2 strings match if they are non-empty and their first chars are the same. Loop over the given list of non-empty strings

We'll say that 2 strings "match" if they are non-empty and their first chars are the same. Loop over the given list of non-empty strings as follows: if a string "matches" an earlier string in the list, swap the two strings in the list. When a position in the list has been swapped, it no longer matches anything and can not be swapped again. Using a dictionary, this can be solved making just one pass over the array.
all_swap(['ab', 'ac']) ['ac', 'ab']
all_swap(['ax', 'bx', 'cx', 'cy', 'by', 'ay', 'aaa', 'azz']) ['ay', 'by', 'cy', 'cx', 'bx', 'ax', 'azz', 'aaa']
all_swap(['ax', 'bx', 'ay', 'by', 'ai', 'aj', 'bx', 'by']) ['ay', 'by', 'ax', 'bx', 'aj', 'ai', 'by', 'bx']
complete in python Starter code is listed bellow do not delete the starter code
def all_swap(lst):
pass #delete this when your function has actual code
lst1 = ['ab', 'ac']
all_swap(lst1)
print(lst1) #['ac', 'ab']
lst2 = ['ax', 'bx', 'cx', 'cy', 'by', 'ay', 'aaa', 'azz']
all_swap(lst2)
print(lst2) #['ay', 'by', 'cy', 'cx', 'bx', 'ax', 'azz', 'aaa']

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions