93.4k views
0 votes
#Write a function called alter_list. alter_list should have#two parameters: a list of strings and a list of integers.##The list of integers will represent indices for the list of#strings. alter_list should alter the capitalization of all#the words at the designated indices. If the word was all#capitals, it should become all lower case. If it was all#lower case, it should become all capitals. You may assume#that the words will already be all-caps or all-lower case.##For example:## string_list = ["hello", "WORLD", "HOW", "are", "you"]# index_list = [0, 2]# alter_list(string_list, index_list) -> # ["HELLO", "WORLD", "how", "are", "you"]##After calling alter_list, the strings at indices 0 and 2#have switched their capitalization. ##Note that it may be the case that the same index is present#in the second twice. If this happens, you should switch the#text at that index twice. For example:## string_list = ["hello", "WORLD", "HOW", "are", "you"]# index_list = [0, 2, 2]# alter_list(string_list, index_list) -> # ["HELLO", "WORLD", "HOW", "are", "you"]##2 is in index_list twice, so the string at index 2 is#switched twice: capitals to lower case, then back to#capitals.#Write your function here!#Below are some lines of code that will test your function.#You can change the value of the variable(s) to test your#function with different inputs.##If your function works correctly, this will originally#print:#["hello", "WORLD", "HOW", "are", "you"]#["HELLO", "WORLD", "HOW", "are", "you"]print(alter_list(["hello", "WORLD", "HOW", "are", "you"], [0, 2]))print(alter_list(["hello", "WORLD", "HOW", "are", "you"], [0, 2, 2]))

User Intracer
by
8.0k points

2 Answers

6 votes

Final answer:

To alter the capitalization of specific words in a list of strings based on a list of indices, you can create a Python function called alter_list. This function iterates over the indices and switches the capitalization of the corresponding strings. The function handles cases where the same index appears multiple times in the index list.

Step-by-step explanation:

To meet the given requirements, you can create a function called alter_list that takes in two parameters: a list of strings and a list of integers representing the indices. Here's how you can implement this function in Python:

def alter_list(string_list, index_list):
for index in index_list:
if string_list[index].isupper():
string_list[index] = string_list[index].lower()
else:
string_list[index] = string_list[index].upper()
return string_list

In this implementation, the function iterates over each index in the index_list. If the string at that index is all uppercase, it is converted to lowercase using the lower() method. If the string is not all uppercase (i.e., all lowercase), it is converted to uppercase using the upper() method. Finally, the modified string_list is returned as the result.

Now, you can call the alter_list function with the given string_list and index_list to see the expected output:

string_list = ['hello', 'WORLD', 'HOW', 'are', 'you']
index_list = [0, 2]
print(alter_list(string_list, index_list))

# Output: ['HELLO', 'WORLD', 'how', 'are', 'you']

If the same index appears multiple times in the index_list, you need to switch the capitalization twice. This will be taken care of by the implementation of the alter_list function.

Once again, you can call the alter_list function with the given string_list and index_list to see the expected output:

string_list = ['hello', 'WORLD', 'HOW', 'are', 'you']
index_list = [0, 2, 2]
print(alter_list(string_list, index_list))

# Output: ['HELLO', 'WORLD', 'HOW', 'are', 'you']

User Dyslexit
by
7.5k points
3 votes

Answer:

The Python code is given below with appropriate comments

Step-by-step explanation:

def alter_list(strings,index):

for i in index:

s = strings[i]

if s.islower(): #checking if lowercase

strings[i] = s.upper() #assigning variable in strings to uppercase in s

if s.isupper(): #checking if uppercase

strings[i] = s.lower() #assigning variable in strings to lowercase in s

return strings #returns strings for the function

print(alter_list(["hello", "WORLD", "HOW", "are", "you"], [0, 2]))

print(alter_list(["hello", "WORLD", "HOW", "are", "you"], [0, 2, 2]))

User Amogh Mishra
by
6.6k points