import math
from math import sqrt
def isPrime(n):
prime_flag = 0
if(n > 1):
for i in range(2, int(sqrt(n)) + 1):
if (n % i == 0):
prime_flag = 1
break
if (prime_flag == 0):
return True
else:
return False
else:
return False
# main sequence
concatprime = False
start = 2
for a in range(start, 10000):
if isPrime(a) and concatprime == False:
for b in range(a+1, 10000):
if isPrime(b):
ab = str(a)+str(b)
ba = str(b)+str(a)
if isPrime(int(ab)) and isPrime(int(ba)):
#print(str(a)+","+str(b))
for c in range(b+1, 10000):
if isPrime(c):
ac = str(a)+str(c)
ca = str(c)+str(a)
bc = str(b)+str(c)
cb = str(c)+str(b)
if isPrime(int(ac)) and isPrime(int(ca)) and isPrime(int(bc)) and isPrime(int(cb)):
#print(str(a)+","+str(b)+","+str(c))
for d in range(c+1, 10000):
if isPrime(d):
da = str(d)+str(a)
ad = str(a)+str(d)
db = str(d)+str(b)
bd = str(b)+str(d)
dc = str(d)+str(c)
cd = str(c)+str(d)
if isPrime(int(da)) and isPrime(int(ad)) and isPrime(int(db)) and isPrime(int(bd)) and isPrime(int(dc)) and isPrime(int(cd)):
#print(str(a)+","+str(b)+","+str(c)+","+str(d))
for e in range(d+1, 10000):
if isPrime(e):
ea = str(e)+str(a)
ae = str(a)+str(e)
eb = str(e)+str(b)
be = str(b)+str(e)
ec = str(e)+str(c)
ce = str(c)+str(e)
ed = str(e)+str(d)
de = str(d)+str(e)
if isPrime(int(ea)) and isPrime(int(ae)) and isPrime(int(eb)) and isPrime(int(be)) and isPrime(int(ec)) and isPrime(int(ce)) and isPrime(int(ed)) and isPrime(int(de)):
print(str(a)+","+str(b)+","+str(c)+","+str(d)+","+str(e))
print(a+b+c+d+e)
concatprime = True
#start = 10000
break
Nothing fancy, just a personal log with a built in search function so I can recall what is easily forgotten.
Thursday, March 30, 2023
Python Brute Force Solution to Euler Project Problem 60
My cumbersome solution to https://projecteuler.net/problem=60 but I'll take it. I cheated by setting the loop limits to 10000.
Sunday, March 26, 2023
Python - Count Winning Poker Hands in File
import time
start = time.time()
pokerhands = open("p054_poker.txt", "r+")
#pokerhands = open("p054_poker_hand.txt", "r+")
print("File to read: ", pokerhands.name)
# Read single line in file
two_hands = pokerhands.readline().strip()
#---------------- functions -----------
def convertPic(c):
if c == "T":
c = 10
if c == "J":
c = 11
if c == "Q":
c = 12
if c == "K":
c = 13
if c == "A":
c = 14
return c
def stripNums(player):
nums = []
for card in player:
nums.append(int(convertPic(card[0])))
return nums
def stripSuits(player):
suits = []
for card in player:
suits.append(card[1])
return suits
def isFlush(s):
if([s[0]]*len(s) == s):
return True
else:
return False
def isRoyal(nums):
nums.sort()
if (nums == [10, 11, 12, 13, 14]):
return True
else:
return False
def isStraight(nums):
if sorted(nums) == list(range(min(nums), max(nums)+1)) or sorted(nums) == [2,3,4,5,14]:
return True
def lowStraight(nums):
if sorted(nums) == [2, 3, 4, 5, 14]:
return True
def countSimilar(cards):
#see groupcards.py
likecnt = 1
groupings = []
grouped = []
for index in range(0, len(cards)):
if index < 4:
if cards[index] == cards[index+1]:
likecnt+=1
else:
if likecnt > 1:
grouped = [cards[index], likecnt]
groupings.append(grouped)
likecnt = 1
grouped = []
else:
if likecnt > 1:
grouped = [cards[index], likecnt]
groupings.append(grouped)
# no combos
if len(groupings) == 0:
hand = 1
high = max(cards)
# one combo of either pair, three or four
if len(groupings) == 1:
if groupings[0][1] == 4:
hand = 8
high = groupings[0][0]
if groupings[0][1] == 3:
hand = 4
high = groupings[0][0]
if groupings[0][1] == 2:
hand = 2
high = groupings[0][0]
# two combos - two pair or full house
if len(groupings) == 2:
pairs = []
threes = 0
for g in groupings:
if g[1] == 2:
pairs.append(g[0])
if g[1] == 3:
threes = g[0]
# two pairs
if threes == 0:
hand = 3
high = max(pairs)
else:
# full house with high of 3 combo
hand = 7
high = threes
result = [hand, high]
return result
#---------------------
def calcHand(player):
hand = 1
high = 1
nums = stripNums(player)
suits = stripSuits(player)
#check for flush first
if isFlush(suits) == True:
hand = 6
high = max(nums)
if isRoyal(nums) == True:
hand = 10
high = 15
if isStraight(nums) == True:
hand = 9
high = max(nums)
if lowStraight(nums) == True:
hand = 9
nums.remove(14)
high = max(nums)
#fall through to all other possible groupings including none
#scored 1,2,4,3,7,8
if hand == 1 and high == 1:
hand = countSimilar(nums)[0]
high = countSimilar(nums)[1]
return [hand, high]
def nextHigh(player, high):
#get next high if hands identical
nums = stripNums(player)
newnums = [i for i in nums if i != high]
return max(newnums)
# ------ start main
rec_cnt = 0
player1wins = 0
player2wins = 0
while two_hands:
cardarray = two_hands.split(" ")
player1 = cardarray[0:5]
player1.sort()
player1hand = calcHand(player1)[0]
player1high = calcHand(player1)[1]
###################
player2 = cardarray[5:10]
player2.sort()
player2hand = calcHand(player2)[0]
player2high = calcHand(player2)[1]
# compare and count wins
if player1hand > player2hand:
player1wins+=1
else:
if player1hand < player2hand:
player2wins+=1
else:
if player1hand == player2hand:
if player1high > player2high:
player1wins+=1
else:
if player1high < player2high:
player2wins+=1
else:
if player1high == player2high:
if nextHigh(player1, player1high) > nextHigh(player2, player2high):
player1wins+=1
else:
player2wins+=1
############ continue reading file #######
# use readline() to read next line
two_hands = pokerhands.readline().strip()
rec_cnt+=1
end = time.time()
print("execution time : ", (end-start) * 10**3, "ms")
# print scores and close opened file
print("record_cnt = "+str(rec_cnt))
print("player1 wins = "+str(player1wins))
print("player2 wins = "+str(player2wins))
pokerhands.close()
File to read: p054_poker.txt
execution time : 627.8073787689209 ms
record_cnt = 1000
player1 wins = 376
player2 wins = 624
[Program finished]
Friday, March 10, 2023
Saturday, March 4, 2023
Updating Kali NetHunter on Nexus 7 Android Tablet
My last posts regarding Nethunter were back in August and October 2018.
Now I can run "sudo apt install netdiscover". Beats downloading a Google Play network mapping app with ads and other neat stuff.
Subscribe to:
Posts (Atom)