func d(s: String) -> Int{ var c=0 var n=0 var l=0 for v in s.characters{ if(v == "2"){ c = 1 l = 0 n = 0 } if(c == 1 && v == "0"){ c = 2 l = n n = 0 } if(c == 2 && v == "1"){ c = (l == n ? 3 : 0) l = n n = 0 } if(c == 3 && v == "6"){ if(l == n){ return 1 } else{ c = 0 } } //you may be wondering why I didn't use &&. It's because //using && would require me to use !=, which needs spaces //on both sides if(!(v == "2" || v == "0" || v == "1" || v == "6")){ if(v == " "){ n++ } else{ c=0 l=0 n=0 } } } return 0 }
func d(s:String)->Int{var c=0;var n=0;var l=0;for v in s.characters{if(v=="2"){c=1;l=0;n=0};if(c==1&&v=="0"){c=2;l=n;n=0};if(c==2&&v=="1"){c=(l==n ?3:0);l=n;n=0};if(c==3&&v=="6"){if(l==n){return 1}else{c=0}};if(!(v=="2" || v=="0" || v=="1" || v=="6")){if(v==" "){n++}else{c=0;l=0;n=0;}}};return 0}
По сути, учитывая входную строку <input type="text" id="input" value="I can't wait for 2 0 1 6" />
<button onclick="result.textContent=solution(input.value)">Go</button>
<pre id="result"></pre>
, this executes a "substring search" by intersecting the sets:
var solution = x=>(y=[...x]).map((_,i)=>y.map((_,j)=>r?0:(n=x.substr(j,a.length),r=n<a&n>b),s=" ".repeat(i),a=2+s+0+s+1+s,b=a+5,a+=7),r=0)|r
, x=> // x = input string
(y=[...x]) // y = input as array
.map((_,i)=> // loop over every possible amount of spaces between digits
y.map((_,j)=> // loop over every character to check for a match
r?0:( // if r is set we don't have to do anything, if not:
n=x.substr(j,t.length), // n = substring to compare
r=n<a&n>b // check if r is 2016
),
s=" ".repeat(i), // s = spaces between letters
a=2+s+0+s+1+s, // get string to compare with
b=a+5, // b = 2015 (with spaces)
a+=7 // a = 2017 (with spaces)
),
r=0 // r = result
)
|r // return result
, ... до b < substring < a
= more than enough spaces,2015
, , b
, ..., 2017
, ... a
.Если результат найден, такой интервал 2016
string appeared as a substring of the input, and a truthy list containing this result is printed, like 2016
. В противном случае печатается пустой список.
Строка 0
is never constructed as the code runs; only its spaced-out versions are.
1
Возврат x=>(y=[...x]).map((_,i)=>y.map((_,j)=>r?0:(n=x.substr(j,a.length),r=n<a&n>b),s=" ".repeat(i),a=2+s+0+s+1+s,b=a+5,a+=7),r=0)|r
if it matched, otherwise "2016"
.
Перебор всех возможных комбинаций пробелов между ["2 0 1 6"]
and checks for a match at each substring. Note that it never constructs the string 2016
потому что он генерирует a b c d
as a b
и b
as a
(оба с пробелами) и проверяет, есть ли 2*len(input())-1
.
2 0 1 6
2 0 1 6
abcd
qS* Push s p a c e d i n p u t.
_,, Push [0..N-1] where N = len(input).
_Sf* Push ["", " ", " ", " ", ...].
{"2 0 1 6"\*}% Make it ["2 0 1 6", "2 0 1 6", ...].
\ Swap [0..N-1] to top of stack.
:) Increment each, giving [1..N].
@ Rotate spaced input I to top of stack.
few Push [I1ew I2ew .. INew], i.e.
:+ when concatenated, all substrings of I.
& Intersect sets.
` Print as CJam syntax.
qS*_,,_Sf*{"2 0 1 6"\*}%\:)@few:+&`