Question

In: Computer Science

IN PYTHON Complete the following tasks, either in a file or directly in the Python shell....

IN PYTHON

Complete the following tasks, either in a file or directly in the Python shell.

  1. Using the string class .count() method, display the number of occurrences of the character 's' in the string "mississippi".

  1. Replace all occurrences of the substring 'iss' with 'ox' in the string "mississippi".

  1. Find the index of the first occurrence of 'p' in the string "mississippi".

  1. Determine what the following Python function does and describe it to one of your Lab TAs :

def foo(istring):

    p = '0123456789'

    os = ''

    for iterator in istring:

        if iterator not in p:

            os += iterator

    return os

Solutions

Expert Solution

The answer to the above problem is as follows:

QUESTION - Using the string class .count() method, display the number of occurrences of the character 's' in the string "mississippi".

PYTHON CODE-

s = 'mississippi' #store the string
#use the count() to count occurences of 's' and print the value
print(s.count('s'))

IMAGE OF CODE and OUTPUT-

QUESTION - Replace all occurrences of the substring 'iss' with 'ox' in the string "mississippi".

PYTHON CODE-

s = 'mississippi' #store the string
#use the replace() to replace occurences of 'iss' with 'ox'
print(s.replace('iss','ox'))

IMAGE OF CODE and OUTPUT-

QUESTION - Find the index of the first occurrence of 'p' in the string "mississippi"

PYTHON CODE-

s = 'mississippi' #store the string
#use the find() to find first occurence of 'p' in string
print(s.find('p'))

IMAGE OF CODE and OUTPUT-

QUESTION - Determine what the following Python function does and describe it to one of your Lab TAs

The above code fragment does the following -

Takes a string as parameter

And then iterates over the string character by character

and if the character is not a digit,ie, not a character in 0123456789, which means not a digit, -> then add this letter to the output string

and after the whole istring is iterated, then returns the output string(os).

For example, if input string istring = 'a1b2c3'

Then it gets consumed by function foo()

Then istring is iterated, so first letter 'a' is checked if its a digit or not, ie, present in '0123456789' or not

And since 'a' is not a digit, it gets concatenated to output string os.

So. only 'a', 'b' and 'c' were concatenated, and '1','2',and '3' were not included.

So output string os = 'abc'

Feel free to comment for any query.


Related Solutions

Please write shell scripts in Kali Linux to complete the following tasks. Task A - Take...
Please write shell scripts in Kali Linux to complete the following tasks. Task A - Take your UIN Write a script like below that reads and displays the MIDAS ID if the following requirements are satisfied: Only lower case letter [a-z] and numeric character[0-9] are allowed MIDAS ID must between 4 to 8 digits Test your script with the following examples: Your MIDAS ID in lower case Your MIDAS ID w.one upper case A string less than 4 digits A...
(IN PYTHON 3) Complete the following two tasks: 1. Words like first, second, and third are...
(IN PYTHON 3) Complete the following two tasks: 1. Words like first, second, and third are referred to as ordinal numbers. In this exercise, you will write a function that takes an integer as its only parameter and returns a string containing the appropriate English ordinal number as its only result. Your function must handle the integers between 1 and 12 (inclusive). It should return an empty string if a value outside of this range is provided as a parameter....
In Python: Please complete the following two tasks in Tic Tac Toe: 1. Allow the user...
In Python: Please complete the following two tasks in Tic Tac Toe: 1. Allow the user to choose heads/tails to see if the user goes first or the computer and alter your code accordingly. 2. Allow the user to play again. Add a simple strategy for the AI to play against the user.
Write a program to complete the following tasks. Record all results to a data file called "GradeSheet.dat".
 c++Write a program to complete the following tasks. Record all results to a data file called "GradeSheet.dat".(1) Read the grade sheet from attached data file, "Assignment4.dat". Print the original grade sheet.(3) Write a module to sort array GRADE[] and print the sorted grade sheet.(4) Write a module to sort array NAME[] and print the sorted grade sheet.(5) Write a module to sort array ID[] and print the sorted grade sheet.(6) Write a module to print the student's id, name, and...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
PYTHON. Tasks: Create and test the openFileReadRobust() function to open file (for reading) whose name is...
PYTHON. Tasks: Create and test the openFileReadRobust() function to open file (for reading) whose name is provided from the standard input (keyboard) – the name is requested until it is correct Create and test the openFileWriteRobust () function to open file (for writing) whose name is provided from the standard input (keyboard) – the name is requested until is correct Extend the program to count number of characters, words and lines in a file as discussed in the classby including...
Using Python Shell 2.7, create a file called Assignment6_1.py Create an array based on a basic...
Using Python Shell 2.7, create a file called Assignment6_1.py Create an array based on a basic array Create a for loop that will print the array index the array, print out the index append an element to the array, print out the array insert an element into the array, print out the array pop an element from the array, print out the array remove an element from the array, print out the array reverse the order of the array, print...
DIRECTIONS: Complete the following Programming Exercise in Python. Name the file Lastname_Firstname_E1. You just started your...
DIRECTIONS: Complete the following Programming Exercise in Python. Name the file Lastname_Firstname_E1. You just started your summer internship with ImmunityPlus based in La Crosse, Wisconsin. You are working with the forecasting team to estimate how many doses of an immunization drug will be needed. For each drug estimation, you will be provided the following information: corona.txt 39 20 31 10 42 49 54 21 70 40 47 60 - The size of the target population. - The life expectancy, in...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
(Python) Write a program which accomplishes the following tasks: set a variable to the result of...
(Python) Write a program which accomplishes the following tasks: set a variable to the result of mathematical expression including +, -, * and / and of both Integer and Float values (or variables) set a variable to the result of a combination of string values (or variables) set a variable to the result of a combination of string, Integer and Float values (you may need to use the type casting functions) Using the following variables: a = 1.3 b =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT