GO37H3's n00b to 1337 programming/comp thread

  • Thread starter G037H3
  • Start date
  • Tags
    Thread
In summary, GO37H3's n00b to 1337 programming/comp thread is a forum for beginners looking to improve their programming and computer skills. The thread offers a supportive community and resources for learning and practicing various programming languages and techniques. It also provides tips and advice for aspiring programmers to become more proficient in their craft. Participants can share their progress and seek help from more experienced members to become 1337 programmers.
  • #36
G037H3 said:
Hello world:

Code:
>>> print ("Hello, World!")

The output should be:

Code:
Hello, World!
Just checking -- you actually ran this program to see that it gave the output, right? :smile: It might be worth doing it (or some other simple program) both from the interpreter and by writing a script -- if nothing else, just to make sure you are actually capable of using python both ways.
 
Technology news on Phys.org
  • #37
Mark44 said:
"Messing with mistakes" -- debugging -- comes with the territory. Being able to write good code that has no bugs requires attention to detail, among other attributes.

True, but I think that thinking before writing will help a lot. :P
Just checking -- you actually ran this program to see that it gave the output, right? It might be worth doing it (or some other simple program) both from the interpreter and by writing a script -- if nothing else, just to make sure you are actually capable of using python both ways.

I did in interactive mode. I haven't done it as a script yet. I just tried and apparently the syntax I used was incorrect.
 
  • #38
G037H3 said:
True, but I think that thinking before writing will help a lot. :P
That pretty much goes without saying. If you're working on small programs, it's easier to comprehend what the entire program is doing. With a little bit of experience, you can write small programs without bugs.

As programs get larger, with more involved logic, you might be able to write code without syntax errors, but such programs are much more likely to have semantic errors. The compiler/interpreter can always catch syntax errors, since you are using the language incorrectly, but semantic errors, which stem from flaws in your logic, are more difficult to find. It's a fact of life that programmers write code with errors.
 
Last edited:
  • #39
Here are some suggestions that I hope transcend programming in general regardless of language, platform, and other important elements.

1) State

The first thing you should be aware of in any program is its state. It doesn't matter if you're writing code for a HTML page or something in C++, you need to know the entire state of the machine relevant to your program.

The more complex your program the bigger the state you will need to know. Once you get to understanding all the state variables in your code and how they evolve over time, you will become a better programmer.

In relation to state, you not only need to know the variables that you deal with, but you have to know how to handle every effect that these variables have on your program.

The above might sound obvious, but for the sake of getting things done, lots of mistakes/bugs are caused simply because certain states of the system are not handled correctly.

2) Learn a popular assembly language

Most high level languages are useful and its not often economically feasible to write programs big or small in assembly language.

Never the less you should understand the architecture of a common platform, and to do that you will need to learn the relevant assembly language.

Everything from interrupts, to ports, to math operators, rotations and shifts, conditional jumps, calls, the stack, registers, basically everything for a common platform will help you learn all the stuff that aid you to understand how high level languages work in relation to the architecture of the physical platform.

3) Structure

Once you know a platform and how to handle and understand the state of a platform (this could be the local machine architecture, or perhaps a web platform, or a virtual machine etc), you need to understand structure.

Structure is everything. You want to send data over a network? It has structure. TCP/IP reliable messages have structure. Unrealiable UDP packets have structure.

Multimedia files have structure. Images, audio, video, text, everything has an inherit structure.

Look at different kinds of structures. Also look at something like XML or some other container based structural format and incorporate something similar in your programs. An open container based structure like XML allows a kind of "plug-in override" that let's you add functionality through new tokens and combined with a good-plugin architecture allows good additions to systems without having to re-write the fundamental core.

4) Learn about plug-in, expandable architectures

Well written programs allow easy expansion of a system, with minimal changes to the systems core.

Look at things like Microsofts Component Object Model, CORBA, and .NET and look at how extensible/plug-in systems are made.

The reason for this is that you want to make programs that build on other systems where integration between systems is as seemless as possible. If you ever get a job as a developer you will often have very complex systems, easily going over a million lines of code where you will have to integrate many different libraries that have a specialized set of functionality.

The good systems are the ones that allow easy integration as well as addition of features whereby the core of the system minimally changes: Do this and you will be not only a good programmer but a good software architect.

5) Structure II: Abstraction

Building on the ideas of expandable architectures and data structures I, you will want to learn about abstract data structures.

Things such as templates, and common container structures is at the heart of "abstract structures". Things like linked lists, queues, stacks, arrays, directed acyclic graphs, hash tables, class factory and plug-in template classes and so on contribute to you writing code that is short and has wide application.

This leads to my next point:

6) Write code that is as general as possible

You want to get into the habit of creating libraries that have general functionality.

You will create more specific code obviously that meets the needs of your program, but you do not want to write the same thing twice because a) its unnecessary and b) it takes more time to keep a particular function generalized meaning that if something goes wrong it can happen in more than one place in comparison to when you have a general template, or library where the code is centralized and thus helps you fix any problems and confines your errors to a particular code segment and not many.

Hopefully these tips should give you something to think about, and all the best with your learning
 
  • #40
G037H3 said:
I sincerely believe that Python is better as a first language, because the focus is on applications, not explaining syntax. It's self-evident.

Don't fall into this trap. There is a little saying: you can spend your time learning mathematics and then spend the rest of your life learning applications, or you can learn applications and spend the rest of your life learning math. Programming isn't much different. Many people can learn how to program, but what makes certain programmers exceptional is their ability to truly understand a language and mathematics in order to achieve optimization and effectiveness. This will require you to stop complaining and to learn details.
 
  • #41
It's better to have three programs that had bugs that were found and fixed than it is to have one program that never had bugs at all. At some point it becomes counter-productive to take great pains to avoid making bugs.

Believe it or not, debugging is not the hard part -- the hard part is design. Many hard debugging problems are the result of poor design that obscures bugs and makes them easy to write. Many other bugs are incorrectly designed code, rather than code that was implemented incorrectly.



One of the big tricks to programming is how to write code that makes it easy to detect and diagnose bugs, and how to write code that is difficult to use incorrectly.
 
  • #42
I acquired the Boole book. ^_^
 
  • #43
G037H3 said:
True, but I think that thinking before writing will help a lot. :P

Thinking tends to help always, but avoiding bugs is not worth time spent. Also, people usually work in groups and no one has all the knowledge for the project. If you work alone you still can produce bugs depending on the size of the project, amount of alcohol and fatigue.

Friend of mine got home very drunk and decided to write some code. At morning he found that the code is syntactically perfect (no warnings or errors during compilation), but semantically it was total crap.
 
  • #45
Oh, I don't have that sort of attitude, though I do wish to prepare myself mathematically for Knuth >.<
 
  • #46
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

------------------------------------------------------------------------------------
Code:
print(sum([x for x in range(1000) if x % 3 == 0 or x % 5 == 0]))

List comprehensions; u jelly?
 
  • #47
Did the code work (I don't use python)?
 
  • #48
jarednjames said:
Did the code work (I don't use python)?

yep :)
 
  • #49
Are you trying out various functions? Or are you actually building a piece of software to do something (even relatively simple)?

I ask, because I find myself better at learning whilst doing. I can't book study because it just doesn't sink in.

I'd also add that as nice as learning from a book is, until you can apply it to real world scenarios it's pretty much useless.
 
  • #50
jarednjames said:
Are you trying out various functions? Or are you actually building a piece of software to do something (even relatively simple)?

I ask, because I find myself better at learning whilst doing. I can't book study because it just doesn't sink in.

I'd also add that as nice as learning from a book is, until you can apply it to real world scenarios it's pretty much useless.

I play a lot, mixing and matching. I'll write something one way and then try to think of two other ways of doing the same thing. Right now, I'm doing lots of stuff with lists and dictionaries, convoluted manipulations. I'm starting to move towards making useful stuff though. :)
 
  • #51
G037H3 said:
I'll see what other people think of it as well. >.<
I've been using it lately and recommend it as it's pretty well behaved/the easiest of the lot. Pythonwin is also pretty decent, but can be somewhat cranky.

I know I don't *need* one. But I want one. Preferably with a black background and pretty colors. :O
Python cmd line does that, so does ipython, but both are shells, not ides. Actually, you can also just configure IDLE to look like that.

basics out of the way you start trying to learn a web framework such as Django or Pylons.
Start with bottle, as it's just one python file.

The standard 'introductory' book on algorithms is CLRS.
It's considered the bible by most of the computer scientists I've met.

You might want to check out project euler and dive into python while you're at it, and
 
  • #52
You're a little bit behind the times. ;)
 
  • #53
G037H3 said:
You're a little bit behind the times. ;)
Figured the information hadn't been said yet, so why not?

as for this:
I play a lot, mixing and matching. I'll write something one way and then try to think of two other ways of doing the same thing. Right now, I'm doing lots of stuff with lists and dictionaries, convoluted manipulations. I'm starting to move towards making useful stuff though. :)
Why not write a little address book script? It's a mix of lists, dictionaries, loops, and i/o.
 
  • #54
story645 said:
Why not write a little address book script? It's a mix of lists, dictionaries, loops, and i/o.

I'll try that. Will try to remember to post result in next day or two.

FizzBuzz (some people really can't do this?...):

Code:
for i in range(1, 101):

    if i % 15 == 0:
        print ('fizzbuzz')
    elif i % 3 == 0:
        print ('fizz')
    elif i % 5 == 0:
        print ('buzz')
    else:
        print(i)
 
  • #55
G037H3 said:
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
 
  • #56
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:

Code:
[url for url in urlstring.split(',') if 2 <= len(url.split('.')[-1]) <=4]
 
  • #57
davee123 said:
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.
 
  • #58
G037H3 said:
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:

Code:
[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
 
  • #59
davee123 said:
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.
 
Last edited:
  • #60
story645 said:
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:

Code:
http://www.everything2.com/index.pl?node_id=429011,www.google.com,https://www.physicsforums.com/forumdisplay.php?f=165

I'm assuming the 1st split would split it into 3 strings:

Code:
1) http://www.everything2.com/index.pl?node_id=429011
2) www.google.com
3) https://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:

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

2)
A) www
B) google
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?

Code:
foobar@myurl.com,john.doe@example.org,"Mrs. Abigail Whithersby"@junk.com

Again, assuming the 1st split would split it into 3 strings:

Code:
1) foobar@myurl.com
2) john.doe@example.org
3) "Mrs. Abigail Whithersby"@junk.com

Next, splitting by ".":

Code:
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
 
Last edited:
  • #61
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.
 
  • #62
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:
  • #63
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'
 
  • #64
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
 
  • #65
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
 

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
866
  • Programming and Computer Science
Replies
1
Views
722
  • Programming and Computer Science
12
Replies
397
Views
13K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top