Question

Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input aQuestion 1b: Increasing Numbers in List - Longest Streak (3 points) Now write a function numIncreasing2(L) which applies simi

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

# do comment if any problem arises

# Code

Q->1b:

def numIncreasing2(s):

    x=[]

    # temporary list

    temp=[s[0]]

    # iterate over s

    for i in range(1,len(s)):

        # check if current order is increasing

        if s[i]>s[i-1]:

            temp.append(s[i])

        else:

            # check for size of temporary list with x

            if len(temp)>len(x):

                # if yes then copy temp to x

                x=temp[:]

            temp=[s[i]]

    # for last element

    if len(temp)>len(x):

        x=temp[:]

    return x


print("Are my sample test cases correct?")

print("#1",numIncreasing2([5,4,3,2,1])==[5])

print("#2",numIncreasing2([1,2,3,4,5])==[1,2,3,4,5])

print("#3",numIncreasing2([1,2,3,4,4,3,4,5,6,7,7])==[3,4,5,6,7])

Screenshot:

5 11 def numIncreasing2 s): x=[] # temporary list temp= [s[@]] # iterate over s for i in range(1, len(s)): # check if current

Output:

Are my sample test cases correct? #1 True #2 True #3 True

Q->1c:

def numIncreasing(L, longest=False):

    if longest:

        return numIncreasing2(L)

    else:

        return numIncreasing1(L)

Screenshot:

40 42 43 def numIncreasing ( Z, longest=False): if longest: return numIncreasing2(L) else: return numIncreasingi(L) 44 45 46

Add a comment
Know the answer?
Add Answer to:
Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...
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
  • 2. Consider the following function # listOfNumbers is a list of only numbers # def processList(listOfNumbers):...

    2. Consider the following function # listOfNumbers is a list of only numbers # def processList(listOfNumbers): result = [] for i in listOfNumbers: if i<0 == 0: result.append(i*i) else: result.append((i*i)+1) return result First, study and test processList(listOfNumbers) to determine what it does Then rewrite its body so that it accomplishes the same task with a one-line list comprehension. Thus, the resulting function will have exactly two lines, the def line and a return line containing a list comprehension expression. 3....

  • BlockPy: #33.8) Maximum Odd Use the Min/Max pattern to write a function maximum_odd that finds the...

    BlockPy: #33.8) Maximum Odd Use the Min/Max pattern to write a function maximum_odd that finds the highest odd value in the list (consumes a list of numbers and returns a number). Use the provided helper function is_odd (that consumes a single number and returns a boolean indicating whether it is true or false). Do not change the helper function is odd. Call your maximum_odd function on your favorite list of numbers Console Feedback: Instructor Feedback You cannot use the builtin...

  • I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this func...

    I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this function is to calculate the similarity measure between one data segment and the pattern. The first parameter 'data_segment' is a list of (float) values, and the second parameter 'pattern' is also a list of (float) values. The function calculates the similarity measure between the given data segment and pattern and returns the calculated similarity value (float), as described earlier in this assignment outline. The...

  • 2. This function compares two numbers and returns them in increasing order. 1. Fill in the...

    2. This function compares two numbers and returns them in increasing order. 1. Fill in the blanks, so the print statement displays the result of the function call in order. 1 # This function compares two numbers and returns them 2 # in increasing order. 3 - def order_numbers(number1, number2): 4. if number2 > number1: return numberi, number2 6 else: return number2, number1 9 Run # 1) Fill in the blanks so the print statement displays the result # of...

  • def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have...

    def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have a value greater than the average value in the list. • Parameters: num_list is a list of numbers (mixed integers and floats) • Examples: sum_gt_avg([1,2,3,4,5]) → 9 # 4+5 sum_gt_avg([1,2,3,-4,5]) → 10 # 2+3+5 sum_gt_avg([-1,-2,-3,-4,-5]) → -3 # -1-2

  • Assume that L is a list of Boolean values, True and False. Write a function longestFalse(L)...

    Assume that L is a list of Boolean values, True and False. Write a function longestFalse(L) which returns a tuple (start, end) representing the start and end indices of the longest run of False values in L. If there is a tie, then return the first such run. For example, if L is False False True False False False False True True False False 0 1 2 3 4 5 6 7 8 9 10 then the function would return...

  • Using Racket, write a bridgely? function that takes a list of at least 3 numbers and...

    Using Racket, write a bridgely? function that takes a list of at least 3 numbers and returns #true if it is bridgely, otherwise #false. A list of numbers is called "bridgely" if it contains at least 3 numbers, and every number in the list, except for the first and the last, is greater than both the first and the last number. Thus, these lists are bridgely: (list 1 2 3 4 5 6 5 4 3 2 1) (list 0...

  • 6 Write a function named extract_lesser (L, v) that, from a list of numbers It finds...

    6 Write a function named extract_lesser (L, v) that, from a list of numbers It finds all of the 05 values less than v, puts them into a new list, called Less_list, and returns that new list (abai 4) def extract_lesser (L, v): Less_list = [] for ... if num <v: return Less list أدخل إجابتك 7

  • Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e....

    Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e. no duplicated integers), and should return: 1. List (of tuples) containing all pairs of numbers in L that are coprime. 2. Empty list, If no pair of numbers in L is coprime. Your solution must include a function call to the function coprime designed in the previous question. The order of the tuples or the order of the two numbers within the tuples is...

  • 1. Question: Write a function that takes a list L and returns a random sublist of...

    1. Question: Write a function that takes a list L and returns a random sublist of size N of that list. Assume that the indexes must be in increasing order. That is, you cannot go backwards Example L [1, 2, 3, 4, 5] 5 The function should return one of these lists: [2, 3, 4] [2, 3, 5] [2, 4, 5] [3, 4, 5]

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
ADVERTISEMENT