[GO] [Python] I tried to implement stable sorting, so make a note

problem

See the site below

My answer

import collections

def chk_before_tranp_bigger(now, before):
    return int(before[1]) > int(now[1])


def is_stable(inputs: list, sort_: list) -> str:
    """
conditions
・ Cards with the same number
・ Same as the order in which they appear in the input

Once, group the list of input data using numbers as keys
Example)
        {
            '4': ['H4', 'S4'],
            '9': ['C9'],
            '3': ['C3']
        }
    """
    judge = 'Not stable'
    loop = True
    set_num = collections.defaultdict(list)
    for input_ in inputs:
        set_num[input_[1]].append(input_)

    for g, dl in set_num.items():
        if len(dl) <= 1:
            continue
        min_index = -1
        for d in dl:
            if min_index > sort_.index(d):
                loop = False
                break
            min_index = sort_.index(d)
        if not loop:
            break
    else:
        judge = 'Stable'

    return judge


def bubble_sort(n: int, a: list) -> (list, str):
    dl = a.copy()
    for i in range(n):
        for j in range(n-1, i, -1):
            if chk_before_tranp_bigger(dl[j], dl[j-1]):
                dl[j], dl[j-1] = dl[j-1], dl[j]
    return dl, is_stable(a,  dl)


def selected_sort(n: int, a: list) -> (list, str):
    dl = a.copy()
    for i in range(n):
        mini = i
        for j in range(i, n):
            if chk_before_tranp_bigger(dl[j], dl[mini]):
                mini = j
        if mini != i:
            dl[i], dl[mini] = dl[mini], dl[i]

    return dl, is_stable(a, dl)


n = int(input())
a = input().split()

sort_, judge = bubble_sort(n, a)
print(*sort_)
print(judge)

sort_, judge = selected_sort(n, a)
print(*sort_)
print(judge)

Recommended Posts

[Python] I tried to implement stable sorting, so make a note
I tried to implement a pseudo pachislot in Python
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
I tried to implement a one-dimensional cellular automaton in Python
I tried to make a stopwatch using tkinter in python
A Python beginner made a chat bot, so I tried to summarize how to make it.
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to make a Web API
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
[3rd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "date" using Python
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I want to make a game with Python
I tried to implement TOPIC MODEL in Python
I tried to implement selection sort in python
I tried to make a ○ ✕ game using TensorFlow
Python: I tried to make a flat / flat_map just right with a generator
I tried to make a calculator with Tkinter so I will write it
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
A note I looked up to make a command line tool in Python
I tried to implement a card game of playing cards in Python
I was addicted to trying Cython with PyCharm, so make a note
I tried to make a "fucking big literary converter"
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I want to easily implement a timeout in python
I tried to make a simple mail sending application with tkinter of Python
I tried to implement Minesweeper on terminal with python
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
[Patent analysis] I tried to make a patent map with Python without spending money
Continuation ・ I tried to make Slackbot after studying Python3
I tried to implement a recommendation system (content-based filtering)
I tried to implement PCANet
I stumbled upon using MoviePy, so make a note
I tried to implement Dragon Quest poker in Python
I tried to implement an artificial perceptron with python
I tried to implement GA (genetic algorithm) in Python
[Python] I tried to make a Shiritori AI that enhances vocabulary through battles
[Python] I want to make a nested list a tuple
I tried to automatically generate a password with Python3
I tried to implement what seems to be a Windows snipping tool in Python
I tried to implement StarGAN (1)
I set up TensowFlow and was addicted to it, so make a note
I tried to make a real-time sound source separation mock with Python machine learning
I tried to implement a volume moving average with Quantx
I tried to implement a basic Recurrent Neural Network model
I tried to make various "dummy data" with Python faker
[Markov chain] I tried to read a quote into Python.
I tried "How to get a method decorated in Python"
I tried to implement the mail sending function in Python