Question

Write another function buddies(...) that receives a list (with at least 3 integer numbers), and returns...

  1. Write another function buddies(...) that receives a list (with at least 3 integer numbers), and returns the number of pairs of buddies in the list. We will define a “pair of buddies” to be the pair of elements that are contiguous in the list and that are equal, and also so that the contiguous elements to the pair are different (or the pair is in an extreme of the list). For example, [5,2,2,3] has 1 pair of buddies (the 2’s) , and both 5 and 3 are different than 2, so the function would return 1 in this case. The function would return 0 if the list is [2,2,2,3], since there are three 2’s together, and so they are not considered buddies. [4,4,5] and [4,5,5] are considered to have 1 pair of buddies each.

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

buddies.py - D:/Coding/Python/buddies.py (3.7.4) - O File Edit Format Run Options Window Help def buddies (1): #Variable to s

OUTPUT

==================== RESTART: D: /Coding/Python/buddies.py >>> buddies ([5,2,2,3]) >>> buddies ([2,2,2,3]) >>> buddies ( [4,4

CODE :

def buddies(l):
#Variable to store ans
ans = 0
#Variable to store continuous equal elements
c = 1
#Loop from 1 to len(l)
for i in range(1,len(l)):
#Check if current element is equal to previous element.
if l[i]==l[i-1]:
c = c+1
else:
#See if there are 2 continous elements. Increment the answer
if c==2:
ans = ans+1
c = 1
#Check after the iteration
if c==2:
ans = ans+1
return ans

Add a comment
Know the answer?
Add Answer to:
Write another function buddies(...) that receives a list (with at least 3 integer numbers), and returns...
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
  • In PYTHON: Write a function that receives a list of integers and returns the number of...

    In PYTHON: Write a function that receives a list of integers and returns the number of integers that are larger than the element immediately before them. For example, given [1,2,3,-5,0,-5,-10] the function should return 3 (since 1<2, 2<3, and -5<0).

  • For Python: Write a function that receives 3 numbers as arguments and returns “1” if they...

    For Python: Write a function that receives 3 numbers as arguments and returns “1” if they are all the same, and "0" is they are all different. Otherwise, the function returns "-1".

  • Haskell Define function-divisors that receives a number and returns a list of all divisors of that...

    Haskell Define function-divisors that receives a number and returns a list of all divisors of that number.For example, if the input is 20, then the output would be[1,2,4,5,10,20]. You may use list comprehension, list ranges, and function mod for this purpose.Hint:Given numbern, consider all numbers from 1 ton, and then keep only the ones that dividen.

  • Define a function add_nums_x_pos_given_list_nums (..) which receives a list that is guaranteed to only have numbers...

    Define a function add_nums_x_pos_given_list_nums (..) which receives a list that is guaranteed to only have numbers (or the empty list) and returns the sum of each number multiplied by the position where the number is. (In the case of the empty list the function it will return 0) For example add_nums_x_pos_given_list_nums ([10,20,30]) will return the value 80 which is the result of adding: 0 * 10 (10 is in position 0 in the list) + 1 * 20 + 2...

  • 5) Define a function called remainder _is_even which receives two positive integer numbers as parameters: num...

    5) Define a function called remainder _is_even which receives two positive integer numbers as parameters: num and div. This function should return a boolean value. The value to be returned should be True if the remainder of dividing num by div is even and it should return False otherwise. As an example, the following code fragment: print (remainder_is_even(23,2)) should produce the output: False 6) Define a function first_last_repeated which receives as input parameter a string (orig) with at least one...

  • 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...

  • Write a Python function that creates and returns a list of prime numbers between 2 and...

    Write a Python function that creates and returns a list of prime numbers between 2 and N, inclusive, sorted in increasing order. A prime number is a number that is divisible only by 1 and itself. This function takes in an integer and returns a list of integers.

  • PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as...

    PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as parameters. The function randomly chooses n key-value pairs and removes them from the dictionary d. If n is greater than the number of elements in the dictionary, the function prints a message but does not make any changes to d. If n is equal to the number of elements in d, the function clears the dictionary. Otherwise the function goes through n rounds in...

  • 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,...

  • In Javascript Define 2 functions in JSBin: - A function which receives an array of numbers...

    In Javascript Define 2 functions in JSBin: - A function which receives an array of numbers as an argument, and returns the product of all the numbers in that array.   For example, multiply([2,3,4]) should return 24. - A function which receives a string as an argument, and returns the reversal of that string.   For example, reverse('Hello there!') should return "!ereht olleH"

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