Sam and substrings Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'substrings' function below.## The function is expected to return an INTEGER.# The function accepts STRING n as parameter.#defsubstrings(n):# Write your code heremodulo=int(1e9+7)result=sub_sum=int(n[0])fordigit_idxinrange(1,len(n)):digit=int(n[digit_idx])digit_presence=digit_idx+1sub_sum=(sub_sum*10+digit*digit_presence)%moduloresult=(result+sub_sum)%moduloreturnresultif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')n=input()result=substrings(n)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sam and substrings
You are viewing a single comment's thread. Return to all comments →
Solution in Python3: