Question

Write a program that generates a sequence of 20 random die tosses and that prints the...

Write a program that generates a sequence of 20 random die tosses and that prints the die values, marking only the longest run, like this:

1 2 5 5 3 3 2 4 3 (2 2 2 2) 3 6 5 5 6 3 1

programming language: python

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

Program Code to copy

import random

a = [None]*20
#Generating 20 random numbers
for i in range(20):
a[i] = random.randint(1,6)
print("Random numbers: ", a)

count = 1
index = -1
start = -1
maxCount = 0
for i in range(1, len(a)-1):
  
#if previous and current element are same increment count
if a[i] == a[i-1] :
count = count+1
#if previous and current element are not same update maxCount and reset count   
elif a[i] != a[i-1] :
if maxCount < count :
maxCount = count
lastIndex = i-1
count = 1

startIndex = lastIndex - maxCount + 1   
# print(startIndex)
# print(lastIndex)

#Printing array as required in output
for i in range(len(a)) :
# print(i)
if i == startIndex :
print("(", end = "")
print(a[i], end = " ")
  
elif i == lastIndex :
print(a[i], end = "")
print(") ", end = "")
else:
print(a[i], end = " ")
  

Program Screenshot

Program Output

Note: Please refer to screenshot for in case of indentation error.

Add a comment
Know the answer?
Add Answer to:
Write a program that generates a sequence of 20 random die tosses and that prints the...
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