Senior's Blog

ゴルフ(Golf)、python(Pythonista)、メンタルヘルスに関するブログです。

(22)Numbers4予想

数週間前、あるテレビ番組で、Numbers4の数値の選び方をやっていました。

当選数値にある法則があるらしく、下記のような法則でした。

(1)足すと9
(2)連続数字
(3)前回数字を1つ含む

これら全ての条件を満足する数値を導くためのプログラムを作成してみました。

 

1.UI

f:id:SuzyLW:20191105201628j:plain

 

PreNumber欄に、前回の当選数値を入力した後、Startボタン押下で実行です。

候補数値はテキストビューに表示されます。

組合せ数も表示する様にしました。

2.ソース

 ちょっと効率の悪いソースですが、下記の通りです。

 C言語なら、もう少しスッキリ書けるのですが、Python初心者のため

 ご了承ください。

import ui
pS1 = pS2 = pS3 = pS4 = 0
no1 = no2 = no3 = no4 = 0
match9 = 0
matchC = 0
matchP = 0
NumX = 0
NumTotal = 0
#足すと9
#連続数字
#前回数字を1つ含む

def SetPreNum(sender):
  global pS1, pS2, pS3, pS4
  txtfield = sender.superview['textfield0']
  pNum = int(txtfield.text) #preNum
  pS1 = int(pNum/1000) #1st. for pre.
  pNum = pNum - pS1*1000 #renew
  pS2 = int(pNum/100) #2nd. for pre.
  pNum = pNum - pS2*100 #renew
  pS3 = int(pNum/10) #3rd. for pre.
  pS4 = int(pNum%10) #4th. for pre.

 

def CheckPreNum(sender, aNum):
  chkNum = aNum
  cS1 = int(chkNum/1000) #1st. for check
  chkNum = chkNum - cS1*1000
  cS2 = int(chkNum/100) #2nd. for check
  chkNum = chkNum - cS2*100
  cS3 = int(chkNum/10) #3rd. for check
  cS4 = chkNum - cS3*10 #4th. for check
  #前回数字が1つでも含まれているか
  ret = 0
  if cS1 == pS1:
    ret += 1
  if cS1 == pS2:
    ret += 2
  if cS1 == pS3:
    ret += 4
  if cS1 == pS4:
    ret += 8
  if cS2 == pS1:
    ret += 16
  if cS2 == pS2:
    ret += 32
  if cS2 == pS3:
    ret += 64
  if cS2 == pS4:
    ret += 128
  if cS3 == pS1:
    ret += 256
  if cS3 == pS2:
    ret += 512
  if cS3 == pS3:
    ret += 1024
  if cS3 == pS4:
    ret += 2048
  if cS4 == pS1:
    ret += 4096
  if cS4 == pS2:
    ret += 8192
  if cS4 == pS3:
    ret += 16384
  if cS4 == pS4:
    ret += 32768
  return ret

def CheckAdd9(sender, c):
  global no1, no2, no3, no4
  chkNum = int(c)
  no1 = int(chkNum/1000) #1st. for check
  chkNum = chkNum - no1*1000
  no2 = int(chkNum/100) #2nd. for check
  chkNum = chkNum - no2*100
  no3 = int(chkNum/10) #3rd. for check
  no4 = chkNum - no3*10 #4th. for check
  ret = 0 #clear
  if (no1+no2)==9:
    ret = 1 #NO1+NO2=9
  if (no2+no3)==9:
    ret = 2 #NO2+NO3=9
  if (no3+no4)==9:
    ret = 3 #NO3+NO4=9
  return ret

def CheckContinue(sender):
  global no1, no2, no3, no4
  ret = 0
  if (no1-no2)==1 or (no1-no2)==-1:
    ret = 12
  if (no1-no3)==1 or (no1-no3)==-1:
    ret = 13
  if (no1-no4)==1 or (no1-no4)==-1:
    ret = 14
  if (no2-no3)==1 or (no2-no3)==-1:
    ret = 23
  if (no3-no4)==1 or (no3-no4)==-1:
    ret = 34
  return ret

def clickButtonStart(sender):
  global pS1, pS2, pS3, pS4
  global match9, matchC, matchP
  global NumTotal
  SetPreNum(sender)
  # print(pS1,pS2,pS3,pS4)
  txtview = sender.superview['textview1']
  c = 0
  for c in range(10000):
    match9 = CheckAdd9(sender, c)
    matchC = CheckContinue(c)
    matchP = CheckPreNum(sender, c)

    if *1:
      #add list
      NumX = c
      txtview.text += (str(NumX) + '\n')
      NumTotal += 1
  txtfield = sender.superview['textfield1']
  txtfield.text = str(NumTotal)

v = ui.load_view()
v.present('sheet')

*1:match9 != 0) and (matchC != 0) and (matchP != 0