Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,697 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,512 people online right now. Registration is fast and FREE... Join Now!




coding in One python file

 
Reply to this topicStart new topic

coding in One python file

rsidhu
post 6 Oct, 2008 - 02:30 PM
Post #1


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 1


You should solve the problem and write your code in ONE python
file. When you are done, you SHOULD COMPRESS the file into one zip
file and SUBMIT THE COMPRESSED FILE ONLY (for example,
Assignment1.zip). Submission should be made ONLINE through the
PORTAL. NO OTHER FORM OF SUBMISSION will be accepted.
Therefore, if you do not submit online through the portal, you will get ZERO
for this assignment.
Problem: A CONJECTURE
[100 Marks]
Consider the following algorithm:
1. input n
2. print n
3. while n ~= 1
4. if n is odd then n = 3n+1
5. else n = n/2
Given the input 22, the following sequence of numbers will be printed by the above
algorithm:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any
integral input value. Despite the simplicity of the algorithm, it is unknown whether this
conjecture is true. It has been verified, however, for all integers n such that 0 < n <
1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed (including the
1). For a given n this is called the cycle-length of n. In the example above, the cycle
length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all
numbers between i and j (inclusive).
INPUT SPECIFICATION
The input will consist of a series of pairs of integers i and j (i may be greater than j and
vice-versa). All integers will be less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle
length over all integers between and including i and j.
OUTPUT SPECIFICATION
For each pair of input integers i and j you should output the maximum cycle length for
integers between and including i and j. The string to print is as follows:
The Maximum Cycle Length is:
SAMPLE INPUT and OUTPUT
Case 1:
Please Enter the Value for i: 1
Please Enter the Value for j: 10
The Maximum Cycle Length is: 20
Case 2:
Please Enter the Value for i: 100
Please Enter the Value for j: 200
The Maximum Cycle Length is: 125
Case 3:
Please Enter the Value for i: 201
Please Enter the Value for j: 210
The Maximum Cycle Length is: 89
Case 4:
Please Enter the Value for i: 900
Please Enter the Value for j: 1000
The Maximum Cycle Length is: 174
ACKNOWLEDGEMENTS
The original version of this assignment was set as a programming challenge on ACM
(http://acm.uva.es/p/v1/100.html). This is slightly modified in the sense that in the
original problem you have to read from files and you have a time limit within which you
should produce your output.
User is offlineProfile CardPM

Go to the top of the page


Nova Dragoon
post 7 Oct, 2008 - 05:48 AM
Post #2


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,124



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks.
User is online!Profile CardPM

Go to the top of the page

David W
post 7 Oct, 2008 - 11:07 PM
Post #3


D.I.C Regular

Group Icon
Joined: 20 Sep, 2008
Posts: 304



Thanked 15 times

Dream Kudos: 250
My Contributions


This may help get you off to a running start ...

CODE
'''
Consider the following algorithm:
1. input n
2. print n
3. while n ~= 1
4. if n is odd then n = 3n+1
5. else n = n/2
Given the input 22, the following sequence of numbers
will be printed by the above algorithm:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

Example program input/output ...

Please Enter the Value for i: 1
Please Enter the Value for j: 10
The Maximum Cycle Length is: 20

Please Enter the Value for i: 100
Please Enter the Value for j: 200
The Maximum Cycle Length is: 125

Please Enter the Value for i: 201
Please Enter the Value for j: 210
The Maximum Cycle Length is: 89

Please Enter the Value for i: 900
Please Enter the Value for j: 1000
The Maximum Cycle Length is: 174
'''
def maxCycleLen( i, j ):
    maxLen =0 # initial to a lower than the lowest value of 1
    for n in range (i, j+1):
        series = []
        while n != 1:
            series.append(n)
            if n % 2 == 0:
                n  = n/2
            else:
                n = 3*n + 1
        series.append(1) # last value of n = 1
        
        cycleLen = len(series)
        if cycleLen > maxLen:
            maxLen = cycleLen
    return maxLen

def getIJandShowMax():
    while 1: # i.e. loop forever ... until break
        try: # allow only integers > 0 to be accepted for input ...
            i = input('Enter i : ')
            j = input('Enter j : ')
            if i < 1 or j < 1 or type(i) != int or type(j) != int \
               or i >=1000000 or j >=1000000:
                raise # an exception ...
            break # ok ... since no exception raised, we have good data
        except:
            print 'NOT valid input. Enter only integers in the range ' \
                  '1..999999.'
    if i > j: # then swap ...
        j,i = i,j
    maxLen = maxCycleLen( i, j )
    print 'Max cycle length is', maxLen


while 1:
    getIJandShowMax()
    reply = raw_input( 'More y/n ? ' )
    if reply in 'nN' and not reply == '':
        break


This post has been edited by David W: 7 Oct, 2008 - 11:46 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 08:48AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month