Python Answers

Questions answered by Experts: 5 288

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Search

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 or any other source.



Armstrong numbers between two intervals


Write a program to print all the Armstrong numbers in the given range A to B(including A and B). An N-digit number is an Armstrong number if the number equals the sum of the Nth power of its digit.


Input

The first line has an integer A.

The second line has an integer B.


Output

Print all Armstrong numbers separated by a space.

If there are no Armstrong numbers in the given range,print -1.


Sample Input 1

1

3

Sample Input 1

1 2 3


Sample Input 2

10

15

Sample Input 2

-1


call this function

def test_sqrt():

a = 1

while a < 26:

print('a =', a,'| my_sqrt(a) =',my_sqrt(a),'| math.sqrt(a) =', math.sqrt(a),'| diff =', abs(math.sqrt(a)-my_sqrt(a)))

a = a + 1

return 0


Student ID Map


you are given a sequence of student names Ni and their ids Di(corresponding to the student at the same index in Ni).


Write a program to print the student name and his ID from Ni and Di in alphabetical order of the name.


Explanation


For Example, if the given student names sequence and IDs sequenceare the following.


Anand,Ramesh,Kiran

ID102, ID101, ID100


Write a function named test_sqrt that prints a table like the following using a while loop, where "diff" is the absolute value of the difference between my_sqrt(a) and math.sqrt(a). 

a = 1 | my_sqrt(a) = 1 | math.sqrt(a) = 1.0 | diff = 0.0

a = 2 | my_sqrt(a) = 1.41421356237 | math.sqrt(a) = 1.41421356237 | diff = 2.22044604925e-16

a = 3 | my_sqrt(a) = 1.73205080757 | math.sqrt(a) = 1.73205080757 | diff = 0.0

a = 4 | my_sqrt(a) = 2.0 | math.sqrt(a) = 2.0 | diff = 0.0

a = 5 | my_sqrt(a) = 2.2360679775 | math.sqrt(a) = 2.2360679775 | diff = 0.0

a = 6 | my_sqrt(a) = 2.44948974278 | math.sqrt(a) = 2.44948974278 | diff = 0.0

a = 7 | my_sqrt(a) = 2.64575131106 | math.sqrt(a) = 2.64575131106 | diff = 0.0

a = 8 | my_sqrt(a) = 2.82842712475 | math.sqrt(a) = 2.82842712475 | diff = 4.4408920985e-16

a = 9 | my_sqrt(a) = 3.0 | math.sqrt(a) = 3.0 | diff = 0.0 

Modify your program so that it outputs lines for a values from 1 to 25 instead of just 1 to 9. 


You should submit a script file and a plain text output file (.txt) that contains the test output. Multiple file uploads are permitted.

Your submission will be assessed using the following Aspects.

Does the submission include a my_sqrt function that takes a single argument and includes the while loop from the instructions?
Does the my_sqrt function initialize x and return its final value?
Does the test_sqrt function print a values from 1 to 25?
Does the test_sqrt function print the values returned by my_sqrt for each value of a?
Does the test_sqrt function print correct values from math.sqrt for each value of a?
Does the test_sqrt function print the absolute value of the differences between my_sqrt and math.sqrt for each value of a?
Does the my_sqrt function compute values that are almost identical to math.sqrt ("diff" less than 1e-14)?
Part 2

Write a function named test_sqrt that prints a table like the following using a while loop, where "diff" is the absolute value of the difference between my_sqrt(a) and math.sqrt(a).

a = 1 | my_sqrt(a) = 1 | math.sqrt(a) = 1.0 | diff = 0.0
a = 2 | my_sqrt(a) = 1.41421356237 | math.sqrt(a) = 1.41421356237 | diff = 2.22044604925e-16
a = 3 | my_sqrt(a) = 1.73205080757 | math.sqrt(a) = 1.73205080757 | diff = 0.0
a = 4 | my_sqrt(a) = 2.0 | math.sqrt(a) = 2.0 | diff = 0.0
a = 5 | my_sqrt(a) = 2.2360679775 | math.sqrt(a) = 2.2360679775 | diff = 0.0
a = 6 | my_sqrt(a) = 2.44948974278 | math.sqrt(a) = 2.44948974278 | diff = 0.0
a = 7 | my_sqrt(a) = 2.64575131106 | math.sqrt(a) = 2.64575131106 | diff = 0.0
a = 8 | my_sqrt(a) = 2.82842712475 | math.sqrt(a) = 2.82842712475 | diff = 4.4408920985e-16
a = 9 | my_sqrt(a) = 3.0 | math.sqrt(a) = 3.0 | diff = 0.0

Modify your program so that it outputs lines for a values from 1 to 25 instead of just 1 to 9.
CS 1101 Programming Fundamentals - AY2022-T1
Dashboard
My courses
CS 1101 - AY2022-T1
30 September - 6 October
Learning Guide Unit 5

Learning Guide Unit 5
Learning Guide Unit 5

Programming Assignment
This assignment is based on Exercise 7.1 from your textbook.

Part 1

Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that takes a as a parameter, chooses a starting value for x, and returns an estimate of the square root of a.

while True:
y = (x + a/x) / 2.0
if y == x:
break
x = y
for i in displayList:
    print(i[0])
    #loop through displayList
    for i in range(0,len(displayList)):
        #Test if the fist item in the current sub-list contains the text "Price Level
        #Tip: Remeber that each sub-list is a (displayList). So you have
        #       to access its items via displayList followed by TWO indexes.
        ......
            #Extract the second item from the currnet sub-list into variable called priceLevel
        priceLevel = ...
        #Test if priceLevel is between previousPrice and currentPrice OR
        #        priceLevel == previousPrice OR
        #        priceLevel == currentPrice
        if....

        :
            #Sound the alarm. Pass in the frequency and duration.
            if self.__currentPrice > self.__previousPrice:
                frequency = 800
                duration = 700
            else:
                frequency = 400
                duration = 700
            winsound.Beep(frequency, duration)

            #Print the text 'Alarm' with a green background color, so that the user
            #can go back and check when the alarm was sounded.
            ...





4. Negative Fusion


As I've said before, there are three types of numbers; the positive, the zero, and the negative ones. Today, we shall pay attention only to the negative ones now. Make a loop that will accept random decimal/float numbers. When the user inputs 0, the loop will terminate and then output the sum of all negative numbers inputted in 3 decimal places.


Let's code this.


Input

Multiple lines containing a decimal.


2. 4434

-1.3433

-2.444

6.432



Output

A line containing a decimal with three decimal places.


-3.787


LATEST TUTORIALS
APPROVED BY CLIENTS