-
Notifications
You must be signed in to change notification settings - Fork 0
Воронин Андрей #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Воронин Андрей #119
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5a4b999
Add vkposter.py solution
andrwvrn 7cf72e9
Add table solution
andrwvrn f9a082c
Merge branch 'homework_02' of https://github.com/Kinetikm/AppliedPyth…
andrwvrn 16f7a4c
Revise table.py solution
andrwvrn 28c231c
Merge branch 'homework_02' of https://github.com/Kinetikm/AppliedPyth…
andrwvrn d8e9672
Add heap.py, fastmerger.py solutions and revised vkposter.py solution
andrwvrn 64a4e92
Fix table solution
andrwvrn 6cbcdfa
Fix vkposter.py
andrwvrn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # returns file encoding as a string | ||
|
|
||
|
|
||
| def check_e(filename): | ||
| for enc in ('utf-8', 'utf-16', 'cp1251'): | ||
| try: | ||
| with open(filename, encoding=enc) as f: | ||
| data = f.read(10) | ||
| return enc | ||
| except (UnicodeDecodeError, UnicodeError): | ||
| pass | ||
|
|
||
| raise SystemExit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # returns file format as a string | ||
|
|
||
| import json | ||
| import csv | ||
|
|
||
|
|
||
| def check_f(filename, enc): | ||
| try: | ||
| with open(filename, encoding=enc) as f: | ||
| data = json.load(f) | ||
| return 'json' | ||
|
|
||
| except json.decoder.JSONDecodeError: | ||
| pass | ||
|
|
||
| with open(filename, encoding=enc) as f: | ||
| data = csv.reader(f, delimiter='\t') | ||
| length = -1 | ||
| for row in data: | ||
| if (len(row) != length and length != -1): | ||
| raise SystemExit | ||
| length = len(row) | ||
| return 'tsv' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # returns list of strings | ||
|
|
||
|
|
||
| def draw_table(table): | ||
|
|
||
| # find max column lengths | ||
| column_lengths = dict.fromkeys([i for i in range(len(table[0]))], -1) | ||
| for row in table: | ||
| for i in range(len(row)): | ||
| if (column_lengths.get(i) and len(row[i]) > column_lengths[i]): | ||
| column_lengths[i] = len(row[i]) | ||
|
|
||
| # form columns | ||
| for row in table: | ||
| if (table.index(row) != 0): | ||
| for i in range(len(row)): | ||
| if (i == len(row) - 1): # last column | ||
| row[i] = '| ' + ' '*(column_lengths[i] - len(row[i]))\ | ||
| + row[i] + ' |' | ||
| else: # other columns | ||
| row[i] = '| ' + row[i] + ' '*(column_lengths[i] - | ||
| len(row[i])) + ' ' | ||
|
|
||
| else: | ||
| for i in range(len(row)): | ||
|
|
||
| s = (column_lengths[i] - len(row[i]))//2 | ||
|
|
||
| if (i == len(row) - 1): # last column first row | ||
| if ((column_lengths[i] - len(row[i])) % 2 == 0): | ||
| row[i] = '| ' + ' '*s + row[i] + ' '*s + ' |' | ||
| else: | ||
| row[i] = '| ' + ' '*s + row[i] + ' '*(s+1) + ' |' | ||
| else: # other columns first row | ||
| if ((column_lengths[i] - len(row[i])) % 2 == 0): | ||
| row[i] = '| ' + ' '*s + row[i] + ' '*s + ' ' | ||
| else: | ||
| row[i] = '| ' + ' '*s + row[i] + ' '*(s+1) + ' ' | ||
|
|
||
| added_symbols = (len(table[0]) - 1) * 5 + 6 | ||
| table_length = sum(column_lengths.values()) + added_symbols | ||
|
|
||
| table.append('-'*table_length) | ||
| table.insert(0, '-'*table_length) | ||
| return table |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,22 +5,56 @@ | |
| class Heap(): | ||
|
|
||
| def __init__(self, array): | ||
| pass | ||
| self._heap = array[:] | ||
| self.build_heap() | ||
|
|
||
| def add(self, elem_with_priority): | ||
| pass | ||
| self._heap.append(elem_with_priority) | ||
| self.sift_up() | ||
|
|
||
| def build_heap(self): | ||
| pass | ||
| for i in range(len(self._heap)//2, -1, -1): | ||
| self.sift_down(i) | ||
|
|
||
| def sift_up(self): | ||
| i = len(self._heap) - 1 | ||
| while True: | ||
| if (i > 0 and comparator_d(self._heap[i], self._heap[(i-1)//2])): | ||
|
|
||
| self._heap[i], self._heap[(i-1)//2] = self._heap[(i-1)//2],\ | ||
| self._heap[i] | ||
| i = (i-1)//2 | ||
| else: | ||
| break | ||
|
|
||
| def sift_down(self, element_index): | ||
| i = element_index | ||
| largest = element_index | ||
| if (2*i+1 <= len(self._heap)-1 and comparator_d(self._heap[2*i+1], | ||
| self._heap[i])): | ||
| largest = 2*i + 1 | ||
| if (2*i+2 <= len(self._heap)-1 and comparator_d(self._heap[2*i+2], | ||
| self._heap[largest])): | ||
| largest = 2*i + 2 | ||
| if (largest != element_index): | ||
| self._heap[i], self._heap[largest] = self._heap[largest],\ | ||
| self._heap[i] | ||
| self.sift_down(largest) | ||
|
|
||
|
|
||
| class MaxHeap(Heap): | ||
|
|
||
| def __init__(self, array): | ||
| raise NotImplementedError | ||
| super().__init__(array) | ||
|
|
||
| def extract_maximum(self): | ||
| pass | ||
| max_element = None | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1,5б |
||
| if (len(self._heap) != 0): | ||
| max_element = self._heap.pop(0) | ||
| if (len(self._heap) != 0): | ||
| self._heap.insert(0, self._heap.pop()) | ||
| self.sift_down(0) | ||
| return max_element | ||
|
|
||
|
|
||
| def comparator_d(x, y): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,33 @@ | ||
| import sys | ||
| import check_encoding as ce | ||
| import check_format as cf | ||
| import withdraw_data as wd | ||
| import construct_table as ct | ||
|
|
||
| # Ваши импорты | ||
|
|
||
| if __name__ == '__main__': | ||
| filename = sys.argv[1] | ||
| try: | ||
| filename = sys.argv[1] | ||
| except IndexError: | ||
| print('Файл не валиден') | ||
| raise SystemExit | ||
|
|
||
| # Ваш код | ||
| try: | ||
| with open(filename, 'rb') as f: | ||
| pass | ||
| except FileNotFoundError: | ||
| print('Файл не валиден') | ||
| raise SystemExit | ||
|
|
||
| try: | ||
|
|
||
| enc = ce.check_e(filename) | ||
| frmt = cf.check_f(filename, enc) | ||
| data = wd.receive_data(filename, enc, frmt) | ||
| table = ct.draw_table(data) | ||
|
|
||
| for row in table: | ||
| print(''.join(row)) | ||
|
|
||
| except SystemExit: | ||
| print("Формат не валиден") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # returns list of lists that should be interpreted as rows of a table | ||
|
|
||
| import json | ||
| import csv | ||
| import copy | ||
|
|
||
|
|
||
| def test_data(table): | ||
| s = '' | ||
| length = -1 | ||
| for row in table: | ||
| if (len(row) != length and length != -1): | ||
| raise SystemExit | ||
| length = len(row) | ||
| l = copy.deepcopy(row) | ||
| s += ''.join(l) | ||
| if len(s) == 0: | ||
| raise SystemExit | ||
|
|
||
|
|
||
| def receive_data(filename, enc, frmt): | ||
| table = [] | ||
| if (frmt == 'json'): | ||
| with open(filename, encoding=enc) as f: | ||
| data = json.load(f) | ||
| table_header = [] | ||
| table_content = [] | ||
|
|
||
| for column_name in data[0]: # get table header from first dict | ||
| table_header.append(column_name) | ||
|
|
||
| for content in data: # every row has same header | ||
| if list(content.keys()) != table_header: | ||
| raise SystemExit | ||
|
|
||
| table.append(table_header) | ||
| for content in data: | ||
| table_content = [value for value in content.values()] | ||
| table.append(table_content) | ||
| for i in range(len(table)): | ||
| for j in range(len(table[i])): | ||
| table[i][j] = str(table[i][j]) | ||
|
|
||
| elif (frmt == 'tsv'): | ||
| with open(filename, encoding=enc) as f: | ||
| data = csv.reader(f, delimiter='\t') | ||
| for row in data: | ||
| table.append(row) | ||
| test_data(table) | ||
|
|
||
| return table |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0.5б