Home > other >  How to determine if 6 elements are equal in a list with 100 eelements
How to determine if 6 elements are equal in a list with 100 eelements

Time:07-22

So I have this exercise to do : ,,Write a program to find out how often a streak of six heads or a streak of six taiils comes up in a randomly generated list of head and tails and if there is a streak you add to to the variable number_of_steaks''

I made the loop for adding H and T to the list but i don t know how to check if there is s streak in that list I ried this code but i get this error: if th[experiment_number][z] == th[experiment_number][z 1]: IndexError: string index out of range

(Note I am new to programming, I am still learning)

import random


number_of_streaks = 0
th = []
for  experiment_number in range(10000):
    for x in range(100):
        if random.randint(0, 1):
            th.append('H')
        else:
            th.append('T')
    first = 0
    last = 5
    for x in range(100):
        for z in range (6):
            if th[experiment_number][z] == th[experiment_number][z 1]:
                number_of_streaks  = 1

CodePudding user response:

Walk the list, and keep a count of the length of the current streak. If the current item is equal to the previous item, increase the length. Otherwise, reset the counter to 0. When the counter reaches 6, you've found a streak.

CodePudding user response:

The first half of your code works, the second half doesn't.

This code should work:

# The number of consecutive Heads or Tails needed to form a streak
streak_num = 6
# Iterate over the list th, excluding the last 5 (streak_num-1) elements of the list
for n in range(0,len(th)-streak_num 1):
    # Create a list to append values to
    list = []
    # Iterate streak_num times to check for a streak
    for m in range(0,streak_num):
        # Starting from the nth element in the list
        # Append that nth element to the list and the next streak_num-1 elements to the list
        list.append(th[n m])
    # Check to see if all elements in list are the same
    if len(set(list)) == 1:
        # If they are all the same, a streak of size streak_num has been found
        # Therefore add it the count
        number_of_streaks = number_of_streaks   1
    

The part you're having trouble with is the following portion:

# Iterate over the list th, excluding the last 5 (streak_num-1) elements of the list
for n in range(0,len(th)-streak_num 1):

If you are looking for a streak of 6, you have to stop checking the list the 6th last element.

Let me know if you have any questions.

CodePudding user response:

Assumed that a streak is described as the least amount of consecutive identical outcomes. So, if it is 6 then a streak can be made of 6, 7, 8, ... outcomes.

from itertools import groupby
from collections import Counter
import random

# initialize the random number generator - for testing!
random.seed(121)

n_throws = 100
streak_threshold = 6 # exact value or more
outcomes = 'HT'

# flip the coin
experiment = [outcomes[random.randint(0, 1)] for _ in range(n_throws)]

# streaks ids
grps_by_streak = [grp_id for grp_id, grp in groupby(experiment) if len(list(grp)) >= streak_threshold]
print(grps_by_streak)
#['H', 'H', 'T']

# frequency for each result
cc = Counter(grps_by_ streak)
print(cc)
#Counter({'H': 2, 'T': 1})

number_of_streaks = sum(cc.values())
print(number_of_streaks)
#3

If a streak is not a threshold value but an exact one just smalls fixed are needed:

  • replace streak_threshold = 6 to streak = 6 (just for consistency)
  • use equality and change of variable: grps_by_streak = [grp_id for grp_id, grp in groupby(experiment) if len(list(grp)) == streak]
  • Related