Sorting Associated List by Value

Sorting Associated List by Value

am 19.09.2004 21:22:47 von Spahrep

I have an associated list, but i would like to sort it based on the
values, not the keys.

Any help would be appreciated.

--

Re: Sorting Associated List by Value

am 19.09.2004 21:51:16 von Spahrep

Spahrep@yahoo.com wrote:
> I have an associated list, but i would like to sort it based on the
> values, not the keys.

> Any help would be appreciated.

> --

Got it:
foreach $key (sort {$indexer{$a} <=> $indexer{$b}} keys %indexer){
.....
}

re: sort

am 27.12.2006 20:00:17 von aha

I am a newbie and learning perl.

Can anyone help me understand and answer this question:

Lets say there is an array as follows:

@array = (5,3,2,1,4)

Using 2 loops and an if statement I need to sort the contents of the
array so that the numbers go from lowest to highest.

The hint I was given is:

One loop should progress through the array comparing and swapping
adjacent elements (if necessary). The second loop is used to make sure
this happens multiple times based on the size of the array.

Your help would be greatly appreciated.

Thanks

Re: sort

am 28.12.2006 00:28:20 von Paul Lalli

aha wrote:
> I am a newbie and learning perl.

Get your money back immediately.

> Can anyone help me understand and answer this question:
>
> Lets say there is an array as follows:
>
> @array = (5,3,2,1,4)
>
> Using 2 loops and an if statement I need to sort the contents of the
> array so that the numbers go from lowest to highest.
>
> The hint I was given is:
>
> One loop should progress through the array comparing and swapping
> adjacent elements (if necessary). The second loop is used to make sure
> this happens multiple times based on the size of the array.
>
> Your help would be greatly appreciated.

This is beyond absurd. There is *No* excuse for telling a person
learning Perl to write such horrid code. Whoever your instructor is
has no business teaching a course.

You do not want to use any loops, nor any if statement. You want to
use exaclty one line of code, that uses the built-in sort() function.

If you were to apply for a job and were asked to write a short program
that sorted a list of numbers, and you wrote something that involved
nested loops and a conditional, you would NEVER be hired. Your
instructor is doing you a massive disservice, and you should not be
paying for this.

@array = sort { $a <=> $b } @array;

Paul Lalli

Re: sort

am 28.12.2006 01:40:07 von aha

Paul Lalli wrote:
> aha wrote:
>> I am a newbie and learning perl.
>
> Get your money back immediately.
>
>> Can anyone help me understand and answer this question:
>>
>> Lets say there is an array as follows:
>>
>> @array = (5,3,2,1,4)
>>
>> Using 2 loops and an if statement I need to sort the contents of the
>> array so that the numbers go from lowest to highest.
>>
>> The hint I was given is:
>>
>> One loop should progress through the array comparing and swapping
>> adjacent elements (if necessary). The second loop is used to make sure
>> this happens multiple times based on the size of the array.
>>
>> Your help would be greatly appreciated.
>
> This is beyond absurd. There is *No* excuse for telling a person
> learning Perl to write such horrid code. Whoever your instructor is
> has no business teaching a course.
>
> You do not want to use any loops, nor any if statement. You want to
> use exaclty one line of code, that uses the built-in sort() function.
>
> If you were to apply for a job and were asked to write a short program
> that sorted a list of numbers, and you wrote something that involved
> nested loops and a conditional, you would NEVER be hired. Your
> instructor is doing you a massive disservice, and you should not be
> paying for this.
>
> @array = sort { $a <=> $b } @array;
>
> Paul Lalli
>
Thanks Paul,

Everything was flowing well in the course and suddenly I got this
question and its broken the momentum a bit.

I will go with your suggestion and use the built in, although they said
not to use it which was silly given that the previous question involved
sorting strings with the sort operator.

Thanks again.

Re: sort

am 28.12.2006 19:13:47 von Janwillem Borleffs

aha wrote:
> Everything was flowing well in the course and suddenly I got this
> question and its broken the momentum a bit.
>
> I will go with your suggestion and use the built in, although they
> said not to use it which was silly given that the previous question
> involved sorting strings with the sort operator.
>

OTOH, the meaning of the exercise can also be figuring out the program
logics not to dismiss the sort function, but to get familiar with loops and
conditions. The fact that you do not directly know the solution, makes it
worthwhile to figure it out.



JW

Re: sort

am 29.12.2006 05:07:23 von paduille.4060.mumia.w

On 12/27/2006 01:00 PM, aha wrote:
> [...]
> The hint I was given is:
>
> One loop should progress through the array comparing and swapping
> adjacent elements (if necessary). The second loop is used to make sure
> this happens multiple times based on the size of the array.
> [...]

This sounds like bubblesort to me. Ask your teacher about bubblesort.


--
paduille.4060.mumia.w@earthlink.net
http://home.earthlink.net/~mumia.w.18.spam/

Re: sort

am 29.12.2006 18:24:35 von Uri Guttman

>>>>> "PL" == Paul Lalli writes:

>>
>> Using 2 loops and an if statement I need to sort the contents of the
>> array so that the numbers go from lowest to highest.

>> One loop should progress through the array comparing and swapping
>> adjacent elements (if necessary). The second loop is used to make sure
>> this happens multiple times based on the size of the array.

PL> This is beyond absurd. There is *No* excuse for telling a person
PL> learning Perl to write such horrid code. Whoever your instructor is
PL> has no business teaching a course.

he is obviously trying to get them to rediscover bubble sorts and it
isn't a bad thing. knowing the basic (if slow) algorithm is a useful
thing. now the context in which this was taught and making them discover
it on their own is the stupid part.

PL> If you were to apply for a job and were asked to write a short program
PL> that sorted a list of numbers, and you wrote something that involved
PL> nested loops and a conditional, you would NEVER be hired. Your
PL> instructor is doing you a massive disservice, and you should not be
PL> paying for this.

well, i would expect any decent coder to be able to whip up a bubble
sort. not that i do tests like that but it would be a decent baseline
coding and thinking test IMO. it shows understanding of loops,
comparisons, algorithms, short cuts (like not scanning all the way each
loop), etc. and it is short enough to be done during an interview. just
seeing how it is coded would tell me volumes about the coder's skills
and how they think about coding.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: sort

am 29.12.2006 19:50:05 von Paul Lalli

Uri Guttman wrote:
> >>>>> "PL" == Paul Lalli writes:
>
> >>
> >> Using 2 loops and an if statement I need to sort the contents of the
> >> array so that the numbers go from lowest to highest.
>
> >> One loop should progress through the array comparing and swapping
> >> adjacent elements (if necessary). The second loop is used to make sure
> >> this happens multiple times based on the size of the array.
>
> PL> This is beyond absurd. There is *No* excuse for telling a person
> PL> learning Perl to write such horrid code. Whoever your instructor is
> PL> has no business teaching a course.
>
> he is obviously trying to get them to rediscover bubble sorts and it
> isn't a bad thing. knowing the basic (if slow) algorithm is a useful
> thing. now the context in which this was taught and making them discover
> it on their own is the stupid part.

If the assignment was "this is a description of bubble sort. Implement
it." then I would agree with you. It wasn't. It was "Sort this list
of numbers. Use two for loops and an if statement."

> PL> If you were to apply for a job and were asked to write a short program
> PL> that sorted a list of numbers, and you wrote something that involved
> PL> nested loops and a conditional, you would NEVER be hired. Your
> PL> instructor is doing you a massive disservice, and you should not be
> PL> paying for this.
>
> well, i would expect any decent coder to be able to whip up a bubble
> sort. not that i do tests like that but it would be a decent baseline
> coding and thinking test IMO. it shows understanding of loops,
> comparisons, algorithms, short cuts (like not scanning all the way each
> loop), etc. and it is short enough to be done during an interview. just
> seeing how it is coded would tell me volumes about the coder's skills
> and how they think about coding.

And if you told your interviewe "Write up a bubble sort implementation
in Perl" that'd be fine. But if you told your interviewe "Write Perl
code to sort this list of numbers" and he/she wrote a bubble-sort
algorithm, what would that tell you about their coding skills and how
they think about coding?

Paul Lalli

Re: sort

am 29.12.2006 22:46:00 von Uri Guttman

>>>>> "PL" == Paul Lalli writes:

PL> Uri Guttman wrote:

>> he is obviously trying to get them to rediscover bubble sorts and it
>> isn't a bad thing. knowing the basic (if slow) algorithm is a useful
>> thing. now the context in which this was taught and making them discover
>> it on their own is the stupid part.

PL> If the assignment was "this is a description of bubble sort. Implement
PL> it." then I would agree with you. It wasn't. It was "Sort this list
PL> of numbers. Use two for loops and an if statement."

i agree the teacher is a moron and that is the wrong way to teach
it. but it still is trying to get them to figure out how to make a
bubble sort.

PL> And if you told your interviewe "Write up a bubble sort
PL> implementation in Perl" that'd be fine. But if you told your
PL> interviewe "Write Perl code to sort this list of numbers" and
PL> he/she wrote a bubble-sort algorithm, what would that tell you
PL> about their coding skills and how they think about coding?

very different things from very different questions. and i do judge on
code quality independently of CS skills. they are independent. you can
teach coding skills and not get into algorithms. i wouldn't expect
someone to know all the algorithms and such that i know but i would want
them to have enough coding sense to work with and be able to learn
better coding. i am doing that right now with a subcontractor for a
personal project. i am doing the design and all major decisions and he
is coding up parts as i give them to him. and i do strict code review so
the work is up to my standards. i never asked him about his design
skills as i didn't need those. but he is learning my design philoophy
well and is improving in that area. that is all i really want, someone
who can code and learn from me.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org