Question

In: Computer Science

1. Consider the loop from Section 8.3 of your textbook. prefixes = 'JKLMNOPQ' suffix = 'ack'...

1. Consider the loop from Section 8.3 of your textbook.


prefixes = 'JKLMNOPQ'
suffix = 'ack'

for letter in prefixes:
print(letter + suffix)

Put this code into a Python script and run it. Notice that it prints the names "Oack" and "Qack".

Modify the program so that it prints "Ouack" and "Quack" but leaves the other names the same.

Include the modified Python code and the output in your submission.

2. Give at least three examples that show different features of string slices. Describe the feature illustrated by each example. Invent your own examples. Do not copy them for the textbook .

Solutions

Expert Solution

1 SOL)

Given Python code implementation:

prefixes = 'JKLMNOPQ'
suffix = 'ack'

for letter in prefixes:
print(letter + suffix)

Code Picture:

Given code's output:

NOTE: Above picture is the output for the given code in the question.

Modified python code in our required format:

prefixes = "JKLMNOPQ" #Prefixes string

suffix = "ack" #Suffix string

for letter in prefixes: #Traversing the given prefixes string

if letter == "O" or letter == "Q": #If the prefix is either "O" or or "Q"

print(letter + "u" + suffix) #Printing the word by adding letter "u" to the word after "O" or "Q" letter

continue #Program execution won't go down in this "for" loop

print(letter + suffix) #Printing the remaining words as it is.

CODE implementation:

Required output:

NOTE: Here, from above you can see that for the prefix "O" and prefix "Q" the output has been changed compared from the first uploaded output.

NOTE: Be aware of the indentation while copying the code into your editor by observing the above uploaded CODE pictures

2 SOL)

a) Reversing the string:

We can reverse a string by the following:

Output:

b) Slicing the string with negative indices:

We can slice the string with negative indices :

eg: string[-10:-1] ---> means accessing the last 10 characters of a string.

Implementation:

Output:

NOTE: Here as you can see from the string "United States of America" only the last 9 characters are only printed..(Space is also a character)

c) Skipping the characters while traversing :

We can skip the characters while traversing the string by the following:

eg: string[0:11:2] ---> accessing only every second character of the string starting from index 0 to index 11...Let's implement this by an example for a better understanding.

Implementation:

Output:

NOTE: From the string "United States of America" only every two characters are printed..

United_States of America ---> Only the underlined characters are only printed from (index 0 to index 18 skipping every 2 characters)

d) printing every nth character from last :

We can print every third character from last by the following :

eg: string[-1:-16:-3] ---> means printing every 3 character from the last of a string..

Implementation:

Output:

NOTE: syntax will be string[starting: end: step] ---> step : skipping the every stepth index


Related Solutions

Q.1: Suffixes & Prefixes (15 x 0.3 = 4.5 marks) Suffix Meaning Terminology -gram        record -algia...
Q.1: Suffixes & Prefixes (15 x 0.3 = 4.5 marks) Suffix Meaning Terminology -gram        record -algia pain -osis abnormal condition -plasty surgical repair -ist specialist -ole little, small -ic pertaining to -y      condition, process -sclerosis hardening -megaly enlargement -globin protein -itis inflammation -emia blood condition Prefixes Meaning Terminology contra- against, opposite anti- against dys- difficult hemi- half ec-     out, outside mal- bad neo- new sub- under poly- many, much de- down, lack of nulli- no, none trans- across, through Q.2:...
From information in the “Data section” of your textbook, calculate the standard Gibbs free energy and...
From information in the “Data section” of your textbook, calculate the standard Gibbs free energy and the equilibrium constant at (a) 25°C and (b) 50°C for the reaction CH4(g) + 3 Cl2(g) <--> CHCl3(l) + 3 HCl (g). Assume that the reactionenthalpy isindependent of temperature. ΔG°f (CHCl3, l) =-­71.8 kJ/mol, ΔH°f(CHCl3, l) =‐132.2 kJ/mol Which extra DATA can i provide from the textbook. What values should I look for?
Rework problems 13 and 14 from section 2.4 of your textbook (page 81) about the bucket...
Rework problems 13 and 14 from section 2.4 of your textbook (page 81) about the bucket containing orange tennis balls and yellow tennis balls from which 5 balls are selected at random, but assume that the bucket contains 6 orange balls and 8 yellow balls. (1) What is the probability that, of the 5 balls selected at random, at least one is orange and at least one is yellow? equation editorEquation Editor (1) What is the probability that, of the...
Revise the substitutionEncrypt function from section 3.5 (p. 102) of the textbook to (1) remove all...
Revise the substitutionEncrypt function from section 3.5 (p. 102) of the textbook to (1) remove all spaces from the plaintext message before it is encrypted and (2) generate the substitution cipher key from a password. (substitutionEncrypt will call genKeyFromPass to do this.) The password should be a parameter, psw, which will replace key as a parameter in the function header. Write function substitutionDecrypt, which will have two parameters, cipherText, a string, a message encrypted by substitutionEncrypt, and psw, the password...
1. For this discussion, you are asked to examine the structure of DNA from your textbook....
1. For this discussion, you are asked to examine the structure of DNA from your textbook. 2. Using your knowledge or the material covered in Weeks 1 – 12, your general knowledge of organic chemistry I, your textbooks and the Internet to answer the following questions regarding the DNA molecule. a. Label and give the name for the major functional groups in the DNA molecule? b. Functional groups are sites where chemical reactions take place. Using your knowledge of basic...
Consider the estimated equation from your textbook: Test score = 698.9 - 2.28x STR, R^2 =...
Consider the estimated equation from your textbook: Test score = 698.9 - 2.28x STR, R^2 = 0.051, SER = 18.6 (10.4)(0.52) Test for the null hypothesis H0: B1 = -2 with a significant level of 5% Show steps please
I. Read Chapters 1 to 3 from your Textbook. II. NCLEX Review Questions Chapters 1 to...
I. Read Chapters 1 to 3 from your Textbook. II. NCLEX Review Questions Chapters 1 to 3 III. Chapter 3 : Functional Assessment in the Elderly Patient Profile H.J., a 74-year-old African American male, is a retired army military officer. His wife of over 50 years passed away four years ago. He has four daughters and two sons who are all grown and married. He has fourteen grandchildren with a great-grandchild on the way. His son brought him to the...
In response to question 4.2 from the textbook, demonstrate your understanding of the differences in the...
In response to question 4.2 from the textbook, demonstrate your understanding of the differences in the design, applications, and usage of the relational database as compared to the use of spreadsheets. Question 4.2 Most DBMS packages contain data definition, data manipulation, and data query languages. For each of the following, indicate which language would be used and why. A database administrator defines the logical structure of the database. The controller requests a cost accounting report containing a list of all...
Add a 1- to 2-page section to your Playbook/Runbook. Create a prescriptive section of your playbook...
Add a 1- to 2-page section to your Playbook/Runbook. Create a prescriptive section of your playbook providing guidelines to secure each of these areas: Network connections Mobile devices Cloud services
Your discussion board question comes from module 8.3, Social and Personality Development in Middle Adulthood. Think...
Your discussion board question comes from module 8.3, Social and Personality Development in Middle Adulthood. Think about your personality.  According to McCrae and Costa, some basic elements of our personality do not change over the life span.  These five factors are described in the following chart: Describe your personality.  Do you think it has been this way since you were young? Do you believe you will stay this way the rest of your life?  Explain why you agree or disagree. FACTOR LOW SCORE HIGH...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT