GO37H3's n00b to 1337 programming/comp thread

  • Thread starter Thread starter G037H3
  • Start date Start date
  • Tags Tags
    Thread
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
64 replies · 10K views
story645 said:
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.
 
Physics news on Phys.org
G037H3 said:
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
 
Last edited:
G037H3 said:
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.

dave said:
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:
Code:
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'
 
story645 said:
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:

Code:
@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
 
davee123 said:
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:

Code:
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