I am trying to write a program to generate hailstone sequences for different starting values of n and have achieved this. Now however, I am trying to find the average length of the sequences and what the longest and shortest sequences are in the range(5,1000)and am not sure how to go about this. Have tried a few things but with out much success! Thank you.

Give error message and relevant inputs with problematic code and explain how the output/result differ from your expectations.

commented: great answer +13

Start by making a list containing the lengths of the 1000 sequences. Tony is right, we're dying to see the code.

Member Avatar for abc0502

This can be acheived by appending the result of the hailstone function to a dictionary, where the key will be the number and the values will be a list of the sequences.

Then using two for/loops you get the total number of the keys and the total number of all elements in the lists "the sequences" and divide them that will give you the average.

and about the shortest and longest sequence can be done also by a dictionary. i will post the code as soon as i finish.

Member Avatar for abc0502

Hi agian :)
This is my code, i made my own hailstone sequence cause la:) didn't add his to test on it, but this should work for him too.
Please note that the average will contain the last 6 digits 4,2,1,4,2,1 in the calculation.
Test it with known data first, i'm not a python expert :)

# python 3.2

# Hailstone sequences
def hailstone(Num):
    Num = int(Num)
    List = []
    List.append(Num)
    while True:
        if Num % 2 == 0:
            List.append(int(Num/2))
            Num = List[-1]
            if List[-1] == 1 and List[-2] == 2 and List[-3] == 4 and List[-4] == 1 and List[-5] == 2 and List[-6] == 4:
                break            
        else:
            if Num % 2 != 0:
                List.append(int((Num*3)+1))
                Num = List[-1]
                if List[-1] == 1 and List[-2] == 2 and List[-3] == 4 and List[-4] == 1 and List[-5] == 2 and List[-6] == 4:
                    break
            else:
                break
    return List


result = {}
for i in range(5,13):           # <<< Add your range here >>>
    result[i] = hailstone(i)    # Append each number and it's sequence to a dictionary "result"

keys = 0                        # Total Keys in result
values_length = 0               # Total lengeth of all lists belong to the keys
length_dict = {}                # 2nd Dictionary for saving each number and it's sequnce length
for i in result:
    keys += 1                           # getting total number of all keys "numbers"
    values_length += len(result[i])     # getting total count number of all elements in all sequences
    length_dict[i] = len(result[i])     # appending each number with it's sequence length to a new dictionary

average = values_length / keys          # calculating the average value
print("average length of the sequences: ", average)

"""\
    All Credits for getting longest and shortest valeues from
    a dictionary Goes to "the wolf" from "Stackoverflow"
    """
value = list(length_dict.values())
key = list(length_dict.keys())
max_len = key[value.index(max(value))]  # max sequence lenght
min_len = key[value.index(min(value))]  # min sequence length

# This will show the Numbers that has the longest and shortest sequences, not the lenght of the sequence
print("Number That Has The Shortest Sequence: ", min_len)
print("Number That Has The Longest Sequence: ", max_len)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.