![]() |
|
Re: ** Python Support Thread **
For future reference, "find" gives you the index of the earliest appearance of an expression (and returns "-1" if there aren't any). For instance, here's a recursive version in python 3:
Code:
def counter(mystring, expression): |
Re: ** Python Support Thread **
I'm sure this is lolcode but it works. What would be a cleaner way of finding a straight?
Code:
def evalmystraight(): #mysevenlist is your 2 cards + community cards |
Re: ** Python Support Thread **
You can use (x in list) as a boolean
I haven't tried this in the 7-card case (I wrote a video poker sim recently but it was for 5-card draw), but I think you could do something like this: Code:
#Whatever code makes aces count as both high and low |
Re: ** Python Support Thread **
Quote:
Code:
def count(x): |
Re: ** Python Support Thread **
Quote:
Code:
Quote:
Just saying that if you end up with .count() class and .count() built-in, etc, then you run into problems. Regardless, not even sure if your code is doing at all, tbh, I get that you are trying to return a 'count' of something, but I get the answer: 3, which I'm pretty sure isn't the intended output, but I could be wrong here. I'm also using Python 3 so I had to add the requisite parens to this which may have blown my output, but please explain what this is doing and why it works for the question. |
Re: ** Python Support Thread **
swiss's algorithm just counts every instance of two neigboring letters that match each other. It doesn't allow for arbitrary substrings or even searching for 'ss' while excluding 'xx'. It does return the right answer for scrolls's problem if you change 'xxxakdd' to 'sss'
EDIT: dave, yours doesn't actually work. Try init = 'sss000' |
Re: ** Python Support Thread **
Here's my quick one liner implementation.
Didn't put much thought into it so I Haven't thought of all the corner cases but it seems to work ok. Code:
Code:
|
Re: ** Python Support Thread **
Quote:
Neko: Thanks to you I just learned something I didn't know about slicing. :) Code:
a = [1, 2, 3] |
Re: ** Python Support Thread **
yeah, it seems like it should throw an IndexError but it's pretty nice that it doesn't.
|
Re: ** Python Support Thread **
Quote:
"How can I count how many time the string 'ss' appears in 'sss'?" Not: "How many time does 'ss' appear in an arbitrary string?" or: "How many times does 's' appear next to each other in an arbitrary string?" I could answer version two adding a few letters into the code, thus it is well-formed IMO since it is easily extensible to even "How many times does an arbitrary string appear in an arbitrary string?" My code, as it stands, will work with: "how many times does 's' x 500 appear in string 's' x 100**100." I took his question too literally, obv. |
If you really thought he was asking only about the specific question posted then why did you go for the massive overkill solution instead of "return 2"?
|
Originaly, I was going to tell him to type 2 in the command line and press enter, but clearly, clicking a mouse over the Python icon would have been overkill in that case.
|
Re: ** Python Support Thread **
Quote:
Yield is used to make generators. Check out (link) for more info. I suggested it, because this option is often forgotten and is really useful if you can start processing partial results. My code should work in python 3.0. The only thing I can think of that doesnt work is print. In python 3.0 you cant use Code:
print objectCode:
print(object) |
Re: ** Python Support Thread **
I thought goal was to just count neighboring letters. My bad.
If you just want to count number of substrings in a string, here is a neat solution using recursion: Code:
myhay = 'sssxxss' |
Re: ** Python Support Thread **
One thing that sucks about Python is that it has maximum recusion depths. Not sure if this is included in all programs, but I threw that error a few times doing Project Eulers, which makes me pretty ashamed to admit. I def agree that Yield is a good addition though an option I don't use enough.
|
Re: ** Python Support Thread **
Yeah, I was going for easy but I realize the recursive version could have issues for large strings. I don't really see the utility of yield in this problem because intermediate results are useless. Is there a reason to prefer a generator over a loop or a comprehension like Neko's?
|
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
Here's my shortest solution to the question at hand:
Code:
|
Re: ** Python Support Thread **
Quote:
in your above example what should counter be? 2? |
Re: ** Python Support Thread **
Code:
myString = 'ss'len(myString) is the same value every time it is called so I would imagine that setting it before hand might save a few cpu cycles. Below is code that I wrote to figure out if you could save the extra variable. I am pretty sure that I created Arrays instead of Tuples but I will leave my code as is, because it was a learning experience :) Code:
|
Re: ** Python Support Thread **
Anyone here know of any good replacements for python's re module? Attempting to upgrade in increase speed. Tried out pyre2 which is based on google's RE2, but it turned out to be half-complete in it's mapping to the way python handles regex. Look aheads are not supported, and i got several UTF-8 encoding errors whereas the standard re produced none. Thanks
|
Re: ** Python Support Thread **
I'm not aware of any other re modules...just to eliminate the obvious solution, presumably you are using re.compile already?
|
Re: ** Python Support Thread **
Quote:
|
Re: ** Python Support Thread **
Have you seen this module http://code.google.com/p/mrab-regex-hg/? I've never used it and have no idea how good or fast it is, but probably worth a look.
|
| All times are GMT -4. The time now is 06:18 PM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive