Problem:


Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7



Sample input:
1222


Sample output:
1+2+2+2=7

Submission status: Accepted

Solution code:
li = [int(n) for n in list(input())]
Ops = []

def func(i, x, li, ops):
  if i==0:
    if x+li[0]==7:
      return True
    if x-li[0]==7:
      return True
  else:
    if func(i-1, x-li[i], li, ops):
      ops.append('-')
      return True
    if func(i-1, x+li[i], li, ops):
      ops.append('+')
      return True
    
func(3, 0, li, Ops)
Ops.append('=7')
for i in range(4):
  print(li[i], Ops[i], sep='', end='')

Submission status: Accepted

Solution code:
import sys

S = input()

def ticket_sum(i, s, num, memo):
    if i == len(S):
        if num == 7:
            memo.append(s+"=7")
            return memo
        else:
            return
    
    # 数字を分ける場所
    pos = i - len(S)

    # "+"を入れる場合
    ticket_sum(i+1, s[:pos]+"+"+s[pos:], num+int(s[pos]), memo)
    # "-"を入れる場合
    ticket_sum(i+1, s[:pos]+"-"+s[pos:], num-int(s[pos]), memo)

    return memo

memo = []
print(ticket_sum(1, S, int(S[0]), memo)[0])

Submission status: Accepted

Solution code:
a,b,c,d = map(int,list(input()))

#場合分け
if a+b+c+d==7:print('{}+{}+{}+{}=7'.format(a,b,c,d));exit()
if a-b+c+d==7:print('{}-{}+{}+{}=7'.format(a,b,c,d));exit()
if a+b-c+d==7:print('{}+{}-{}+{}=7'.format(a,b,c,d));exit()
if a+b+c-d==7:print('{}+{}+{}-{}=7'.format(a,b,c,d));exit()
if a-b-c+d==7:print('{}-{}-{}+{}=7'.format(a,b,c,d));exit()
if a+b-c-d==7:print('{}+{}-{}-{}=7'.format(a,b,c,d));exit()
if a-b+c-d==7:print('{}-{}+{}-{}=7'.format(a,b,c,d));exit()
if a-b-c-d==7:print('{}-{}-{}-{}=7'.format(a,b,c,d));exit()

Submission status: Accepted

Solution code:
S = list(input())
ops = []
lst = []

for i in range(8):
    ops.append(bin(i))
    ops[i] = ops[i][2:].zfill(3)

for i in range(8):
    res = S[0]
    for j in range(3):
        if ops[i][j] == "0":
            res = res + "+" + S[j+1]
        else:
            res = res + "-" + S[j+1]
    if eval(res) == 7:
        res = res + "=7"
        lst.append(res)
        break

print(lst[0])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

def dfs(ind, l):
    if ind == 4:
        if sum(l) ==7:
            for i, num in enumerate(l):
                if num >= 0 and i != 0:
                    print('+', end='')
                print(num, end='')
        else:
            return
        print('=7')
        exit()
    dfs(ind+1, l+[A[ind]])
    dfs(ind+1, l+[(-1)*A[ind]])

A = list(map(int, list(input())))
dfs(1, [A[0]])

Submission status: Accepted

Solution code:
s = list(input())
n = len(s)

def dfs(i , f , s_sum):
    if i == n-1:
        if s_sum == 7:
            print(f+"=7")
            exit()
    else:
        dfs(i+1 , f + "+" + s[i+1] , s_sum + int(s[i+1]))
        dfs(i+1 , f + "-" + s[i+1] , s_sum - int(s[i+1]))

dfs(0 , s[0] , int(s[0]))

Submission status: Accepted

Solution code:
n = [int(x) for x in input()]
k=len(n)-1
for i in range(2**k):
    op=["-"]*(k)
    for j in range(k):
        if ((i>>j)&1):
            op[k-1-j]="+"
    formula = ""
    for p_n, p_o in zip(n, op + [""]):  
        formula += (str(p_n) + p_o)
    if eval(formula) == 7:
        print(formula + "=7")
        break

Submission status: Accepted

Solution code:
import sys
S=input()
n=len(S)
for i in range(2**(n-1)):
  s=int(S[0])
  L=[S[0]]
  for j in range(n-1):
    if (i>>j) & 1:
      s+=int(S[j+1])
      L+=["+"]+[S[j+1]]
    else:
      s-=int(S[j+1])
      L+=["-"]+[S[j+1]]
  if s==7:
    L+=["=7"]
    print(*L,sep="")
    sys.exit()

Submission status: Accepted

Solution code:
s = input()
n = len(s)

ops = None
for i in range((n-1)**2):
  tmp = int(s[0])
  for j in range(0, n-1):

    if (i>>j)&1:
      tmp += int(s[j+1])
    else:
      tmp -= int(s[j+1])

  if tmp == 7:
    ops = i
    break

res = ""
for i in range(n):
  res += s[i]
  if i < n-1:
    if (ops>>i)&1:
      res += "+"
    else:
      res += "-"

print(res+"=7")

Submission status: Accepted

Solution code:
s = input()
n = len(s)

correct_ops = None
def helper(cur, i, ops=[]):
  global correct_ops
  if i == n and cur == 7: 
    correct_ops = ops
    return ops
  if i == n and cur != 7: return []

  if helper(cur+int(s[i]), i+1, ops+["+"]): return ops

  if helper(cur-int(s[i]), i+1, ops+["-"]): return ops

helper(int(s[0]), 1)

res = ""
for i in range(n):
  if i == n-1:
    res += s[i]
  else:
    res += s[i]+correct_ops[i]

print(res+"=7")

Submission status: Accepted

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    A,B,C,D = list(map(int,list(_in[0])))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    for a in [1,-1]:
        for b in [1,-1]:
            for c in [1,-1]:
                if A+a*B+b*C+c*D == 7:
                    s = str(A)+("+" if a==1 else "-")+str(B)+("+" if b==1 else "-")+str(C)+("+" if c==1 else "-")+str(D)+'=7'
                    break
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(s)

Submission status: Accepted

Solution code:
#ABC 079 C
a,b,c,d=list(input())
s="+-"

for i in range(2):
    for j in range(2):
        for k in range(2):
            if eval(a+s[i]+b+s[j]+c+s[k]+d)==7:
                print(str(a+s[i]+b+s[j]+c+s[k]+d)+"=7")
                exit()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n = input()
    aida = len(n) - 1
    for i in range(2 ** aida):
        kigo = ['-'] * aida
        for j in range(aida):
            if ((i >> j) & 1):
                kigo[-1 - j] = '+'
    
        kigo.append('')
     
        siki = ''
        for kazu, seifu in zip(n, kigo):
            siki += (kazu + seifu)
      
        if eval(siki) == 7:
            print(siki + '=7')
            break

resolve()

Submission status: Accepted

Solution code:
s = input()

for bit in range(1 << 3):
    ans = int(s[0])
    f = s[0]
    
    for i in range(3):
        if bit & (1 << i):
            ans += int(s[i + 1])
            f += "+"
        else:
            ans -= int(s[i + 1])
            f += "-"
        f += s[i + 1]

    if ans == 7:
        print(f + "=7")
        exit()

Submission status: Accepted

Solution code:
ABCD = input()
if int(ABCD[0]) + int(ABCD[1]) + int(ABCD[2]) + int(ABCD[3]) == 7: print(ABCD[0] + "+" + ABCD[1] + "+" + ABCD[2] + "+" + ABCD[3] + "=7")
elif int(ABCD[0]) + int(ABCD[1]) + int(ABCD[2]) - int(ABCD[3]) == 7: print(ABCD[0] + "+" + ABCD[1] + "+" + ABCD[2] + "-" + ABCD[3] + "=7")
elif int(ABCD[0]) + int(ABCD[1]) - int(ABCD[2]) + int(ABCD[3]) == 7: print(ABCD[0] + "+" + ABCD[1] + "-" + ABCD[2] + "+" + ABCD[3] + "=7")
elif int(ABCD[0]) + int(ABCD[1]) - int(ABCD[2]) - int(ABCD[3]) == 7: print(ABCD[0] + "+" + ABCD[1] + "-" + ABCD[2] + "-" + ABCD[3] + "=7")
elif int(ABCD[0]) - int(ABCD[1]) + int(ABCD[2]) + int(ABCD[3]) == 7: print(ABCD[0] + "-" + ABCD[1] + "+" + ABCD[2] + "+" + ABCD[3] + "=7")
elif int(ABCD[0]) - int(ABCD[1]) + int(ABCD[2]) - int(ABCD[3]) == 7: print(ABCD[0] + "-" + ABCD[1] + "+" + ABCD[2] + "-" + ABCD[3] + "=7")
elif int(ABCD[0]) - int(ABCD[1]) - int(ABCD[2]) + int(ABCD[3]) == 7: print(ABCD[0] + "-" + ABCD[1] + "-" + ABCD[2] + "+" + ABCD[3] + "=7")
elif int(ABCD[0]) - int(ABCD[1]) - int(ABCD[2]) - int(ABCD[3]) == 7: print(ABCD[0] + "-" + ABCD[1] + "-" + ABCD[2] + "-" + ABCD[3] + "=7")

