Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ASAP! Python need help with explanations! So I know the answers for these and wi

ID: 3669633 • Letter: A

Question

ASAP! Python need help with explanations! So I know the answers for these and will provide them please go somewhat into detail explaining why the answer is that. Go easy on me I'm new to this type of thing haha.Sorry if spacing is weird since chegg adjusts it. Answers will be provided in bold.

1)What does q3( ) print?

def magnitude(n):

if n > 0:

return 1 + magnitude( n // 10 )

else:

return 0

def q3():

print(magnitude(1234567))

The answer for this is 7

2) What does q4( ) print?

def scrub(li,m):
for i in range(len(li)):
if li[i] == m:
li[i] = 0
def scrub_all(li, bad):
for el in bad:
scrub(li, el)
def q4():
ar = [ 3, 7, -5, 10, -17, 20 ]
negs = [ ]
for item in ar:
if item < 0:
negs.append(item)
scrub_all(ar, negs)
tot = 0
for item in ar:
tot += item
print(tot)

The answer is 40

3) Complete the function max_area, consistent with its docstring.

class Rect:
def __init__(self, height, width):
"""Create rectangle. Height and width must be positive."""
assert(height > 0 and width > 0)
self.height = height
self.width = width
def area(self):
return self.height * self.width
def max_area(li):
"""
Find the area of the biggest rectangle in a list.
Args:
li: A list of Rect objects.
Returns:
the maximum of the areas of Rect objects in li.
Returns 0 if li is empty.
Examples:
max_area( [ Rect(5,3), Rect(4,2), Rect(3,3) ]) = 15
max_area( [ Rect(1,2), Rect(2,1), Rect(1,1) ]) = 2
max_area( [ ] ) = 0
"""

#ANSWER FOR THIS QUESTION LISTED BELLOW! IN BOLD
max = 0
for r in li:
if r.area() > max:
max = r.area()
return max

4)Finish the function groups below, consistent with the docstring. Your solution should run in linear time.

def groups(li):
"""
Partition li into groups of identical items
Args:
li: A list of integers
Returns:
A list containing lists of items from li, in the same order
as li. Each sub-list contains a sequence of identical elements.
Examples:
groups( [1, 2, 2, 2, 3, 4, 5, 5, 6] ) = [[1],[2, 2, 2], [3], [4], [5, 5], [6]]
groups( [1, 3, 4] ) = [[1], [3], [4]]
groups( [3, 3, 3] ) = [[3, 3, 3]]
groups( [ ] ) = [ ]
"""
if len(li) == 0:
return [ ]
prev = li[0]
group = [ ]
result = [ ]
for el in li:
if el == prev:
group.append(el)
else:
result.append(group)
group = [ el ]
prev = el
result.append(group)
return result

5)Finish the function reformat below, consistent with the docstring.

def reformat(s, fmt):
"""
Reformat phone number s into pattern fmt.
Args:
s is a string, typically of digits.
fmt is a string in which # is a place-holder for a character from s.
Returns:
A string identical to fmt except each # is replaced by one character
from s, in order. If s has more characters than fmt has #, the extras
go at the end. If s has too few characters, the extra # are discarded.
Examples:
reformat("5413464140", "(###) ###-####") = "(541) 346-4140"
reformat("15413464140", "+# ###.###.####") = "+1 541.346.4140"
reformat("3464140", "(###) ###-####") = "(346) 414-0"
reformat("15413464140", "(###) ###-####") = "(154) 134-64140"
"""
sp = 0
result = ""
for ch in fmt:
if ch != "#":
result += ch
elif sp < len(s):
result += s[sp]
sp += 1
while sp < len(s):
result += s[sp]
sp += 1
return result

Explanation / Answer

Q1:

The magnitude is given a parameter of n = 1234567

as n > 0, it will execute return 1 + magnitude( n // 10 )

which is = 1 + n Modulus 10 = 1 + 123456 = 123457

hence the actual answer would be 123457


Q2:

The positive elements in the array are: [ 3, 7, 10, 20 ]

their sum is = 3+7+10+20 = 40

the for loop iterates through each item in the array called ar

if an item is negative ( <0 ) then it is appended to a separate array called negs

initially the list li stores all numbers – both positive and negative

the scrub function will remove the parameter passed from the list

the function is passing the negs as bad parameter

for each item el in bad array, it is scrubbed or removed from li

hence li will have just the positive numbers

then tot adds it and prints hence 40

Q3:

It first assigns variable max with the value of zero

for each value r in list li,

it compares the r.area with the max value – if greater then it swaps

and returns the latest max value

Q4:

The list li is first grouped into identical terms – like all ones together, all twos together, all 3s together etc

Then it is appended to the result and the result is returned

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote