Question

# Python Problem 1 Create a function, biggest_ip_sum, outside of the ServerClass class, that: 1. Takes two...

Python

Problem 1

Create a function, biggest_ip_sum, outside of the ServerClass class, that:

1. Takes two ServerClass objects

2. Sums the octets of the IP together (example 127.0.0.1 = 127+0+0+1 = 128)

3. Returns the larger number

Example:

server_one = ServerClass("127.0.0.1")

server_two = ServerClass("192.168.0.1")

result = biggest_ip_sum(server_one, server_two)

print(result)

In the example above, result will be 361.

_________________________________

```class ServerClass:
""" Server class for representing and manipulating servers. """

def __init__(self, server_ip):
""" Create a ServerClass with a server_ip instantiated"""
self.server_ip = server_ip

# TODO - Write biggest_ip_sum function
def biggest_ip_sum():
return```

____________________________________

Problem 2

Create two server objects using the class

create a function, biggest_server_object_sum, outside of the ServerClass class, that:

1. Takes the IP

2. Sums the octets of the IP together (example 127.0.0.1 = 127+0+0+1 = 128)

3. Returns the Server object with the larger sum

Example:

server_one = ServerClass("127.0.0.1")

server_two = ServerClass("192.168.0.1")

result = biggest_ip_sum(server_one, server_two)

print(result)

Hint

Modify get_server_ip in the previous problem.

__________________________

```class ServerClass:
""" Server class for representing and manipulating servers. """

def __init__(self, server_ip):
""" Create a ServerClass with a server_ip instantiated"""
self.server_ip = server_ip

# TODO - biggest_server_object_sum
def biggest_server_object_sum(server_one, server_two):
return```

______________________________

Problem 3

Create a variable, server_name, and assign a value to it inside `ServerClass` .

_____________________________

```class ServerClass:
""" Server class for representing and manipulating servers. """
# TODO - Add server_name variable

def get_server_name(self):
return self.server_name```

Have a look at the below code. I have put comments wherever required for better understanding.

Problem 1 and 2 are quite similar. So I have explained problem 1.

Problem 1 & 2... Problem 3... ``````class ServerClass:
""" Server class for representing and manipulating servers. """

def __init__(self, server_ip):
""" Create a ServerClass with a server_ip instantiated"""
self.server_ip = server_ip

# TODO - Write biggest_ip_sum function
def biggest_ip_sum(server_one,server_two):
# split the ip adrress by . literal
one = server_one.server_ip.split(".")
two = server_two.server_ip.split(".")
# create variables to store sum
sum_one,sum_two = 0,0
# loop in through the first ip address to sum
for i in one:
sum_one+=int(i)
# loop in through the second ip address to sum
for j in two:
sum_two+=int(j)
# return greater sum
return sum_one if sum_one>sum_two else sum_two

server_one = ServerClass("127.0.0.1")
server_two = ServerClass("192.168.0.1")
result = biggest_ip_sum(server_one, server_two)
print(result)

class ServerClass:
""" Server class for representing and manipulating servers. """
# TODO - Add server_name variable

def __init__(self):
# This is the server name variable
self.server_name = "XYZ"

def get_server_name(self):
return self.server_name
``````

Happy Learning!

#### Earn Coins

Coins can be redeemed for fabulous gifts.