Python Forum
Python OOP - two numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python OOP - two numbers
#1
Okay, basically, the question is in hebrew ( my land launage), so I am translating it for you in english, hopefully it will be a good translation:

Write a class named: TwoNumbers

attributes:
first number - small
second number - big

methodes:
1. print in this format: small - (something), big - (something )
for example: small - 3, big - 9

2. a constructor which receives two numbers and reset the attributes of both of them ( no know the order of numbers )

3.GCD of both the numbers ( EDIT: forgot to mention, recursively )

4. print the difference between two numbers by seven boom.

Okay, so I basically did it all, I am sure in my code in all questions, execpt the fourth one...
I get it good, but I could do it only as a list and its a pretty long code for that function...
If anybody know any way of fixing the seven boom function to be shorter ( except using list comprehension, I dont like using it, its more readable that way, atleast for me... ) or how to do it using INT, I will be happy to get tips to change it ( or a list is good for it ).
( I know how to do seven boom with str, but I cant do it with return statement, only with print statement )

Code:
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, small, big):
        empty = []
        if big > small:
            small, big = big, small
        if small > big:
            for i in range(1, small - big + 1):
                if "7" in str(i) or i % 7 == 0:
                    empty.append("BOOM")
                else:
                    empty.append(i)
            empty.insert(0, 0)
        return empty

    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(self.small, self.big)}"


numbers = TwoNumbers(10, 20)
print(numbers)


OUTPUT:
Small - 10, Big - 20
GCD - 10 
Seven Boom Range: [0, 1, 2, 3, 4, 5, 6, 'BOOM', 8, 9, 10]
Reply


Messages In This Thread
Python OOP - two numbers - by ben1122 - Sep-27-2021, 05:35 PM
RE: Python OOP - two numbers - by ben1122 - Sep-27-2021, 05:42 PM
RE: Python OOP - two numbers - by ben1122 - Sep-28-2021, 05:16 PM
RE: Python OOP - two numbers - by Yoriz - Sep-28-2021, 05:36 PM
RE: Python OOP - two numbers - by ben1122 - Sep-28-2021, 05:55 PM
RE: Python OOP - two numbers - by ben1122 - Sep-30-2021, 04:30 PM
RE: Python OOP - two numbers - by Yoriz - Sep-30-2021, 06:04 PM
RE: Python OOP - two numbers - by ben1122 - Sep-30-2021, 06:17 PM
RE: Python OOP - two numbers - by Yoriz - Sep-30-2021, 06:22 PM
RE: Python OOP - two numbers - by ben1122 - Sep-30-2021, 06:38 PM
RE: Python OOP - two numbers - by Yoriz - Sep-30-2021, 06:51 PM
RE: Python OOP - two numbers - by ben1122 - Sep-30-2021, 07:03 PM
RE: Python OOP - two numbers - by Yoriz - Sep-30-2021, 07:07 PM
RE: Python OOP - two numbers - by ben1122 - Sep-30-2021, 07:09 PM
RE: Python OOP - two numbers - by ben1122 - Sep-30-2021, 07:16 PM
RE: Python OOP - two numbers - by Yoriz - Sep-30-2021, 07:14 PM
RE: Python OOP - two numbers - by Yoriz - Sep-30-2021, 07:28 PM
RE: Python OOP - two numbers - by ben1122 - Sep-30-2021, 07:40 PM
RE: Python OOP - two numbers - by Yoriz - Sep-30-2021, 07:59 PM
RE: Python OOP - two numbers - by ben1122 - Sep-30-2021, 08:06 PM
RE: Python OOP - two numbers - by deanhystad - Sep-30-2021, 09:25 PM
RE: Python OOP - two numbers - by ben1122 - Oct-01-2021, 09:40 AM
RE: Python OOP - two numbers - by ben1122 - Oct-01-2021, 09:46 AM
RE: Python OOP - two numbers - by deanhystad - Oct-01-2021, 01:15 PM
RE: Python OOP - two numbers - by ben1122 - Oct-01-2021, 05:39 PM
RE: Python OOP - two numbers - by deanhystad - Oct-01-2021, 05:50 PM
RE: Python OOP - two numbers - by Yoriz - Oct-01-2021, 05:55 PM
RE: Python OOP - two numbers - by ben1122 - Oct-01-2021, 06:22 PM
RE: Python OOP - two numbers - by Yoriz - Oct-01-2021, 06:24 PM
RE: Python OOP - two numbers - by ben1122 - Oct-01-2021, 07:18 PM
RE: Python OOP - two numbers - by deanhystad - Oct-01-2021, 07:44 PM
RE: Python OOP - two numbers - by ben1122 - Oct-02-2021, 01:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,497 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 3,094 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,164 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