1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
NISA [10]
3 years ago
6

Write a Python program string_functions.py that defines several functions. Each function re-implements Python's built-in string

methods.1. my_compare(str1, str2): Takes two strings as parameters (str1, str2) and 
returns a -1 if str1 is alphabetically less than str2, returns a 0 if str1 is alphabetically equal to str2, and returns a 1 if str1 is alphabetically greater than str2. 
2. is_char_in(string, char): Takes a string and a character as parameters and returns True if the character is in the string or False if it is not.3. is_char_not_in(string, char): Takes a string and a character as parameters and returns True if the character is NOT in the string or False if it is.4. my_count(string, char): Takes a string and a character as parameters and returns an integer count for each occurrence of the character. For example, if the function takes in 'abracadabra' and 'a', the function should return 5.5. my_endswith(string, char): Takes a string and a character as parameters and returns True if the character is the last character in the string or False if it is not. For example, if the function takes in 'quartz' and 'z', the function should return True. If the function takes in 'quartz' and 'q', the function should return False.6. my_find(string, char): Takes a string and a character as parameters and returns the first index from the left where the character is found. If it does not find the character, return -1. For example, if the function takes in ‘programming’ and ‘g’, it should return 3. Do not use str.find() for this.7. my_replace(string, char1, char2): Takes a string and two characters (char1 and char2) as parameters and returns a string with all occurrences of char1 replaced by char2. 
For example, if the string is ‘bamboozle’ and char1 is ‘b’ and char2 is ‘t’ then your function should return ‘tamtoozle’.8. my_upper(string): Takes a string as a parameter and returns it as a string in all uppercase. For example, 'victory' would be 'VICTORY'. Hint: Use the ord(c) function to get the ASCII / Unicode code-point. For example, ord('a') returns the integer 97. Use chr(i) function to convert an integer back to a character. For example, chr(97) returns the string 'a'. You cannot use the built-in string function str.upper() for this.9. my_lower(string): Takes a string as a parameter and returns it as a string in all lowercase. Hint: Similar to the previous item, use ord() and chr(). Do not use str.lower() for this. 
Extra Credit (10 points): my_title(string): Takes a string and returns a string with the first character capitalized for every word. For example, if the input to the function is "I like Python a lot", the function should return the string "I Like Python A Lot".Note- You may not use Python's built-in string methods - e.g., str.find(), str.replace(), str.lower(), lstrip(), startswith(), endswith(), join() … - to implement yours. However, you *should* use them to test your functions to see if they produce the same results. You can do this in the main or in another defined function which is called by the main. You can use all other Python functions.
Computers and Technology
1 answer:
grigory [225]3 years ago
5 0

Answer:

def my_compare(str1, str2):

   mylist = sorted([str1, str2])

   if str1 == str2:

       return 0

   elif str1 == mylist[0]:

       return -1

   elif str1 == mylist[1]:

       return 1

def is_char_in(string, char):

   if char in string:

       return True

   return False

def is_char_not_in(string, char):

   if char not in string:

       return True

   return False

def my_count(string, char):

   return string.count(char)

def my_endswith(string, char):

   return string.endswith(char)

def my_find(string, char):

   if char in string:

       return string.index(char)

   else:

       return -1

def my_replace(string, char1, char2):

   lst = [i for i in string]

   print(lst)

   for i, v in enumerate(lst):

       if v==char1:

           lst[i] = char2

   return "".join(lst)

Explanation:

Above are defined functions similar to the string built-in functions in python. To use them, type in the function name and pass in the required arguments. Save the file name as "string_functions" with a ".py" file extension and use the functions in other files by importing all the function as "import string_functions" or import individual functions with "from string_function import 'function_name' ".

You might be interested in
] why was drone-defense equipment deployed at airports in the United Kingdom?
VMariaS [17]

To stop any terrorists

7 0
3 years ago
Do you know how to change your grades on a printer???????????
BabaBlast [244]

Answer:

To change ur grade make sure to do it on the website first by right clicking your mouse and clicking inspect element and once done changing x out and it will save

Explanation:

5 0
3 years ago
Write the definition of a function named swapints that is passed two int variables. The function returns nothing but exchanges t
lisov135 [29]

Answer:

Following are the program in the C++ Programming Language.

#include <iostream>

//header file

using namespace std;

 //using namespace

void swapints(int &j, int &k){

 //definition of the function

int temp;

temp = j;

j = k;

k = temp;

}

// function declaration

void swapints(int &j, int &k);

int main() {

  // local variable declaration:

 int j = 15;

 int k = 23;

 std::cout << "Before the swapping, value of j : "<<j<<endl;

 std::cout << "Before the swapping, value of k : "<<k<<endl;

  /* calling a function to swap the values using variable reference.*/

 swapints(j, k);

 std::cout << "After the swapping, value of j : "<<j<< endl;

 std::cout << "After the swapping, value of k : "<<k<< endl;

 

  return 0;

}

Output:

Before the swapping, value of j : 15

Before the swapping, value of k : 23

After the swapping, value of j : 23

After the swapping, value of k : 15

Explanation:

Here, we define the header fine and namespace <iostream> and "std".

Then, we define a function named "swapints()" and pass two reference variables in parameter.

Then, inside the function, we perform the codes of swapping.

Then, we define the main() function inside it we define two integer variable "j" and "k" assign value "j" to 15 and "k" to 23 and print both the variables.

Then, inside main() function we call the function "swapints()" and pass value to its parameters.

4 0
3 years ago
Write a program that prompts the user to enter a Social Security number in the format ddd-dd-dddd, where d is a digit. The progr
koban [17]

ssn = input("Enter a valid Social Security number: ")

dashes = 0

nums = 0

message = "Invalid SSN"

if len(ssn) == 11:

   for x in ssn:

       if x.isdigit():

           nums += 1

       elif x == "-":

           dashes += 1

if nums == 9 and dashes == 2:

   message = "Valid SSN"

print(message)

I wrote my code in python 3.8. I hope this helps!

3 0
3 years ago
MULTI-SELECT
Nataliya [291]

Answer:

Need for better data storage

Explanation:

Just got is right on edg

6 0
3 years ago
Read 2 more answers
Other questions:
  • Assume the availability of a function called printStars. The function receives an integer value as an argument. If the argument
    8·1 answer
  • What is out put.what is data. what is microprocessor
    14·1 answer
  • sara has just started using the Internet. She would like to be more efficient . In 3-4, give sara some advice about how to be mo
    5·1 answer
  • Using a single formatting _______ helps to make reading researched information easier; it lets the reader know what to expect.
    7·1 answer
  • How to do “Pseudocode” and “FlowChart” in this question ? <br><br> Please Help mee
    9·1 answer
  • Look at the following program and answer the question that follows it. 1 // This program displays my gross wages. 2 // I worked
    8·1 answer
  • One shortcoming of rapid application development (RAD) is _____.
    9·1 answer
  • I need to find out how to mark brainliest so just answer this and the first person will be marked brainliest.
    12·1 answer
  • Briefly explain the conceptual of effective computer based instruction for adults outlining the three units output process and i
    14·1 answer
  • A data mart is the operational database for the company. group of answer choices true false
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!