Re: [Tutor] Creating lists with 3 (later4) items occuring only once
On 27 September 2015 at 12:39, marcus lütolf wrote: > Hello Martin. > I never exspected to get such motivating comments and advice !!! Thank you > again. > Referring to my statments below > > 1. I explain my task in plain text: > Flights (in golfers language) or Triples (in computer language) composed of > 3 golfers (in golfers language) or 3 letters (in > computer language) play once a day for n days. > Each golfer or letter should only once be combined with another, meaning a > combination of 2 golfers/letters should never > be>1 for n days. > > 2. I considered ['a +b', 'a + c', 'b+c'] == ['a','b','c'] > > 3. I'am glad that it can be done with all letters. However, with Triples I > need a number dividable by 3 so the maximum would be > 24 golfers or letters. I hope to get a program allowing to change the > varables like number of days played(n) and number of > golfers/letters, up to a max of 24 according to different situations. > > That is why I limited my samples to 9 and 15. 5 and 7 would not work since > ther are prime numbers. Ah okay. This is what I thought your problem was but with an additional constraint. I will spell out the two constraints formally: You want a set of triples where each triple consists of three distinct players and: 1) Each player plays each other player exactly once. 2) It is possible to arrange the triples so that every player plays once on each day. Constraint 1 means that you need N=1mod6 or N=3mod6. Constraint 2 means that you need N to be a multiple of three so we cannot have N=1mod6. This means that we must have N=3mod6 or in other words: N = 3, 9, 15, 21, 27, ... If I understand correctly you are only interested to know how to find a solution to constraints 1) and 2) for these values of N. You do not care what happens for values of N where a perfect solution is impossible. > The posting of your sample with 5 letters below is correct. Having discarded > the subsequences with "already seen's" > 4 Triples/sequences are left but a variance of the contained letters: > 'a' occurs 3 times > 'b' occurs 1 time > 'c' occurs 3 times > 'd' occurs 3 times > 'e' occurs 2 times > which of course does not fit my task. That made me think itertools might > not be the right tool. > However, I noticed variance gets smaller with bigger samples and might turn > 0 with 24 letters. So you want every player to play the same number of times? That's reasonable. Is it also important that every player plays every other player exactly once (i.e. constraint 1) )? Unfortunately you will not be able to meet constraint 1) for N=24 since it is an even number. > But I don't know how to eliminate the "already seen's" by code. > > You are absolutely right that one has to write down first exactly his task > to be accomplished. But my knowledge goes only > as far as "Python for Infomatics" (by MOOC/Coursera) and "Practical > Programming" . I know there are a myriad of other > modules and tools etc. and there I need the help of "Pythonistas". > To where should I proceed now ? I would suggest that you begin working on pen and paper. How can you construct a solution for N=3, N=9 etc? An algorithm occurs to me as follows. Consider N=9 so we have golfers ABCDEFGHI. We know that player A needs to play some games so let's begin there (A... ... Player A will at some point need to play both B and C and has not played them yet so (ABC) ... Player A has not played everyone and will need to play players D and E so (ABC) (ADE) ... Continuing we have (ABC) (ADE) (AFG) (AHI) ... Now player A has played everyone but player B hasn't so (ABC) (ADE) (AFG) (AHI) (B...) ... Player B has already played both A and C which leaves the possibilities DEFGHI. At some point player B must play D so let's assume that's what happens: (ABC) (ADE) (AFG) (AHI) (BD...) ... Who can play with B and D? Well B has played A and C and D has played A and E. So that leaves FGHI. We now loop through those possibilities trying each out. This is where the algorithm recurses: (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI ... Now B has still not played everyone and at some point B must play G so let's try that out: (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI (BG...) ... Okay so who can play with B and G. Well B has played ACDF and G has played AF so we need something that is not ABCDFG which leaves EHI as possibilities. Let's loop through these trying them out. We'll begin with E: (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI (BGE) - E could be HI ... Now B still hasn't played everyone and there's only two possibilities H and I. which gives (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI (BGE) - E could be HI (BHI) <- conflict!! ... Oh dear H and I have already played each other. Now we need to back track. Maybe we were wrong to choose E? We'll try H instead: (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI (BGH) - H could be I (E was already rejected on backtrack) ... Now B h
Re: [Tutor] stx, etx (\x02, \x03)
> > Hi Richard, > > Just to check: what operating system are you running your program in? > Also, what version of Python? > Hi Danny, using Linux and Python 2.7 > > > > ## > with open('input/test.xml', 'rU') as f1: ... > ## > > > Question: can you explain why the program is opening 'mod1.xml' in 'a' > append mode? Why not in 'w' write mode? > > > This may be important because multiple runs of the program will append > to the end of the file, so if you inspect the output file, you may be > confusing the output of prior runs of your program. Also, it's likely > that the output file will be malformed, since there should just be one > XML document per file. In summary: opening the output file in append > mode looks a bit dubious here. > > > > To your other question: > > > What am I missing? > > Likely, the lines being returned from f1 still have a line terminator > at the end. You'll want to interpose the '\x03' right before the line > terminator. Mark Laurence's suggestion to use: > > s = '\x02' + line[:-1] + '\x03\n' > > looks ok to me. > Actually, you solved it, but it required both with open('input/test.xml', 'rU') as f1:... and s = 'x02' + line[:-1] + 'x03\n' Either of the above alone do not resolve the issue, but both together do. As far as the write vs append, I understand your concerns but that is for testing during scripting only. Ultimately the script outputs to a socket on another machine, not to a file, so that gets commented out. The append is because I am using a 100 line test file with a for line in f1: statement in it, and I want the results for each line appended to the output file. I delete the results file before each run. Thanks for all the help! regards, Richard -- All internal models of the world are approximate. ~ Sebastian Thrun ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Python type confusion?
A simple "type" problem? The following code works as a py file with the XX'd lines replacing the two later "raw_input" lines. Why do the "raw_input" lines yield a TypeError: 'str' object is not callable? Same result if I use/omit the parens around the poly tuple. evaluate a polynomial as formula for a defined function ##poly = (0.0, 0.0, 5.0, 9.3, 7.0)# f(x) = 7x^4 + 9.3x^3 + 5x^2 ##x = -13 poly = raw_input("Type, 0.0-n ,")## these do not work in place of above x = raw_input("Type your val of x, ")## 'str' object is not callable? total = 0.0 for i in range(len(poly)): totalTerm = poly[i]* (x ** i) total += totalTerm print "totalTerm ", i , totalTerm print "Equation Value", total Good answer follows: totalTerm 0 0.0 totalTerm 1 0.0 totalTerm 2 845.0 totalTerm 3 -20432.1 totalTerm 4 199927.0 Equation Value 180339.9 >>> thanks, Ken Hammer ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python type confusion?
On Mon, Sep 28, 2015 at 1:27 PM, Ken Hammer wrote: > A simple "type" problem? > > The following code works as a py file with the XX'd lines replacing the two > later "raw_input" lines. > Why do the "raw_input" lines yield a TypeError: 'str' object is not callable? > Same result if I use/omit > the parens around the poly tuple. > > evaluate a polynomial as formula for a defined function > ##poly = (0.0, 0.0, 5.0, 9.3, 7.0)# f(x) = 7x^4 + 9.3x^3 + 5x^2 > ##x = -13 > poly = raw_input("Type, 0.0-n ,")## these do not work in place of > above > x = raw_input("Type your val of x, ")## 'str' object is not callable? y = int(x) #see below > > total = 0.0 > for i in range(len(poly)): > totalTerm = poly[i]* (x ** i) Here you would get "unsupported operand type" since you are getting the ith power of a string. > total += totalTerm > print "totalTerm ", i , totalTerm > print "Equation Value", total > > Good answer follows: > totalTerm 0 0.0 > totalTerm 1 0.0 > totalTerm 2 845.0 > totalTerm 3 -20432.1 > totalTerm 4 199927.0 > Equation Value 180339.9 > thanks, Ken Hammer > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python type confusion?
On Mon, Sep 28, 2015 at 4:13 PM, C Smith wrote: > On Mon, Sep 28, 2015 at 1:27 PM, Ken Hammer wrote: >> A simple "type" problem? >> >> The following code works as a py file with the XX'd lines replacing the two >> later "raw_input" lines. >> Why do the "raw_input" lines yield a TypeError: 'str' object is not >> callable? Same result if I use/omit >> the parens around the poly tuple. >> >> evaluate a polynomial as formula for a defined function >> ##poly = (0.0, 0.0, 5.0, 9.3, 7.0)# f(x) = 7x^4 + 9.3x^3 + 5x^2 >> ##x = -13 >> poly = raw_input("Type, 0.0-n ,")## these do not work in place of >> above >> x = raw_input("Type your val of x, ")## 'str' object is not callable? > y = int(x) #see below > >> >> total = 0.0 >> for i in range(len(poly)): >> totalTerm = poly[i]* (x ** i) > Here you would get "unsupported operand type" since you are getting > the ith power of a string. EDIT: change "x" to "y" > > >> total += totalTerm >> print "totalTerm ", i , totalTerm >> print "Equation Value", total >> >> Good answer follows: >> totalTerm 0 0.0 >> totalTerm 1 0.0 >> totalTerm 2 845.0 >> totalTerm 3 -20432.1 >> totalTerm 4 199927.0 >> Equation Value 180339.9 > >> thanks, Ken Hammer >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python type confusion?
On Mon, Sep 28, 2015 at 1:27 PM, Ken Hammer wrote: > A simple "type" problem? > > The following code works as a py file with the XX'd lines replacing the two > later "raw_input" lines. > Why do the "raw_input" lines yield a TypeError: 'str' object is not callable? > Same result if I use/omit > the parens around the poly tuple. As C Smith notes, raw_input() returns a string. As the name suggests, it treats its input as raw text, and does not try to interpret it as data. Interpreting text as data isn't too bad if we follow certain conventions. One of the most popular conventions is to treat the text as JavaScript object notation (JSON), because basically everything knows how to parse JSON these days. We can use the 'json' module to parse JSON-encoded text. For example, here's some sample use of the json.loads() string-parsing function from the interactive interpreter: ### >>> import json >>> json.loads('[0.0, 0.0, 5.0, 9.3, 7.0]') [0.0, 0.0, 5.0, 9.3, 7.0] >>> json.loads('42') 42 ### The main limitation here is that this knows how to handle lists, but it doesn't know how to handle tuples, since there's no such thing as tuples in JSON. Hopefully that isn't too painful for your case, but let us know if it is. To use this parser for your own program, add the import to the top of your program: # import json # and then wrap a use of json.loads() around each of the raw_input() calls. Like this: ## import json poly = json.loads(raw_input("Type, 0.0-n ,")) x = json.loads(raw_input("Type your val of x, ")) # ... rest of program here ## Hope this helps! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python type confusion?
> As C Smith notes, raw_input() returns a string. As the name suggests, > it treats its input as raw text, and does not try to interpret it as > data. Whoops! I slightly misspoke here: I mean to say that it does not try to interpret it as *structured* data. That is, we want things that look like numbers to be treated as numbers. Likewise, we'd like a string that looks like a list of numbers to be treated as a list of numbers. That's the role of a parser, and why we need to do something, such as using the json.loads() function. Others on the list might suggest instead using input() instead of raw_input(), which will try to interpret what you enter in as if it were a Python expression. Effectively, this will also parse the text into structured values. However, although this is a straightforward way to make your program work, I don't think it's a good approach because input() has a hidden use of the infamous "eval()" function embedded in it. Certain inputs to input() can cause your program to do weird things due to its use of eval(), so I think it's best to avoid it. Dunno if you're familiar with all the problems with eval(); if you are interested, ask, and I'm sure there will be a lot of response from the list. Best of wishes to you! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Custom Function that Takes argument
Hello, I am learning how to create custom functions and watched the tutorial online that uses API for locu to do cool stuff. I wanted to go little bit beyond the tutorial and add my own features. The problem that I am running into is that I cannot figure out how to prompt a user to input their zip code and use that information against the function. here is my code: import urllib2 import json locu_api = 'not included here for obvious reasons' zip = raw_input('What is your zip: ') def zip_search(query): api_key = locu_api url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key zip = query.replace(' ', '%20') final_url = url + '&zip=' + zip + "&category=restaurant" jason_obj = urllib2.urlopen(final_url) data = json.load(jason_obj) for item in data['objects']: print item['name'], item['phone'], item['street_address'], item['categories'] print zip_search(zip_search()) Thank you. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Custom Function that Takes argument
On 29/09/15 00:45, Nym City via Tutor wrote: I am learning how to create custom functions and watched the > tutorial online that uses API for locu Since most folks here probably don't know locu you should maybe give us a URL. Then we can know what it is you are talking about. I cannot figure out how to prompt a user to input their zip > code and use that information against the function. The normal way is to pass it in as a parameter of the function: def myfunc(aZipCode): print aZipCode zip = raw_input('What is your zip: ') myfunc(zip) import urllib2 import json locu_api = 'not included here for obvious reasons' zip = raw_input('What is your zip: ') def zip_search(query): api_key = locu_api url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key zip = query.replace(' ', '%20') final_url = url + '&zip=' + zip + "&category=restaurant" jason_obj = urllib2.urlopen(final_url) data = json.load(jason_obj) for item in data['objects']: print item['name'], item['phone'], item['street_address'], item['categories'] print zip_search(zip_search()) Here you are passing the results of your own function in as an argument to your function. That should give an error since the inner call has no argument and the function is looking for one. You need to pass in your query string: print zip_search(zip) Always post any errors you get it helps us help you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python type confusion?
On 28/09/2015 21:27, Ken Hammer wrote: As you've all ready had answers I've just one thing to say below. total = 0.0 for i in range(len(poly)): totalTerm = poly[i]* (x ** i) total += totalTerm print "totalTerm ", i , totalTerm print "Equation Value", total Perhaps the most used piece of code that although not actually wrong, is certainly frowned upon as you can write:- for i, item in enumerate(poly): totalTerm = item * (x ** i) ... -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] skip/slice more than every second?
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] how can I show the first then skip three and show the next and so on? For example: show 1 then skip 2,3,4 then show 5 then skip 6,7,8 then show 9 then skip 10,11,12, etc. Any feedback will be greatly appreciated. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] skip/slice more than every second?
thankyou but I just realised I wrote the question wrong - how do I do the inverse of above so hide 1 show 2,3,4 hide 5, show 6,7,8 etc. thanks in advance On Tue, Sep 29, 2015 at 1:33 PM, Sebastian Cheung < sebastian_che...@yahoo.com> wrote: > print range(1,15,4) > > ans: [1, 5, 9,13] > > > > Sent from my iPhone > > > On 29 Sep 2015, at 04:16, questions anon > wrote: > > > > a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] > > > > how can I show the first then skip three and show the next and so on? > > For example: > > show 1 > > then skip 2,3,4 > > then show 5 > > then skip 6,7,8 > > then show 9 > > then skip 10,11,12, > > > > etc. > > > > Any feedback will be greatly appreciated. > > ___ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] skip/slice more than every second?
On Tue, Sep 29, 2015 at 01:16:36PM +1000, questions anon wrote: > a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] > > how can I show the first then skip three and show the next and so on? > For example: > show 1 > then skip 2,3,4 > then show 5 > then skip 6,7,8 > then show 9 > then skip 10,11,12, Here is one method, using slicing: py> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] py> a[::4] [1, 5, 9, 13] The slice a[::4] is equivalent to a[0:len(a):4], that is, it starts at position 0, keeps going until the end, and extracts every fourth value. Here is another method: py> it = iter(a) py> while True: ... try: ... print(next(it)) ... who_cares = next(it), next(it), next(it) ... except StopIteration: ... break ... 1 5 9 13 This grabs each item in turn, prints the first, then throws away three, then prints the next, and so on. This is a good technique for when you want something that doesn't fit into the fairly simple patterns available using slicing, e.g.: - print 1 item; - throw away 2; - print 3 items; - throw away 4; - print 5 items; - throw away 6; - print 7 items; - throw away 8; etc. But for cases where slicing does the job, it is better to use that. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] skip/slice more than every second?
On Tue, Sep 29, 2015 at 01:54:44PM +1000, questions anon wrote: > thankyou but I just realised I wrote the question wrong - > > how do I do the inverse of above > so > hide 1 show 2,3,4 hide 5, show 6,7,8 etc. > > thanks in advance A little more tricky. The version I showed using iter() and a while loop will work, but perhaps this is simpler: py> for i in range(0, len(a), 4): ... print(a[i+1:i+4]) ... [2, 3, 4] [6, 7, 8] [10, 11, 12] [14] -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor