Skip to content

Instantly share code, notes, and snippets.

@towkir
Last active October 28, 2016 16:36
Show Gist options
  • Save towkir/7b779880fd07663cd8f75e1ffd166dea to your computer and use it in GitHub Desktop.
Save towkir/7b779880fd07663cd8f75e1ffd166dea to your computer and use it in GitHub Desktop.
Takes a list of number separated by comma as input, sorts the order of numbers, calculates their frequency and returns a sorted list of the result.
#!/usr/bin/python
print("This is a calculator that takes a series of numbers separated by comma,")
print("calculates their frequency and returns the result in a sorted method.")
print("Press Ctrl+C to exit the calculator")
try:
while True:
print("\nEnter your series of numbers separated by a 'comma':")
numbers = input()
numbers = numbers.split(",")
result = {}
for a in numbers:
try:
int(a)
except ValueError:
print("Please enter numbers only and separate them by comma.")
break
numbers = [int(a) for a in numbers]
numbers = sorted(numbers)
counter = 0
print("Here is a sorted list of numbers and their frequency:")
for a in numbers:
try:
result[a]
except KeyError:
for b in numbers:
if b == a:
counter += 1
result[a] = counter
counter = 0
else:
continue
for x, y in sorted(result.items()):
print (str(x) + " = " + str(y))
except KeyboardInterrupt:
print("You have exited the 'frequency calculator'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment