3,386 Posted Topics
I had posted here already here to forum a [beautiful code for solving Sudoku puzzles](http://www.daniweb.com/software-development/python/code/294304/eleagant-and-fast-sudoku-with-generator-expressions), so I was in kind of dilemma what to do, as I read the task to do a sudoku solve as one task in Project Euler. To use the code from before and just take … | |
Saw somebody was viewing this thread and thought the qrange needed one update. From post [url]http://www.daniweb.com/code/snippet216627.html[/url]. | |
Re: [CODE]print('\n'.join('%s, %s' % t for t in [(182, 'the'), (128, 'and'), (123, 'will')]))[/CODE] Newer and quite useful format: [CODE]print('\n'.join('{1}, {0}'.format(*t) for t in [(182, 'the'), (128, 'and'), (123, 'will')]))[/CODE] | |
Re: This is marked solved, but here is my style of solution also: [CODE]s= '00101' res = tuple('Lane fail {place}.'.format(place =index) for index, value in enumerate(reversed(s)) if value == '0') print '\n'.join(res) if res else 'All lanes pass' """ Output: Lane fail 1. Lane fail 3. Lane fail 4. """ [/CODE] | |
Here is my newest version of the pretty printer I posted earlier. Not doing all that fancy stuff of printing looped recursive structures, but if you put one deeper structure inside flat one, works better for me than pprint module. I do not use often object oriented structures though. Dedicated … | |
Re: Length is simple number and can not be iterated. You need one function starting with r or x to produce sequence of values. | |
Re: Your code and details of your problem with it, thank you. | |
Re: To my mind comes at least using numpy for generating the point list. Numpy was missing from your list. | |
Re: [QUOTE=Dane2259;1463249]Could someone explain to me why this code will not print the 10th prime number? It says there is a syntax error on the fourth line (the x = range(3, math.sqrt(test_num)). It also says there is a syntax error on the sixth line (while prime_count < 10:). import math test_num … | |
Re: It is considered bad, because this time for example you had same bug repeated 14 times in your code, for example. Instead insert print functions and any input statement for pause or use debugger break points and watches. | |
![]() | Re: Can't you .split('\r') and insert with linestart line by line? ![]() |
Re: [QUOTE=;][/QUOTE] how about doubling key 'north' as 'n' etc and get rid of the if. Would only use try except to catch incorrect input. | |
Re: prove putting copy slice everywhere where there is append i.e. [code]Npray.append(pray[:])[/code] please take out capitalzed names and use python_style_names ;) Are you sure about repeating lines 45 vs 47? I would also check namedtuple type for making nice named access to attributes. | |
Re: You must pack or grid from the stored variable. Otherwise you get value None. Similarly as append and sort methods return None. That means they are procedural not functinal. | |
Re: I do not like much your style and I am not OO guru, but this at least shows the info if you happen to guess correct name exactly (I did it to use only lowercase to make it little easier): [CODE] ---- snip, snip --------- def open(self, first, last): file_name … | |
Re: Looks like you use class when you should have module. Your numbers class have not any numeric value. The same function exist in module operator as operator.add and operator.mul. Nothing stops you to use sum function instead of operator.add. I would not use input in Python 2. | |
Re: Your program looks strange. You close file explicitely inside with block. | |
Re: Just link to the file name, not save file object. You must probably use full path names à la os.path.abspath. | |
Re: Do help(humansize) or dir(humansize) and call the main function of the file. Why can you not open the file normally and run it from editor side by F5. Final testing is better done without IDE from CMD prompt or launch the program by double clicking the file. I currently like … | |
I have left my source of information as docstring comment on code side. If you want to solve the problem yourself do not run the code. This code actually gives the correct answer. I have not reacted to the border case comment in my formula source as it is quite … | |
Re: You are looking wrong place. Check itertools.permutations and random.choise | |
Re: I do not understand what you are doing this seen set stuff. By testing the code I understood you wanted to record the number of each word as it occurs. I would do for example like this instead of the length stuff: [CODE]from __future__ import print_function try: input = raw_input … | |
Re: Your indention is incorrect and the function is not inside the class. | |
Re: You don't need destroy anything, what user sees need not be same as program sees. See the forget method, the opposite of pack. | |
Re: I would do two variable assignment instead of temp variables needed in other languages. Looks nice code inspite of this point. Irritates me though that there seems not be faster way to split list for quicksort than doing two passes of list with complement conditions. | |
Re: This could prove helpfull seeing the amount of your code: [URL="http://en.wikipedia.org/wiki/Code_refactoring"]Code refactoring[/URL] | |
Re: You have at least problem with indentation. | |
Re: Alternative approach can be found in my code snippet [URL="http://www.daniweb.com/code/snippet285434.html"]Super simple one word anagrams[/URL] and follow up with immediate response by preparing lookup in file of all anagrams for future use: [URL="http://www.daniweb.com/code/snippet288327.html"]One word anagrams by lookup[/URL] | |
Re: So the code should be like my lookup version only missing the saving of anagram file. | |
| |
Re: Here in this [URL="http://www.daniweb.com/forums/post1245223.html#post1245223"]old thread [/URL]is my code for example for the dynamic programming algorithm for lcs (longest common substring). The link points to version were I only keep one row of record, but original algorithm was indexing normal array. | |
Re: If you have saved the data from your program, I would change thd file output to use cPicle or JSON formats. That leads to cleaner, more maintainable code. | |
Re: [CODE]class A: def __init__(self): print 'A()' class B(A): def __init__(self): A.__init__(self) print 'B()' class C(B): def __init__(self): B.__init__(self) print 'C()' c = C() [/CODE] | |
Re: Close the pylab window and the prompt comes back and your print will print. | |
Re: Looks like it has both repr and string form, so you should have not any difficulty finding data using those: [url]http://pysnmp.sourceforge.net/docs/2.x/asn1-octetstring-objects.html[/url] | |
| |
Re: Looks little over complicated for me. We do not need datetime objects. [CODE]import string def only_numbers(x): res= int(x.replace('-','').strip(string.letters + string.punctuation)) # print res return res files = ('site-website-20110105-101303.jpa', 'site-website-20110108-094822.jpa', 'site-website-20110108-095834.jpa', 'site-website-20110109-203148.jpa', 'site-website-20110110-040326.jpa', 'site-website-20110106-163312.jpa', 'site-www.website-20110109-040402.jpa', 'site-www.website-20110111-040219.jpa') print('Files sorted:\n\t' + '\n\t'.join(sorted(files, key = only_numbers)))[/CODE] | |
Re: Python has very flexible max function, which you can use with enumerate like above: [CODE] # put definition in function for reuse, put the definitions mainly in beginning of the file def max_with_ignore(seq, not_these): """ this function takes sequence and set (or less efficiently list) of exception indexes and returns … | |
Re: Here you are richiekid ;) Concised version of the whole code. [CODE]with open('test.txt', 'w') as locx: locx.write('\n'.join(str(i) for i in range(9))) [/CODE] | |
Re: [code]#untested values = [] for count in range(int(raw_input('Number of inputs'))): values.append(raw_input('%i:' % (count+1))) [/code] | |
Re: Yes, richieking, I see that this n-gram are starting from every letter of the document, so my suggestion would be instead of wooees dict to record something like file position counter for n-gram starting positions. Would add quite a lot to space requirements though. To speed things up you would … | |
Re: Could you give sample of your coding, say one function with proper docstring documentation, so maybe we can give you advice on your style. | |
Re: bug at line 20. Debug bug ;) See line 5. | |
Re: How about my named tuple code in code snippets? | |
Re: Also N*K as initial value worked in my own distance algorithm. | |
Re: Red rectangles looks same size for me and spacing is also constant. Role of spacing rectangles and red rectangles only alternate with spacing white ones. 3 white, 3 red, 3 white .... Swap the spacing and rectangles after each line: [CODE]rect, spacing = spacing, rect[/CODE] | |
Re: I think easiest would be to just post process apropriately the last match from list of matches. You could also prove \w+ instead of \D+. | |
Re: Little logic, even partial, included works also miracles for speed, even when generating all permutations: [CODE] letters = ('s', 'm', 'e','n','d','o','r','y') coefs = (1000, 1000-10000, 100+1-10, 10-100, 1, 100-1000, 10, -1) def sendmoremoney(): # python 2 or 3 # solve SEND + MORE = MONEY by brute force with little … |
The End.