Question

In: Computer Science

Using Python:     "We have a list of strings: \n",     "```python\n",     "CharList = ['a',...

Using Python:
    "We have a list of strings: \n",
    "```python\n",
    "CharList = ['a', 'b', 'c', 'd']\n",
    "```\n",
    "Now, we write a program to delete some elements from the list. <br> \n",
    "The indexes of the elements to be deleted are stored in IndexList <br>\n",
    "\n",
    "```Scenario-1```: IndexList = [3, 0]\n",
    "```python\n",
    "CharList = ['a', 'b', 'c', 'd']\n",
    "IndexList = [3, 0]\n",
    "for n in range(0, len(IndexList)):    \n",
    "    index=IndexList[n]\n",
    "    print(index)\n",
    "    print(CharList[index])\n",
    "    del CharList[index]\n",
    "```\n",
    "The code under Scenario-1 works well: 'a' and 'd' are deleted <br>\n",
    "<br>\n",
    "```Scenario-2```: IndexList = [0, 3]\n",
    "```python\n",
    "CharList = ['a', 'b', 'c', 'd']\n",
    "IndexList = [0, 3]\n",
    "for n in range(0, len(IndexList)):    \n",
    "    index=IndexList[n]\n",
    "    print(index)\n",
    "    print(CharList[index])\n",
    "    del CharList[index]\n",
    "```\n",
    "The code under Scenario-2 does not work: You will see \"IndexError: list index out of range\" <br>\n",
    "<br>\n",
    "Write a function in which the elements can be deleted from the list <br>\n",
    "It must be a general solution. <br>\n",
    "CharList contains strings, not other stuff <br>\n",
    "IndexList contains integers, not other stuff <br>\n",
    "```python\n",
    "def Answer18(CharList, IndexList)\n",
    "    # parameters:\n",
    "    #     CharList is a list of strings\n",
    "    #     IndexList is a list of integers, and it can be [2, 3, 1], [1, 3, 2], etc\n",
    "    # return: None\n",
    "    # If the index of an element of CharList is in IndexList, \n",
    "    # then the element will be deleted from CharList.\n",
    "```"

Solutions

Expert Solution

"""''''''"""
"""The idea behind the code is that, after every deletion from the CharList, list size decreases by one.
To overcome the problem of indices always remain in the range, we have to subtract 1 from all the indices which are greater than
current index, so that the indices never get out of the range."""
def Answer18(CharList, IndexList):          #function definition
    for n in range(0, len(IndexList)):      #looping through index list
        index=IndexList[n]
        print(index)            #printing index
        print(CharList[index])          #printing character to be deleted
        del CharList[index]             #deleting character

        '''here we subtract 1 from each index greater than current index'''
        for i in range(0, len(IndexList)):      #looping through index list
            if index < IndexList[i]:            #checking if current index is smaller than index in the list
                IndexList[i] = IndexList[i] - 1             #subtracting 1
    return CharList         #returning the remaining character list

CharList = ['a', 'b', 'c', 'd']         #sample testcase
IndexList = [0, 3]
print('Character List after deletion: ', Answer18(CharList, IndexList))

Screenshot of code:

Screenshot of output:


Related Solutions

Using Python In this assignment we will try to add tuples, lists, if statements and strings...
Using Python In this assignment we will try to add tuples, lists, if statements and strings to our program. For example, you can ask for a user for a couple items, their name and age – and depending on their age, print out a predefined list. You could ask for some string input and decide to do something with the output. You could ask for three items, 1) age, 2) are you a male or female and 3) are your...
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
IN PYTHON: i have a list of strings: ['1,099', '1,749', '119', '129', '129', '13,299', '149', '15,299',...
IN PYTHON: i have a list of strings: ['1,099', '1,749', '119', '129', '129', '13,299', '149', '15,299', '159', '159', '175', '179', '179', '186', '189', '189', '199', '2,549', '226', '257', '290', '3,755', '35,279', '390', '399', '399', '4,549', '415', '415', '498', '499', '499', '499', '499', '549', '60', '819', '849', '89', '89', '899', '899', '899', '899', '999'] and i want to remove the commas from each string containing a comma, and then convert each element to an integer. (for example, i have '1,099' but...
Python This part involves creating a function that will manage a List of unique strings. The...
Python This part involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test program that takes a string as input and then calls the function over and over until the...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
Python , no strings, len(), or indexing Write the function setKthDigit(n, k, d) that takes three...
Python , no strings, len(), or indexing Write the function setKthDigit(n, k, d) that takes three # non-negative integers -- n, k, and d -- where d is a single digit # (between 0 and 9 inclusive), and returns the number n but with # the kth digit replaced with d. Counting starts at 0 and goes # right-to-left, so the 0th digit is the rightmost digit.
[02] For n ≥ 1, how many strings of length n using letters a,b,c are there...
[02] For n ≥ 1, how many strings of length n using letters a,b,c are there if the letter a must occur an even number of times?
Using Python answer the following questions. 1. Describe the class of strings matched by the following...
Using Python answer the following questions. 1. Describe the class of strings matched by the following regular expressions. Provide examples that can be captured by the regular expression pattern. (a) [A-Za-z]+ (b) ^[A-Za-z]+.\d$ 2. Which of the following matches regexp /(very )+(thick )?use(ful|less) book/? Explain why. A. very thick book B. very very useful book C. thick useless book D. very useless book
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT