Harigami
ログイン
Harigamiは、ログイン不要のコード共有サービスです。コードの投稿後に発行されるURLでコードを共有できます。
PythonやRubyなど一部の言語は、投稿後にオンラインで実行することもできます。
誰でも無料で使えて、広告もありません。コードレビューやスニペットの代わりに使ってみてください。


詳細設定
+
anonymous タイトルなし
Python
#初期化
ans=[[[1,2,3,4,5,6,7,8,9]for i in range(9)]for j in range(9)]
data=[[0 for i in range(9)] for j in range(9)]

#入力
for i in range(9):
    data[i]=list(map(int,input().split()))


#確定したマスを周りのマスの数字から消す関数
def solve(i,j):
    for k in range(9):  # 縦
        if (k == j):
            continue
        if (data[i][j] in ans[i][k]):
            ans[i][k].remove(data[i][j])

    for k in range(9):  # 横
        if (k == i):
            continue
        if (data[i][j] in ans[k][j]):
            ans[k][j].remove(data[i][j])

    for k in range(3):  # 3×3
        for l in range(3):
            if (data[i][j] in ans[i + k - i % 3][j + l - j % 3]):
                if (k == i % 3 and l == j % 3):
                    continue
                ans[i + k - i % 3][j + l - j % 3].remove(data[i][j])
#関数ここまで

#最初から与えられているマスに値を入れる
for i in range(9):
    for j in range(9):
        if (data[i][j] != 0):
            ans[i][j] = [data[i][j], "END"]
        solve(i,j)

#一回の実行で1マスは絶対確定するから81回回せば理論上解ける(はず)
for x in range(81):
    for i in range(9):
        for j in range(9):
            if (len(ans[i][j]) == 1):

                data[i][j]=ans[i][j][0]
                solve(i, j)
                #確定したマスはENDをつけて次回からスキップ
                ans[i][j].append("END")

#ここまでで解き終わる

for i in range(9):
    for j in range(9):
        if("END" in ans[i][j]):
            ans[i][j].remove("END")
#ENDを外す

for i in range(9):
    for j in range(9):
        print(ans[i][j][0],end=" ")
    print()
#結果を出力
  • 0
  • 1
anonymous タイトルなし
Python
#初期化
ans=[[[1,2,3,4,5,6,7,8,9]for i in range(9)]for j in range(9)]
data=[[0 for i in range(9)] for j in range(9)]

#入力
for i in range(9):
    data[i]=list(map(int,input().split()))


#確定したマスを周りのマスの数字から消す関数
def solve(i,j):
    for k in range(9):  # 縦
        if (k == j):
            continue
        if (data[i][j] in ans[i][k]):
            ans[i][k].remove(data[i][j])

    for k in range(9):  # 横
        if (k == i):
            continue
        if (data[i][j] in ans[k][j]):
            ans[k][j].remove(data[i][j])

    for k in range(3):  # 3×3
        for l in range(3):
            if (data[i][j] in ans[i + k - i % 3][j + l - j % 3]):
                if (k == i % 3 and l == j % 3):
                    continue
                ans[i + k - i % 3][j + l - j % 3].remove(data[i][j])
#関数ここまで

#最初から与えられているマスに値を入れる
for i in range(9):
    for j in range(9):
        if (data[i][j] != 0):
            ans[i][j] = [data[i][j], "END"]
        solve(i,j)

#一回の実行で1マスは絶対確定するから81回回せば理論上解ける(はず)
for x in range(81):
    for i in range(9):
        for j in range(9):
            if (len(ans[i][j]) == 1):

                data[i][j]=ans[i][j][0]
                solve(i, j)
                #確定したマスはENDをつけて次回からスキップ
                ans[i][j].append("END")

#ここまでで解き終わる

for i in range(9):
    for j in range(9):
        if("END" in ans[i][j]):
            ans[i][j].remove("END")
#ENDを外す

for i in range(9):
    for j in range(9):
        print(ans[i][j],end=" ")
    print()
#結果を出力
  • 0
  • 0
anonymous タイトルなし
Python
import numpy as np
import time

def input_sudoku():
    L=[]
    for i in range(9):
        L.append([int(x) for x in input().split()])
    print(L)


def check(a):
    for x in range(9):
        for y in range(9):
            i=a[y][x]
            if i not in (row(a,y) | col(a,x) | sq(a,x,y)):
                return x,y
    return True

def row(a,n):
    return set(a[n])

def col(a,n):
    return set(a[:,n])

def sq(a,x,y):
    if y<=2:
        k=a[:3]
        if x<=2:
            k=k[:,:3]
        elif x<=5:
            k=k[:,3:6]
        else:
            k=k[:,6:9]
    elif y<=5:
        k=a[3:6]
        if x<=2:
            k=k[:,:3]
        elif x<=5:
            k=k[:,3:6]
        else:
            k=k[:,6:9]
    else:
        k=a[6:9]
        if x<=2:
            k=k[:,:3]
        elif x<=5:
            k=k[:,3:6]
        else:
            k=k[:,6:9]

    return set(k.flatten())

def print_if_progress(a,mx):#埋けれた数が増えた時のみその時の進捗を出力
    filled=np.count_nonzero(a!=0)
    if filled>mx:
        mx=filled
        print(mx)
        print(a)
    return mx


def dfs(a,mx):#mxの因数は出力用/なくても動く
    if np.min(a)!=0:
        return a
    space=np.argmin(a)#左上から左へ、下へと見ていき、最小値(==空白 : 0以外が最小値の時は、上のif文でreturn)のindex(スカラ)を返す
    x=space%9  #x座標
    y=space//9 #y座標
    for i in range(1,10):
        if i not in (row(a,y) | col(a,x) | sq(a,x,y)):
            a[y][x]=i

            mx=print_if_progress(a,mx)#出力/なくても動く
    
            r=dfs(a,mx)
            if r.shape[0]:
                return r
            
    a[y][x]=0
    
    return np.array([])

def main():
    L=[]
    for i in range(9):
        L.append([int(x) for x in input().split()])
   
    a=np.array(L)

    if a.shape!=(9,9):
        print('入力値エラー:各行半角スペース区切りで9つの数字が入っていることを確認してください')
        return

    start=time.time()
    
    result=dfs(a,0)
        
    print(time.time()-start,"sec")
    print('---------------------')

    if result.shape[0]:
        for row in result:
            print(" ".join([str(x) for x in row.tolist()]))
    else:
        print('有効な解がありません')


if __name__ == '__main__':
    main()
  • 0
  • 0
LC23 タイトルなし
Python
//@version=3

study(title = "AWABI改")

source = hlc3
length = input(26, minval=1), multi = input(2.0, minval=0.001,title="シグマ", maxval=50)
len = input(14, minval=1, title="MA1")
len2 = input(20, minval=1, title="MA2")

//RSI
rsi1 = rsi(source, length)

// BB of RSI
basis = wma(rsi1, length)
dev = multi * stdev(rsi1, length)
upper = basis + dev
lower = basis - dev
br = (rsi1 - lower)/(upper - lower)
obLevel1 = input(1, "Over Bought Level 1")
obLevel2 = input(0.5, "Over Bought Level 2")
obLevel3 = input(0, "Over Bought Level 3")
//MA OF RSI
er = wma(br,len)
er2 = wma(br,len2)

//ENTRY条件
LONG = crossover(er,er2)
SHORT = crossunder(er,er2)


switch = 0
setA = 0
setB = 0

if (LONG and (switch[1] == 0))
    switch := 1
    setA := 1
    setB := 0
else    
    if (SHORT and (switch[1] == 1))
        switch := 0
        setA := 0
        setB := 1
    else
        switch := nz(switch[1],0)
        setA := 0
        setB := 0



alertcondition(setA, title = "LONG", message = "LLLLL!")
alertcondition(setB, title = "SHORT", message = "SSSSS!")



//COLOR
mc = er > er2 ? lime:red
plot(br, color=aqua)
plot(obLevel1, title="ov1",color=silver, style=3)
plot(obLevel2, title="ov2",color=silver, style=3)
plot(obLevel3, title="ov3",color=silver, style=3)
plot(er, title="MA1", style=line, linewidth=1, color=mc,transp=10)
plot(er2, title="MA2", style=line, linewidth=1, color=mc,transp=10)
fill(plot(er), plot(er2), color = mc)
bgcolor(setA ? green : na, transp=90, offset=-1)
bgcolor(setB ? red : na, transp=90, offset=-1)
  • 0
  • 0
LC23 タイトルなし
Python
//@version=3

study(title = "AWABI改")

source = hlc3
length = input(26, minval=1), multi = input(2.0, minval=0.001,title="シグマ", maxval=50)
len = input(14, minval=1, title="MA1")
len2 = input(20, minval=1, title="MA2")

//RSI
rsi1 = rsi(source, length)

// BB of RSI
basis = wma(rsi1, length)
dev = multi * stdev(rsi1, length)
upper = basis + dev
lower = basis - dev
br = (rsi1 - lower)/(upper - lower)
obLevel1 = input(1, "Over Bought Level 1")
obLevel2 = input(0.5, "Over Bought Level 2")
obLevel3 = input(0, "Over Bought Level 3")
//MA OF RSI
er = wma(br,len)
er2 = wma(br,len2)

//ENTRY条件
LONG = crossover(er,er2)
SHORT = crossunder(er,er2)


switch = 0
setA = 0
setB = 0

if (LONG and (switch[1] == 0))
    switch := 1
    setA := 1
    setB := 0
else    
    if (SHORT and (switch[1] == 1))
        switch := 0
        setA := 0
        setB := 1
    else
        switch := nz(switch[1],0)
        setA := 0
        setB := 0



alertcondition(setA, title = "LONG", message = "LLLLL!")
alertcondition(setB, title = "SHORT", message = "SSSSS!")



//COLOR
mc = er > er2 ? lime:red
plot(br, color=aqua)
plot(obLevel1, title="ov1",color=silver, style=3)
plot(obLevel2, title="ov2",color=silver, style=3)
plot(obLevel3, title="ov3",color=silver, style=3)
plot(er, title="MA1", style=line, linewidth=1, color=mc,transp=10)
plot(er2, title="MA2", style=line, linewidth=1, color=mc,transp=10)
fill(plot(er), plot(er2), color = mc)
  • 0
  • 0
anonymous タイトルなし
Python
//@version=3

strategy(title = "AWABI改")

source = hlc3
length = input(26, minval=1), multi = input(2.0, minval=0.001,title="シグマ", maxval=50)
len = input(14, minval=1, title="MA1")
len2 = input(20, minval=1, title="MA2")

//RSI
rsi1 = rsi(source, length)

// BB of RSI
basis = wma(rsi1, length)
dev = multi * stdev(rsi1, length)
upper = basis + dev
lower = basis - dev
br = (rsi1 - lower)/(upper - lower)
obLevel1 = input(1, "Over Bought Level 1")
obLevel2 = input(0.5, "Over Bought Level 2")
obLevel3 = input(0, "Over Bought Level 3")
//MA OF RSI
er = wma(br,len)
er2 = wma(br,len2)

//ENTRY条件
LONG = crossover(br,er)
SHORT = crossunder(br,er)
strategy.entry("LONG",true,when=LONG)
strategy.entry("SHORT",false,when=SHORT)

//COLOR
mc = er > er2 ? lime:red
plot(br, color=aqua)
plot(obLevel1, title="ov1",color=silver, style=3)
plot(obLevel2, title="ov2",color=silver, style=3)
plot(obLevel3, title="ov3",color=silver, style=3)
plot(er, title="MA1", style=line, linewidth=1, color=mc,transp=10)
plot(er2, title="MA2", style=line, linewidth=1, color=mc,transp=10)
fill(plot(er), plot(er2), color = mc)
  • 0
  • 0
LC23 タイトルなし
Python
//@version=3

strategy(title = "AWABI改")

source = hlc3
length = input(26, minval=1), multi = input(2.0, minval=0.001,title="シグマ", maxval=50)
len = input(14, minval=1, title="MA1")
len2 = input(20, minval=1, title="MA2")

//RSI
rsi1 = rsi(source, length)

// BB of RSI
basis = wma(rsi1, length)
dev = multi * stdev(rsi1, length)
upper = basis + dev
lower = basis - dev
br = (rsi1 - lower)/(upper - lower)
obLevel1 = input(1, "Over Bought Level 1")
obLevel2 = input(0.5, "Over Bought Level 2")
obLevel3 = input(0, "Over Bought Level 3")
//MA OF RSI
er = wma(br,len)
er2 = wma(br,len2)

//ENTRY条件
LONG = crossover(br,er)
SHORT = crossunder(br,er)
strategy.entry("LONG",true,when=LONG)
strategy.entry("SHORT",false,when=SHORT)

//COLOR
mc = er > er2 ? lime:red
plot(br, color=aqua)
plot(obLevel1, title="ov1",color=silver, style=3)
plot(obLevel2, title="ov2",color=silver, style=3)
plot(obLevel3, title="ov3",color=silver, style=3)
plot(er, title="MA1", style=line, linewidth=1, color=mc,transp=10)
plot(er2, title="MA2", style=line, linewidth=1, color=mc,transp=10)
fill(plot(er), plot(er2), color = mc)
  • 0
  • 0
LC23 タイトルなし
Python
//@version=3
study("MA20MTF GMMA", overlay=true)


A = input(title='5M', type=resolution, defval="1")
B = input(title='15M', type=resolution, defval="1")
C = input(title='30M', type=resolution, defval="1")
D = input(title='1H', type=resolution, defval="1")
E = input(title='4H', type=resolution, defval="1")


A1 = security(tickerid, A, sma(close, 100))
B1 = security(tickerid, B, sma(close, 300))
C1 = security(tickerid, C, sma(close, 600))
D1 = security(tickerid, D, sma(close, 1200))
E1 = security(tickerid, E, sma(close, 4800))



plot(A1 , color=gray, linewidth=1)
AL=plot(B1 , color=lime, linewidth=1)
BL=plot(C1 , color=lime, linewidth=1)
CL=plot(D1 , color=red, linewidth=1)
DL=plot(E1 , color=red, linewidth=1)
fill(AL,BL ,color=lime,transp=80)
fill(CL,DL ,color=red,transp=80)
  • 0
  • 0
LC23 タイトルなし
Python
//@version=2

strategy(title = "インフルエンサー Strategy", overlay = true, pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)

// === INPUTS ===
useRes      = input(defval = true, title = "Use Alternate Resolution? ( recommended )")
stratRes    = input(defval = "1440", title = "Set Resolution ( should not be lower than chart )", type = resolution)
useMA       = input(defval = true, title = "Use MA? ( otherwise use simple Open/Close data )")
basisType   = input(defval = "DEMA", title = "MA Type: SMA, EMA, DEMA, TEMA, WMA, VWMA, SMMA, HullMA, LSMA, ALMA ( case sensitive )", type = string)
basisLen    = input(defval = 14, title = "MA Period", minval = 1)
offsetSigma = input(defval = 6, title = "Offset for LSMA / Sigma for ALMA", minval = 0)
offsetALMA  = input(defval = 0.85, title = "Offset for ALMA", minval = 0, step = 0.01)
useStop     = input(defval = true, title = "Use Trailing Stop?")
slPoints    = input(defval = 200000, title = "Stop Loss Trail Points", minval = 1)
slOffset    = input(defval = 400000, title = "Stop Loss Trail Offset", minval = 1)
// === /INPUTS ===

// === BASE FUNCTIONS ===
// Returns MA input selection variant, default to SMA if blank or typo.
variant(type, src, len, offSig, offALMA) =>
    v1 = sma(src, len)                                                  // Simple
    v2 = ema(src, len)                                                  // Exponential
    v3 = 2 * v2 - ema(v2, len)                                          // Double Exponential
    v4 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len)               // Triple Exponential
    v5 = wma(src, len)                                                  // Weighted
    v6 = vwma(src, len)                                                 // Volume Weighted
    v7 = na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len    // Smoothed
    v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))   // Hull
    v9 = linreg(src, len, offSig)                                       // Least Squares
    v10 = alma(src, len, offALMA, offSig)                               // Arnaud Legoux
    type=="EMA"?v2 : type=="DEMA"?v3 : type=="TEMA"?v4 : type=="WMA"?v5 : type=="VWMA"?v6 : type=="SMMA"?v7 : type=="HullMA"?v8 : type=="LSMA"?v9 : type=="ALMA"?v10 : v1
// security wrapper for repeat calls
reso(exp, use, res) => use ? security(tickerid, res, exp) : exp
// === /BASE FUNCTIONS ===

// === SERIES SETUP ===
// open/close
closeSeries = useMA ? reso(variant(basisType, close, basisLen, offsetSigma, offsetALMA), useRes, stratRes) : reso(close, useRes, stratRes)
openSeries  = useMA ? reso(variant(basisType, open, basisLen, offsetSigma, offsetALMA), useRes, stratRes) : reso(open, useRes, stratRes)
trendState  = closeSeries > openSeries ? true : closeSeries < openSeries ? false : trendState[1]
// === /SERIES ===

// === PLOTTING ===
barcolor(color = closeSeries > openSeries ? #006600 : #990000, title = "Bar Colours")
// channel outline
closePlot   = plot(closeSeries, title = "Close Line", color = #009900, linewidth = 2, style = line, transp = 90)
openPlot    = plot(openSeries, title = "Open Line", color = #CC0000, linewidth = 2, style = line, transp = 90)
// channel fill
closePlotU  = plot(trendState ? closeSeries : na, transp = 100, editable = false)
openPlotU   = plot(trendState ? openSeries : na, transp = 100, editable = false)
closePlotD  = plot(trendState ? na : closeSeries, transp = 100, editable = false)
openPlotD   = plot(trendState ? na : openSeries, transp = 100, editable = false)
fill(openPlotU, closePlotU, title = "Up Trend Fill", color = #009900, transp = 40)
fill(openPlotD, closePlotD, title = "Down Trend Fill", color = #CC0000, transp = 40)
// === /PLOTTING ===

// === STRATEGY ===
// conditions
longCond    = crossover(closeSeries, openSeries)
shortCond   = crossunder(closeSeries, openSeries)
// entries and base exit
strategy.entry("long", strategy.long, when = longCond)
strategy.entry("short", strategy.short, when = shortCond)
// if we're using the trailing stop
if (useStop)
    strategy.exit("XL", from_entry = "long", trail_points = slPoints, trail_offset = slOffset)
    strategy.exit("XS", from_entry = "short", trail_points = slPoints, trail_offset = slOffset)
// not sure needed, but just incase..
strategy.exit("XL", from_entry = "long", when = shortCond)
strategy.exit("XS", from_entry = "short", when = longCond)
// === /STRATEGY ===
  • 0
  • 0
LC23 タイトルなし
Python
//@version=3
study("MA20MTF GMMA", overlay=false)

// SMA or EMA
SEswitch = input(title="on:EMA off:SMA", type=bool, defval=true)

//Set resolutions
resA = input(title='5M', type=resolution, defval="1")
resB = input(title='15M', type=resolution, defval="1")
resC = input(title='30M', type=resolution, defval="1")
resD = input(title='1H', type=resolution, defval="1")
resE = input(title='4H', type=resolution, defval="1")

//Set switches
maAswitch = input(title="MA1 On/Off", type=bool, defval=true)
maBswitch = input(title="MA2 On/Off", type=bool, defval=true)
maCswitch = input(title="MA3 On/Off", type=bool, defval=true)
maDswitch = input(title="MA4 On/Off", type=bool, defval=true)
maEswitch = input(title="MA5 On/Off", type=bool, defval=true)

//get data
dataA = security(tickerid, resA, SEswitch ? ema(close,100 ) : sma(close, 100))
dataB = security(tickerid, resB, SEswitch ? ema(close, 300) : sma(close, 300))
dataC = security(tickerid, resC, SEswitch ? ema(close, 600) : sma(close, 600))
dataD = security(tickerid, resD, SEswitch ? ema(close, 1200) : sma(close, 1200))
dataE = security(tickerid, resE, SEswitch ? ema(close, 4800) : sma(close, 4800))


//Plotting
plot(maAswitch ? dataA : na, color=gray, linewidth=1)
AL=plot(maBswitch ? dataB : na, color=lime, linewidth=2)
BL=plot(maCswitch ? dataC : na, color=lime, linewidth=2)
CL=plot(maDswitch ? dataD : na, color=red, linewidth=2)
DL=plot(maEswitch ? dataE : na, color=red, linewidth=2)
fill(AL,BL ,color=lime,transp=45)
fill(CL,DL ,color=red,transp=45)
  • 0
  • 0