Sep-27-2021, 05:35 PM
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:
OUTPUT:
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]