Posts: 6,798
Threads: 20
Joined: Feb 2020
I think your __repr__ is wrong. The first requirement says "print in this format: small - (something), big - (something )". __repr__ should not include the gcd() or the sevenBoom().
I don't think sevenBoom should require you to pass values for big and small. TwoNumbers already has big and small and sevenBoom(self) should calculate the difference using self.big - self.small.
Since you already know big will be >= small, you can remove that test from sevenBoom.
Since you know sevenBoom will always start with [0, you may as well initialize empty = [0] instead of empty = []. Then you don't have to insert zero after the list is built.
I also don't think gcd() should require you to pass values for big and small. You should be able to do this:
numbers = TwoNumbers(10, 20)
print(numbers)
print(numbers.gcd())
print(numbers.sevenBoom()) I know it is not a stated requirement, but it is bad design, and good design should always be an expected requirement.
You will have to figure out a way to write gcd() so you can call it without passing small and big, but still call it recursively to get the solution. That is a little tricky, but a common pattern for recursion. It is very common that the first function call and the last function call in a recursion are different than the intermediate function calls.
Posts: 54
Threads: 7
Joined: Jul 2021
Oct-01-2021, 09:40 AM
(This post was last modified: Oct-01-2021, 09:40 AM by ben1122.)
(Sep-30-2021, 09:25 PM)deanhystad Wrote: I think your __repr__ is wrong. The first requirement says "print in this format: small - (something), big - (something )". __repr__ should not include the gcd() or the sevenBoom().
I don't think sevenBoom should require you to pass values for big and small. TwoNumbers already has big and small and sevenBoom(self) should calculate the difference using self.big - self.small.
Since you already know big will be >= small, you can remove that test from sevenBoom.
Since you know sevenBoom will always start with [0, you may as well initialize empty = [0] instead of empty = []. Then you don't have to insert zero after the list is built.
I also don't think gcd() should require you to pass values for big and small. You should be able to do this:
numbers = TwoNumbers(10, 20)
print(numbers)
print(numbers.gcd())
print(numbers.sevenBoom()) I know it is not a stated requirement, but it is bad design, and good design should always be an expected requirement.
You will have to figure out a way to write gcd() so you can call it without passing small and big, but still call it recursively to get the solution. That is a little tricky, but a common pattern for recursion. It is very common that the first function call and the last function call in a recursion are different than the intermediate function calls.
1. repr: Basically, you are right, I shouldn't do repr on the gcd and seven boom.
Although it wasnt stated I should not do it, I did it for one reason: I thought it would look better for this small question if it all will be in one print.
I mean, it doesnt matter what numbers he chose, he will get the small big gcd and seven boom.
That is why I did it, of course in a real project I wouldnt do it all together, but its a small question and I think its fine to do it that way, since I get it all in one print so its okay like that.
2. seven boom:
yea, Yoriz told me that, we actually discussed it in page 2, I fixed it :) to def something(self) and only self.something and self.other_something
3.seven boom:
yea, as said in 2.
4. empty:
Hmm, I didnt think of it, I didnt knew it was possible to do it that way.
Seems like a good idea, it will save me a line and look way more good.
5.gcd:
recursively with one statement? hmm, I will try it, it will be really tricky I think so too.. if I will be in trouble, I will update here.
Although I am really very basic on python, I dont think its basic level to do it recursively without putting parameters there...
I can do it by google, but it wont be smart as I wont learn it myself... anyway I will update
Basically, after all fixes and such, my code is that:
class TwoNumbers:
def __init__(self, small, big):
self.small = small
self.big = big
if self.small > self.big:
self.big, self.small = self.small, self.big
def gcd(self, small, big):
if big == 0:
return small
else:
return self.gcd(big, small % big)
def seven_boom_range(self):
lst = [0]
for i in range(1, self.big - self.small + 1):
if "7" in str(i) or i % 7 == 0:
lst.append("BOOM")
else:
lst.append(i)
return lst
def __repr__(self):
return f"Small - {self.small}, Big - {self.big}\nGCD - {self.gcd(self.small, self.big)} \
\nSeven Boom Range: {self.seven_boom_range()}"
numbers = TwoNumbers(30, 20)
print(numbers)
Posts: 54
Threads: 7
Joined: Jul 2021
Hmm, I couldn't really do it..
I didnt learn how to do recursive without parameters, I didnt know it even exist till now...
I think its good if I keep it that way, or you can give me a tip ( not the answer, cus google exist lol ) on what to do ( a small tip ).
Posts: 6,798
Threads: 20
Joined: Feb 2020
Here is a hint. Pass but fo not require.
def gcd(self, small=None, big=None)::
Posts: 54
Threads: 7
Joined: Jul 2021
(Oct-01-2021, 01:15 PM)deanhystad Wrote: Here is a hint. Pass but fo not require.
def gcd(self, small=None, big=None)::
uh, what is the difference?
its still like having parameters...
Posts: 6,798
Threads: 20
Joined: Feb 2020
The parameters are not required. You can call gcd() and it does not raise an exception. You need to figure out how to handle the case when gcd() is called without any parameters.
Posts: 2,168
Threads: 35
Joined: Sep 2016
class TwoNumbers:
def __init__(self, small, big):
self.small = small
self.big = big
if self.small > self.big:
self.big, self.small = self.small, self.big could be replaced with
class TwoNumbers:
def __init__(self, small, big):
self.small, self.big = (small, big) if big > small else (big, small)
class TwoNumbers:
...
def seven_boom_range(self):
lst = [0]
for i in range(1, self.big - self.small + 1):
if "7" in str(i) or i % 7 == 0:
lst.append("BOOM")
else:
lst.append(i)
return lst could be replaced with
class TwoNumbers:
...
def seven_boom_range(self):
lst = [0]
for i in range(1, self.big - self.small + 1):
lst.append("BOOM" if "7" in str(i) or i % 7 == 0 else i)
return lst
Posts: 54
Threads: 7
Joined: Jul 2021
Oct-01-2021, 06:22 PM
(This post was last modified: Oct-01-2021, 06:22 PM by Yoriz.
Edit Reason: removed unnecessary quote of previous post
)
Uhh, of course I could make it that way, but how is it any different? its just basically shorter, but same efficiency and less understandable, I remember that I read PEP 8, its not exactly what you did, but I remember that its less preferred to put statements at same line, like:
if bla bla: something
edit ( this line only ): although, only to me its not understable probably, but still, I prefer for me like I did.
But it doesnt matter, its basically the same thing.
But thanks for the offer :)
I guess the code is finished now, I can say goodbye to this code now hehe, thank you both for helping me with it, 3 pages and a few days, huge thanks :)
Posts: 2,168
Threads: 35
Joined: Sep 2016
Good luck, if your teacher has any questions about your code you could just send them here.
Posts: 54
Threads: 7
Joined: Jul 2021
(Oct-01-2021, 06:24 PM)Yoriz Wrote: Good luck, if your teacher has any questions about your code you could just send them here.
Oh I dont have to send him these, he doesnt really check it or something. its just "homework" ( not really ) for us to train our skills.
its a pre-course before studys ( which ended one month ago lol, this question just remained and got it a few days ago ), I never actually sent him anything, he just taught us stuff and sent us exercises to train ourselves, from there we are kind of alone ( we can send him a PM in mail and such, but I dont think it will be good if I will dig his mind ).
And thanks ( for the good luck :) )
|