Submission status: Accepted

Solution code:
n = [int(x) for x in input()]
op_cnt = len(n)-1

for i in range (2**op_cnt):
  op = ['-']*op_cnt
  for j in range(op_cnt):
    if ((i >>j)&1):
      op[op_cnt-1-j] ='+'


  formula =''

  for p_n, p_o in zip(n, op+[""]):
    formula += (str(p_n) + p_o)
  if eval(formula) == 7:
    print(formula + "=7")
    exit()

Submission status: Accepted

Solution code:
S = list(input())
for i in range(2 ** 3):
    pm = ['-'] * 3
    for j in range(3):
        if i >> j & 1:
            pm[j] = '+'
    ans = [None] * 7
    ans[::2] = S
    ans[1::2] = pm
    ans = ''.join(ans)
    if eval(ans) == 7:
        print(ans + '=7')
        break

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000) # 再帰上限を増やす

def main():
    ticket = input()
    N = 4
    num_list = []
    for num in ticket:
        num_list.append(int(num))

    for bit in range(1 << (N-1)):
        sum_value = num_list[0]
        op_list = [None] * N

        for i in range(N-1):
            if bit & (1 << i):
                # +を入れるパターン
                op_list[i] = "+"
                sum_value += num_list[i+1]
            else:
                # -を入れるパターン
                op_list[i] = "-"
                sum_value -= num_list[i+1]

        if sum_value == 7:
            op_list[3] = "="
            for num, op, in zip(num_list, op_list):
                print(num, op, sep="", end="")
            print(7)
            exit()

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000) # 再帰上限を増やす

def main():
    input = sys.stdin.readline  # 文字列に対してinputした場合は、rstripするのを忘れずに！
    ticket = input().rstrip()
    N = 4
    num_list = []
    for num in ticket:
        num_list.append(int(num))

    for bit in range(1 << (N-1)):
        sum_value = num_list[0]
        op_list = [None] * N

        for i in range(N-1):
            if bit & (1 << i):
                # +を入れるパターン
                op_list[i] = "+"
                sum_value += num_list[i+1]
            else:
                # -を入れるパターン
                op_list[i] = "-"
                sum_value -= num_list[i+1]

        if sum_value == 7:
            op_list[3] = "="
            for num, op, in zip(num_list, op_list):
                print(num, op, sep="", end="")
            print(7)
            exit()

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000) # 再帰上限を増やす

def main():
    input = sys.stdin.readline  # 文字列に対してinputした場合は、rstripするのを忘れずに！
    ticket = input().rstrip()
    N = 4
    num_list = []
    for num in ticket:
        num_list.append(int(num))

    for bit in range(1 << (N-1)):
        sum_value = num_list[0]
        op_list = []

        for i in range(N-1):
            if bit & (1 << i):
                # +を入れるパターン
                op_list.append("+")
                sum_value += num_list[i+1]
            else:
                # -を入れるパターン
                op_list.append("-")
                sum_value -= num_list[i+1]

        if sum_value == 7:
            op_list.append("=")
            for num, op, in zip(num_list, op_list):
                print(num, op, sep="", end="")
            print(7)
            exit()

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
import sys
import math

s=input()
t=[]
for i in range(4):
    t.append(int(s[i]))
temp=0
for i in range(2):
    temp+=(t[0]+t[1] if i==0 else t[0]-t[1])
    for j in range(2):
        temp+=(t[2] if j==0 else -t[2])
        for k in range(2):
            temp+=(t[3] if k==0 else -t[3])
            if temp==7:
                print(s[0]+('+' if i==0 else '-')+s[1]+('+' if j==0 else '-')+s[2]+('+' if k==0 else '-')+s[3]+'=7')
                sys.exit()
            else:
                temp-=(t[3] if k==0 else -t[3])
        temp-=(t[2] if j==0 else -t[2])
    temp=0

Submission status: Accepted

Solution code:
n = input()
a, b, c, d = int(n[0]), int(n[1]), int(n[2]), int(n[3])

if a + b + c + d == 7:
    print('%d+%d+%d+%d=7' % (a, b, c, d))
elif a + b + c - d == 7:
    print('%d+%d+%d-%d=7' % (a, b, c, d))
elif a + b - c + d == 7:
    print('%d+%d-%d+%d=7' % (a, b, c, d))
elif a + b - c - d == 7:
    print('%d+%d-%d-%d=7' % (a, b, c, d))
elif a - b + c + d == 7:
    print('%d-%d+%d+%d=7' % (a, b, c, d))
elif a - b + c - d == 7:
    print('%d-%d+%d-%d=7' % (a, b, c, d))
elif a - b - c + d == 7:
    print('%d-%d-%d+%d=7' % (a, b, c, d))
else:
    print('%d-%d-%d-%d=7' % (a, b, c, d))

Submission status: Accepted

Solution code:
A, B, C, D = map(str, input())

for op1 in '+-':
    for op2 in '+-':
        for op3 in '+-':
            if eval(A+op1+B+op2+C+op3+D) == 7:
                print(A+op1+B+op2+C+op3+D+'=7')
                exit()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(num_str: str):
    N = len(num_str) - 1

    for bit in range(2 ** N):
        ans = num_str[0]
        for i in range(N):
            if bit & 1 << i:
                ans += "+"
            else:
                ans += "-"
            ans += num_str[i + 1]

        if eval(ans) == 7:
            print(ans + "=7")
            return

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    ABCD = next(tokens)
    solve(ABCD)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(ABCD: str):
    num_str = ABCD
    N = len(num_str) - 1

    for bit in range(2 ** N):
        tmp = num_str[0]
        for i in range(N):
            if bit & 1 << i:
                tmp += "+"
            else:
                tmp += "-"
            tmp += num_str[i + 1]
        res = eval(tmp)

        if res == 7:
            print(tmp + "=7")
            return

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    ABCD = next(tokens)
    solve(ABCD)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
li = []
s = input()
li2 = []
for i in range(4):
  li.append(int(s[i]))
n1 = 4
def func(n, t):
  if n == 1:
    if t == li[0]:
      return True
    else:
      return False
  if func(n-1, t+li[n-1]):
    li2.append('-')
    return True
  elif func(n-1, t-li[n-1]):
    li2.append('+')
    return True
  else:
    return False
if func(n1, 7):
  print("{0}{4}{1}{5}{2}{6}{3}=7".format(li[0], li[1], li[2], li[3], li2[0], li2[1], li2[2]))

Submission status: Accepted

Solution code:
s = input()
pat = []
for i in range(2**3):
    tmp = ['']
    for j in range(3):
        if i >> j & 1:
            tmp.append('+')
        else:
            tmp.append('-')
    pat.append(tmp)

for p in pat:
    tmp = ''
    for i, j in zip(p, s):
        tmp += i+j
    if eval(tmp) == 7:
        print(tmp + '=7')
        exit()

Submission status: Accepted

Solution code:
# 全パターンを書き並べるのは面倒
# bit全探索: https://qiita.com/gogotealove/items/11f9e83218926211083a をコピペ，加筆

n = input()
op_cnt = len(n) - 1  # すき間の個数
for i in range(2 ** op_cnt):
    op = ["-"] * op_cnt  # あらかじめ ["-", "-", "-"] というリストを作っておく
    for j in range(op_cnt):
        if ((i >> j) & 1): # ビットを右にシフトし，1だったら以下の処理を行う
            op[op_cnt - 1 - j] = "+"  # フラグが立っていた箇所を "+" で上書き

    formula = ""
    for p_n, p_o in zip(n, op + [""]):  # 少々モヤッとする
        formula += (p_n + p_o)
    if eval(formula) == 7: # eval: 文字列を式として評価してくれる
        print(formula + "=7")
        break

Submission status: Accepted

Solution code:
n = [int(x) for x in input()]
op_cnt = len(n)-1 #隙間の数
for i in range(2**op_cnt):
  op = ["-"] * op_cnt
  for j in range(op_cnt):
    if ((i>>j)&1):
      op[op_cnt-1-j] = "+"
  
  ans = ""
  for x, y in zip(n, op+[""]): #opの数はnよりひとつ少ないので数合わせしておく
    ans += str(x)+y
  if eval(ans)==7:
    print(ans+"=7")
    break

Submission status: Accepted

Solution code:
s = input()

def func(i, s_temp): #iは何回判断したかで，s_tempはそこまでの結果
    #s_tempには最初sの最初も文字を入れておく
    
    #ベースラインとして，３回判断したら，結果を確かめる
    if i == 3:
        #もし結果が７ならそれを表示する
        if eval(s_temp) == 7:
            print("{}=7".format(s_temp))
            exit()
    
    #そうでなければ，左からi+1番目の間に+を入れたものと-を入れたものを実行する
    else:
        func(i+1, s_temp+"+"+s[i+1])
        func(i+1, s_temp+"-"+s[i+1])

func(0, s[0])

Submission status: Accepted

Solution code:
s = input()

for i in range(8): #全部で2**3=8通りの入れ方がある。
    s_temp = s[0]
    for j in range(3): #調べるのは数と数の間３つ
        if i >> j & 1: #もし２進法で右からj+1番目が1なら
            s_temp += "+" + s[j+1] #左からj+1番目の間に+を入れて
        else: #そうでなければ
            s_temp += "-" + s[j+1] #そこに-を入れる
    #+と-を入れ終わったら計算結果をチェックして
    if eval(s_temp) == 7: #もし合っているなら
        ans = s_temp + "=7"
        print(ans) #答えを表示する
        exit()

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
import itertools


def sr(): return sys.stdin.readline().rstrip()


def ir(): return int(sr())


def lr(): return list(map(int, sr().split()))


S = sr()
ans = 0


def dfs(depth, p_list):
    global ans

    if depth == 4:
        sum_list = []
        T = S
        text = ''
        for j, p in enumerate(p_list):
            if p == 1:
                sum_list.append(int(T[:1]))
            elif p == -1:
                sum_list.append(int(T[:1]) * -1)

            if len(text) == 0:
                text += T[:1]
            else:
                if p == 1:
                    text += '+' + T[:1]
                elif p == -1:
                    text += '-' + T[:1]
            T = T[1:]
        # print(p_list, sum_list)
        ans = sum(sum_list)
        if ans == 7:
            text += '=7'
            print(text)
            exit()

        return

    for i in [1, -1]:
        p_list[depth] = i
        dfs(depth + 1, p_list)


def main():

    p_list = [0] * (4)
    dfs(0, p_list)


main()

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
opr = ["+++", "++-", "+--", "---", "-++", "--+", "-+-", "+-+"]

ABCD = list(input())

for op in opr:
	ans = []
	ABCD_ = ABCD.copy()
	op = list(op)
	ans.append(ABCD_.pop(0))
	while ABCD_ != []:
		ans.append(op.pop(0))
		ans.append(ABCD_.pop(0))
	if eval("".join(ans)) == 7:
		print("".join(ans), "=7", sep="")
		exit()

Submission status: Accepted

Solution code:
s = str(input())
l = list(s)

for i in range(1 << len(s)-1):
    li = ''
    
    for j in range(len(s)):
        li += l[j]
        if j == len(s)-1:
            continue
        if i & (1 << j):
            li += '+'
        else:
            li += '-'
#     print(li)
    r = eval(li)
    if r == 7:
        break
        
print(li+'=7')

Submission status: Accepted

Solution code:
def ans(i, j, k):
    if i == 3:
        if j == 7:
            print(k + '=7')
            exit()
        return
    ans(i+1, j-int(n[i+1]), k+'-'+n[i+1]), 
    ans(i+1, j+int(n[i+1]), k+'+'+n[i+1])

n = input()

ans(0, int(n[0]), n[0])

Submission status: Accepted

Solution code:
def ans(i, j, k):
    if i == 3:
        if j == 7:
            print(k + '=7')
            exit()
    else:      #return ではなぜかけない？
        ans(i+1, j-int(n[i+1]), k+'-'+n[i+1]),
        ans(i+1, j+int(n[i+1]), k+'+'+n[i+1])

n = input()

print(ans(0, int(n[0]), n[0]))

Submission status: Accepted

Solution code:
A, B, C, D = list(input())
for n in range(8):
    formula = "{}{}{}{}{}{}{}".format(A, ["+", "-"][n & 1], B, ["+", "-"][(n >> 1) & 1], C, ["+", "-"][(n >> 2) & 1], D)
    if eval(formula) == 7:
        print(formula + "=7")
        quit()

Submission status: Accepted

Solution code:
a,b,c,d = input()

for i in range(2**3):
    op = ["+","+","+"]
    for j in range(3):
        if ((i>>j)&1) == 1:
            op[j] = "-"
    ans = eval(a + op[0] + b + op[1] + c + op[2] +d)
    if ans == 7 :
        print("{}{}{}{}{}{}{}={}".format(a,op[0],b,op[1],c,op[2],d,ans))
        break

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log2
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
S = list(input())

ans = []

for i in range(1 << 3):
	i = list("{:b}".format(i).zfill(3))
	shiki = []
	S_ = S.copy()
	shiki.append(S_.pop(0))
	while S_ != []:
		if i.pop(0) == "0":
			shiki.append("+")
		else:
			shiki.append("-")

		shiki.append(S_.pop(0))

	if eval("".join(shiki)) == 7:
		print("".join(shiki) + "=7")
		exit()

Submission status: Accepted

Solution code:
num = [int(i) for i in input()]
a, b, c, d = num[0], num[1], num[2], num[3]

if a + b + c + d == 7:
    print('{}+{}+{}+{}=7'.format(a, b, c, d))
elif a + b + c - d == 7:
    print('{}+{}+{}-{}=7'.format(a, b, c, d))
elif a + b - c + d == 7:
    print('{}+{}-{}+{}=7'.format(a, b, c, d))
elif a + b - c - d == 7:
    print('{}+{}-{}-{}=7'.format(a, b, c, d))
elif a - b + c + d == 7:
    print('{}-{}+{}+{}=7'.format(a, b, c, d))
elif a - b + c - d == 7:
    print('{}-{}+{}-{}=7'.format(a, b, c, d))
elif a - b - c + d == 7:
    print('{}-{}-{}+{}=7'.format(a, b, c, d))
elif a - b - c - d == 7:
    print('{}-{}-{}-{}=7'.format(a, b, c, d))

Submission status: Accepted

Solution code:
import numpy as np

nyuryoku=input()

s=[int(i) for i in nyuryoku]

s_array=np.array(s)

if np.sum(s_array)==7:
    Ans=['+'+str(i) for i in s]
    Ans[0]=str(s[0])
    print(''.join(Ans)+'=7')
    
else:
    for i in range(3):
        if np.sum(s_array)-2*s_array[i+1]==7:
            Ans=['+'+str(i) for i in s]
            Ans[0]=str(s[0])
            Ans[i+1]='-'+str(s[i+1])
            print(''.join(Ans)+'=7')
            break
            
        elif np.sum(s_array)-2*s_array[1]-2*s_array[2]==7:
            Ans=['+'+str(i) for i in s]
            Ans[0]=str(s[0])
            Ans[1]='-'+str(s[1])
            Ans[2]='-'+str(s[2])
            print(''.join(Ans)+'=7')
            break
            
        elif np.sum(s_array)-2*s_array[2]-2*s_array[3]==7:
            Ans=['+'+str(i) for i in s]
            Ans[0]=str(s[0])
            Ans[2]='-'+str(s[2])
            Ans[3]='-'+str(s[3])
            print(''.join(Ans)+'=7')
            break
        
        elif np.sum(s_array)-2*s_array[1]-2*s_array[3]==7:
            Ans=['+'+str(i) for i in s]
            Ans[0]=str(s[0])
            Ans[1]='-'+str(s[1])
            Ans[3]='-'+str(s[3])
            print(''.join(Ans)+'=7')
            break
            
        elif np.sum(s_array)-2*s_array[1]-2*s_array[2]-2*s_array[3]==7:
            Ans=['+'+str(i) for i in s]
            Ans[0]=str(s[0])
            Ans[1]='-'+str(s[1])
            Ans[2]='-'+str(s[2])
            Ans[3]='-'+str(s[3])
            print(''.join(Ans)+'=7')
            break

Submission status: Accepted

Solution code:
a,b,c,d = map(int,list(input()))

if a+b+c+d==7:print('{}+{}+{}+{}=7'.format(a,b,c,d));exit()
if a-b+c+d==7:print('{}-{}+{}+{}=7'.format(a,b,c,d));exit()
if a+b-c+d==7:print('{}+{}-{}+{}=7'.format(a,b,c,d));exit()
if a+b+c-d==7:print('{}+{}+{}-{}=7'.format(a,b,c,d));exit()
if a-b-c+d==7:print('{}-{}-{}+{}=7'.format(a,b,c,d));exit()
if a-b+c-d==7:print('{}-{}+{}-{}=7'.format(a,b,c,d));exit()
if a+b-c-d==7:print('{}+{}-{}-{}=7'.format(a,b,c,d));exit()
if a-b-c-d==7:print('{}-{}-{}-{}=7'.format(a,b,c,d));exit()

Submission status: Accepted

Solution code:
A = input()
op_cnt = len(A) - 1
result = 0
for i in range(2 ** op_cnt):
    f = ""
    bag = []
    for j in range(op_cnt):
        if (i >> j) & 1:
            bag.append("+")
        else:
            bag.append("-")
    for p_n, p_o in zip(A, bag + [""]):
        f += (p_n + p_o)
    if eval(f) == 7:
        print(f+'=7')
        break

Submission status: Accepted

Solution code:
N=list(map(int,list(input())))

for n in range(2**3):
    sum_N=N[0]
    l=[]
    for i in range(3):
        if (n>>i)&1==1: #convert from decimal to binary and shift right
            sum_N+=N[i+1]
            l.append("+")
        else:
            sum_N-=N[i+1]
            l.append("-")
    if sum_N==7:
        break
print("{}{}{}{}{}{}{}".format(N[0],l[0],N[1],l[1],N[2],l[2],N[3])+"=7")

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions
import heapq

r = stdin.readline().rstrip()

for i in range(2 ** 3):
    op = ["-"] * 3
    for j in range(3):
        if ((i >> j) & 1):
            op[2 - j] = "+"
    ss = r[0] + op[0] + r[1] + op[1] + r[2] + op[2] + r[3]
    if eval(ss) == 7:
        print(ss + "=7")
        break

Submission status: Accepted

Solution code:
import sys

S = input()
for b_op in range(2):
    for c_op in range(2):
        for d_op in range(2):
            ans = S[0]
            total = int(S[0])
            if bool(b_op):
                total += int(S[1])
                ans += "+" + S[1]
            else:
                total -= int(S[1])
                ans += "-" + S[1]

            if bool(c_op):
                total += int(S[2])
                ans += "+" + S[2]
            else:
                total -= int(S[2])
                ans += "-" + S[2]

            if bool(d_op):
                total += int(S[3])
                ans += "+" + S[3]
            else:
                total -= int(S[3])
                ans += "-" + S[3]

            if total == 7:
                print(ans + "=7")
                sys.exit()

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(k) for k in input().split()]
def base10to(n, b):
    if (int(n/b)):
        return base10to(int(n/b), b) + str(n%b)
    return str(n%b)
#template
S = input()
for i in range(8):
    k = base10to(i,2)
    if len(k) < 3:
        for j in range(3-len(k)):
            k = '0'+k
    tmp = int(S[0])
    for j in range(1,4):
        if k[j-1] == '0':
            tmp += int(S[j])
        else:
            tmp -= int(S[j])
    if tmp == 7:
        li = []
        for t in range(4):
            if t == 3:
                li.append(S[t])
            else:
                li.append(S[t])
                if k[t] == '0':
                    li.append('+')
                else:
                    li.append('-')
        li += ['=','7']
        print(''.join(li))
        exit()

Submission status: Accepted

Solution code:
import itertools
import sys
import numpy as np
import collections
s=input()
a,b,c,d=s[0],s[1],s[2],s[3]
ia,ib,ic,id=int(a),int(b),int(c),int(d)
l=[ia,ib,ic,id]
if sum(l) == 7:
    print('{0}+{1}+{2}+{3}=7'.format(a,b,c,d))
    exit(0)
elif ia+ib+ic-id ==7:
    print('{0}+{1}+{2}-{3}=7'.format(a, b, c, d))
elif ia+ib-ic+id ==7:
    print('{0}+{1}-{2}+{3}=7'.format(a, b, c, d))
elif ia+ib-ic-id ==7:
    print('{0}+{1}-{2}-{3}=7'.format(a, b, c, d))
elif ia-ib+ic+id ==7:
    print('{0}-{1}+{2}+{3}=7'.format(a, b, c, d))
elif ia-ib+ic-id ==7:
    print('{0}-{1}+{2}-{3}=7'.format(a, b, c, d))
elif ia-ib-ic+id ==7:
    print('{0}-{1}-{2}+{3}=7'.format(a, b, c, d))
else:
    print('{0}-{1}-{2}-{3}=7'.format(a, b, c, d))

Submission status: Accepted

Solution code:

SUM = 7

def get_Operator(i):
    if i == 1: return "+"
    else: return "-"
def solve(S):
    n = [int(c) for c in S]
    operators = [1, -1]
    for i in operators:
        for j in operators:
            for k in operators:
                if(n[0] + i * n[1] + j * n[2] + k * n[3] == SUM):
                    foamula = (str(n[0]) + get_Operator(i) + str(n[1]) + get_Operator(j) + str(n[2]) + get_Operator(k) + str(n[3]) + "=7")
                    return foamula
def main():
    S = input()
    print(solve(S))
    
main()

Submission status: Accepted

Solution code:
S = input()
A,B,C,D = [S[i] for i in range(4)]
signal = ['+', '-']
flag = False
for op1 in signal:
    for op2 in signal:
        for op3 in signal:
            formula = '{}{}{}{}{}{}{}'.format(A,op1,B,op2,C,op3,D)
            if eval(formula) == 7:
                print(formula+'=7')
                flag = True
                break
        if flag:
            break
    if flag:
        break

Submission status: Accepted

Solution code:
ref_num = list(input())

for i in range(2**3): # 2**len(ref_num)
    fomula = ref_num[0]
    for j in range(3):
        if i >> j & 1:
            fomula += "+" + ref_num[j+1]
        else:
            fomula += "-" + ref_num[j+1]
    if eval(fomula) == 7:
        print(fomula+"=7")
        break

Submission status: Accepted

Solution code:
A = [int(i) for i in list(input())]
n = 3
for i in range(1 << n):
    a, b, c, d = A
    o = ["", "", ""]
    p = [1, 1, 1]
    ans = 0
    for j in range(n):
        if i >> j & 1:
            o[j] = "+"
            p[j] = 1
        else:
            o[j] = "-"
            p[j] = -1

    if a + (b*p[0]) + (c*p[1]) + (d*p[2]) == 7:
        print("{}{}{}{}{}{}{}=7".format(a, o[0], b, o[1], c, o[2], d))
        break

Submission status: Accepted

Solution code:
a,b,c,d = input()
for i in range(2**3):
  ls = ["+","+","+"]
  for j in range(len(ls)):
    if (i>>j) & 1:
      ls[j]="-"
  if eval(a+ls[0]+b+ls[1]+c+ls[2]+d) == 7:
        print(a+ls[0]+b+ls[1]+c+ls[2]+d+"=7")
        break