![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Banned
![]() ![]() |
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. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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;
} |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I don't know how classes work in Python. :p
|
|
|
|
|
|
#5 |
|
Banned
![]() ![]() |
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. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#7 |
|
Banned
![]() ![]() |
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?? |
|
|
|
|
|
#8 | ||
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
def kill_digit(self):
self.val //= 10
def add_digit(self):
self.val *= 10
self.randomize()Quote:
--OH. |
||
|
|
|
|
|
#9 |
|
Banned
![]() ![]() |
By no point, I meant no point to distinguish it from the digits needing to be removed from either left or right. But okay.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|