Python Forum
Python OOP - two numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python OOP - two numbers
#21
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.
Reply
#22
(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)
Reply
#23
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 ).
Reply
#24
Here is a hint. Pass but fo not require.

def gcd(self, small=None, big=None)::
Reply
#25
(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...
Reply
#26
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.
Reply
#27
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
ben1122 likes this post
Reply
#28
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 :)
Reply
#29
Good luck, if your teacher has any questions about your code you could just send them here.
ben1122 likes this post
Reply
#30
(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 :) )
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,427 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 3,014 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,111 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020