-
Notifications
You must be signed in to change notification settings - Fork 0
merge 2 sorted lists with simple tests #8
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
Open
maesbrisa
wants to merge
5
commits into
master
Choose a base branch
from
feature/rise_repo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
992bdae
merge 2 sorted lists with simple tests
maesbrisa 1a0a4bb
simplify indexes
maesbrisa a3b215a
simplify indexes, now really
maesbrisa 26e5c7c
first approach to merge sort algorith on my own because i think this …
maesbrisa 9c7ba1a
using indexes and modifying the original list
maesbrisa 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,124 @@ | ||
|
|
||
| def mergeboth(a, b): | ||
| index = 0 | ||
| length = len(a) + len(b) | ||
| c = [] | ||
| if len(a) == 0: | ||
| print('----- Final result -----') | ||
| print(b) | ||
| print('------------------------') | ||
| return b | ||
| if len(b) == 0: | ||
| print('----- Final result -----') | ||
| print(a) | ||
| print('------------------------') | ||
| return a | ||
| while len(c) < length-1: | ||
| if a[index] <= b[index]: | ||
| print(str(a[index]) + ' minor than ' + str(b[index])) | ||
| if len(c) > 1 and a[index] < c[-1]: | ||
| c.insert(-1, a[index]) | ||
| else: | ||
| c.append(a[index]) | ||
| c.append(b[index]) | ||
| print('Current C array') | ||
| print(c) | ||
| else: | ||
| print(str(a[index]) + ' major than ' + str(b[index])) | ||
| if len(c) > 1 and b[index] < c[-1]: | ||
| c.insert(-1, b[index]) | ||
| else: | ||
| c.append(b[index]) | ||
| c.append(a[index]) | ||
| print('Current C array') | ||
| print(c) | ||
| if index == len(a) -1: | ||
| if index < len(b)-1: | ||
| print('there are elements left from: ' + str(index) ) | ||
| print('elements left from ' + str(index)) | ||
| print(b[index+1:]) | ||
| c.extend(b[index+1:]) | ||
| print('Check lengths') | ||
| print(length, len(c)) | ||
| else: | ||
| if index == len(b)-1: | ||
| print('there are elements left from: ' + str(index) ) | ||
| print('elements left from ' + str(index)) | ||
| print(a[index+1:]) | ||
| c.extend(a[index+1:]) | ||
| print('Check lengths') | ||
| print(length, len(c)) | ||
| else: | ||
| index += 1 | ||
| print('----- Final result -----') | ||
| print(c) | ||
| print('------------------------') | ||
| return c | ||
|
|
||
| def test1(): | ||
| print('starting test1') | ||
| a = [1,2,5,8,8] | ||
| b = [3,6,7,7] | ||
| assert mergeboth(a, b) == [1, 2, 3, 5, 6, 7, 7, 8, 8] | ||
| print('------------------------') | ||
|
|
||
|
|
||
| def test2(): | ||
| print('starting test2') | ||
| f = [1,4,6,8,9] | ||
| g = [2,5,7] | ||
| assert mergeboth(f,g) == [1, 2, 4, 5, 6, 7, 8, 9] | ||
| print('------------------------') | ||
|
|
||
|
|
||
| def test3(): | ||
| print('starting test3') | ||
| d = [2,6,8] | ||
| e = [1,7,9] | ||
| assert mergeboth(d,e) == [1, 2, 6, 7, 8, 9] | ||
| print('------------------------') | ||
|
|
||
| def test4(): | ||
| print('starting test4') | ||
| a = [1,3,5,7] | ||
| b = [2,4,6] | ||
| assert mergeboth(a,b) == [1, 2, 3, 4, 5, 6, 7] | ||
| print('------------------------') | ||
|
|
||
| def test5(): | ||
| print('starting test5') | ||
| a = [1,2,9] | ||
| b = [5,7,8] | ||
| assert mergeboth(a,b) == [1, 2, 5, 7, 8, 9] | ||
| print('------------------------') | ||
|
|
||
| def test6(): | ||
| print('starting test6') | ||
| a = [1,2] | ||
| b = [] | ||
| assert mergeboth(a,b) == [1, 2] | ||
| print('------------------------') | ||
|
|
||
| def test7(): | ||
| print('starting test7') | ||
| a = [] | ||
| b = [] | ||
| assert mergeboth(a,b) == [] | ||
| print('------------------------') | ||
|
|
||
| def test8(): | ||
| print('starting test8') | ||
| a = [] | ||
| b = [1,2] | ||
| assert mergeboth(a,b) == [1, 2] | ||
| print('------------------------') | ||
|
|
||
| if __name__ == "__main__": | ||
| test1() | ||
| test2() | ||
| test3() | ||
| test4() | ||
| test5() | ||
| test6() | ||
| test7() | ||
| test8() | ||
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,42 @@ | ||
|
|
||
|
|
||
| def merge_sort(original_list): | ||
| print('entering divide') | ||
| if len(original_list) > 1: | ||
|
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. Nitpick: No se si esto se puede hacer en paiton, pero, podrías probar algo como un early return? Asi te evitas tener todo tu codigo indexado. |
||
| middle_index = len(original_list)//2 | ||
| left = original_list[:middle_index] | ||
| right = original_list[middle_index:] | ||
| merge_sort(left) | ||
| merge_sort(right) | ||
| print('i am back!') | ||
| left_index, right_index, original_index = 0, 0, 0 | ||
| while left_index < len(left) and right_index < len(right): | ||
| if left[left_index] <= right[right_index]: | ||
| original_list[original_index] = left[left_index] | ||
| left_index += 1 | ||
| else: | ||
| original_list[original_index] = right[right_index] | ||
| right_index += 1 | ||
| original_index += 1 | ||
| print('we have just put two values in order from both halves') | ||
| print(right[right_index-1], left[left_index-1]) | ||
|
|
||
| while left_index < len(left): | ||
| print('this means that right half is out of stock') | ||
| print(right, right_index, left, left_index) | ||
| original_list[original_index] = left[left_index] | ||
| left_index += 1 | ||
| original_index += 1 | ||
|
|
||
| while right_index < len(right): | ||
| print('this means that left half is out of stock') | ||
| print(right, right_index, left, left_index) | ||
| original_list[original_index] = right[right_index] | ||
| right_index += 1 | ||
| original_index += 1 | ||
|
|
||
| if __name__ == '__main__': | ||
| list_to_sort = [5,4,2,7,9,1] | ||
| merge_sort(list_to_sort) | ||
| print('now sorted') | ||
| print(list_to_sort) | ||
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.
Veo que estás usando el mismo indice para ambos vectores.
Si usas distintos indices para cada vector, te permite insertar uno, y mover su indice, para comprobar a[1] con b[0], por ejemplo, y, probablemente, insertar a[1] en lugar de b[0], y luego comparar a[2] con b[0].
Quieres intentarlo de esa forma?