브뤼셀 선택으로 연결되어 있습니까?
배경
두 숫자, \$a\$및 \$b\$, 브뤼셀 선택 연산으로 연결된다고합니다 * if \$b\$\ 에서 도달 할 수 있습니다.$a\$\ 의 밑수 10 표현에서 하위 문자열 (하위 문자열은 비어 있지 않아야하며 선행 0을 포함 할 수 없지만 0이 될 수 있음)을 두 배 또는 절반 (짝수 인 경우)하여$a\$
* 이 작업은이 문서에 정의 된 작업과 약간 다릅니다. 주로 종이에 정의 된 작업은 빈 하위 문자열을 허용하고 하위 문자열 "0"을 선택할 수 없습니다.
예를 들어에서 도달 할 수있는 모든 번호 5016:
508 (50[16] half -> 50[8])
2508 ([5016] half -> [2508])
2516 ([50]16 half -> [25]16)
5013 (501[6] half -> 501[3])
5016 (5[0]16 half -> 5[0]16)
(5[0]16 double -> 5[0]16)
5026 (50[1]6 double -> 50[2]6)
5032 (50[16] double -> 50[32])
10016 ([5]016 double -> [10]016)
([50]16 double -> [100]16)
10026 ([501]6 double -> [1002]6)
10032 ([5016] double -> [10032])
50112 (501[6] double -> 501[12])
직무
두 개의 양의 정수가 입력으로 주어 졌을 때 그들이 단일 브뤼셀 선택 연산으로 서로 도달 할 수 있다면 진실 된 값을 출력하고 그렇지 않으면 거짓 값을 출력하는 프로그램 / 함수를 작성하십시오.
득점
이것은 코드 골프 이므로 가장 짧은 바이트가 이깁니다.
샘플 테스트 케이스
2, 4 -> Truthy
4, 2 -> Truthy
101, 101 -> Truthy
516, 58 -> Truthy
58, 516 -> Truthy
516, 5112 -> Truthy
5112, 516 -> Truthy
1, 3 -> Falsey
123, 123 -> Falsey
151, 252 -> Falsey
112, 221 -> Falsey
101, 999 -> Falsey
999, 1001 -> Falsey
101, 1001 -> Falsey
브뤼셀 초이스에서 영감을 얻음-Numberphile
답변
Retina , 38 바이트
L$w`0|[1-9]\d* $`$.(*2*)$'
m`^(.+),\1$
온라인으로 시도하십시오! 링크에는 테스트 케이스가 포함됩니다. 0Falsey의 경우 출력 , Truthy의 경우 0이 아닙니다. 설명:
L$w`0|[1-9]\d*
로 0시작하는 정수를 포함하여 두 입력의 모든 정수 부분 문자열과 일치합니다 0.
$`$.(*2*)$'
해당 하위 문자열 만 두 배로 늘린 결과를 나열합니다.
m`^(.+),\1$
이것이 동일한 정수 쌍을 생성하는지 확인하십시오.
파이썬 2 , 180 \$\cdots\$ 121111 바이트
Mukundan314 덕분에 2 바이트 절약 !!! Dominic van Essen
의 아이디어 덕분에 무려 16 바이트를 절약했습니다 !!! Kevin Cruijssen 덕분에 7 바이트 절약 !!! Neil 덕분에 10 바이트 절약 !!!
def f(*p):a=`min(p)`;return max(a[:i]+`2*int(a[i:j])`+a[j:]==`max(p)`for j in range(len(a)+1)for i in range(j))
온라인으로 시도하십시오!
True또는을 반환합니다 False.
Brachylog , 13 12 바이트
p{~c↺×₂ʰ↻c}ᵈ
그것을 시도 ONL 이네!
-Zgarb 덕분에 1 바이트
[a, b]입력 변수를 통해 입력을 목록으로 취하고 성공 또는 실패를 통해 출력합니다.
p Permute the input.
{ }ᵈ For the first element of the input:
~c take some partition of it,
×₂ double
↺ ʰ↻ the second element of the partition,
c and re-concatenate it.
{ }ᵈ It's possible for the result to be the second element of the input.
R , 120 117 112 106 바이트
편집 : Giuseppe 덕분에 -5 바이트 (다시)
EDIT2 : -6에 갇힌 인수를 사용하여 바이트 substring및 paste모든 루프를 그렇게 피
function(v,m=min(v),`[`=substring,b=rep(1:m,e=m))max(v)%in%paste0(m[0,b-1],as.double(m[1:m,b])*2,m[1:m+1])
온라인으로 시도하십시오!
어떻게?
is_brussels=
function(v, # v is vector of 2 input values
m=min(v), # m is smaller input value
s=substring) # s is alias to substring function.
max(v) %in% # Main function : Is larger input value present in ...
sapply(1:m, # the results of applying all combinations of 1..m ...
function(b) # to the 'brussels' function with beginning b ...
paste0(s(m,0,b-1), # which pastes together m (up to b)...
as.numeric(s(m,b,1:m))*2,
# onto 2x m (from b to all values of e from 1:m) ...
s(m,2:m) # onto m (from e onwards)?
)
)
05AB1E , 31 27 23 바이트
{R`.œv3FyDNè·Nyg%ǝ])Jćå
-4 바이트 버그 수정- 주목 해 주신 @ovs 덕분 입니다. (버그 수정이 자주 발생하지 않기 때문에 바이트를 줄이십시오, 하하. : D)
정수 쌍으로 입력합니다.
온라인으로 시도 하거나 모든 테스트 사례를 확인하십시오 .
설명:
{R # Sort the (implicit) input-pair from highest to lowest
` # Pop and push both values separated to the stack
.œ # Get all partitions of the top lowest integer
v # Loop over each partition `y`:
3F # Inner loop `N` in the range [0, 3):
yD # Push partition `y` twice
Nè # Get the `N`'th item of the partition (modulair 0-based)
· # Double it
N # Push index `N` again
yg% # Modulo the length of the current partition `y`
# (since builtin `ǝ` doesn't index modulair)
ǝ # Insert the doubled substring back into the partition at that index
] # Close both loops
) # Wrap all values on the stack into a list
J # Join each partition back to a single string
ć # Extract the head; pop and push the remainder-list and first item
# separated to the stack (which is the highest value that was still on
# the stack)
å # Check if the list contains this highest integer
# (after which the result is output implicitly)
Raku , 48 바이트
{;*eq[|] m:ex/.+/>>.&{.replace-with($_/(2|.5))}}
온라인으로 시도하십시오! 죄송합니다. TIO에는 Raku의 최신 버전이 없습니다replace-with.즉,. repl.it 온라인 으로 가야 할 것 같아요 ! 대신.
JavaScript (ES9), 102 바이트
false또는을 반환합니다 1.
f=(a,b,i)=>(g=r=>a.replace(o=RegExp('(?<='+r),n=>o=n*2)==b&++o||o&&g('.'+r)|g(r+'.'))`)`||!i&&f(b,a,1)
온라인으로 시도하십시오!
어떻게?
\ 의 첫 번째 발생과 일치하는 가능한 모든 정규식을 재귀 적으로 빌드합니다.$p\$\ 앞에 오는 숫자$q\$lookbehind 어설 션을 사용하여 숫자. 일치하는 문자열은 정수로 강제 변환되고 두 배가되고 다시 문자열로 강제 변환됩니다.
예:
"123456".replace(/(?<=..).../, n => n * 2) // -> "12[345]6" -> "12[690]6"
교체가 발생하지 않았거나 (실패) 결과가 다른 숫자 (성공)와 같으면 재귀를 중지합니다. 우리는 a로 b또는 b로 전환하려고합니다 a.
댓글 작성
f = ( // f is a recursive function taking:
a, b, // the input numbers a and b
i // a flag i telling if the numbers were already swapped
) => ( //
g = r => // g is a recursive function taking a pattern r
a.replace( // replace in a:
o = // initialize o to a non-numeric value
RegExp('(?<=' + r), // turn r into a regular expression of the form /(?<=..)../
n => // if something is matched: replace the substring n with
o = n * 2 // 2 * n and assign the result to o
) == b // end of replace(); success if the result is equal to b
& ++o // and o is an even number, which becomes odd when incremented
|| o && // otherwise, if o is not equal to NaN:
g('.' + r) | // try again with 1) another leading '.' in r
g(r + '.') // and 2) with another trailing '.' in r
)`)` // initial call to g with r = ')'
|| !i && f(b, a, 1) // if i is not set, try again with a and b exchanged
Python 3.8 (사전 출시) , 115 바이트
lambda*x:(a:=str(min(x)))and{str(max(x))}&{a[:i]+str(2*int(a[i:j]))+a[j:]for j in range(len(a)+1)for i in range(j)}
온라인으로 시도하십시오!
Python 3 , 139116 바이트
Noodle9의 답변 골프 . b브뤼셀 선택 항목이 있으면 포함 된 집합을 출력하고 그렇지 않으면 빈 집합을 출력합니다.
def f(*x):a,b=map(str,sorted(x));return{b}&{a[:i]+str(2*int(a[i:j]))+a[j:]for j in range(len(a)+1)for i in range(j)}
온라인으로 시도하십시오!
perl -Mfeature = say -na, 68 바이트
($_,$n)=@F;m[.+(?{say$=if$n eq$`.($&/2).$'||$n eq$`.($&*2).$'})(?!)]
온라인으로 시도하십시오!
입력에서 두 개의 숫자를 읽고 숫자를 연결하는 방법이 있으면 60을 인쇄하고 그렇지 않으면 아무것도 인쇄하지 않습니다.
어떻게 작동합니까?
우리는 첫 번째 숫자의 가능한 모든 비어 있지 않은 부분 문자열을 취합니다. 에서 사용할 수 있습니다 $&. 현재 부분 문자열로 이어지는 첫 번째 숫자의 부분은에 $` 있고 그 뒤의 부분은에 $'있습니다. 그러면 어느 2 문자열을 승산, 2로 나누어과의 사이를 개재하여 두 개의 새로운 문자열을 작성 $` 하고 $'. 그런 다음이를 두 번째 숫자와 비교 $=하고 일치하는 항목이 있으면 인쇄 합니다 ( $=기본적으로 60 포함). 부분 문자열이 홀수이면 결과 문자열에 소수점이 포함되므로 두 번째 숫자와 일치하지 않습니다.
Perl 5 -pl , 122 바이트
$t=<>;for$a(1..y///c){for$b(0..y///c){$z||=/(.{$b})(.{$a})/&&$2!~/^0./&&grep$t==$_,$2%2==0&&$1.$2/2 .$',$1.$2*2 .$'}}$_=$z
온라인으로 시도하십시오!