Ad
Code
Diff
  • helloWorld=n=>'Hello '+n
    • helloWorld=n=>`Hello ${n}`
    • helloWorld=n=>'Hello '+n
Fundamentals
Strings
Data Types
Algorithms
Logic
Code
Diff
  • palindrome=lambda w:w[::-1]==w
    
    • palindrome = lambda w:w[::-1] == w
    • palindrome=lambda w:w[::-1]==w

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.

Code
Diff
  • 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)

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)

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