# GO37H3's n00b to 1337 programming/comp thread

by G037H3
Tags: 1337, go37h3, n00b, programming or comp
P: 657
 Quote by G037H3 FizzBuzz (some people really can't do this?...):
I've never tried it, but we did have some people that might not have been able to. We once were interviewing people for Perl jobs, and someone claimed that they had spent the last 3 years programming with Perl's Mason library for Amazon. Sounded OK, but then we asked them this simple question:

 Write a Perl program that does the following: 1. Opens a file whose name is given on the commandline 2. Replaces all occurrences of 'Y' with 'K' 3. Writes the results to a new file with ".mod" appended to the original filename 4. Runs the external command "swizzle" with the original filename as an argument
The applicant started to write a combination of Perl and C, and couldn't finish the program. It was just sad. We expect that the 3 years of Perl experience reported were simply adjusting the HTML parts of the Mason templates, and not really working with the underlying Perl.

I think the sad part isn't so much that people can't do it-- it's that people that can't do it actually APPLY for programming jobs.

DaveE
 P: 326 Given a string containing a series of addresses, separated by commas, here's a list comprehension to create a list of those addresses with domain names of 2-4 characters: [url for url in urlstring.split(',') if 2 <= len(url.split('.')[-1]) <=4]
P: 670
 Quote by davee123 I think the sad part isn't so much that people can't do it-- it's that people that can't do it actually APPLY for programming jobs.
I think it's because a lot of people don't realize just quite how bad they are. I thought I was a pretty decent coder when I was a freshie, (I'd been coding for a few years at that point) and a friend decided to tutor me in C. It was a disaster and I realized that I was a lousy coder, and it took a couple of years and lots of coding intensive research later to get my coding up to half-decent. I think a lot of students refuse to hit the "I'm lousy" stage and therefore don't put in the time coding and poring over documentation to get to the half-decent stage.
P: 657
 Quote by G037H3 Given a string containing a series of addresses, separated by commas, here's a list comprehension to create a list of those addresses with domain names of 2-4 characters: [url for url in urlstring.split(',') if 2 <= len(url.split('.')[-1]) <=4]
I admittedly don't know Python, but this seems wrong. What sort of "addresses" are you assuming your input contains? What's a sample input and your expected resultant list?

DaveE
P: 670
 Quote by davee123 I admittedly don't know Python, but this seems wrong.
It actually works just as expected. What do you think is wrong with it? The python looks fine to me, and I use python all the time.

The python's sort of advanced, but he got the answer from stackoverflow.
P: 657
 Quote by story645 It actually works just as expected. What do you think is wrong about it? The python looks fine to me, and I use python all the time.
Guh, well, ok-- I'd still just like a sample expected input and output.

To start, it says "urlstring", which implies that it's a list of URLs, but the text says "addresses", which implies "e-mail addresses". Assuming you had a list of URLs:

http://www.everything2.com/index.pl?node_id=429011,www.google.com,http://www.physicsforums.com/forumdisplay.php?f=165
I'm assuming the 1st split would split it into 3 strings:

1) http://www.everything2.com/index.pl?node_id=429011
3) http://www.physicsforums.com/forumdisplay.php?f=165
Next, you're splitting on "." (which I assume means "." and not the regexp of "any character" as it might in Perl), which would split it into a resulting set of arrays:

1)
A) http://www
B) everything2
C) com/index
D) pl?node_id=429011

2)
A) www
C) com

3)
A) http://www
B) physicsforums
C) com/forumdisplay
D) php?f=165
Next, it only looks at the ones where the final element in the array is between 2 and 4 characters:

1) "pl?node_id=429011", 17 characters
2) "com", 3 characters
3) "php?f=165", 9 characters

So, none of those look like domain names that it's comparing against-- the closest is "com" in "www.google.com", but while that's TECHNICALLY a domain name, most people would think of the domain name as "google.com" (the subdomain), and not simply "com".

Now what if it were a list of e-mail addresses?

foobar@myurl.com,john.doe@example.org,"Mrs. Abigail Whithersby"@junk.com
Again, assuming the 1st split would split it into 3 strings:

1) foobar@myurl.com
2) john.doe@example.org
3) "Mrs. Abigail Whithersby"@junk.com
Next, splitting by ".":

1)
A) foobar@myurl
B) com
2)
A) john
B) doe@example
C) org
3)
A) "Mrs
B) Abigail Whithersby"@junk
C) com
And looking at the last elements again:

1) "com", 3 characters
2) "org", 3 characters
3) "com", 3 characters

