Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The best strategy is divide process to 3 parts:
- Read
- Sort
- Print
- Reading from and filling data list can be done via raw list of lists or list of objects like I made in example below.
- Sorting you can make via bultin sorted function and key argument or use another way.
- Printing the result, I don't sure how it must shown so I printed content without formatting, but you can change it.
- ```python
- from functools import cmp_to_key
- from operator import attrgetter
- class Router:
- def __init__(self, name, wifi_spec, memory, price):
- self.name = str(name)
- self.wifi_spec = str(wifi_spec)
- self.memory = int(memory)
- self.price = int(price)
- def router_cmp(r1: Router, r2: Router):
- # 1 - worse
- # -1 - better
- # 0 - tie
- if r1.price > r2.price:
- return 1
- if r1.price < r2.price:
- return -1
- if r1.memory < r2.memory:
- return 1
- if r1.memory > r2.memory:
- return -1
- if r1.name > r2.name:
- return 1
- if r1.name < r2.name:
- return -1
- return 0
- def main():
- router_list = []
- # Input data
- with open('routers.txt') as f:
- for line in f.readlines():
- name, wifi_spec, memory, price = line.split(',')
- if wifi_spec in 'Wi-Fi 6' or wifi_spec in '802.11ax' or wifi_spec in 'ax' or wifi_spec in 'WiFi 6':
- if 'MiB' in memory:
- m1 = int(memory[:-3])
- memory = m1 * 1024
- elif 'GiB' in memory:
- m2 = int(memory[:-3])
- memory = m2 * 1024 * 1024
- router = Router(name, wifi_spec, memory, price)
- router_list.append(router)
- # Sort data
- sorted_router_list = sorted(router_list, key=cmp_to_key(router_cmp))
- # Print data
- for router in sorted_router_list:
- print(router.name, router.wifi_spec, router.memory, router.price, sep=',')
- if __name__ == '__main__':
- main()
- ```
- For example:
- ```txt
- Router1,Wi-Fi 6,802MiB,10500
- Router2,Wi-Fi 6,123MiB,13500
- Router3,Wi-Fi 6,2GiB,10500
- Router4,Wi-Fi 6,2GiB,10500
- ```
- The output:
- ```txt
- Router3,Wi-Fi 6,2097152,10500
- Router4,Wi-Fi 6,2097152,10500
- Router1,Wi-Fi 6,821248,10500
- Router2,Wi-Fi 6,125952,13500
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement