Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solution to HackerRank Problem - Sherlock and the Beast
#!/bin/python
import sys
def combinations_sum(n):
k = n/2
combs = []
answers = []
for i in range(0,k+1):
a,b = i, n-i
t = a,b
if a == 0:
if b%3 == 0:
answers.append('5'*b)
elif b%5 == 0:
answers.append('3'*b)
elif a%3 == 0:
if b%5 == 0:
answers.append('5'*a + '3'*b)
elif a%5 == 0:
if b%3 == 0:
answers.append('5'*b + '3'*a)
answer_list = []
if len(answers) == 0:
return -1
else:
for el in answers:
answer_list.append(int(el))
return max(answer_list)
def comb_sum(n):
y = n
while y%3 != 0:
y -= 5
if y%3 == 0 and (n-y)%5 != 0:
y -= 5
return int(y*'5'+(n-y)*'3')
t = int(raw_input().strip())
for a0 in xrange(t):
n = int(raw_input().strip())
if n < 30:
print combinations_sum(n)
else:
print comb_sum(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.