Question

In python, please help me fill in these functions given their limitations. def positionalSum(a, b): """...

In python, please help me fill in these functions given their limitations.

def positionalSum(a, b):

"""

Given 2 lists, add the 2 lists together. The list will be as long as the shortest list.

Input: 2 lists of numbers

Return: A new list of the size of the smaller list of the 2 lists added together positional.

Example:

>>> positionalSum([1, 2, 3, 4], [4, 3, 2]) # [5, 5, 5]

>>> positionalSum([1, 2, 3, 4], [1, 1, 1, 4]) # [2, 3, 4, 8]

"""

pass

def scalarMatrix(n, m):

"""

Given a matrix, size a x b, multiply each position by n.

Input: an number to multiply each item in the matrix (list of list)

Return: A new list of lists that multiplies all values by n

Limitation: No using numpy

Examples: n = 3

[[4, 4, 4]

[1, 2, 4]

[10, -1, 2]]

Result:

[[12, 12, 12]

[3, 6, 12]

[30, -3, 6]]

"""

pass

def isSubString(sub, longStr):

"""

Determine if a string exists in another string.

Not allowed to check for direct membership. You need to use looping

Input: 2 strings

Returns: Boolean if there is substring.

Limitation: No "in" membership

Example:

>>> isSubString("cat", "Concatenate") # True

>>> isSubString("orange", "Concatenate") # False

>>> isSubString("acsd", "car") # False

"""

pass

def whereSubString(sub, longStr):

"""

Determine last position of the start of a substring occurs.

Not allowed to check for direct membership. You need to use looping

Input: 2 strings

Returns: Integer showing where the substring starts in the longStr. Return -1 if

the substring doesn't exist

Limitation: No "in" membership

Example:

>>> whereSubString("cat", "Concatenate") # 3

>>> whereSubString("orange", "Concatenate") # -1

>>> whereSubString("acsd", "car") # -1

"""

pass

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def positionalSum(a, b):
if len(a)>len(b): #check for the smaller list
for i in range(len(b)): #traverse the list as per the length of list
b[i]+=a[i] #add element from the longer list to the shorter one
return b #return the horter list
elif len(a)<len(b):
  
for i in range(len(a)):
a[i]+=b[i]
return a
else:
for i in range(len(a)):
a[i]+=b[i]
return a

def scalarMatrix(n, m):
y=[] #an empty list which will get appended with the sub lists to create the 2d matrix
for i in m: #traverse the list entered
z=[] #empty list which will be loaded with elements and re-intialized to empty list onceappended to the y list
for j in i: #traverse elements of sub-list
z.append(j*n) #multiply each element with the entered number and append to z
y.append(z) #once all memebers of the sub-list are multiplied and z is created, it will be appened to the y list
return y

def isSubString(sub, longStr):
for j in range(len(longStr)): #traverse the longer string as per the length of string
#if the first alphabet of sub matched with a alphabet in longstr then check the subsequent string of sub's length
#if it matches completely with sub
if(sub[0]==longStr[j] and sub==longStr[j:j+len(sub)]):
return True
return False

def whereSubString(sub, longStr):
for j in range(len(longStr)):
if(sub[0]==longStr[j] and sub==longStr[j:j+len(sub)]):
return j
return -1

Add a comment
Know the answer?
Add Answer to:
In python, please help me fill in these functions given their limitations. def positionalSum(a, b): """...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT