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
zvonat [6]
2 years ago
4

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam

or racecar. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama!", "Was it a car or a cat I saw?" or "No 'x' in Nixon". Write a function that takes a string as an argument and returns True if the string is a palindrome and False otherwise.
Computers and Technology
1 answer:
Softa [21]2 years ago
5 0

Answer:

The solution code is written in Python 3.

  1. import math
  2. def processString(myStr):
  3.    process = myStr.lower()
  4.    process = process.replace(" ","")
  5.    
  6.    p_list = '''!()-[]{};:'"\,<>./[email protected]#$%^&*_~'''
  7.    for p in p_list:
  8.        process = process.replace(p, "")
  9.    
  10.    return process
  11. def isPalindrome(myStr):
  12.    myStr = processString(myStr)
  13.    for i in range(0, math.ceil(len(myStr))):
  14.        lastIndex = len(myStr) - 1 - i
  15.        if(myStr[0] != myStr[lastIndex]):
  16.            return False  
  17.        
  18.        return True  
  19. print(isPalindrome("racecar"))
  20. print(isPalindrome("apple"))
  21. print(isPalindrome("Was it a car or a cat I saw?"))
  22. print(isPalindrome("No 'x' in Nixon"))
  23. print(isPalindrome("Programming is fun!"))

Explanation:

Since the palindrome function is expected to work on a sentence, we need to process the string prior to checking if the string is a palindrome. To process a string, a separate function is written, <em>processString() </em>with one parameter, "<em>myStr</em>" that will accept one input string. (Line 3 - 11)

Within the processString() function, there are three processing steps on the input string,<em> my_str:</em>

  1. convert all letters in string to lowercase using Python string <em>lower() </em>method (Line 4)
  2. remove all word dividers (Line 5) using <em>replace() </em>method
  3. remove all punctuation (Line 7-9). To remove punctuation, we need to define a punctuation list (Line 7). Next, use for-loop to traverse the punctuation list and whenever there is a punctuation appears in the input string, remove it using <em>replace()</em> method (Line 8-9).

After the function processString() is ready, we can proceed to work on the palindrome function,<em> isPalindrome()</em>. This function has one parameter that will accept one input string.

Within the function <em>isPalindrome()</em>, we call the function processString by passing the input string, <em>my_str</em> as argument (Line 14). The function <em>processString() </em>will operate on the <em>my_str </em>by converting all the letters to lowercase, removing all the word dividers and punctuation.

Next the processed string will be returned to <em>my_str</em>. Next, within a for-loop, traverse through the letters in <em>my_str </em>and compare the first letter with the last letter, second letter with second last letter until it reaches the middle point. (Line 15-17) If any comparison is found unmatched, return false since it is not a palindrome. If there is no unmatched found in the for-loop, the function will just return true.

At last, we test our function <em>isPalindrome() </em>with several test strings (Line 22 26) and the output is as follows:

True

False

True

True

False

You might be interested in
Which act was used to penalize Sara? Sara was found to be in possession of a controlled substance for which she had no justifica
andre [41]
<span>si la sustancia es droga, cárcel</span>
6 0
2 years ago
Suppose a host has a 1-MB file that is to be sent to another host. The file takes 1 second of CPU time to compress 50%, or 2 sec
kozerog [31]

Answer: bandwidth = 0.10 MB/s

Explanation:

Given

Total Time = Compression Time + Transmission Time

Transmission Time = RTT + (1 / Bandwidth) xTransferSize

Transmission Time = RTT + (0.50 MB / Bandwidth)

Transfer Size = 0.50 MB

Total Time = Compression Time + RTT + (0.50 MB /Bandwidth)

Total Time = 1 s + RTT + (0.50 MB / Bandwidth)

Compression Time = 1 sec

Situation B:

Total Time = Compression Time + Transmission Time

Transmission Time = RTT + (1 / Bandwidth) xTransferSize

Transmission Time = RTT + (0.40 MB / Bandwidth)

Transfer Size = 0.40 MB

Total Time = Compression Time + RTT + (0.40 MB /Bandwidth)