So, in that case, if it was splitting on "@" instead of ".", it would make sense, but I don't think that's what it's doing. And again, TECHNICALLY those are "domain names", but not how people think of them-- you'd expect it to be pulling out the full "example.org", etc., and not just the "org" part.

Anyway, am I misinterpreting something?

DaveE
P: 326
 Quote by story645 It actually works just as expected. What do you think is wrong with it? The python looks fine to me, and I use python all the time. The python's sort of advanced, but he got the answer from stackoverflow.
yeah, I was the asker of that question. I joined stackoverflow and stuff. I had a different solution, but it was more convoluted. I actually don't find list comprehensions to be very advanced at all...it's a lot easier to me than using dictionaries.
P: 657
 Quote by G037H3 yeah, I was the asker of that question. I joined stackoverflow and stuff. I had a different solution, but it was more convoluted. I actually don't find list comprehensions to be very advanced at all...it's a lot easier to me than using dictionaries.
No, it's not very complex-- it's something you'd learn in your first course in programming.
So, why look for only things that have 2-4 letters at the end of the string? If you're looking for valid domains in the list, you probably want to check the validity of the leading characters. Perl-wise, I'd probably do a regex for that:

grep /^([a-z0-9\-]+\.)*[a-z0-9]{2,4}$/i, split /,/,$url;

Dunno how tricky it is to incorporate pattern matches in Python, but it probably makes for a better solution if you're checking for validity rather than something simple like length.

DaveE
P: 670
 Quote by G037H3 yeah, I was the asker of that question. I joined stackoverflow and stuff. I had a different solution, but it was more convoluted.
Convoluted solutions are good-that's how you learn the ins and outs of a language. I usually go through a whole bunch of them before finding the magical list comprehension that does what I need.

 I actually don't find list comprehensions to be very advanced at all...it's a lot easier to me than using dictionaries.
They're not, but a list comprehension with two levels of filtering and an uncommon condition is what tipped me off that you probably didn't write it. Be careful with using stackoverflow when you're starting out 'cause the guru answers often obscure the steps you need to learn to be able to craft those answers.

 Quote by dave Dunno how tricky it is to incorporate pattern matches in Python
Dead simple 'cause the python re library uses perl style regexs. The stackoverflow link actually has the regex variant too:
urls= 'albatross.org, boogaloo.boolean.net, bedei9.paralex.zenoparadox.herc.gr, takeawalkon.the.wildside,fuzzy.logic.it, bronzeandiron.age, areyou.serious, mydom.dom.net, hun.com'
regex = re.compile('''[[a-zA-Z0-9\-\.]+\.]*[a-zA-Z0-9\-]{2,4}\.[^\.\,]+''')
url_list = regex.findall(urls)
So the code G037H3 gave acts on the same urls input.
You're pretty much spot on with how it works, but to recap:

urlssplit(') would take urls= 'albatross.org, boogaloo.boolean.net, bedei9.paralex.zenoparadox.herc.gr, takeawalkon.the.wildside,fuzzy.logic.it, bronzeandiron.age, areyou.serious, mydom.dom.net, hun.com'

and split on (',') to return a list of urls
['albatross.org' , 'boogaloo.boolean.net', ...., 'mydom.net', 'hun.com']

then split on ('.') would work on every element in the url list and split it up into lists, so:
['albatross', 'org'], ['boogaloo', 'boolean', 'net'], .... ['mydom', 'net'],...['hun', 'com']

and 2 <= len(url.split('.')[-1]) <=4 tests every last list element, so:
'org', 'net', ..., 'net', 'com'
P: 657
 Quote by story645 a list comprehension with two levels of filtering and an uncommon condition is what tipped me off that you probably didn't write it.
That's true-- a beginner solution to the same problem would be more step-by-step rather than rolled up into a single line:

@list = split /,/,$urls; @final_list = (); foreach my$domain (@list) {
@parts = split /\./,$domain; if(length($parts[-1]) >= 2 && length($parts[-1]) <= 4) { push @final_list,$domain;
}
}
Works just as well as the posted solution (probably takes a negligible amount more memory/CPU), but is easier to understand piece by piece.

DaveE
P: 670
 Quote by davee123 That's true-- a beginner solution to the same problem would be more step-by-step rather than rolled up into a single line:
The python equivalent is:

urlslist = urls.split(',')
final_list = []
for domain in urllist:
parts = domain.split('.')
if ((len(parts[-1]) >=2) and (len(parts[-1])<=4)):
final_list.append(parts[-1])
list is a reserved word in python

 Related Discussions Academic Guidance 6 Engineering, Comp Sci, & Technology Homework 1 Academic Guidance 4 Career Guidance 3 Programming & Computer Science 8