in reply to Dice roll chances

Hello,

apart from the long title (I used the better one used by toolic) and the poorly formatted text I suggest you to sign in order, in the future, to be able to edit your own post. For you personal colture PERL does not exists: Perl is the language name and perl is the program that executes your programs.

The, first, basic rule in programming is to translate a spoken problem or algorithm to a program.

> I must write a PERL program that randomly generates a number between 1 and 50 in groups of 5. Sum each group and display the results so that each number is on its own line.

The missing spec is: how many times you want to do this? let's say 3 time..

Let's see:

# a PERL program that.. use strict; use warnings; # the missing spec: 3 times for (1..3){ my $sum; # in groups of 5 for (1..5){ # randomly generates a number between 1 and 50 # Sum each group $sum += int (rand(50)+1); } # display the results so that each number is on its own line print "$sum\n"; }

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
  • Comment on Re: Sum group of numbers and display each number is on its own line -- plain way
  • Download Code

Replies are listed 'Best First'.
Re^2: Sum group of numbers and display each number is on its own line -- plain way
by Laurent_R (Canon) on Nov 11, 2017 at 22:40 UTC
    Hi Discipulus,

    I like your solution because it's doing the work using Perl code. I do not think that is is a great idea to use several different modules to solve such a simple problem, especially for a beginner probably learning the art of programming. I think it is better for a beginner to actually learn coding such simple algorithms before using ready-made solutions from modules. (I suspect that some monks will probably disagree with me and say that you shouldn't reinvent the wheel, but I insist that the most essential quality of a programmer is to be able to understand a problem and to implement things with no ready-made crutches.)

    I have seen too many self-styled Web developers just using intensively Laravel or Symphony, but more or less unable to write five lines of consistent PHP code. IMHO, it is important to learn doing the things by yourself before you use some extra libraries doing the hard work for you.

      I agree with "the most essential quality of a programmer is to be able to understand a problem" ...

      I'm not sure I agree with "with no ready-made crutches" ...

      First, they are tools, not crutches. Second, why would you not use the tools if they exist? What is important, as with electric power saws, is learning the use of the tool, and that includes understanding what it does. I do agree with you that often that can not be grasped without working though an implementation manually. But part of learning the craft is learning the tools.

      OTOH: This week I had several interviews for a $job, many with observed coding tests. The one I had most trouble with was some numeric array index-related problem, and I found I couldn't refocus my mind from hours of talk about system architecture to slice and splice and (0..$#x), with the two guys chattering amongst themselves, and it being the end of a long day. Just froze and became irritated. "Would you like me to write an object class, or maybe a log adapter, or something *meaningful*?" I asked, darkly.

      They changed the subject and we talked about concurrency and parallelization, and of course 5 minutes after the call ended I emailed them a simple solution to their task, which was not dissimilar to many questions here. I don't think they will hold it against me too much, but I guess I must concur that keeping in the habit of using the basics is a good policy!


      The way forward always starts with a minimal test.
        Second, why would you not use the tools if they exist?
        Of course, 1nickt++, I fully agree with you on that.

        I was saying these things in the context of someone who is obviously a beginner not only in Perl but most probably also in programming, someone who states not to be able to figure out how to sum up five numbers of an array. I think that this person should really learn to use loops and, in that case, nested loops, as shown by Discipulus.

        So, yes, we should definitely be using tools when they exist and make our lives easier. But, IMHO, a beginner should really learn and understand the basic techniques of programming before using tools that automatize standard, almost boiler-plate, code. That's all I was saying.

        As an example, there is probably no reason to code a sorting algorithm in Perl for a real-life problem, since we have the sort built-in, and this is a very useful and efficient tool that we should use when we need to sort a list or an array. But it is probably very useful, for pedagogical purposes, for a beginner to understand and be able to implement manually a couple of the most common sort algorithms.

        But I suspect we probably mostly agree on this.