Created
December 12, 2015 01:49
-
-
Save anirudhjayaraman/478128372ff1baeb70eb to your computer and use it in GitHub Desktop.
Solution to HackerRank Problem - Sherlock and the Beast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
if this code not work, then put
k=n//2