Answered # Hailstone sequence

pyTony 888 Gribouillis 1,313 abc0502 abc0502 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

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.

*Edited 4 Years Ago by La:)*

1

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

0

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

*Edited 4 Years Ago by Gribouillis*

0

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.

0

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

*Edited 4 Years Ago by abc0502*: Adding Note

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:

```
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click
Dim txtB1 As New TextBox
Dim i
For i = ...
```