Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Small is beautiful.


devel / comp.lang.python / Re: Selection sort

SubjectAuthor
o Re: Selection sortdn

1
Re: Selection sort

<mailman.29.1640389438.3079.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16492&group=comp.lang.python#16492

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonL...@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: Re: Selection sort
Date: Sat, 25 Dec 2021 12:43:32 +1300
Organization: DWM
Lines: 247
Message-ID: <mailman.29.1640389438.3079.python-list@python.org>
References: <CAHVaEHhaaQ+b7xES9Ktv3f3DdJcaM2Nkb5c4k45QGR-DdPi_hA@mail.gmail.com>
<5aeaea49-80b9-740c-3efc-e7640719039a@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de pJLaIAk75b61q3T1BrHgLgnIgCFwHJtcmaVprwOhvGpQ==
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=hIMclGkH; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'def': 0.04;
'containing': 0.05; 'python:': 0.05; 'cards': 0.07; 'used.': 0.07;
'=dn': 0.09; 'breaks': 0.09; 'computing': 0.09; 'describe': 0.09;
'eyes': 0.09; 'from:addr:danceswithmice.info': 0.09;
'from:addr:pythonlist': 0.09; 'it)': 0.09; 'later,': 0.09;
'perspective': 0.09; 'set,': 0.09; 'coding': 0.13; 'help,': 0.14;
'"in': 0.16; '"what': 0.16; '(except': 0.16; "(i've": 0.16;
'(why': 0.16; 'additionally': 0.16; 'algorithm,': 0.16;
'conclusion': 0.16; 'earlier,': 0.16; 'finger': 0.16; 'integer':
0.16; 'item,': 0.16; 'late,': 0.16; 'loop?': 0.16; 'message-
id:@DancesWithMice.info': 0.16; 'numbers?': 0.16; 'objective':
0.16; 'outer': 0.16; 'paper,': 0.16; 'print(': 0.16; 'question,':
0.16; 'received:51.254': 0.16; 'received:51.254.211': 0.16;
'received:51.254.211.219': 0.16; 'received:cloud': 0.16;
'received:rangi.cloud': 0.16; 'sorted': 0.16; 'sorting,': 0.16;
'static': 0.16; 'step-by-step': 0.16; 'subject:sort': 0.16;
'through,': 0.16; 'using:': 0.16; 'ways.': 0.16; 'wider': 0.16;
'wrote:': 0.16; 'problem': 0.16; 'python': 0.16; 'values': 0.17;
'code.': 0.17; "can't": 0.17; 'implement': 0.19; 'to:addr:python-
list': 0.20; 'language': 0.21; 'written': 0.22; "i've": 0.22;
'languages': 0.22; 'science': 0.22; 'code': 0.23; 'run': 0.23;
'list,': 0.24; 'thanks!': 0.24; '(and': 0.25; 'python,': 0.25;
'programming': 0.25; 'questions,': 0.26; 'space': 0.26;
'behavior': 0.26; 'clicked': 0.26; 'else': 0.27; 'bit': 0.27;
'function': 0.27; 'done': 0.28; 'purpose': 0.28; 'thinking': 0.28;
'wrong': 0.28; 'computer': 0.29; 'recently': 0.29; 'asked': 0.29;
'it,': 0.29; 'error': 0.29; 'header:User-Agent:1': 0.30;
'attempt': 0.31; 'code,': 0.31; 'header:Organization:1': 0.31;
'think': 0.32; "doesn't": 0.32; 'question': 0.32; 'right': 0.68;
'exactly': 0.68; 'items': 0.68; 'revert': 0.68; 'during': 0.69;
'order': 0.69; 'actively': 0.69; 'compare': 0.69; 'desk': 0.69;
'manner': 0.69; 'piece': 0.69; 'playing': 0.69; 'relate': 0.69;
'sequence': 0.69; 'reach': 0.69; 'within': 0.69; 'terms': 0.70;
'above,': 0.70; 'them,': 0.70; 'success': 0.73; 'easy': 0.74;
'features': 0.75; 'challenges': 0.76; 'confidence': 0.76;
'implemented': 0.76; 'seek': 0.81; 'position': 0.81; 'left': 0.83;
'happens': 0.84; '"2"': 0.84; "'all": 0.84; 'again:': 0.84;
'changed,': 0.84; 'cycle': 0.84; 'cycles': 0.84; 'flat': 0.84;
'index.': 0.84; 'indexes': 0.84; 'paper.': 0.84; 'pause': 0.84;
'sequence.': 0.84; 'slips': 0.84; 'stage,': 0.84;
'subject:Selection': 0.84; 'successful,': 0.84; 'thus,': 0.84;
'type,': 0.84; 'until...': 0.84; 'we?': 0.84; 'brain': 0.91;
'time?': 0.91; 'line,': 0.93; 'positions': 0.96
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on vps517507.ovh.net
X-Spam-Level:
X-Spam-Status: No, score=-5.1 required=5.0 tests=ALL_TRUSTED,BAYES_00,
DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,NICE_REPLY_A autolearn=ham
autolearn_force=no version=3.4.0
DKIM-Filter: OpenDKIM Filter v2.11.0 mail.rangi.cloud 0CC55764
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1640389421;
bh=bZCn/9GPOO6y1KDuv60+28uCnLhhAPCQuc2eEqmBHYk=;
h=Date:Subject:To:References:From:In-Reply-To:From;
b=hIMclGkHMtb29e/PrKQ4XP9eSD0/n79cZSdJVyuQIWwiRj/cYBgnuWDWJRxuaREDt
E7hO3dwIoliKrXgzUDXf3hwR+XgTl8o+U2xOl9F+dhftHbrhUbTGjAGgtielScThuF
rvOP8N4K/4ckM3jgEu4JbljcEIUEVx3SiBtGZctrWfhDGBShIjVZ8Pp6MuCy3bFiH9
onyPpfQBkym4EKVw8J1Bs9r1Xy69JU14RMVfMT9EIi3ocyf9QR+JqbvUbW3z7jdJir
XQS6O8dijptmAnztatVsVSnIQrcAfqjJg89Wrd54uimGW+sRTMjh5YDPYOnUxIKWE0
k+idGManbt9iw==
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Content-Language: en-GB
In-Reply-To: <CAHVaEHhaaQ+b7xES9Ktv3f3DdJcaM2Nkb5c4k45QGR-DdPi_hA@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <5aeaea49-80b9-740c-3efc-e7640719039a@DancesWithMice.info>
X-Mailman-Original-References: <CAHVaEHhaaQ+b7xES9Ktv3f3DdJcaM2Nkb5c4k45QGR-DdPi_hA@mail.gmail.com>
 by: dn - Fri, 24 Dec 2021 23:43 UTC

On 25/12/2021 03.22, vani arul wrote:
> Hello,
> I am trying write a code.Can some help me find the error in my code.
> Thanks!
>
>
> def selectionsort(arr):
> # le=len(arr)
> for b in range(0,len(arr)-1):
> pos=b
> for a in range(b+1,len(arr)-1):
> if arr[b]>arr[a+1]:
> arr[b],arr[a+1]=arr[a+1],arr[b]
> return arr
>
> arr=[3,5,9,8,2,6]
> print(selectionsort(arr))

This looks like a typical 'homework question'. Accordingly, we are happy
to help, but that means helping you to learn Python, NOT to help by
writing a homework-solution!

Evidently you are expected to learn some Python, but additionally an
algorithm (or method) for sorting. Also apparent, is that the current
attempt has been taken from another programming language - rather than
using a more 'pythonic' idiom. All fine, but with a "but...".

One assumes, the Python sort() built-in function may not be used. Even
so, you can ask the computer to test the success (or otherwise) of your
efforts, by adding to the end of the existing code, something like:

print( "Was the algorithm successful?",
selection_sort( arr ) == sorted( arr )
)

(I've recently 'enjoyed' an eye operation, so actively seek ways of
having the computer 'see' or check things that are difficult for me -
pending already-excellent recovery progress...)

The question has already been asked: "What are you expecting the code to
do?". This is vital. What is your target? (and when you ask us a
question, how do we know exactly what your target might be?).

Regardless, it is *always* the first question to be asked in attempting
to find a solution to any problem - thus, has much wider application
than computing then! How does one know if the aim is true, without first
defining "the target"?

The key to coding any algorithm is to (temporarily) forget Python, and
to first understand the method - how it works.

What I have often done in the past, is to ask trainees to bring a
pack/deck of Playing Cards - however a few scraps of paper with the
numbers: 3,5,9,8,2, and 6 written on them, will do just as well.

What we want is three sets of the same numbers. One set to move around
(the above), and the other two static - so could write them (in a row)
across a sheet of paper. The first 'static' set, should be the numbers
in the sequence given by the question, ie 'the start position'. Put this
at the 'top' of a desk or flat surface. The second 'static' set, is the
"target", ie the same digits, but in sorted-order. Artie them across a
second piece of paper, and place that at the 'bottom' of the desk. So,
now we can see a starting-position, and a final sequence.

The 'magic' is the bit that happens "in the middle" ("in-between")!

Now, using the space on your desk, between the 'start' and 'end',
arrange the 'numbers' in the given sequence, across a 'row'.

With your brain (and hands) as the 'computer', work the algorithm you've
already started using:

1 commence with the first item in the list:
ie take the "3" in your left hand
(or perhaps put a suitable finger on it)
2 then compare that with the "5" (right hand/finger)
3 given that three is less than five, what should happen?
4 next compare the "3" with the "9"
(left finger doesn't move, right hand/finger does!)
5 what should happen at this time?
6 keep going, until...
you compare the "3" with the "2"
what happens now?

That's the basic algorithm done!

Continuing the above, after a while there are no more items left to
compare with the one under your left hand/finger. What is the state of
the list?

Not much has changed, but what can we say about the situation
(placement) of the item which is in the first-position/at the left side
of the scraps of paper/playing cards? How does it relate to the
first-position in the target-row?

For the next stage, start with the second item in the list (move your
left hand/pointing-finger!):
1 run through, similar to the above
2 until you (your right hand) reach the end
(yes, it is starting to become a bit boring, but...)

When you have once-again 'run out' of items to compare, what can now be
said about the state of the list? About the values in the first two
positions (and their equivalents in the 'target')?

So, change of thinking: could it be considered that we have two
'sub-lists' (amongst our paper-scraps/playing-cards)? One with two items
in it, the other containing 'all the others'. What can we say about the
'left-sub-list' (and specifically its sequencing)?

So, at this time, please decide if you need to continue with the above
cycles in order to fully-understand how the method arrives at the
desired conclusion - or keep going to prove 'everything' to your
satisfaction, by running right-through. Has the algorithm managed to
arrange the slips of paper/playing cards into the correct sequence, as
portrayed by the bottom-row of numbers?

Once you have confidence in the algorithm, it can be implemented in
code. (but not before! Don't be in too much of a hurry!) So, let's think
about how to represent the algorithm *and* the data, as program-code.

When sorting, items each can be described in two ways. The first item
(in the sample data provided) has the *value* "3". That same item, is in
the first *position* of the list.

Alternately, we can say/think, that the first item in the sample-data
occupies the first position in the list, and has the value "3".

NB I've written the value "3" as inside quotation-marks for
emphasis/identification - it may or may not be a Python-string because
(the sample list) will sort correctly either way, but it appears to be
of integer type, so the quotation-marks are just a way of writing and
emphasis, OK?)

Consider this concept of the two ways to look at each item in the list,
from the perspective of the algorithm/method:-

The *value* of each item will never change (during the sort). We are not
going to increase each value by 10%, for example - we're not giving
employees a salary-raise, are we?

However, the *position* of items may change - because the objective of
sorting is to arrange the items into some definition of sequence (and we
kind-of assume that 'change' is required, because the (sample) list does
not start-out as being in-order).

Accordingly, one of the challenges is to keep separate in our minds, an
item's value, and its position in the list - even though the two
describe the same item!

NB the English word "position", is usually called an "index" or even an
"offset" in Computer Science - ie the item's relative-position in
relation to the beginning of the list. In Python, lists, or rather their
indexes/indices, are zero-based - which means that the first item is
"addressed" as having an index of "0" - ie in Python:

list_name[ 0 ]

Thinking back to our 'playing with paper/cards' above, what is the
purpose of the outer loop? Considering "targets" again: by the end of
each cycle through the loop, what will have been achieved?

Similarly, that of the inner loop? During a single cycle of the
outer-loop, what is the objective of the complete run through the
inner-loop?

When you answer these two questions, how are you expressing the problem
in terms of each item's "position" - and its "value"?

It turns-out, we only need to look any item's value or items' values
within the if-condition (and no-where else in the code). Whereas, we use
indexing to control both of the for-loops (and later, if an exchange of
position is necessary).

So, now (finally!) we can build a 'skeleton' of code, taking it (an
algorithmic) step-by-step to add, or 'flesh-out', more detail gradually:

1
code an outer-loop to traverse the list. With nothing else 'inside' it,
print the index once for each execution of the loop, ie execution should
show you "0, 1, 2, etc" (except each on its own line, not
comma-separated - unless you know how to do that in Python). Ensure the
indexing starts and stops correctly - not too early, nor too late, in
terms of the length of the list - watch-out for that "zero-based"
consideration mentioned earlier, and ensure that last item is not
'visited'! (why not?)

2
add code for the inner-loop. Remembering the observation (above) that
this should only traverse the right-hand sub-list (but should this one
go all the way to 'the end'?). Insert another print statement and
observe the behavior of this (second) index. Ensure that it starts, and
stops, correctly (wrt to the length of the *sub*-list)?

3
Now the program(me) features two indexes/indices. Thinking back to the
algorithm, to what does the first (outer-loop) index 'point'? Similarly,
to what does the second (inner-loop) index point? With those two
thoughts in-mind, does this make it easier to decide which two values to
compare when it comes to writing the if-condition?


Click here to read the complete article
1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor