Re: [Tutor] Projects (fwd)
Tiger12506 wrote: >> Nope, if you read the code you'll see the only mapping done is up to 20 >> and then by tens up to 100, that's all. >> The same code could be used with a list, you'd only have to change the >> exception name. > > I see. There were "..." in between each of the tens entries which I took to > mean that "big huge dictionary but left out some values in email for > simplicity". I feel ashamed for not recognizing that the ellipses where > there for some other purpose. ;-) > Checked my mail and yes, it was not outright clear what I meant in the dictionary, sorry. Anyway if you check the rest of the code you'll see it works as I said and you don't use the intermediate values between the tens. As usual I learned something from the exercise, I thought that indexed access would be faster than list access, didn't think lists where implemented as C arrays (because they have no fixed size), now I can see tuples will be faster and will save memory because memory will not have to be reserved in chunks. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects (fwd)
> Nope, if you read the code you'll see the only mapping done is up to 20 > and then by tens up to 100, that's all. > The same code could be used with a list, you'd only have to change the > exception name. I see. There were "..." in between each of the tens entries which I took to mean that "big huge dictionary but left out some values in email for simplicity". I feel ashamed for not recognizing that the ellipses where there for some other purpose. ;-) Anyway, beyond speed-up you might be able to get some better memory usage stats by switching my lists to tuples, since the contents never change. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects (fwd)
Tiger12506 wrote: >> Isn't dictionary access faster than list access? Why are three lists >> 'much more efficient'? > > Oh no, no, no. Dictionaries are faster when you are *searching through* for > a particular value. If you already know the index of the item in the list, > lists are much faster. > > Dictionaries are hash based. Somewhere it has to calculate the hash of the > key you give it... > These three lists are more efficient in terms of size/output ratio. It > appeared as if the dictionary that was presented as an example was just > going to map one to one all of the values from zero to 999,999 (to match my > list version capabilities). Not only is that bad programming style, it's > just plain unecessary. Nope, if you read the code you'll see the only mapping done is up to 20 and then by tens up to 100, that's all. The same code could be used with a list, you'd only have to change the exception name. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects (fwd)
Danny Yoo wrote: > [snip] > First, it ignores zero, which is a cardinal sin. Or is it an ordinal sin? [snip] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects (fwd)
On 24/01/2008, Ricardo Aráoz <[EMAIL PROTECTED]> wrote: > Isn't dictionary access faster than list access? Why are three lists > 'much more efficient'? Well, not necessarily. If you want a dictionary, you could use a list of tuples: myDict = [('a', 'one'), ('b', 'two), ('c', 'three')] Then you could do lookup as: def lookup(key, listDict): for k, v in listDict: if k == key: return v But, as you say, this would be a lot slower than using a dict (lookup time is proportional to the length of the list for this naive dictionary, whereas lookup time is essentially constant with a dict). However, in this case, the keys are integers starting from 0 with no holes. Instead of having to search, our lookup is just: return digits[i] which is also constant time. If you really want to know which is faster, the timeit module is your friend. Here's what my computer says: Morpork:~ repton$ python -m timeit -s 'd = {0:"zero", 1:"one", 2:"two", 3:"three", 4:"four", 5:"five", 6:"six", 7:"seven", 8:"eight", 9:"nine"}' 'd[5]' 1000 loops, best of 3: 0.127 usec per loop Morpork:~ repton$ python -m timeit -s 'd = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]' 'd[5]' 1000 loops, best of 3: 0.102 usec per loop So, if that extra 25 nanoseconds is important to you, definitely go with the list! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects (fwd)
> Isn't dictionary access faster than list access? Why are three lists > 'much more efficient'? Oh no, no, no. Dictionaries are faster when you are *searching through* for a particular value. If you already know the index of the item in the list, lists are much faster. Dictionaries are hash based. Somewhere it has to calculate the hash of the key you give it... These three lists are more efficient in terms of size/output ratio. It appeared as if the dictionary that was presented as an example was just going to map one to one all of the values from zero to 999,999 (to match my list version capabilities). Not only is that bad programming style, it's just plain unecessary. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects (fwd)
> This could be written much more efficiently. It can be done with only > these > lists~ > ones = > ['zero','one','two','three','four','five','six','seven','eight','nine'] > teens = > ['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'] What is it with me and mistakes lately? These two lists above should be combined into one. Saves a couple of range checks in the program. :-{ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects (fwd)
Tiger12506 wrote: >> up to a thousand (not tested) >> >> words = {0:'zero', 1:'one', 2:'two', 3:'three', ... , 10:'ten', >> 11:'eleven', 12:'twelve', ..., 19:'nineteen', >> 20:'twenty', , 90:'ninety', 100:'one hundred' } >> def digitToString(n) : >>try : >>retStr = words[n] >>except KeyError : >>if n > 100 : >>retStr = (digitToString(n // 100) >> + ' hundred and ' >> + digitToString(n % 100)) >>else : >>retStr = (digitToString(n - (n % 10)) >> + ' ' >> + digitToString(n % 10)) >> >>return retStr > > This could be written much more efficiently. It can be done with only these > lists~ > ones = > ['zero','one','two','three','four','five','six','seven','eight','nine'] > teens = > ['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'] > tens = > ['','','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety'] > hundstr = 'hundred' > thousand = 'thousand' > Isn't dictionary access faster than list access? Why are three lists 'much more efficient'? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects (fwd)
> up to a thousand (not tested) > > words = {0:'zero', 1:'one', 2:'two', 3:'three', ... , 10:'ten', > 11:'eleven', 12:'twelve', ..., 19:'nineteen', > 20:'twenty', , 90:'ninety', 100:'one hundred' } > def digitToString(n) : >try : >retStr = words[n] >except KeyError : >if n > 100 : >retStr = (digitToString(n // 100) > + ' hundred and ' > + digitToString(n % 100)) >else : >retStr = (digitToString(n - (n % 10)) > + ' ' > + digitToString(n % 10)) > >return retStr This could be written much more efficiently. It can be done with only these lists~ ones = ['zero','one','two','three','four','five','six','seven','eight','nine'] teens = ['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'] tens = ['','','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety'] hundstr = 'hundred' thousand = 'thousand' Exercise for reader to make it work ;-) Unless of course you genuinely want to see my code... Current range 0 - 999,999Easily extendable to millions, trillions, billions, etc. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects (fwd)
Danny Yoo wrote: > Hi Jason, > > > Looking back at that Java code: > > > static String convertDigitToEnglish(int d) { > switch ( d ) > { >case 1: return "one"; >case 2: return "two"; >case 3: return "three"; >case 4: return "four"; >case 5: return "five"; >case 6: return "six"; >case 7: return "seven"; >case 8: return "eight"; >case 9: return "nine"; >default: return "\nFatal Error!\n"; // should I abort pgm? > } // end of switch >} // end of convertDigitToEnglis > > > > Frankly, this seems silly to me. First, it ignores zero, which is a cardinal > sin. I'm being somewhat serious about this: functions should do what they > say, > and that function isn't. > > > But the code could also be written much more tightly as: > > > static String digitToString(int n) { >String[] words = {"zero", "one", "two", "three", "four", > "five", "six", "seven", "eight", "nine"}; >if (0 <= n && n < 10) { >return words[n]; >} >throw new IllegalArgumentException("input not a single digit"); > } > > > I don't mean to make this post so Java-centric; it just seems a little unfair > to compare a bad Java routine to a good Python routine. :) Writing an > equivalent in Python is also pretty straightforward: > > # > ## Pseudocode: just a sketch > def digitToString(n): > words = ["zero", "one", ...] > if 0 <= n < 10: > return words[n] > ... > # > > Like dictionaries, the list data structure works pretty well for key/value > lookup if the input key is a small number. > > > Good luck! > up to a thousand (not tested) words = {0:'zero', 1:'one', 2:'two', 3:'three', ... , 10:'ten', 11:'eleven', 12:'twelve', ..., 19:'nineteen', 20:'twenty', , 90:'ninety', 100:'one hundred' } def digitToString(n) : try : retStr = words[n] except KeyError : if n > 100 : retStr = (digitToString(n // 100) + ' hundred and ' + digitToString(n % 100)) else : retStr = (digitToString(n - (n % 10)) + ' ' + digitToString(n % 10)) return retStr HTH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects (fwd)
Hi Jason, Looking back at that Java code: static String convertDigitToEnglish(int d) { switch ( d ) { case 1: return "one"; case 2: return "two"; case 3: return "three"; case 4: return "four"; case 5: return "five"; case 6: return "six"; case 7: return "seven"; case 8: return "eight"; case 9: return "nine"; default: return "\nFatal Error!\n"; // should I abort pgm? } // end of switch } // end of convertDigitToEnglis Frankly, this seems silly to me. First, it ignores zero, which is a cardinal sin. I'm being somewhat serious about this: functions should do what they say, and that function isn't. But the code could also be written much more tightly as: static String digitToString(int n) { String[] words = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; if (0 <= n && n < 10) { return words[n]; } throw new IllegalArgumentException("input not a single digit"); } I don't mean to make this post so Java-centric; it just seems a little unfair to compare a bad Java routine to a good Python routine. :) Writing an equivalent in Python is also pretty straightforward: # ## Pseudocode: just a sketch def digitToString(n): words = ["zero", "one", ...] if 0 <= n < 10: return words[n] ... # Like dictionaries, the list data structure works pretty well for key/value lookup if the input key is a small number. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects
Terry Carroll wrote: > I'm no Java expert, but isn't Java's Map more or less equivalent to > Python's dictionary? More or less, except Python dicts are about 10x easier to use. Some (overly complicated) examples here: http://personalpages.tds.net/~kent37/stories/00017.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects
On Wed, 23 Jan 2008, Jason Massey wrote: > An example routine to translate a number into [its] english equivalent > was given (again, this is Java): > static String convertDigitToEnglish(int d) { > switch ( d ) > { > > case 1: return "one"; > case 2: return "two"; > case 3: return "three"; > case 4: return "four"; > case 5: return "five"; > case 6: return "six"; > case 7: return "seven"; > case 8: return "eight"; > case 9: return "nine"; > default: return "\nFatal Error!\n"; // should I abort pgm? > } // end of switch > } // end of convertDigitToEnglish > > In Python I'd just use a dictionary. I'm no Java expert, but isn't Java's Map more or less equivalent to Python's dictionary? http://java.sun.com/javase/6/docs/api/java/util/Map.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects
On Jan 22, 2008 5:10 PM, Damian Archer <[EMAIL PROTECTED]> wrote: > So anyone have any good project ideas, perhaps projects that people have > undertaken before?? > > I'm taking a Java class this semester and our first program is a number translator. Here's the assignment: *Below is a sample run:* Welcome to my number translator! Enter amount [0-99.99; 0 to exit]: 1234.56 Translation: one thousand two hundred thirty-four and 56/100 Enter amount [0-99.99; 0 to exit]: 17775 Translation: seventeen thousand seven hundred seventy-five and 00/100 Enter amount [0-99.99; 0 to exit]: -45 Enter amount [0-99.99; 0 to exit]: 9.99 Enter amount [0-99.99; 0 to exit]: 22.95 Translation: twenty-two and 95/100 Enter amount [0-99.99; 0 to exit]: 0.01 Translation: zero and 01/100 Enter amount [0-99.99; 0 to exit]: 909909.99 Translation: nine hundred nine thousand nine hundred nine and 99/100 Enter amount [0-99.99; 0 to exit]: 0 Bye bye! *Input* You may assume that your input is in floating point format (no dollar signs or commas or other special characters other than a single decimal point). But you will need to check to make sure your input satisfies the specs given above. (Although with exception handling it's not difficult to validate that you have floating point format.) --- An example routine to translate a number into it's english equivalent was given (again, this is Java): static String convertDigitToEnglish(int d) { switch ( d ) { case 1: return "one"; case 2: return "two"; case 3: return "three"; case 4: return "four"; case 5: return "five"; case 6: return "six"; case 7: return "seven"; case 8: return "eight"; case 9: return "nine"; default: return "\nFatal Error!\n"; // should I abort pgm? } // end of switch } // end of convertDigitToEnglish In Python I'd just use a dictionary. HTH, jason ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects
Damian Archer wrote: > So anyone have any good project ideas, perhaps projects that people have > undertaken before?? This just came up on the list: http://mail.python.org/pipermail/tutor/2008-January/059653.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects
On Tue, Jan 22, 2008 at 11:10:18PM +, Damian Archer wrote: >So anyone have any good project ideas, perhaps projects that people >have >undertaken before?? I am in the same situation as you are. What I now did was doing something practical and some fun stuff: such as writing a statistic-tool for my mails, a script what automagically produces latex-letters and playing around with www.pythonchallenge.com -- Benjamin Eckenfels OpenPGP Key id: CF56E489 Key fingerprint = 386D CBE1 0833 4C12 2871 F51E 839D 18EF CF56 E489 Public Key: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xCF56E489 signature.asc Description: Digital signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Projects
Guys and Gals, Firstly 'Hi'. So I am new to Python have been learning it for about two months now on and off between working and playing. Its the first language I have tried to learn, I am taking to it quite well and enjoying it, which is always good. The problem I am having is finding project ideas for beginners. I know I should be looking at things I need, but quite honestly the level I am at I think these will be beyond me. Plus I want to master the basics I've learnt before I get ahead of myself. So anyone have any good project ideas, perhaps projects that people have undertaken before?? Thanks in advance! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] projects for beginners
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of josipSent: Monday, June 19, 2006 8:39 AMTo: tutor@python.orgSubject: [Tutor] projects for beginners Hi! I have read learning python and mede some smaller examples. Now I want to make smoe project, I'm thinking about text editor. Can someone give me pointers like (where and how to start, etc.)? Or maybe another project suitable for beginners. http://pyfaq.infogami.com/tutor-im-learning-python-what-should-i-program Has a couple of ideas for projects. Mike http://users.adelphia.net/~mahansen/programming/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] projects for beginners
Hi! I have read learning python and mede some smaller examples. Now I want to make smoe project, I'm thinking about text editor. Can someone give me pointers like (where and how to start, etc.)? Or maybe another project suitable for beginners. Sneak preview the all-new Yahoo.com. It's not radically different. Just radically better. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tutor projects
Hi there, This list is great! I really like the aspect of mentor-mentee relationship. Ok, I have been programming in various languages for many many years, and here is my dilemna. I jump into a huge project such as Zope or Mambo (php side) and spend a couple of weeks on it, get frustrated, and give up. What I am requesting is an interesting project that is good for the intermediate level hacker, and somebody to assist me when I run into a problem. Any takers? -Tino ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor