Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2005, 9:26 PM   #1
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
Classes

I guess I should start understanding classes better.

So could anyone write me a program that takes an image of a line and rotates it in a circle pivoting on one side. And printing the [x,y] of the opposite side of the pivot (in a class).

This would help me immensely, thanks.

P.S. If you don't know how to use the pygame.rotate thingy, and know how to do classes. Could you go to the Lounge and in the "500 ways to program numbers 1 through 10" do one using classes?

Last edited by Sane; Apr 27th, 2005 at 9:38 PM.
Sane is offline   Reply With Quote
Old Apr 28th, 2005, 1:45 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
It's a waste of space, but here you go (this is in C++):
#include <iostream>
using namespace std;

class Number
{
private:
    int num;
    
    // this is a constructor - it's executed as soon as the class is created.
    Number ()
    {
        num = 0;
    }
    
    // this is a destructor - it's executed when the class is destroyed.
    ~Number ()
    {
    }
    
public:
    int getNum ()
    {
        return num;
    }
    
    void setNum (int newNum)
    {
        num = newNum;
    }
    
    void printNum ()
    {
        cout << num << endl;
    }
    
    void incrementNum ()
    {
        num++;
    }
}

int main ()
{
    Number num; // this creates a new instance of the class called "num"
    
    while (num.getNum() <= 10)
    {
        num.incrementNum();
        num.printNum();
    }
    
    return 0;
}
NB: this is untested. Enjoy!
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 28th, 2005, 1:48 PM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
I could be mistaken, but I thought he wanted this in Python??
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 28th, 2005, 1:52 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I don't know how classes work in Python. :p
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
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
Old Apr 28th, 2005, 11:16 PM   #6
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
1/ Don't use "txt" to hold a number - it's confusing.
2/ Rather than adding and removing digits from the left of a number, add and remove them from the right. This will allow you to use multiplication and division rather than conversions to strings
3/ You can replace the smallest and biggest code with calls to min() and max()

--OH.
hydroxide is offline   Reply With Quote
Old Apr 29th, 2005, 6:53 AM   #7
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
Adding and removing digits from the right would take just as much multiplication/division with strings as from the left.

Adding digit:
self.txt = int(str(self.txt) + str(random.randrange(0,10)))

Removing digit:
self.txt = int(str(self.txt)[0:len(self.txt)-2])

And why should it matter anyways, if the program really has no point, and digits are generally added and removed from the left. o_O??
Sane is offline   Reply With Quote
Old Apr 29th, 2005, 8:36 AM   #8
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Sane
Adding and removing digits from the right would take just as much multiplication/division with strings as from the left.

Adding digit:
self.txt = int(str(self.txt) + str(random.randrange(0,10)))

Removing digit:
self.txt = int(str(self.txt)[0:len(self.txt)-2])
Erm... no - if you go from the right you don't need strings - a huge difference in beauty/brevity:
    def kill_digit(self):
        self.val //= 10

    def add_digit(self):
        self.val *= 10
        self.randomize()
Quote:
And why should it matter anyways, if the program really has no point, and digits are generally added and removed from the left. o_O??
Because good habits matter when your programs do have a point, and pretty code is generally easier to maintain/improve.

--OH.
hydroxide is offline   Reply With Quote
Old Apr 29th, 2005, 1:50 PM   #9
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
By no point, I meant no point to distinguish it from the digits needing to be removed from either left or right. But okay.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC