Test your skills (2005)
Author |
Message |
wtd
|
Posted: Thu Nov 24, 2005 7:10 pm Post subject: (No subject) |
|
|
Try that again after:
Python: | foo.grades.append(100) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Mon Nov 28, 2005 11:24 am Post subject: (No subject) |
|
|
wtd wrote: Here's a python question.
Python: | class Student(object):
def __init__(self, name, *grades):
self.name = name
self.grades = grades
def __str__(self):
return "%s has an average of %.2f" % (self.name, self.average_grade) |
Add a single line of code which makes the above class work correctly. It must not only work, but also accurately report the student's average grade.
The solution:
Python: | class Student(object):
def __init__(self, name, *grades):
self.name = name
self.grades = grades
def __str__(self):
return "%s has an average of %.2f" % (self.name, self.average_grade)
average_grade = property(lambda self: float(sum(self.grades)) / len(self.grades)) |
|
|
|
|
|
|
rizzix
|
Posted: Thu Dec 01, 2005 2:21 pm Post subject: (No subject) |
|
|
Write a Perl one-liner to print out the 8-12th lines of a file. (this means i should be able to execute the code with perl -e) |
|
|
|
|
|
wtd
|
Posted: Thu Dec 01, 2005 2:40 pm Post subject: (No subject) |
|
|
code: | perl -ne "BEGIN{$c=0}print if++$c>7&&$c<13" file_name_here |
|
|
|
|
|
|
rizzix
|
Posted: Thu Dec 01, 2005 3:02 pm Post subject: (No subject) |
|
|
hmm.. ok.. but may i add one more criteria?
Try and avoid the braces {} |
|
|
|
|
|
rizzix
|
Posted: Thu Dec 01, 2005 5:14 pm Post subject: (No subject) |
|
|
Write a Perl one-liner, but avoid using any {} blocks.
The one-liner should parse a file extracting all Java-like comments where nesting comments is not allowed. (i.e you can safely assume there are no nested comments)
Sample Input: Java: | import java.awt.*;
import javax.swing.*;
public class test {
public static void main (String[] args ) {
// insert your application initialization code here
JFrame frame = new JFrame("Test Application"); /* create a new JFrame */
Dimension screenSize = frame. getToolkit(). getScreenSize();
frame. getContentPane(). add(new JLabel("Hello World!"));
frame. setBounds(screenSize. width/ 4, screenSize. height/ 4,
screenSize. width/ 2, screenSize. height/ 2);
frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
frame. setVisible(true); /* always the last SWING method in main*/
}
} |
Sample Output: code: | 6: insert your application initialization code here
7: create a new JFrame
14: always the last SWING method in main
|
|
|
|
|
|
|
wtd
|
Posted: Thu Dec 01, 2005 5:34 pm Post subject: (No subject) |
|
|
The simplest way I can think of does involve using the BEGIN block again. |
|
|
|
|
|
wtd
|
Posted: Thu Dec 01, 2005 7:31 pm Post subject: (No subject) |
|
|
Language: O'Caml
code: | # let h = Hashtbl.create 10;;
val h : ('_a, '_b) Hashtbl.t = <abstr>
# Hashtbl.add h "foo" 42;;
- : unit = ()
# Hashtbl.add h "bar" 27;;
- : unit = ()
# h;;
- : (string, int) Hashtbl.t = <abstr> |
Now, we know that the Hashtbl module has a "fold" function which takes a function, a hashtable, and an initial value.
The function it takes accepts three arguments: the key of the current entry, the value of the current entry, and the "accumulator" value.
Knowing this, write an expression which retrieves the keys from "h" as a list of strings. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rizzix
|
Posted: Thu Dec 01, 2005 8:26 pm Post subject: (No subject) |
|
|
rizzix wrote: Write a Perl one-liner to print out the 8-12th lines of a file. (this means i should be able to execute the code with perl -e)
The solution...
Perl: | 8 .. 12 and print while (<>) |
|
|
|
|
|
|
wtd
|
Posted: Thu Dec 01, 2005 8:52 pm Post subject: (No subject) |
|
|
rizzix wrote: rizzix wrote: Write a Perl one-liner to print out the 8-12th lines of a file. (this means i should be able to execute the code with perl -e)
The solution...
Perl: | 8 .. 12 and print while (<>) |
The parens shouldn't be necessary due to the way postfix loops work. |
|
|
|
|
|
rizzix
|
Posted: Thu Dec 01, 2005 9:07 pm Post subject: (No subject) |
|
|
yep... now solve the other one |
|
|
|
|
|
rizzix
|
Posted: Mon Dec 05, 2005 8:22 pm Post subject: (No subject) |
|
|
rizzix wrote: Write a Perl one-liner, but avoid using any {} blocks.
The one-liner should parse a file extracting all Java-like comments where nesting /* */ comments is not allowed. (i.e you can safely assume there are no nested /* */ comments)
The Solution... Perl: | (s|.*/ {2}(.* )|$ 1| or s|.*/\*|| .. s|\*/.*|| ) and s|\ s*|| and printf "%3d: %s", $., $_ while <> |
Breaking it down...
First we match /* to */ and extract contents within // to \n with the following regexs: Perl: | s|.*/\*|| .. s|\*/.*|| | Note: we use the substitution since we are only interested in the content that lies within these delimiters.
Then we trim out the whitespace off the result:Finally we print it: |
|
|
|
|
|
Hikaru79
|
Posted: Wed Dec 07, 2005 4:30 pm Post subject: (No subject) |
|
|
Holy mother of God. I'm oddly reminded of the bash.org quote that goes:
bash.org wrote: s7ank: i want to be one of those guys that types
"s/j&jd//.^$ueu*///djsls/sm."
and it's a perl script that turns dog crap into gold. |
|
|
|
|
|
md
|
Posted: Wed Dec 07, 2005 5:46 pm Post subject: (No subject) |
|
|
My mind just went *boink*. You should really put warnings up before showing code like that... |
|
|
|
|
|
wtd
|
Posted: Tue Dec 13, 2005 5:28 am Post subject: (No subject) |
|
|
Java TYS:
code: | import java.util.*;
public class OldArrayListTest {
public static void main(String[] args) {
ArrayList a = new ArrayList();
a.add("hello");
a.add("world");
}
} |
Demonstrate the command that will compile this without warnings using the Java 1.5.0 compiler. |
|
|
|
|
|
|
|