Total Time = 2 s + RTT + (0.40 MB / Bandwidth)

Compression Time = 2 sec

Setting the total times equal:

1 s + RTT + (0.50 MB / Bandwidth) = 2 s + RTT + (0.40 MB /Bandwidth)

As the equation is simplified, the RTT term drops out(which will be discussed later):

1 s + (0.50 MB / Bandwidth) = 2 s + (0.40 MB /Bandwidth)

Like terms are collected:

(0.50 MB / Bandwidth) - (0.40 MB / Bandwidth) = 2 s - 1s

0.10 MB / Bandwidth = 1 s

Algebra is applied:

0.10 MB / 1 s = Bandwidth

Simplify:

0.10 MB/s = Bandwidth

The bandwidth, at which the two total times are equivalent, is 0.10 MB/s, or 800 kbps.

(2) . Assume the RTT for the network connection is 200 ms.

For situtation 1:  

Total Time = Compression Time + RTT + (1/Bandwidth) xTransferSize

Total Time = 1 sec + 0.200 sec + (1 / 0.10 MB/s) x 0.50 MB

Total Time = 1.2 sec + 5 sec

Total Time = 6.2 sec

For situation 2:

Total Time = Compression Time + RTT + (1/Bandwidth) xTransferSize

Total Time = 2 sec + 0.200 sec + (1 / 0.10 MB/s) x 0.40 MB

Total Time = 2.2 sec + 4 sec

Total Time = 6.2 sec

Thus, latency is not a factor.

5 0
2 years ago
____ twisted pair is the least quality twisted pair wire that should be used in a data/voice application.
xxMikexx [17]

Answer:

Category 5 (Cat 5) twisted pair is the least quality twisted pair wire that should be used.

Explanation:

The reason is as follows:

  1. Category 1 twisted pair is not suitable for transmitting data but comes in handy for telephone communications. Hence, it should not be used in a data/voice application.
  2. Category 2 twisted pair is suitable for data transmission, however, it can only transmit at 4Mbps which is very slow. Hence, it should not be used in a data/voice application.
  3. Category 3 twisted pair can transmit data with speed reaching 10Mbps which is slow for voice/data communication.
  4. Category 4 twisted pair can transmit data with speed reaching 16Mbps and is used in token ring networks. It is slow for communicating in a voice/data application.
  5. Category 5 twisted pair is the least qualified to be used in a voice/data application because it can transmit data at speeds that reach 100Mbps.

Hence, Cat 5 or Category 5 twisted pair is the least quality twisted pair that should be used in a data/voice application.

6 0
2 years ago
Which of the following best describes open-source web browsers?
podryga [215]
Hello, Good Works mate!

Answer: A) <span>Open-source web browsers allow non-paid access and distribution.

Kind Regards.</span>
4 0
2 years ago
Read 2 more answers
Write a algorithm to attend birthday party​
kolezko [41]

Answer:

2     No

5 14    Yes

Explanation:

8 0
2 years ago
Read 2 more answers
Other questions:
  • Tina reported a safety hazard at her workplace to OSHA. Representatives from OSHA
    5·1 answer
  • Write a class called Counter that represents a simple tally counter, which might be used to count people as they enter a room. T
    9·1 answer
  • An analyst receives an alert from the SIEM showing an IP address that does not belong to the assigned network can be seen sendin
    9·1 answer
  • 3.5 Code Practice
    11·2 answers
  • The effectiveness of a(n) _____ process is essential to ensure the success of a data warehouse. Select one: a. visual basic b. e
    15·2 answers
  • Write a while loop that prints usernum divided by 2 until user_num is less than 1. The value of user_num changes inside of the l
    14·1 answer
  • Suppose that, when you arrive at the sale site for $50 Apple laptops (described in the text) at 1:45 A.M., you find more than 2,
    8·1 answer
  • Suppose that a class named ClassA contains a private nonstatic integer named b, a public nonstatic integer named c, and a public
    14·1 answer
  • Which of the following is not true of how computers represent complex information
    5·2 answers
  • JAVA...Write a statement that calls the recursive method backwardsAlphabet() with parameter startingLetter.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!