Question

Python3: Write the function findLongestStreak(lst) which takes a list of numbers, lst, and returns the longest...

Python3: Write the function findLongestStreak(lst) which takes a list of numbers, lst, and returns the longest streak of numbers which occur in the list. A streak of numbers is a list of numbers where each number is one greater than the one that occurred before it- for example, [4, 5, 6, 7]. So findLongestStreak([3, 4, 8, 2, 3, 4, 5, 7, 8, 9]) would return [2, 3, 4, 5]. If there is more than one streak with the longest length, return the first one that occurs in the list.

Restrictions: for loop, 'in' key word, slicing, lambda function cannot be used.

It is recommended to use while loop to solve this problem

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

# do comment if any problem arises

# do thumbs up if it helps

# this function returns longest streak in given list

def findLongestStreak(lst):

# this is temprary variable to current list which is longest streak

current = [lst[0]]

# this is return variable which holds longest streak named prev ie previous longest streak

prev = []

# length is length of given lst

length = len(lst)

# initiailise i to 1 this variable is to create a while loop till length of given list

i = 1

while i != length:

# if current element is one greater than previous element

if(lst[i] == 1+lst[i-1]):

# append current element to current list

current.append(lst[i])

# else if length of previous longest streak is less than current streak

# copy current list to previous list

else:

if(len(prev) < len(current)):

n = len(current)

j = 0

prev=[]

while j != n:

prev.append(current[j])

j += 1

# clear current list and append current element to this list

current=[]

current.append(lst[i])

# increment i

i += 1

# if length of previous longest streak is less than current streak

# copy current list to previous list

# this case works if last element is included in longest streak

if(len(prev) < len(current)):

n = len(current)

j = 0

prev=[]

while j != n:

prev.append(current[j])

j += 1

return prev

# testing above function

print(findLongestStreak([3, 4, 8, 2, 3, 4, 5, 7, 8, 9]))

Screenshot of code:

Output:

Add a comment
Know the answer?
Add Answer to:
Python3: Write the function findLongestStreak(lst) which takes a list of numbers, lst, and returns the longest...
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
  • Python3: Write the function longestSubpalindrome(s), that takes a string s and returns the longest palindrome that...

    Python3: Write the function longestSubpalindrome(s), that takes a string s and returns the longest palindrome that occurs as consecutive characters (not just letters, but any characters) in s. so longestSubpalindrome("ab-4-be!!!") returns "b-4-b".If there is a tie, return the lexicographically larger value -- in Python, a string s1 is lexicographically greater than a string s2 if (s1 > s2). So: longestSubpalindrome("abcbce") returns "cbc", since ("cbc" > "bcb"). Note that unlike the previous functions, this function is case-sensitive (so "A" is not...

  • Python3 : Write the function longestRun(s, chars) that takes a possibly-empty string s and a second...

    Python3 : Write the function longestRun(s, chars) that takes a possibly-empty string s and a second possibly-empty string of chars. We will say that a character is "good" if it is in the chars string (case insensitively, so "A" and "a" would match). The function should return the length of the longest consecutive run of good characters in the given string s. For example, consider: longestRun("abbcazBbcababb","bz"). This returns 3 (look for "zBb"). Restrictions: for loop, slicing cannot be used, if...

  • #We've written the function, sort_with_bubbles, below. It takes #in one list parameter, lst. However, there are...

    #We've written the function, sort_with_bubbles, below. It takes #in one list parameter, lst. However, there are two problems in #our current code: # - There's a missing line # - There's a semantic error (the code does not raise an # error message, but it does not perform correctly) # #Find and fix these problems! Note that you should only need #to change or add code where explicitly indicated. # #Hint: If you're stuck, use an example input list and...

  • Using Python3 Write a function that takes an array of integers and returns it in increasing...

    Using Python3 Write a function that takes an array of integers and returns it in increasing order. There can be negative numbers, but they are treated as positive numbers. Example list= list=[-6,7,6,7,-9,1,0,-3,-2,1,4] Answer list= [0, 1, 1, -2, -3, 4, 6, -6, 7, 7, 3]

  • Python3 command line arguments; there are unknown number of arguments, 'only number // number and the...

    Python3 command line arguments; there are unknown number of arguments, 'only number // number and the length of the argument is three' is a valid argument,for example,2//5,3//5 are valid arguments,a//2,b//4,7//0,b//k are not valid arguments, calculate each valid argument and the number of valid arguments, and then print hint: split is very useful restrictions: [1:], for loop, slicing, lambda function, 'in' keyword,isdigit(), isalpha() cannot be used. Exception handling must be used, it is recommended to use while loop to solve this...

  • Please use DrRacket Programming Write a structurally recursive function named (tails lst) that takes a list...

    Please use DrRacket Programming Write a structurally recursive function named (tails lst) that takes a list as an argument and returns all the sublists of the list. For example: > (tails '(1 2 3)) '((1 2 3) (2 3) (3) ()) > (tails '((a b) (c d))) '(((a b) (c d)) ((c d)) ()) Think carefully about what the function should return if it receives the empty list as an argument. Finish this template: (define tails (lambda (lst)

  • PYTHON The function longest that returns the longest of two strings. The function count_over that takes...

    PYTHON The function longest that returns the longest of two strings. The function count_over that takes a list of numbers and an integer nand returns the count of the numbers that are over n. The function bmi that takes two parameters height and weight and returns the value of the body mass index: a person's weight in kg divided by the square of their height in meters. def longest(string1, string2): """Return the longest string from the two arguments, if the...

  • Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...

    Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • 2. Write a scheme function get-even-nums that accepts a list of numbers and returns a new...

    2. Write a scheme function get-even-nums that accepts a list of numbers and returns a new list that contains only the even numbers. (Hint: use reminder function) Sample runs: (get-even-nums ‘(1 2 3 4 5 6 7 8 9)) should return (8 6 4 2). (get-even-nums ‘(1 3 5 7 9)) should return ’( ).

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