Merge pull request #5 from lorendsnow/master

Proposed solution for Chapter 4, Exercise 1
This commit is contained in:
Rick van Hattem 2023-08-08 02:21:48 +02:00 committed by GitHub
commit 86d88d7fe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,16 @@
from collections.abc import Callable
class SortedDict:
def __init__(self, _dict: dict, keyfunc: Callable) -> None:
self.unsorted = _dict
self.keyfunc = keyfunc
self._dict = self.sort()
def sort(self) -> dict:
sorted_list = sorted(self.unsorted.items(), key=self.keyfunc)
sorted_dict = {k:v for k, v in sorted_list}
return sorted_dict
def __repr__(self):
return repr(self._dict)