![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Programmer
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0
![]() |
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') |
|
|
|
|
|
#22 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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')people = dict(
bob = ['hammer', 'saw'],
joe = ['saw', 'drill', 'leveler'],
bill = ['hammer', 'leveler']
)people = {
'bob' : ['hammer', 'saw'],
'joe' : ['saw', 'drill', 'leveler'],
'bill' : ['hammer', 'leveler']
}print "Bob's tools:", people['bob'] for person, tools in people.items(): print person, "has", tools def whoowns(tool):
return [person for person, tools in people.items() if tool in tools] |
|
|
|
|
|
#23 |
|
Programmer
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0
![]() |
I thought of using dictionaries, but I didn't know they were very useful.
|
|
|
|
|
|
#24 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Here is a small math problem from Bitwise Magazine that can be solved with the aid of Python:
Quote:
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
|
#25 |
|
Programmer
|
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 |
|
|
|
|
|
#26 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#27 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#28 | |
|
Programmer
|
Quote:
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 |
|
|
|
|
|
|
#29 |
|
Programmer
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0
![]() |
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'))) |
|
|
|
|
|
#30 |
|
Newbie
Join Date: May 2006
Posts: 1
Rep Power: 0
![]() |
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!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|