Thread: Classes
View Single Post
Old Apr 28th, 2005, 8:59 PM   #5
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
I think I've got it down thanks to Hydroxide.

My record for running this program is 1000. See what you can get without altering the code. :p

import random

class Range(object):

    def __init__(self, txt, end):
        self.txt = txt-1
        self.end = end
        self.count = 0

    def add(self):
        self.txt += 1
        self.count += 1

    def kill_digit(self):
        self.txt = int(str(self.txt)[1:])

    def add_digit(self):
        self.txt = int(str(random.randrange(0,10))+str(self.txt))

    def randomize(self):
        self.txt += random.randrange(0, 10)

    def run(self):
        while series.txt != series.end:
            
            self.add()
            
            if self.txt > 991:
                self.kill_digit()
            elif self.txt < 100:
                self.add_digit()
            else:
                self.randomize()
                    
series = Range(1, 1000) ; series.run()
print "Took", series.count, "tries to get to", "%s%s"%(series.txt,".")

I modified it to loop through and return the least amount of tries it needed to do: 8. :eek:
import random

class Range(object):
    def __init__(self, txt, end):
        self.txt = txt-1
        self.end = end
        self.count = 0

    def add(self):
        self.txt += 1
        self.count += 1

    def kill_digit(self):
        self.txt = int(str(self.txt)[1:])

    def add_digit(self):
        self.txt = int(str(random.randrange(0,10))+str(self.txt))

    def randomize(self):
        self.txt += random.randrange(0, 10)

    def run(self):
        while series.txt != series.end:  
            self.add()
            
            if self.txt > 991:
                self.kill_digit()
            elif self.txt < 100:
                self.add_digit()
            else:
                self.randomize()

x=[]

for a in range(100):                    
    series = Range(1, 1000) ; series.run()
    x.append(series.count)
    
smallest = 9999999
biggest = 0

for a in range(len(x)):
    if x[a] < smallest: smallest = x[a]
    if x[a] > biggest : biggest  = x[a]
print "%d and %d are the smallest and biggest amount of triest to get to %s."%(smallest, biggest, series.txt)

Last edited by Sane; Apr 28th, 2005 at 9:10 PM.
Sane is offline   Reply With Quote