Skip to content

Instantly share code, notes, and snippets.

@JanneSalokoski
Created March 23, 2015 13:47
Show Gist options
  • Save JanneSalokoski/3ac0b9f3f2a33e05457a to your computer and use it in GitHub Desktop.
Save JanneSalokoski/3ac0b9f3f2a33e05457a to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import re
def congruence(a, b, n, **kwargs):
if kwargs["verbose"] != None:
if kwargs["verbose"] == True:
verbose = True
else:
verbose = False
if verbose == True:
print("a = {}\nb = {}\nn = {}".format(a, b, n))
print("({} - {}) (mod {}) = {}\n".format(a, b, n, (a-b) % n))
if (a-b) % n == 0:
print("{} ≡ {} (mod {}) = 0".format(a, b, n))
print("{} and {} are congruent (mod {})".format(a, b, n))
else:
print("{} ≡ {} (mod {}) = {}".format(a, b, n, (a-b) % n))
print("{} and {} are not congruent (mod {})".format(a, b, n))
def main():
try:
while True:
u_input = input("> ")
u_input = re.sub("\s", "", u_input)
#print(u_input)
if re.findall("--help", u_input) or re.findall("-h", u_input):
print("Usage: [flags] a, b, n")
print("\t-h --help: Display this help.")
print("\t-v --verbose: Display more data.")
print("\t-q --quit: Quit the program.")
continue
elif re.findall("--quit", u_input) or re.findall("-q", u_input):
break
variables = re.findall("(\d*?),", u_input)
variables.append(re.findall(".*,(\d*?)$", u_input)[0])
#for item in variables:
# print(variables)
a = int(variables[0])
b = int(variables[1])
n = int(variables[2])
if (re.findall("--verbose", u_input) or
re.findall("-v", u_input)):
congruence(a, b, n, verbose=True)
else:
congruence(a, b, n, verbose=False)
except Exception as e:
print("Unexpected exception:\n\t\"{}\"".format(e))
main()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment