I'm still a bit new to python and I am trying to learn how to properly format my code for real world applications and interviews.
The code below takes a number as an input and then returns how many numbers in a list are above and below the given number. I create a class, solution, to store the function ,placeOfNum, that does all of the processing logic.
If I want to output my answer as I have below is it best practice call the Solution class function as I have below, or should I keep everything in the class to help with readability, or should I make another function, answer for example, within the class and output the solution within that class?
def placeOfNum(self, n, array):
aboveNum = 0
belowNum = 0
array = sorted(array)
for x in array:
if x < n:
belowNum += 1
if x > n:
aboveNum += 1
return (above, below)
numList = [1,5,21,2,1,10,232]
num = 21
x = Solution()
answer = x.placeOfNum(num, numList)
print("above:", answer[0], "below:", answer[1])
# returns "above:1, below:5"
selfin the only method defined forSolution. You don't need a class at all. (That said, isfindPlacesupposed to beplaceOfNum?) Python isn't Java; you can define functions outside of a class.camelCasenames, usesnake_case.