Skip to content

Instantly share code, notes, and snippets.

@iamjosephmj
Last active January 18, 2021 17:26
Show Gist options
  • Save iamjosephmj/11fab084d9bcc13a2c6a597b720756c6 to your computer and use it in GitHub Desktop.
Save iamjosephmj/11fab084d9bcc13a2c6a597b720756c6 to your computer and use it in GitHub Desktop.
1.) Problem solving
def main():
# input
# stores
alphabets, num = str(input()), input()
if not alphabets.isalpha():
raise Exception("Expected only alphabets")
else:
alphabets = alphabets.lower()
if not num.isnumeric():
raise Exception("Expected an interger")
num = int(num)
# Complexity O(n)
for item in alphabets:
'''
The logic involves 2 steps
*) scale down to 0 - 26,by sub 97
*) add the number to the alphabet(character)
*) scale the sum between 0 - 26 by using %
*) up convert to lowercase by adding 97
it does have sub + add + modules operations which are the most fastest
operations in a programming language, the time for this is very negligible. This
makes us to only consider the complexity part, which i believe is minimum for this particular program.
'''
print(chr(((((ord(item) - 97) + num) % 26) + 97)), end="")
if __name__ == "__main__":
main()
'''
QA:
1) algorithm + program is mentioned above + complexity is O(n)
2) I don't really understand the 2nd questions(I had used the ascii method to solve the problem :) ).
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment