hakr14's implementation runs on the test faster than 深紅心's implementation, so I just decided to add some comments and more tests to that version.
def dict_sort(dictionary): return sorted(dictionary, key=dictionary.__getitem__) def convert_to_base(decimal_number, base, digits): if decimal_number == 0: return '' return digits[decimal_number % base] + convert_to_base(decimal_number // base, base, digits) def base_to_dec(string, base, digits): if string == '': return 0 return digits.index(string[0]) + base_to_dec(string[1:], base, digits) * base class converter: def __init__(self, codec=None): if codec == None: self.codec = '' else: self.codec = codec def fit(self, strings): chars = {} # Dictionary of characters and how many times they are used if type(strings) == list: string = '\n'.join(strings) else: string = strings # Find and count characters for x in string: if x not in chars: chars[x] = string.count(x) if type(strings) == list: chars['\n'] = chars['\n'] - len(strings) + 1 # Correct \n count self.codec = ''.join(dict_sort(chars)) def stoi(self, string): return base_to_dec(string, len(self.codec), self.codec) def itos(self, number): return convert_to_base(number, len(self.codec), self.codec)
- def dict_sort(dictionary):
- return sorted(dictionary, key=dictionary.__getitem__)
- def convert_to_base(decimal_number, base, digits):
- if decimal_number == 0:
- return ''
- return digits[decimal_number % base] + convert_to_base(decimal_number // base, base, digits)
- def base_to_dec(string, base, digits):
- if string == '':
- return 0
- return digits.index(string[0]) + base_to_dec(string[1:], base, digits) * base
- class converter:
- def __init__(self, codec=None):
- if codec == None:
- self.codec = ''
- else:
- self.codec = codec
- def fit(self, strings):
chars = {}- chars = {} # Dictionary of characters and how many times they are used
- if type(strings) == list:
- string = '\n'.join(strings)
- else:
- string = strings
- # Find and count characters
- for x in string:
- if x not in chars:
- chars[x] = string.count(x)
- if type(strings) == list:
- chars['\n'] = chars['\n'] - len(strings) + 1 # Correct \n count
- self.codec = ''.join(dict_sort(chars))
- def stoi(self, string):
- return base_to_dec(string, len(self.codec), self.codec)
- def itos(self, number):
- return convert_to_base(number, len(self.codec), self.codec)
import codewars_test as test import solution @test.describe("Tests") def test_group(): test_texts = ['Hello World!','I\'m a program!','Some testing text','spam','more spam'] converter_var = solution.converter() converter_var.fit(test_texts) for x in test_texts: test.assert_equals(converter_var.itos(converter_var.stoi(x)), x)
- import codewars_test as test
# TODO Write testsimport solution # or from solution import example- import solution
# test.assert_equals(actual, expected, [optional] message)@test.describe("Basic tests")- @test.describe("Tests")
- def test_group():
converter_var = converter()converter_var.fit('Hello World! I am a program!')test.assert_equals(converter_var.itos(converter_var.stoi('I am a program!')), 'I am a program!')- test_texts = ['Hello World!','I\'m a program!','Some testing text','spam','more spam']
- converter_var = solution.converter()
- converter_var.fit(test_texts)
- for x in test_texts:
- test.assert_equals(converter_var.itos(converter_var.stoi(x)), x)
A Python class that converts strings to integers and back.
itos: int-to-string
stoi: string-to-int
Note: Currently broken
def dict_sort(dictionary):
return sorted(dictionary, key=dictionary.__getitem__)
def convert_to_base(decimal_number, base, digits):
remainder_stack = []
while decimal_number > 0:
remainder = decimal_number % base
remainder_stack.append(remainder)
decimal_number = decimal_number // base
new_digits = []
while remainder_stack:
new_digits.append(digits[remainder_stack.pop()])
return ''.join(new_digits)
def base_to_dec(string, base, digits):
num_str = string[::-1]
num = 0
for k in range(len(num_str)):
dig = num_str[k]
if dig.isdigit():
dig = int(dig)
else:
dig = digits.index(dig.lower())-digits.index('a')+10
num += dig*(base**k)
return int(num)
class converter:
def __init__(self, codec=None):
if codec == None:
self.codec = ''
else:
self.codec = codec
def fit(self, strings):
chars = {}
if type(strings) == list:
string = '\n'.join(strings)
else:
string = strings
for x in string:
if x not in chars:
chars[x] = string.count(x)
self.codec = ''.join(dict_sort(chars))
def stoi(self, string):
return base_to_dec(string, len(self.codec), self.codec)
def itos(self, number):
return convert_to_base(number, len(self.codec), self.codec)
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Basic tests")
def test_group():
converter_var = converter()
converter_var.fit('Hello World! I am a program!')
test.assert_equals(converter_var.itos(converter_var.stoi('I am a program!')))
It will get a dictionary and return a list of the keys, all sorted by their corrosponding values in increasing order.
def dict_index(dictionary, value):
dict_keys = list(dictionary)
dict_values = list(dictionary.values())
return dict_keys[dict_values.index(value)]
def dict_sort(dictionary):
dict_values = list(dictionary.values())
new_keys = []
for x in range(len(dictionary)):
max_value = max(dict_values)
dict_values.remove(max_value)
new_keys.append(dict_index(dictionary, max_value))
new_keys.reverse()
return new_keys
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(dict_sort({'a':1,'b':2,'c':-1}), ['c','a','b'])
test.assert_equals(dict_sort({'dog':25,'rat':-7}), ['rat','dog'])