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