as a project to get used to writing in Python (the only language I have any knowledge of at the moment) I am writing a Cribbage score counter.
I have found all of the 3, 4 and 5 length subsets from a set of 5 numbers. The subsets are all sorted into numerical order. The function below is supposed to find the subsets which are numbers in order eg [1, 2, 3] or [3, 4, 5].
def straight_counter(subset, length):
straights = []
a = 0 # Variable for incremental increase
for i in range(0, length - 2): # Select one of the first three numbers of a set of 3-5 (which are already ordered)
run = 1
x = 1
while i + x < length: # Ensure that we do not go beyond the final index
if subset[i + x - 1] + 1 == subset[i + x]: # Is the next number one greater than the currently selected number
run += 1 # Count how many numbers we have in order (will be maximum of 5)
x += 1
else:
break
Here is where I try to get only the subsets which are all numbers in order. But when I attempt to add the into a nested list I keep writing over the first index of the list each time and the list is only ever one item long when I print it
if run == length: # If the run of consecutive numbers uses all of the numbers e.g [1, 2, 3] and not [1, 2, 4]
straights.insert(a, subset) # I would like to add the list as the first index of a different list
a += 1 # Increase a so that the next valid subset will be at the next index
print(straights)
Is there an obvious reason why this is happening? Or maybe there's a much neater way of attempting this. Thanks
For example from the input list of numbers
[1, 1, 2, 3, 4]
I am attempting to create a nested list of:
[[1, 2, 3], [1, 2, 3], [2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]].
Which are all the sets of consecutive numbers of length 3 or greater.
You can find below a solution. The idea is to find the first and last number of consecutive sets.
# -*-coding:Utf-8 -*
data = [1, 4, 5, 6, 10, 15, 16, 17, 18, 22, 25, 26, 27, 28]
from operator import itemgetter
from itertools import groupby
for k, g in groupby(enumerate(data), lambda ix : ix[0] - ix[1]):
group = list(map(itemgetter(1), g))
print ((group[0], group[-1]))
At this point you get this :
(1, 1)
(4, 6)
(10, 10)
(15, 18)
(22, 22)
(25, 28)
Then you simply need to select the right ones and to keep them (difference above 2). You can also recreate a list if needed.
OP: You're mentioning 5 cards, which would be a 'kept hand' + starter or crib + starter. What about when there are more? Specifically, The Play. Looking ahead, should the same piece of code be used for both purposes?
Imagine players have A-3-5-7 and 2-3-4-6
The maximum count would be (1+3+5+7)+(2+3+4+6) => 31. Whilst there's a finite number of combinations and the order in which the cards are played, it's a bit trickier.
BTW: as a bit (pun intended) of Cribbage trivia, The Sting will be on Flix (09/02) and Showtime (09/07). It's one of the few places you'll see Cribbage played onscreen.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.