Posts: 54
Threads: 7
Joined: Jul 2021
Sep-27-2021, 05:35 PM
(This post was last modified: Sep-27-2021, 05:42 PM by ben1122.)
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]
Posts: 54
Threads: 7
Joined: Jul 2021
Sep-27-2021, 05:42 PM
(This post was last modified: Sep-27-2021, 05:42 PM by ben1122.)
Actually, I changed something in the seven boom and now its shorter in a little ( just realized I can do it and the code still looks good, but still long ):
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
Posts: 54
Threads: 7
Joined: Jul 2021
Sep-28-2021, 05:16 PM
(This post was last modified: Sep-28-2021, 05:16 PM by ben1122.)
up? anybody?\
EDIT: Why is the post moved to Homework? it was on code review.
its not homework, as said in the post... its code review, the code is already done, I need it to be reviewed... please move it back as it doesnt belong here and that is why no one answers... ( empty forum )
Posts: 2,168
Threads: 35
Joined: Sep 2016
Probably because of the way it is presented it looks a lot like homework.
Posts: 54
Threads: 7
Joined: Jul 2021
(Sep-28-2021, 05:36 PM)Yoriz Wrote: Probably because of the way it is presented it looks a lot like homework.
I can understand what you mean, but you can basically see the code is done.
The review I need is basically for all code, but especially the seven boom code since it is pretty long. ( if there was a way to make it shorter but also efficiency like mine ), or its good like that? ( and without a list ).
I think I understand why you said home work, cus I wrote stuff like changing it to not from a list and such.
Posts: 54
Threads: 7
Joined: Jul 2021
anybody? please help?
I wanna know if the code is good...
Posts: 2,168
Threads: 35
Joined: Sep 2016
- TwoNumbers doesn't seem to be a very descriptive name for what the class represents
__init__ has parameters named small & big when it doesn't matter which is small and which is big.
__init__ defines self.small & self.big twice , could figure out which passed in attribute is small and big and assign once.
__init__ sorted could be used to order the passed in attributes.
- method
gcd may as well be a function, it does not use any of the class attributes or methods.
- method
seven_boom_range also could be a function that returns the __repr__ 's str value, it also does not use any of the class attributes or methods, this looks like a function trying to look like a class.
empty is not a very good variable name for a container that will have items added to it.
seven_boom_range repeats the ordering of small & big that has already been done in the __init__ .
Posts: 54
Threads: 7
Joined: Jul 2021
Sep-30-2021, 06:17 PM
(This post was last modified: Sep-30-2021, 06:24 PM by Yoriz.
Edit Reason: removed unnecessary quote of previous post
)
Hi, thanks for the replay, appreciated :)
1. It is , look at my question - it says exactly what to do ( first start with small, big ) - then make it so it doesnt matter ( but keep the small and big thingy - I think, he didnt say to remove it, only just so if I write big or small, it will do the same order.
2. as said in 1, the question wanted it
3.sorted - ofc, it could, but it would ruin the point lol, our teacher dont want us to use python infunctions yet, he want us to learn ( we learnt a few time ago bubble, merge, select and quick sort, we had a lot of practice ). thats why I didnt use sort, it would ruin the whole point of the question ( as said in 1 and 2 ).
4. GCD - again, as said in the question, he asked for it recursively, I wrote it.
5. method seven_boom_range also could be a function that returns the __repr__'s str value, it also does not use any of the class attributes or methods, this looks like a function trying to look like a class.
didnt really understand what you mean? could explain it further? I didnt understand what you mean by that it doesnt use any class attributes?
6. empty - yea I know, its a little meaningless as I didnt name it for a project, but I guess you are right, I should get used to name variables to their names in small questions also.
7. seven_boom_range repeats the ordering of small & big that has already been done in the __init__
what do you mean? what range? you mean the if statement about if bla bla > bla? If I remove it, the code wont work. I will try to make it work somehow, but the list wont be any good.
But just a question. the seven boom function, is there any way of creating it with a return statement but without a list? i can do it with print, but I need it to be on return statement.
Posts: 2,168
Threads: 35
Joined: Sep 2016
Sep-30-2021, 06:22 PM
(This post was last modified: Sep-30-2021, 06:33 PM by Yoriz.)
(Sep-28-2021, 05:16 PM)ben1122 Wrote: up? anybody?\
EDIT: Why is the post moved to Homework? it was on code review.
its not homework, as said in the post... its code review, the code is already done, I need it to be reviewed... please move it back as it doesnt belong here and that is why no one answers... ( empty forum )
(Sep-30-2021, 06:17 PM)ben1122 Wrote: 3.sorted - ofc, it could, but it would ruin the point lol, our teacher dont want us to use python infunctions yet, he want us to learn ( we learnt a few time ago bubble, merge, select and quick sort, we had a lot of practice ). thats why I didnt use sort, it would ruin the whole point of the question ( as said in 1 and 2 ).
4. GCD - again, as said in the question, he asked for it recursively, I wrote it.
(Sep-30-2021, 06:17 PM)ben1122 Wrote: 5. method seven_boom_range also could be a function that returns the __repr__'s str value, it also does not use any of the class attributes or methods, this looks like a function trying to look like a class.
didnt really understand what you mean? could explain it further? I didnt understand what you mean by that it doesnt use any class attributes?
if I took the method seven_boom_range out of the class, removed the parameter self so it become a function, it would still work.
def seven_boom_range(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
Posts: 54
Threads: 7
Joined: Jul 2021
Sep-30-2021, 06:38 PM
(This post was last modified: Sep-30-2021, 06:40 PM by Yoriz.
Edit Reason: removed unnecessary quote of previous post
)
oh you meant that.
yea that I know.
what is the difference? I dont really understand?
if I do a function in a class, it should work only inside the class? if I make same function outside of class, it shouldn't work? why so? so it means that my GCD recursive should not be good ( although it was asked ).
I cant really understand, how should I do seven boom so it works only inside class?
|