Re: Homework question list
by planetscape (Chancellor) on May 20, 2005 at 22:49 UTC
|
Do you have access to programming books that deal with other languages? Those should have exercises that you can implement in Perl.
The Monks are very discerning; many times they recognize homework questions. Sometimes SoPW has homework questions unabashedly posted as such. Try Super Search on "homework."
Or Super Search for "Perl Golf." If the questions are too difficult, take a look at the answers, and reverse-engineer them so you understand why they do what they do.
If all else fails, create (and solve) your own small challenges. Tackle just a section of those questions that seem like work.
Good luck,
planetscape
| [reply] [Watch: Dir/Any] |
Re: Homework question list
by Zaxo (Archbishop) on May 20, 2005 at 23:53 UTC
|
TIMTOWDI provides a class of Perl questions which I favor: "Show N ways to do X in Perl. Describe their different properties."
For instance,
Show three ways to remove trailing newlines from the elements of a Perl array. Which would you prefer, and why?
Seekers of Perl Wisdom provides numerous cases which could be turned into this kind of question, complete with answers.
This tests knowledge of perl, rather than programming speed or CS-fu, which is good if that's what you mean to test.
| [reply] [Watch: Dir/Any] |
Re: Homework question list
by mrborisguy (Hermit) on May 20, 2005 at 19:50 UTC
|
I think this is a good idea, too! Maybe not even just homework questions, but little scripts to write to hone a monk's skills would be good. Anyway, take a look at Should PM have a Puzzles Corner?, there's been some discussion on that thread concerning something somewhat like this.
-Bryan
| [reply] [Watch: Dir/Any] |
Re: Homework question list
by cLive ;-) (Prior) on May 21, 2005 at 08:38 UTC
|
Slightly OT, but I've been interviewing long distance recently, and I used the following 3 questions tasks (ffs). Bear in mind these are for web application development, so front end is important too.
- create a calculator using HTML/Javascript, that processes the entered equation through a Perl CGI script and displays the result. This tests JS usage and, the biggie, whether they untaint user input in the CGI before 'eval'ing. Also, use of CGI/Mason/Template Toolkit is examined (and mentioned in the original question).
- Compare two arrays and show the elements that exist in both (I actually screwed up in my live question, but the answers were still informative). This is a great question because it shows how someone thinks. Lower level devs use nested loops (I did, when this was thrown at me a few years ago). Smart people use hash slices. Really smart people use 'undef'd hash slices to save memory (or, rather, time - ty runrig).
- Search google for a particular search term and present the first ten results in a web page. Another good one to see how someone thinks. LWP is quick and dirty, the Google SOAP Perl API is elegant, yet non-intuitive if you haven't heard of it.
Maybe we should start a quiz section where these sorts of questions can be dumped? I'm sure there are many more that push you to think laterally - or use aspects of Perl that involve some interesting thinking.
cLive ;-)
| [reply] [Watch: Dir/Any] |
|
2. Compare two arrays and show the elements that exist in both (I actually screwed up in my live question, but the answers were still informative). This is a great question because it shows how someone thinks. Lower level devs use nested loops (I did, when this was thrown at me a few years ago). Smart people use hash slices. Really smart people use 'undef'd hash slices to save memory.
Could you give an example of the "hash slice" method? Using hashes is a good way (something like my %temp; my @doubles = grep { ++$temp{$_} == 2 } @a1, @a2; works nicely), but I can't seem to figure out the "(undef'd) hash slice" method. Please enlighten me :-)
Arjen
| [reply] [Watch: Dir/Any] [d/l] |
|
Your example fails if @a1 or @a2 have a value more than once. For example, @a1 = (1, 2, 3, 2, 1) and @a2 = (4, 5, 6, 5, 4). Your grep would show 1, 2, 4, and 5 as elements existing in both arrays, when the right answer is obviously zero.
I will also disagree with cLive ;-) that "really smart people" use the undef'd hash slices to save memory - if memory is a concern, sure. But in general, the difference is not going to be significant. Premature optimisation and all that. I've even found sometimes where using the standard "++$hash{$value}" turns out to be handy three months later when the number of times a value shows up becomes relevant. I didn't need to change nearly as much code because I wasn't "really smart" according to cLive's definition.
Anyway, as always, TIMTOWTDI, so being able to use the undef'd hash slice is still a tool to keep handy:
my %a1;
undef @a1{@a1};
my @in_both = grep { exists $a1{$_} } @a2;
Unfortunately, this has the side effect of showing duplicates if @a1 has a value, and @a2 has that value multiple times. Which, of course, may be what you want, but it's not explicit in the original requirement.
my (%a1, %a2);
undef @a1{@a1};
undef @a2{@a2};
my @in_both = grep { exists $a1{$_} } keys %a2;
Oh, and I also recommend better variable names than what I'm using here. :-)
Update: Added the italicised part in the last line. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
Interestingly, you made the same mistake an applicant made when doing the compare - as Tanktalus has pointed out :)
By the way, Tanktalus, by "really smart people", I was referring to tilly here ;-)
cLive ;-)
| [reply] [Watch: Dir/Any] |
|
|
Smart people use hash slices. Really smart people use 'undef'd hash slices to save memory.
undef'd hash slices save time not memory.
Update: Also, as pointed out to me in a previous post, only use this method if you don't care about the values of the hash, as the values do not necessarily get set to undef.
| [reply] [Watch: Dir/Any] |
|
# given two arrays you want to compare for common elements, @a1 & @a2
+...
my %common;
undef @common{@a1};
delete @common{@a2};
my @common = keys %common;
#for example:
DB<1> @a1 = qw(a b c d e)
DB<2> @a2 = qw(a c e )
DB<3> undef @common{@a1}
DB<4> x \%common
0 HASH(0x8408418)
'a' => undef
'b' => undef
'c' => undef
'd' => undef
'e' => undef
DB<5> delete @common{@a2}
DB<6> x \%common
0 HASH(0x8408418)
'b' => undef
'd' => undef
DB<7> print join " ", keys %common
b d
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
Re: Homework question list
by dynamo (Chaplain) on May 20, 2005 at 22:17 UTC
|
Any fairly simple task or solution might be considered a homework question. You're looking for an archive, and we have one: Categorized Questions and Answers. Look there for the simpler looking stuff. | [reply] [Watch: Dir/Any] |
Re: Homework question list
by TedPride (Priest) on May 21, 2005 at 07:26 UTC
|
Just browse the boards every day and try to solve the more algorithmic-style questions without looking at the code people have posted. Then compare your code to theirs. A week or two of this will give you a fairly good grasp of Perl.
By the way, the simplest way to remove trailing newlines from an array is:
chomp for @arr;
You could also use chop, substr, or s///, but chomp has the best combination of speed and flexibility for this task. | [reply] [Watch: Dir/Any] [d/l] |
|
chomp @arr;
because when chomp is fed a list, it'll chomp each element.
Arjen | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Homework question list
by ysth (Canon) on May 22, 2005 at 20:14 UTC
|
| [reply] [Watch: Dir/Any] |