Question

python a) Write a function, hailstone(), that takes an integer value n as a parameter, and...

python
a) Write a function, hailstone(), that takes an integer value n as a parameter,
and displays the hailstone sequence for the given integer. The hailstone sequence
is determined as follows: if the value is even, divide by 2 (floor division) or if the
value is odd, calculate 3 * n + 1. The function should display each value and
continue updating the value until it becomes 1.
b) Write a program to display the hailstone sequence of all integers between 5
and 10.
Sample Run:
5 16 8 4 2 1
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
and also write a docstring comment for it

Homework Answers

Answer #1

Please find the code, inline comments and output below.

a)

def hailstone(number,steps = 0):

print(number, end = " ");
if (number == 1 and steps == 0):
return steps;
elif (number == 1 and steps != 0):
steps = steps + 1;
elif (number % 2 == 0):
  
# If number is Even.
steps = steps + 1;
steps = hailstone(int(number / 2), steps);
elif (number % 2 != 0):
  
# If number is Odd.
steps = steps + 1;
steps = hailstone(3 * number + 1, steps);
return steps;
  
# Driver Code
number= int(input("Enter the number : "));
  
# Function call to generate Hailstone Numbers
x = hailstone(number);
  


  

b)

Write a program to display the hailstone sequence of all integers between 5 and 10.


''' Program to display the hailstone sequence of all integers between 5 and 10 '''

def hailstone(number,steps = 0):

print(number, end = " ");
if (number == 1 and steps == 0):
return steps;
elif (number == 1 and steps != 0):
steps = steps + 1;
elif (number % 2 == 0):
  
# If number is Even.
steps = steps + 1;
steps = hailstone(int(number / 2), steps);
elif (number % 2 != 0):
  
# If number is Odd.
steps = steps + 1;
steps = hailstone(3 * number + 1, steps);
return steps;
  
# Driver Code to display the hailstone sequence of all integers between 5 and 10

number=5
while(number<11):

print( "\nHailstone Numbers Sequences for "+str(number)+" is : ")
# Function call to generate Hailstone Numbers
x = hailstone(number);
number +=1

Know the answer?
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
Similar Questions
In C++ 1) Write the function definition that has 1 pass by value integer parameter. The...
In C++ 1) Write the function definition that has 1 pass by value integer parameter. The function contains logic to return true if the parameter is even and false if it is not. 2 ) Write a main function that prompts the user for a value, calls the function that you created in step one with the value entered by the user, display "even" if the function return true and otherwise displays "odd"
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Python question: Write a function that takes a number and an integer (X,N) in a time...
Python question: Write a function that takes a number and an integer (X,N) in a time complexity of O(log(N)). for an example: calling power(2, 2) would return 4
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2) We repeat this...
Write a Python program that prompts a user for two integers and stores the two integers...
Write a Python program that prompts a user for two integers and stores the two integers the user types into the shell. Print the product, float division, integer division, remainder, sum, and difference of the two integers to the console. The entire equation must be printed (see below for formatting). Only display 2 digits after the decimal place for results that are floats. Include a module docstring that summarizes the program.. Sample Run 1: Enter an integer: 5 Enter another...
PYTHON: Write a function that takes in a number as a parameter and calculates the square...
PYTHON: Write a function that takes in a number as a parameter and calculates the square of the number up to and including the value of the parameter. The function must perform the process recursively.
Python: Write a recursive function that takes in a number as a parameter and calculates the...
Python: Write a recursive function that takes in a number as a parameter and calculates the square of the number up to and including the value of the parameter.
write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The...
write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The function should return the sum of all the integers x, x-2, x-4, x-6, etc. as long as the numbers are positive. For example, [sum-alternate 5] should evaluate to 5 + 3 + 1, and [sum-alternate 6] should evaluate to 6+4+2.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT