Python 3 - My code gets timed out for higher conditions can anyone help me optimise it | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python 3 - My code gets timed out for higher conditions can anyone help me optimise it

#!/bin/python3 import math import os import random import re import sys mat = [] a = 0 c =0 # Complete the cipher function below. def cipher(n,k, s): global a global c global mat mat.append(s[0]) for i in range(1,n): if i < k: a = 0 for j in range (i): a += int(mat[j],2) e = bin(a) b = e[-1] if s[i] == b: mat.append("0") else: mat.append("1") else: c = 0 q = i-k+1 for q in range(i-k+1,i): c += (int(mat[q],2)) f = bin(c) d = f[-1] if s[i]== d: mat.append("0") else: mat.append("1") return "".join(mat) if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') nk = input().split() n = int(nk[0]) k = int(nk[1]) s = input() result = cipher(n,k, s) fptr.write(result + '\n') fptr.close()

29th Oct 2018, 6:05 PM
Muhammadh Juhaif Syed Abubacker
Muhammadh Juhaif Syed Abubacker - avatar
1 Answer
+ 2
Is this a homework assignment? At the very least you should create a CodePlayground code so that we can test it. When you do this you may have to fix some whitespace errors (sometimes these are introduced by copy+pasting). ~ Just delete any syntax errors pointing to spaces--they're HTML spaces, not code spaces--and then put the spaces back normally. Note: The os.environ variable you're querying does not exist on SoloLearn. Also, what inputs does it take? The inputs I tried as guesses just made it crash, not time out.
29th Oct 2018, 6:21 PM
Kirk Schafer
Kirk Schafer - avatar