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
wariber [46]
3 years ago
3

Two strings are anagrams if they are permutations of each other. For example, "aaagmnrs" is an anagram of "anagrams". Given an a

rray of strings, remove each string that is an anagram of an earlier string, then return the remaining array in sorted order. For example, given the strings s = ['code', 'doce', 'ecod', 'framer', 'frame'], the strings 'doce' and 'ecod' are both anagrams of 'code' so they are removed from the list. The words 'frame' and 'framer' are not anagrams due to the extra 'r' in 'framer', so they remain. The final list of strings in alphabetical order is ['code', 'frame', 'framer'].

Computers and Technology
1 answer:
Alex3 years ago
4 0

Answer:

Here is the Python program:

def removeAnagram(array):  #method to remove string from array of strings that is anagram of earlier string

   s = set()  #creates a set

   output = []  #creates an output list

   for string in array:  # iterates through each string in array of strings

       if ''.join(sorted(string)) not in s:  #if sorted string of array is not in s

           output.append(string)  #append that string to output list

           s.add(''.join(sorted(string)))  #add that sorted string to s set

   return sorted(output)  #returns output list

   

array = ['code', 'doce', 'ecod', 'framer', 'frame']  #creates a list of words

print(removeAnagram(array))  #calls method to remove each string that is an anagram of earlier string

Explanation:

I will explain the program with an example:

Suppose array = ['code', 'doce', 'ecod', 'framer', 'frame']

Now the for loop works as follows:

At first iteration:

first string of array is 'code'

if ''.join(sorted(string)) not in s: this is an if statement which has two method i.e. join() and sorted(). First each letter of the string i.e. 'code' is sorted in alphabetical order then these separated characters are joined together into a word with join() method. So

sorted(string) becomes:

['c', 'd', 'e', 'o']                                                                                                           and ''.join(sorted(string)) becomes:

cdeo

Now if ''.join(sorted(string)) not in s condition checks if cdeo is not in s set. This is true so the statement:

output.append(string) executes which appends the string to output list. So now output has:

['code']                                                                                                                         Next s.add(''.join(sorted(string))) statement  adds the sorted and joined string of array to s set. So the set has:

{'cdeo'}                                                                                                                        

So at each iteration each word from the array is sorted and joined and then checked if output array contains that word or not. If not then it is added to the output array otherwise not. For example at 2nd iteration the word 'doce' which is anagram of code and it is checked when it is sorted and joined and it becomes cdeo which is already in output array. So this is how the anagram is removed from the array. At the end the output array which returns the remaining array in sorted order is returned by this method. The screenshot of the program along with its output is attached.

You might be interested in
Cyber security includes which of the following?
Law Incorporation [45]

Answer:

d. All of above

Explanation:

All answers are correct!

5 0
3 years ago
Write an algorithm that receives a number from the user (you can store the number in a variable called N). Then the algorithm sh
lbvjy [14]

Answer:

Algorithm:

1. Declare an integer variable N.

2. Read the value N from user.

3.While(N):

 3.1 find r=N%10;

 3.2 print r in new line.

 3.3 Update N as N=N/10.

4.end program.

Implementation in C++.

// header

#include <bits/stdc++.h>

using namespace std;

// main function

int main()

{

// variable

int N;

cout<<"Enter an Integer:";

   cin>>N;

// find the digits of number

   while(N)

   {

       // last digit

       int r=N%10;

       // print last digit

       cout<<r<<endl;

       // update the number

       N=N/10;

}

return 0;

}

Output:

Enter an Integer:329                                        

9                                                          

2                                                          

3

7 0
3 years ago
Extend to also calculate and output the number of 1 gallon cans needed to paint the wal. Hint: Use a math function to round up t
Crank

Answer:

Here is the Python program:

import math #import math to use mathematical functions

height = float(input("Enter wall height (feet): ")) #prompts user to enter wall height and store it in float type variable height

width = float(input("Enter wall width (feet): ")) #prompts user to enter wall width and store it in float type variable width

area = height *width #computes wall area

print('Wall area: {} square feet'.format(round(area))) #displays wall area using round method that returns a floating-point number rounded

sqftPerGallon = 350 #sets sqftPerGallon to 350

paintNeeded = area/ sqftPerGallon #computes needed paint

print("Paint needed: {:.2f} gallons".format(paintNeeded)) #displays computed paint needed up to 2 decimal places

cansNeeded = int(math.ceil(paintNeeded)) #computes needed cans rounding the paintNeeded up to nearest integer using math.ceil

print("Cans needed: {} can(s)".format(cansNeeded)) #displays computed cans needed

colorCostDict = {'red': 35, 'blue': 25, 'green': 23} #creates a dictionary of colors with colors as key and cost as values

color = input("Choose a color to paint the wall: ") #prompts user to enter a color

if color in colorCostDict: #if the chosen color is present in the dictionary

    colorCost = colorCostDict.get(color) #then get the color cost from dictionary and stores it into colorCost using get method that returns the value(cost) of the item with the specified key(color)

    cost = cansNeeded * colorCost #computes the cost of purchasing paint of specified color per cansNeeded

  print("Cost of purchasing {} paint: ${}".format(color,colorCostDict[color])) #displays the real cost of the chosen color paint

print("Cost of purchasing {} paint per {} gallon can(s): ${}".format(color,cansNeeded, cost)) #displays the cost of chosen color paint per cans needed.

Explanation:

The program first prompts the user to enter height and width. Lets say user enter 20 as height and 50 as width so the program becomes:

Wall area computed as:

area = height *width

area = 20 * 50

area = 1000

Hence the output of this part is:

Wall area: 1000 square feet                                                                                                                     Next program computes paint needed as:

paintNeeded = area/ sqftPerGallon

Since sqftPerGallon = 350 and area= 1000

paintNeeded = 1000 / 350

paintNeeded = 2.86

Hence the output of this part is:

Paint needed: 2.86 gallons

Next program computes cans needed as:      

cansNeeded = int(math.ceil(paintNeeded))

This rounds the computed value of paintNeeded i.e. 2.86 up to nearest integer using math.ceil so,

cansNeeded = 3                                                                  

Hence the output of this part is:

Cans needed: 3 can(s)                                                                                                                            Next program prompts user to choose a color to paint the wall

Lets say user chooses 'blue'

So the program get the cost corresponding to blue color and multiplies this cost to cans needed to compute the cost of purchasing blue paint per gallon cans. So

cost = cansNeeded * colorCost

cost = 3 * 25

cost = 75

So the output of this part is:

Cost of purchasing blue paint per 3 gallon can(s): $75                                                                                        The screenshot of the program along with its output is attached.

5 0
3 years ago
_____ is a predefined format used for text the can include multiple font formatting features
seraphim [82]
The correct answer is



style
8 0
3 years ago
Ann wants to download Adobe Acrobat software from the Internet. Prior to downloading, a standardized online contract appears on
IceJOKER [234]
Answer: terms and services
5 0
3 years ago
Other questions:
  • What are the 21St century competencies or skills required in the information society?<br>​
    15·1 answer
  • Checking the ___________ will reduce the possibility of having to rebuild or replace the engine.
    9·2 answers
  • What is the binding time for the following in C/C++ program. Explain. a. The location of memory of a local variable in a functio
    9·1 answer
  • "The fact that we could create and manipulate an Account object without knowing its implementation details is called"
    9·1 answer
  • An individual who understands and uses a variety of symbols yet uses one symbol at a time to share a message needs more developm
    10·1 answer
  • An overall indication of the dependability of data may be obtained by examining the ________, credibility, reputation, and _____
    15·2 answers
  • A solid understanding of __________ is the foundation of verbal communication
    7·1 answer
  • If a function doesn’t return a value, the word _________ will appear as its return type.
    6·1 answer
  • What are Database administrators? <br> Thank you :)
    7·1 answer
  • Maria is conducting a security investigation and has identified a suspect. The suspect is an employee of the organization who ha
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!