answer.
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
Pavel
1 month ago
13

Start with the following Python code. alphabet = "abcdefghijklmnopqrstuvwxyz" test_dups = ["zzz","dog","bookkeeper","subdermatog

lyphic","subdermatoglyphics"] test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"] # From Section 11.2 of: # Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press. def histogram(s): d = dict() for c in s: if c not in d: d[c] = 1 else: d[c] += 1 return d Copy the code above into your program but write all the other code for this assignment yourself. Do not copy any code from another source. Part 1 Write a function called has_duplicates that takes a string parameter and returns True if the string has any repeated characters. Otherwise, it should return False. Implement has_duplicates by creating a histogram using the histogram function above. Do not use any of the implementations of has_duplicates that are given in your textbook. Instead, your implementation should use the counts in the histogram to decide if there are any duplicates. Write a loop over the strings in the provided test_dups list. Print each string in the list and whether or not it has any duplicates based on the return value of has_duplicates for that string. For example, the output for "aaa" and "abc" would be the following. aaa has duplicates abc has no duplicates Print a line like one of the above for each of the strings in test_dups. Part 2 Write a function called missing_letters that takes a string parameter and returns a new string with all the letters of the alphabet that are not in the argument string. The letters in the returned string should be in alphabetical order. Your implementation should use a histogram from the histogram function. It should also use the global variable alphabet. It should use this global variable directly, not through an argument or a local copy. It should loop over the letters in alphabet to determine which are missing from the input parameter. The function missing_letters should combine the list of missing letters into a string and return that string. Write a loop over the strings in list test_miss and call missing_letters with each string. Print a line for each string listing the missing letters. For example, for the string "aaa", the output should be the following. aaa is missing letters bcdefghijklmnopqrstuvwxyz If the string has all the letters in alphabet, the output should say it uses all the letters. For example, the output for the string alphabet itself would be the following. abcdefghijklmnopqrstuvwxyz uses all the letters Print a line like one of the above for each of the strings in test_miss. Submit your Python program. It should include the following. The provided code for alphabet, test_dups, test_miss, and histogram. Your implementation of the has_duplicates function. A loop that outputs duplicate information for each string in test_dups. Your implementation of the missing_letters function. A loop that outputs missing letters for each string in test_miss. Also submit the output from running your program. Your submission will be assessed using the following Aspects. Does the program include a function called has_duplicates that takes a string parameter and returns a boolean? Does the has_duplicates function call the histogram function? Does the program include a loop over the strings in test_dups that calls has_duplicate on each string? Does the program correctly identify whether each string in test_dups has duplicates? Does the program include a function called missing_letters that takes a string parameter and returns a string parameter? Does the missing_letters function call the histogram function? Does the missing_letters function use the alphabet global variable directly? Does the program include a loop over the strings in test_miss that calls missing_letters on each string? Does the program correctly identify the missing letters for each string in test_miss, including each string that "uses all the letters"?

Computers and Technology
1 answer:
maria [954]1 month ago
3 0

Answer:

alphabet = "abcdefghijklmnopqrstuvwxyz"

test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]

test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]

# From Section 11.2 of: # Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press.

def histogram(s):

d = dict()

for c in s:

if c not in d:

d[c] = 1

else:

d[c] += 1

return d

#Part 1 Construct a function, called has_duplicates, that takes a string argument and returns True if there are any repeating characters present. If not, it should return False.

def has_duplicates(stringP):

dic = histogram(stringP)

for key,value in dic.items():

if value>1:

return True

return False

# Execute has_duplicates by building a histogram using the above histogram function. Implement a loop over the items in the test_dups list.

# Display each string and indicate whether it has duplicates based on the has_duplicates function's result for that string.

# For instance, the expected output for "aaa" and "abc" would be shown below. aaa has duplicates abc has no duplicates Display a similar message for each string within test_dups.

print("***Implementation of has_duplicates function***")

for sTr in test_dups:

if has_duplicates(sTr):

print(sTr+": has duplicates")

else:

print(sTr+": has no duplicates")

#Part 2 Develop a function named missing_letters that takes a string argument and returns a new string containing all alphabet letters absent from the input string.

#The letters in the resultant string should be arranged in alphabetical order. Your implementation must utilize the histogram from the previous function and should reference the global alphabet variable directly.

#It should iterate over the alphabet letters to figure out which are missing from the input string.

#The function missing_letters should merge the list of missing letters into a string and return it.

def missing_letters(sTr):

missingLettersList = []

dic = histogram(sTr)

for l in alphabet:

if l not in dic:

missingLettersList.append(l)

missingLettersList.sort()

return "".join(missingLettersList)

#Iterate over the strings in the test_miss list and invoke missing_letters for each one. Present a line for each string outlining the missing letters.

#For example, for the string "aaa", the output should be as follows. aaa is missing letters bcdefghijklmnopqrstuvwxyz

#Should the string encompass all alphabet letters, the output should indicate that it utilizes all the letters.

#For instance, output for the alphabet string itself would be: abcdefghijklmnopqrstuvwxyz uses all the letters

#Generate a similar line for each item in test_miss.

print("\n***Implementation of missing_letters function***")

for lTm in test_miss:

sTr = missing_letters(lTm.replace(" ",""))

if sTr!="":

print(lTm+" is missing letters "+sTr)

else:

print(lTm +" uses all the letters")

You might be interested in
Write a program that prompts the user for an integer, then asks the user to enter that many values. Store these values in an arr
Rzqust [962]

Answer:

//To facilitate user input, the Scanner class is imported

import java.util.Scanner;

//The Solution class is being defined

public class Solution {

   //The main method is declared here, marking the start of program execution

   public static void main(String args[]) {

       

       //A Scanner object named 'scan' is instantiated to gather user input

       Scanner scan = new Scanner(System.in);

       //User is prompted to specify the size of the array

       System.out.print("Enter the range of array: ");

       //The user input is stored in arraySize

       int arraySize = scan.nextInt();

       //Initialization of userArray with the size specified by arraySize

       int[] userArray = new int[arraySize];

       

       //A counter is initialized to track the number of elements entered in the array

       int count = 0;

       //The following while loop will continue until the user finishes inputting the elements of the array

       while (count < arraySize){

           System.out.print("Enter each element of the array: ");

           userArray[count] = scan.nextInt();

           count++;

       }

       

       //A blank line is outputted for better readability

       System.out.println();

       

       //A for loop iterates to print all elements of the array in a single line

       for(int i =0; i <userArray.length; i++){

           System.out.print(userArray[i] + " ");

       }

       

       //Another blank line is printed for clarity

       System.out.println();

       

       //A for loop is utilized to reverse the contents of the array

       for(int i=0; i<userArray.length/2; i++){

           int temp = userArray[i];

           userArray[i] = userArray[userArray.length -i -1];

           userArray[userArray.length -i -1] = temp;

       }

       

       //A for loop prints each element of the reversed array in one line

       for(int i =0; i <userArray.length; i++){

           System.out.print(userArray[i] + " ");

       }

     

   }

}

Explanation:

The program is annotated to provide a thorough explanation.

The for-loop responsible for reversing the array operates by splitting the array into two segments and swapping elements from the first segment with those from the second segment. During each loop, an element from the first segment is temporarily stored in temp variable, and then that element is replaced with the corresponding element from the second segment. Subsequently, the element from the second segment is updated with the value held in temp.

3 0
25 days ago
Strlen("seven"); what is the output?
oksian1 [849]

Response:

Your answer is A, and I hope this information is useful.

4 0
1 month ago
If a database named Contacts has a table named tblEmployees and another database named Orders links to that tblEmployees table,
Rzqust [962]

Answer:

cache I guess

Explanation:

unsure or memory HDD or SSD

5 0
21 day ago
Question #5<br> How does the computer decide which path to follow in a selection control structure?
Harlamova29_29 [950]

Answer:

In my opinion, the coding structure's elements assist the software or CPU in interpreting or directing the programming.

Explanation:

6 0
28 days ago
What happened if the offshore team members are not able to participate in the iterations demo due to timezone/infrastructure iss
zubka84 [1004]

What if the offshore team members are unable to join the iterations demonstration because of timezone or infrastructure issues? (c) Not a significant problem. The offshore lead and the onsite team members will attend the demo with the product owner and can relay the feedback to the offshore team afterwards.

Explanation:

Not a significant problem. The offshore lead and the onsite team members will attend the demo with the product owner and can relay the feedback to the offshore team afterwards.

From the previous statement, it is evident that if offshore team members cannot attend the demo alongside the product owner due to issues with time zones or infrastructure, it won't pose a major concern because the onsite team will be present and can share all relevant insights and feedback with the offshore team. They all belong to the same team.

Therefore, the answer (3) is correct

4 0
1 month ago
Other questions:
  • Which of the following statements is true regarding input and output?
    12·2 answers
  • The part of the computer that contains the brain, or central processing unit, is also known as the A.monitor B.modem C.keyboard
    10·1 answer
  • Two middle-order batsmen are compared based on their performance in their previous cricket match.
    7·1 answer
  • Write a loop that reads strings from standard input, where the string is either duck or goose. the loop terminates when goose is
    7·1 answer
  • Write a method printShampooInstructions(), with int parameter numCycles, and void return type. If numCycles is less than 1, prin
    14·1 answer
  • This question involves a simulation of a two-player game. In the game, two simulated players each start out with an equal number
    7·1 answer
  • To finish creating a design for the webpage, use one shape to cut away part of the other. Create a 700 pixel by 700 pixel square
    10·1 answer
  • A computer program is tested by 5 independent tests. If there is an error, these tests will discover it with probabilities 0.1,
    5·1 answer
  • A wireless network does not benefit like a wired network does, when it comes to collision reduction. Which device reduces collis
    6·1 answer
  • An application specifies a requirement of 200 GB to host a database and other files. It also specifies that the storage environm
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!