Re: obj 8-2

Discussion in 'Perl Misc' started by Jürgen Exner, Feb 21, 2014.

  1. Marek Novotny <> wrote:
    >First time poster here. I'm taking a course in Perl, which is knew to me,


    Welcome

    >and I have completed an assignment, but I don't like my answer at all. It
    >feels sloppy to me.


    Well, yeah, let's just say it does lack a bit of finesse,

    >#!/usr/bin/perl
    ># File: obj8-2.pl
    ># Use loops and if statements to find the greatest number in the array.
    ># Once you determine this number be sure to print it out.
    ># Use comments to explain every line of your code:


    You always want to
    use warnings;
    use strict;
    It will make your live so much easier.

    >@array = (42,41,42,31,54,42,56,57,46,58,59,60,34,61);
    >
    >$a = 0;
    >$b = 1;
    >$i = 0;


    What is $a? What is $b? Try to use meaningful names!
    $i is ok as everyone uses it as an index enumerator.

    >while ($i < ($#array * 2)){
    > if (($array[$a]) >= ($array[$b])){
    > $c = ($array[$a]);
    > $b++;
    > }else{
    > if (($array[$b]) >= ($array[$a])){
    > $c = ($array[$b]);
    > $a++;
    > }
    > }
    >$i++;
    >}
    >print "The Answer is: $c\n";


    Well, I'm not even sure how this code works. In any case you are
    thinking waaaaaayyyyy to complicated from an algorithmic point of view.

    >I would have liked to use the sort operator, but I am supposed to use
    >loops and if to accomplish the goal here. The lesson only has 5 integers
    >in the original array but I expanded it and changed it to help me develop
    >what I have now.
    >
    >Thing is, I hate this answer. It works, but I feel like it is sloppy. Is
    >there a better way, again without using the sort operator?


    And this is your mental problem: you are thinking about it with sort()
    in mind while in reality it is much, much simpler.

    Let's pretend I claim that $max is the largest value in the array. How
    would you go about to prove me wrong? You would simply loop through the
    array, compare each element against $max, and if you found a larger
    value then I was wrong.
    Now, the trick is that now I am claiming that new, larger element that
    you just found, to be real $max. Now, this time to prove me wrong again
    you don't even have to go back to first element because you already know
    that it is smaller than my old $max, so it is certainly smaller than my
    new $max.

    So, to make long story short: while looping through the array jsut keep
    one running value $LargestNumberIHaveFoundSoFar which you update every
    time you find a lager value in the array. And when you get to the end of
    the array it must contain the largest value of all the array.

    jue
     
    Jürgen Exner, Feb 21, 2014
    #1
    1. Advertising

  2. Jürgen Exner

    John Bokma Guest

    Jürgen Exner <> writes:

    > So, to make long story short: while looping through the array jsut keep
    > one running value $LargestNumberIHaveFoundSoFar which you update every
    > time you find a lager value in the array. And when you get to the end of
    > the array it must contain the largest value of all the array.


    Depends what's in $LargestNumberIHaveFoundSoFar ;-). Set it to a value
    taken from the array (it doesn't matter which one) before you start to
    loo(p|k)ing.

    --
    John Bokma j3b

    Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
    Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
     
    John Bokma, Feb 21, 2014
    #2
    1. Advertising

  3. John Bokma <> wrote:
    >Jürgen Exner <> writes:
    >
    >> So, to make long story short: while looping through the array jsut keep
    >> one running value $LargestNumberIHaveFoundSoFar which you update every
    >> time you find a lager value in the array. And when you get to the end of
    >> the array it must contain the largest value of all the array.

    >
    >Depends what's in $LargestNumberIHaveFoundSoFar ;-). Set it to a value
    >taken from the array (it doesn't matter which one) before you start to
    >loo(p|k)ing.


    O darn, you spoiled the litmus test. I am always leaving this question
    open on purpose.

    The time it takes a person to figure out that there is an open question
    is a good indicator for how good they are as future programmers.
    If they don't notice the problem at all, then forget about them.
    If they start thinking "Wait a second, I need a start value", then that
    is a start.
    If they find "0 is good for natural numbers" then that's promising.
    If they notice "A fixed value doesn't work as initial value for integers
    or reals", then that is very promising.
    And if an aspiring student comes up with the idea to use some value from
    the array, then he is someone to keep a very close eye on: he is a
    potential candidate for a future student assistent or similar himself.

    jue
     
    Jürgen Exner, Feb 21, 2014
    #3
  4. Jürgen Exner

    John Bokma Guest

    Jürgen Exner <> writes:

    > John Bokma <> wrote:
    >>Jürgen Exner <> writes:
    >>
    >>> So, to make long story short: while looping through the array jsut keep
    >>> one running value $LargestNumberIHaveFoundSoFar which you update every
    >>> time you find a lager value in the array. And when you get to the end of
    >>> the array it must contain the largest value of all the array.

    >>
    >>Depends what's in $LargestNumberIHaveFoundSoFar ;-). Set it to a value
    >>taken from the array (it doesn't matter which one) before you start to
    >>loo(p|k)ing.

    >
    > O darn, you spoiled the litmus test. I am always leaving this question
    > open on purpose.


    Ah, well in the mean time several complete answers have been posted.

    > The time it takes a person to figure out that there is an open question
    > is a good indicator for how good they are as future programmers.
    > If they don't notice the problem at all, then forget about them.
    > If they start thinking "Wait a second, I need a start value", then that
    > is a start.
    > If they find "0 is good for natural numbers" then that's promising.
    > If they notice "A fixed value doesn't work as initial value for integers
    > or reals", then that is very promising.
    > And if an aspiring student comes up with the idea to use some value from
    > the array, then he is someone to keep a very close eye on: he is a
    > potential candidate for a future student assistent or similar himself.


    At least I got a cookie out of it :-D.

    I've helped in the past students with Perl homework (see sig) and most
    people who asked my help had 0 interest in learning Perl (at least that
    was the impression) and 0 interest in becoming a programmer.

    Also, the examples provided by teacher(s) were very poor in quality.

    It has dried up over the past 2 years, the cries for help. I guess they
    have discovered stack overflow and/or Perl is less used in education.

    I still do some things for book(s), like helping a friend out. A book
    ("for in the perl kitchen") just arrived.

    --
    John Bokma j3b

    Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
    Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
     
    John Bokma, Feb 21, 2014
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    3
    Views:
    15,842
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Phil Endecott

    Obj* ptr = new Obj(X)

    Phil Endecott, Jun 3, 2005, in forum: C++
    Replies:
    5
    Views:
    485
    Mark P
    Jun 3, 2005
  3. Shalabh Chaturvedi
    Replies:
    2
    Views:
    460
    Mike C. Fletcher
    Feb 20, 2004
  4. Matthew Thorley

    How do you convert a string obj to a file obj?

    Matthew Thorley, May 4, 2005, in forum: Python
    Replies:
    7
    Views:
    525
    Peter Otten
    May 4, 2005
  5. alf
    Replies:
    9
    Views:
    428
Loading...

Share This Page