Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import string from graphics import * # tells the user what the program does def

ID: 3632456 • Letter: I

Question

import string
from graphics import *

# tells the user what the program does
def printGreeting():
print "This program finds hailstone sequences of numbers that you input."
print "The output numbers are found based on if it is odd or even."
print "If the number is odd, it is multiplied by 3 and then 1 is added to"
print "it. If the number is even its divided in half."
# gives the user menu options
def printMenu():
print " I : View sequences for an Individual value "
print " R : View sequences for a Range of Values "
print " L : find the Longest Chain "
print " H : view a Histogram of chain lengths for a range "
print " Q : Quit "

# returns number if it is odd
def isOdd(n):
return n % 2 == 1

# creates a sequence of numbers and prints them out
def hailstone(num):
sequence = [num]
print " %d -->>" % num,
while num != 1:
# odd number is multiplied by 3 and then adds 1
# number is added to sequence
if isOdd(num):
sequence.append(num)
num = (num * 3) + 1
# even number is divided by 2
# number is added to sequence
else:
sequence.append(num)
num = num / 2
# prints numbers
if num != 1:
print num, "-->>",
else:
# determines length of sequence and prints length
length = len(sequence)
print "; length = ", length
return length
# finds the longest sequence in a range of numbers does not print them out
# for the L option on menu
def hailstone2(num):
length = 1
while num != 1:
if isOdd(num):
num = (num * 3) + 1
length = length + 1

else:
num = num / 2
length = length +1
return length



# error checks for bad numbers
def getValidInt(question, min, max):
# use a bad value to enter the loop
value = max + 1

# compose the prompt
prompt = question + " (" + str(min) + "-" + str(max) + "): "

# continue to get values until the user enters a valid one
while value == "" or value < min or value > max:
value = raw_input(prompt)
if len(value) != 0:
value = int(value)

# return a valid value
return value

def longChain():

quest1 = "Enter the starting interger "
quest2 = "Enter the ending interger "

startNum = getValidInt(quest1, 1, 10000)
stopNum = getValidInt(quest2, startNum + 1, 10000)

largestChain = startNum

for i in range(startNum, stopNum+1):
if largestChain <= hailstone2(i):
largestChain = i
length = hailstone2(i)

print largestChain, " had the longest chain ", length

def histogram():
# initialize variables
longestLength = 1
list = []

start = input("Please enter the beginning interger for the range: ")
for n in range(start, start + 10):
length = hailstone2(n)
list.append(length)
if longestLength <= hailstone2(n):
longestLength = n
length = hailstone2(n)
print longestLength
print list

def main():

choice = ''
quest1 = "Enter the starting interger "
quest2 = "Enter the ending interger "
quest3 = "Enter a number"
printGreeting()

# runs program until they quit
while choice != "Q" and choice != "q":
printMenu()
choice = raw_input("Enter your Choice: ")

# runs program for numbers in range
if choice == "r" or choice == "R" :
start = getValidInt(quest1, 1, 10000)
stop = getValidInt(quest2, start, 10000)

for i in range(start, stop +1):
hailstone(i)

elif choice == "i" or choice == "I":
indVal = input("Enter a number (1-10000): ")
hailstone(indVal)

elif choice == "l" or choice == "L":
longChain()

elif choice == "h" or choice =="H":
histogram()

else:
print choice, "is not a valid choice"

printMenu()
choice = input("Please enter your choice : ").upper()
BUT I WANT TO use some methods from the graphics library to draw a histogram of chain lengths for a range of values using the drawHistogram() function. This histogram will be drawn in a window thats is 500*500 and entitle it as "Histogram of chainlength".For the y axis,The length of the longest chain plus some room for the labels at the bottom where n is shown.For the x axis, We want to show bars for 10 values and histograms look better if there is some space between the bars AND we'll need some space on the left for the lengths to be printed. Let's try 20.i want to have a range of 10 values and just let the user pick the starting value.want to allow the user to draw histograms for more than one range of numbers during the running of the program, we'll want to close the window after having enough time to view it. You should close the window after 10 seconds. You'll need to use sleep() to do this.
All handling of the graphics window should be done within the drawHistogram() function.histogram should look like this
http://www.cs.umbc.edu/courses/201/fall11/images/histo2.jpg
please i will need much help, i forever appreciate it.
thanks in advance
i will rate as soon as possible

Explanation / Answer

# Filename:    hw6.py
# Date:        10/16/09
# Section #:   06
# Description:
# This file contains <strong class="highlight">python</strong> code for hailstone sequence.
# It follows the rules, if a number is even, it will divide the
# next number by two, if the number is odd, it will multiply the next
# number by three and then add 1. This will continuie forming a chain
# until the last number is 1.

# this function prints the chain of numbers using the rules
# also adds up the length chain
# Inputs : n
# Outputs : none

import string
# from graphics import *
from graphics import *

#Constant
MAX = 10000

# printing functions

# this function prints a brief description of the program to the user
# Inputs: none
# Outputs : none
def printGreeting():
    print ""
    print " This program finds hailstones sequences of numbers"
    print " you choose. The next number <strong class="highlight">in</strong> the sequence if found by"
    print " either dividing it by two(if even) or multiplying by 3"
    print " and adding 1(if odd).The program quits when the last"
    print " number <strong class="highlight">in</strong> the sequence is 1 "

# this functions prints the menu for the user
# Inputs: none
# Outputs : none
def printMenu():
    print " Here are your menu choices:"
    print " I - view squence for an individual value "
    print " R - veiw sequence for range of values "
    print " L - Find the longest chain "
    print " H - Print out the histogram "
    print " Q - Quit "

# end of printing funtions


# hailstone(number) prints the hailstone sequence
# Inputs: number
# Outputs: none
def hailstone(n):
   
    length = 1
    print n,
    # checks if n is not sential
    while n != 1:
       
        # if even, divide by 2 (rule). add 1 to length
        if n % 2 == 0:
            n = n / 2
            print "->",n,
            length = length + 1
           
        # if odd, multiply by 3, add 1 (rule). add 1 to length
        elif n % 2 != 0:
            n = ( 3 * n ) + 1
            print "->",n,
            length = length + 1
           
    # print the length at the end of each chain
    print "; length =",length
    print "----------------------------------------------------------------",
    print "-------------- "
    return length

# this function returns the length of each chain from the starting number, n
# Inputs : n
# Outputs : none
def chain(n):
  
    length = 1
    while n != 1:
       
        # if even, divide by 2 (rule). add 1 to length
        if n % 2 == 0:
            n = n / 2
            length = length + 1
           
        # if even, divide by 2 (rule). add 1 to length
        elif n % 2 != 0:
            n = ( 3 * n ) + 1
            length = length + 1
    return length

# getValidInt() prompts the user to enter an integer <strong class="highlight">in</strong> the specified range,
# rejects values not <strong class="highlight">in</strong> that range by requiring new input, and only returns
# a valid integer.
# Inputs: the question of the prompt,
#         the minimum value <strong class="highlight">in</strong> the range
#         and the maximum value <strong class="highlight">in</strong> the range
# Output: an integer <strong class="highlight">in</strong> the specified range
def getValidInt(question, min, max):
   
    # use a bad value to enter the loop
    value = max + 1

    # compose the prompt
    prompt = question + " (" + str(min) + "-" + str(max) + "): "
   
    # continue to get values until the user enters a valid one
    while value == "" or value < min or value > max:
        value = raw_input(prompt)
        if len(value) != 0:
            value = int(value)

    # return a valid value
    return value


# this function finds the longest chain
# Inputs: none
# Outputs : none
def longChain():
     begin = "Please enter the begining integer for the range"
     end = "Please enter the ending integer for the range"

     # calls to getValidInt for starting and ending values
     beginNum = getValidInt(begin, 1, MAX)
     endNum = getValidInt(end, beginNum + 1, MAX)

     largestChain = beginNum

     for i <strong class="highlight">in</strong> range(beginNum, endNum+1):
         if largestChain <= chain(i):
             largestChain = i
             length = chain(i)
            
     print largestChain, " had the longest chain ", length

# this function finds the longest chain***************************8
# Inputs: none*************
# Outputs : none   
def histogram():
    # initialize variables
    longestLength = 1
    list = []

    start = input("Please enter the begining integer for the range: ")
    for n <strong class="highlight">in</strong> range (start, start + 10):
        length = chain(n)
        list.append(length)
        if longestLength <= chain(n):
            longestLength = n
            length = chain(n)
        print longestLength
        print list

def main():
   
    # prints the greeting to the user
    printGreeting()

    # prints the menu to the user
    printMenu()

    # asks user the menu choice
    choice = raw_input("Please enter your choice : ").upper()
   
    # checks to see if choice entered is from the menu
    while choice != 'Q':

        # if choice is "I" or "i", proceeds to follow the individual steps
        if choice == 'I':
            n = input("Please enter your integer (1-10000):")
            hailstone(n)

        # if choice is "R" or "r", proceds print each number and its sequence
        # until the last number is 1, uses getValidInt to get valid integers
        # for the function
        elif choice == 'R':
           
            begin = "Please enter the begining integer for the range"
            end = "Please enter the ending integer for the range"
           
            # calls to getValidInt for starting and ending values
            beginNum = getValidInt(begin, 1, MAX)
            endNum = getValidInt(end, beginNum + 1, MAX)

            # for loop to get the values between starting and ending value
            for n <strong class="highlight">in</strong> range(beginNum,endNum+1):

                #call to the hailstone function
                hailstone(n)

        # if choice is "L" or "l", proceeds to use getValidInt again to get the
        # range, error checks on the range, and then calls the function chain
        # to determine the length.
        elif choice == 'L':
            # call to function longchain
            longChain()

        elif choice == 'H':
            histogram()
     
        # if niether of the menu choices, then it prints that the
        # entered text is not a valid choices
        else:
            print choice, "is not a valid choice"

        # prints the menu to the user
        printMenu()

        # asks user the menu choice
        choice = raw_input("Please enter your choice : ").upper()
     
main()