Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 14th, 2006, 6:47 AM   #21
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0 commodore is an unknown quantity at this point
I was trying to do mrynit's bob, bill and joe thingie, but it doesn't append the last elif person
bob=['hammer', 'saw']
joe=['saw', 'drill', 'leveler']
bill=['hammer', 'leveler']
everyone=[bob, joe, bill]
countOverall=len(bob)+len(joe)+len(bill)
everyoneString=' '.join(bob+joe+bill)

def toolcount(tool):
    count=0
    finding=everyoneString.find(tool)
    while finding!=-1:
        count+=1
        start=finding+1
        finding=everyoneString.find(tool, start)
    return count

def whoowns(tool):
    owners=[]
    if ' '.join(bob).find(tool)!=-1:
        owners.append('bob')
    elif ' '.join(bill).find(tool)!=-1:
        owners.append('bill')
    elif ' '.join(joe).find(tool)!=-1:
        owners.append('joe')
    print owners

whoowns('leveler')
commodore is offline   Reply With Quote
Old May 14th, 2006, 8:45 AM   #22
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
That's because elif is just an extension of the original if statement (it's short for "else if"). If the first if statement succeeds, successive elif statements are not executed. Use "if" in place of "elif".

Also, don't use find. Use in! The in operator returns true if an element is in a list.
if tool in bob:
    owners.append('bob')
You might also wish to use a dictionary to store all of the data, rather than several lists. A dictionary matches keys to values. Consider the following dictionary:
people = dict(
    bob = ['hammer', 'saw'],
    joe = ['saw', 'drill', 'leveler'],
    bill = ['hammer', 'leveler']
)
This associates a set of strings with a set of lists. You can also write the same dictionary out in an alternative format:
people = {
    'bob' : ['hammer', 'saw'],
    'joe' : ['saw', 'drill', 'leveler'],
    'bill' : ['hammer', 'leveler']
}
You can use a dictionary to pull out information about a single person:
print "Bob's tools:", people['bob']
Or iterate through all the people:
for person, tools in people.items():
   print person, "has", tools
With a dictionary, finding out who owns what can be easily done with a list comprehension:
def whoowns(tool):
    return [person for person, tools in people.items() if tool in tools]
Arevos is offline   Reply With Quote
Old May 14th, 2006, 10:06 AM   #23
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0 commodore is an unknown quantity at this point
I thought of using dictionaries, but I didn't know they were very useful.
commodore is offline   Reply With Quote
Old May 18th, 2006, 11:13 AM   #24
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

Here is a small math problem from Bitwise Magazine that can be solved with the aid of Python:
Quote:
Ask a friend to choose a secret three-digit number (1 to 999). Then ask to divide it by 7 and tell the remainder. Then ask to divide the original number by 11 and again tell the remainder. Now repeat for a final time with 13. You now have three small numbers, and from these you can quickly identify the original three-digit number.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old May 18th, 2006, 11:07 PM   #25
gryfang
Programmer
 
gryfang's Avatar
 
Join Date: May 2006
Location: Ohio
Posts: 36
Rep Power: 0 gryfang is on a distinguished road
Send a message via AIM to gryfang
Also you could use exercises from one language (like C++, Java, Perl) that is popular and may have a lot of test Questions and try coding them in Python. I find that helpful because if you can't figure it out you can look at "their answer" as a hint (because it isn't python code).
Warning: some elements in one language are over simplified in another, while others are overcomplicated. This changes the challenge of the questions in some cases.
__________________
--------------------
LOAD "*" ,8,1

God bless
- Gryfang
gryfang is offline   Reply With Quote
Old May 19th, 2006, 12:23 AM   #26
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The problem with using exercises from one language to code into another is that you mostly learn to translate, not to use the specific, individual strengths of a language that set it apart. I could take a C exercise that used char arrays heavily and translate it into Python, but I wouldn't be learning Python, and I wouldn't be improving my C. The thing is to take a problem and solve it in differing ways with different languages.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 19th, 2006, 2:49 AM   #27
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 903
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Check out USACO. Once you register you are given increasingly difficult problems to solve. You have to submit each program and have it function correctly to move on to the next set of problems. Accepted languages include C, C++, Java, and Pascal. Even the "easiest" problems are difficult and require reasonable programming experience.
titaniumdecoy is offline   Reply With Quote
Old May 19th, 2006, 10:18 AM   #28
gryfang
Programmer
 
gryfang's Avatar
 
Join Date: May 2006
Location: Ohio
Posts: 36
Rep Power: 0 gryfang is on a distinguished road
Send a message via AIM to gryfang
Quote:
Originally Posted by DaWei
The problem with using exercises from one language to code into another is that you mostly learn to translate, not to use the specific, individual strengths of a language that set it apart. I could take a C exercise that used char arrays heavily and translate it into Python, but I wouldn't be learning Python, and I wouldn't be improving my C. The thing is to take a problem and solve it in differing ways with different languages.
That's really what I meant, I just suggested looking at the other language's code if you get stuck and need help solving the problem. There are several answers to a problem.

Another fun source for interested problems is ACM intercollegiate Programming Competition . They give a List of problem sets that are supposed to be answered "quickly", but you can take your sweet time. Just go to the past problems section and the archives and have fun.

As DaWei said it's the PROBLEM you need to solve. And no the programming isn't just C/C++ it's C/C++ Java, C/C++, and Pascal. But the problems are there so you can solve them in ANY language you want.

I have to make it know that I and for solving problems, PROBLEMS, PROBLEMS, PROBLEMS, not translations (unless that is the problem ).
__________________
--------------------
LOAD "*" ,8,1

God bless
- Gryfang
gryfang is offline   Reply With Quote
Old May 20th, 2006, 3:21 PM   #29
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0 commodore is an unknown quantity at this point
I didn't code for some time but now I finished the handymans task in a few minutes:
bob=['hammer', 'saw']
joe=['saw', 'drill', 'leveler']
bill=['hammer', 'leveler']
everyone=[bob, joe, bill]
countOverall=len(bob)+len(joe)+len(bill)
everyoneString=' '.join(bob+joe+bill)

def whoowns(tool):
    owners=[]
    if ' '.join(bob).find(tool)!=-1:
        owners.append('bob')
    if ' '.join(bill).find(tool)!=-1:
        owners.append('bill')
    if ' '.join(joe).find(tool)!=-1:
        owners.append('joe')
    return owners

print 'There are ', countOverall, 'tools: \n'
print '%s saws, owned by %s' % (len(whoowns('saw')),', '.join(whoowns('saw')))
print '%s drills, owned by %s' % (len(whoowns('drill')),', '.join(whoowns('drill')))
print '%s levelers, owned by %s' % (len(whoowns('leveler')),', '.join(whoowns('leveler')))
print '%s hammers, owned by %s' % (len(whoowns('hammer')),', '.join(whoowns('hammer')))
commodore is offline   Reply With Quote
Old May 22nd, 2006, 10:54 AM   #30
Basu
Newbie
 
Join Date: May 2006
Posts: 1
Rep Power: 0 Basu is on a distinguished road
Don't write programs, practice creating your own data types. Create classes that have the properties of things like vectors, complex numbers, Cartesian co-ordinates etc. Take your inspiration from math and physics. While making the classes include functions that properly manipulate and allow calculations using those data types. And if you feel like a real challenge,try writing programs which make use of and convert between multiple such classes. Have fun!
Basu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC