if len(color) == 7: return "6-digit" elif len(color) == 4: return "3-digit" else: if color.lower() in PRESET_COLORS: return "preset" return "ERROR" def convert6digitHex(color): r = int(color[1], 16) * 16 + int(color[2], 16) g = int(color[3], 16) * 16 + int(color[4], 16) b = int(color[5], 16) * 16 + int(color[6], 16) return (r, g, b) def convert3digitHex(color): r = int(color[1], 16) * 16 + int(color[1], 16) g = int(color[2], 16) * 16 + int(color[2], 16) b = int(color[3], 16) * 16 + int(color[3], 16) return (r, g, b) # TESTS test.describe('Example tests') test.assert_equals(parse_html_color('#80FFA0'), {'r': 128, 'g': 255, 'b': 160}) test.assert_equals(parse_html_color('#3B7'), {'r': 51, 'g': 187, 'b': 119}) test.assert_equals(parse_html_color('LimeGreen'), {'r': 50, 'g': 205, 'b': 50})
import test # https://www.codewars.com/kata/find-the-odd-int/train/python # Given an array, find the int that appears an odd number of times. # There will always be only one integer that appears an odd number of times. def find_it(seq): occurences = {} for i in seq: if i in occurences: occurences[i] += 1 else: occurences[i] = 1 for number, occurs in occurences.items(): if occurs % 2 == 1: return number return None if __name__ == '__main__': print('tohle se stane když to spustíš') test.describe("Example") test.assert_equals( find_it([20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]), 5) print(find_it([20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]))
cnt_dict[n]+=1 else: cnt_dict[n]=1 #print(cnt_dict) rvt_dict = {} for n, f in cnt_dict.items(): if f in rvt_dict: rvt_dict[f].append(n) else: rvt_dict[f]=[n] #print(rvt_dict) ret.append(len(cnt_dict)) if 1 in rvt_dict.keys(): ret.append(len(rvt_dict[1])) else: ret.append(0) most_f = sorted(rvt_dict.keys(), reverse=True)[0] num_lst = rvt_dict[most_f] item_four=[sorted(num_lst)] item_four.append(most_f) ret.append(item_four) return ret import test test.describe("Example Tests") test.assert_equals(count_sel([-3, -2, -1, 3, 4, -5, -5, 5, -1, -5]), [10, 7, 5, [[-5], 3]]) test.assert_equals(count_sel([5, -1, 1, -1, -2, 5, 0, -2, -5, 3]), [10, 7, 4, [[-2, -1, 5], 2]]) test.assert_equals(count_sel([-2, 4, 4, -2, -2, -1, 3, 5, -5, 5]), [10, 6, 3, [[-2], 3]]) test.assert_equals(count_sel([4, -5, 1, -5, 2, 4, -1, 4, -1, 1]), [10, 5, 1, [[4], 3]]) test.assert_equals(count_sel([4, 4, 2, -3, 1, 4, 3, 2, 0, -5, 2, -2, -2, -5]), [14, 8, 4, [[2, 4], 3]])
import test scores = {"teamA":3,"teamB":3,"teamC":3,"teamD":3} def league_calculate(team1, team2, result): if result=="draw": scores[team1]+=1 scores[team2]+=1 elif result=="win": scores[team1]+=3 result={} for team,score in scores.items(): if score in result.keys(): result[score].append(team) else: result[score]=[team] return [[team,score] for score in sorted(result.keys(), reverse=True) for team in sorted(result[score])] test.describe("Basic tests") test.assert_equals(league_calculate("teamA","teamB","draw"), [["teamA",4],["teamB",4],["teamC",3],["teamD",3]]) test.assert_equals(league_calculate("teamC","teamD","win"), [["teamC",6],["teamA",4],["teamB",4],["teamD",3]]) test.assert_equals(league_calculate("teamA","teamC","draw"), [["teamC",7],["teamA",5],["teamB",4],["teamD",3]]) test.assert_equals(league_calculate("teamB","teamD","win"), [["teamB",7],["teamC",7],["teamA",5],["teamD",3]]) test.assert_equals(league_calculate("teamA","teamB","win"), [["teamA",8],["teamB",7],["teamC",7],["teamD",3]]) test.assert_equals(league_calculate("teamC","teamD","draw"), [["teamA",8],["teamC",8],["teamB",7],["teamD",4]]) test.assert_equals(league_calculate("teamD","teamA","draw"), [["teamA",9],["teamC",8],["teamB",7],["teamD",5]]) test.assert_equals(league_calculate("teamC","teamB","win"), [["teamC",11],["teamA",9],["teamB",7],["teamD",5]]) test.assert_equals(league_calculate("teamB","teamD","win"), [["teamC",11],["teamB",10],["teamA",9],["teamD",5]]) test.assert_equals(league_calculate("teamA","teamB","draw"), [["teamB",11],["teamC",11],["teamA",10],["teamD",5]])
return [1,2,1] else: up = pascal_line(n-1) new = [up[i]+up[i+1] for i in range(len(up)-1)] return [1]+new+[1] #print(pascal_line(0)) #print(pascal_line(1)) #print(pascal_line(2)) #print(pascal_line(3)) #print(pascal_line(4)) #print(pascal_line(5)) def easyline(n): line = pascal_line(n) ret = 0 for n in line: ret+=n*n return ret def testing(actual, expected): test.assert_equals(actual, expected) test.describe("easyline") test.it("Basic tests") testing(easyline(7), 3432) testing(easyline(13), 10400600) testing(easyline(17), 2333606220) testing(easyline(19), 35345263800)
first_band = color_list[tens] else: third_band = "violet" second_band = color_list[tens] first_band = color_list[hundreds] fourth_band = "gold" out_string = first_band + " " + second_band + " " + third_band + " " + fourth_band return out_string # TESTS test.describe("Basic tests") test.it("Some common resistor values") test.assert_equals(encode_resistor_colors("10 ohms"), "brown black black gold") test.assert_equals(encode_resistor_colors("47 ohms"), "yellow violet black gold") test.assert_equals(encode_resistor_colors("100 ohms"), "brown black brown gold") test.assert_equals(encode_resistor_colors("220 ohms"), "red red brown gold") test.assert_equals(encode_resistor_colors("330 ohms"), "orange orange brown gold") test.assert_equals(encode_resistor_colors("470 ohms"), "yellow violet brown gold") test.assert_equals(encode_resistor_colors("680 ohms"), "blue gray brown gold") test.assert_equals(encode_resistor_colors("1k ohms"), "brown black red gold") test.assert_equals(encode_resistor_colors("4.7k ohms"), "yellow violet red gold")
return "NaR" r = "" roman = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] romanN = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] frac = ["", ".", ":", ":.", "::", ":.:", "S", "S.", "S:", "S:.", "S::", "S:.:"] if number == 0: if fraction == 0: return "N" return frac[fraction] while number > 0: for i in range(13): if romanN[i] <= number: r += roman[i] number -= romanN[i] break return r+frac[fraction] import test test.describe("Examples") test.assert_equals(roman_fractions(-12), "NaR") test.assert_equals(roman_fractions(0, -1), "NaR") test.assert_equals(roman_fractions(0, 12), "NaR") test.assert_equals(roman_fractions(0), "N") test.assert_equals(roman_fractions(1), "I") test.assert_equals(roman_fractions(1, 5), "I:.:") test.assert_equals(roman_fractions(1, 9), "IS:.") test.assert_equals(roman_fractions(1632, 2), "MDCXXXII:") test.assert_equals(roman_fractions(5000), "MMMMM") test.assert_equals(roman_fractions(5001), "NaR")
# Multirange iterator def multiiter(*params): print(params) if 0 in params: for i in range(0): yield (i, ) elif len(params) == 1: for i in range(params[0]): yield (i, ) else: for i in range(params[0]): for j in multiiter(*params[1:]): yield (i, *j) import test test.describe("One parameter tests") test.assert_equals(list(multiiter(0)), []) test.assert_equals(list(multiiter(2)), [(0,), (1,)]) test.describe("Two parameter tests") test.assert_equals(list(multiiter(2, 3)), [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]) test.assert_equals(list(multiiter(3, 2)), [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)])
sum+=value else: for value in values: if check(value, criteria): sum+=value return sum def average_if(values,criteria): sum = sum_if(values,criteria) count = count_if(values,criteria) if sum%count==0: return sum/count else: float(sum)/count test.describe('count_if tests') test.it('Criteria is exact match') test.assert_equals(count_if([1,3,5,3],3),2) test.assert_equals(count_if(['John','Steve','Rachel','Rebecca','John','John'],'John'),3) test.it('Criteria is ">="') test.assert_equals(count_if([1,3,5,3],'>=3'),3) test.it('Criteria is "<="') test.assert_equals(count_if([1.5,3,5,3,0,-1,-5],'<=1.5'),4) test.it('Criteria is ">"') test.assert_equals(count_if([1,3,5,3.5],'>3'),2) test.it('Criteria is "<"') test.assert_equals(count_if([1,3,5,3,0,-1,-5],'<1'),3)
print(" ({})".format(var_and_degree(diff(s)))) quit() import sys sys.path.insert(0, './codewars') import test def testThis(config, n=1, val=0): for msg, expected, inp in config: msg, expected, inp = (s.format(val) for s in (msg, expected, inp)) for _ in range(n - 1): inp = diff(inp) test.assert_equals(diff(inp), expected, msg) test.describe("Sample tests") config = [("constant should return 0", "0", "5"), ("x should return 1", "1", "x"), ("x+x should return 2", "2", "(+ x x)"), ("x-x should return 0", "0", "(- x x)"), ("2*x should return 2", "2", "(* x 2)"), ("x/2 should return 0.5", "0.5", "(/ x 2)"), ("x^2 should return 2*x", "(* 2 x)", "(^ x 2)"), ("cos(x) should return -1 * sin(x)", "(* -1 (sin x))", "(cos x)"), ("sin(x) should return cos(x)", "(cos x)", "(sin x)"), ("tan(x) should return 1 + tan(x)^2", "(+ 1 (^ (tan x) 2))", "(tan x)"), ("exp(x) should return exp(x)", "(exp x)", "(exp x)"), ("ln(x) should return 1/x", "(/ 1 x)", "(ln x)")]
# Python 3.4 # Check DESCRIPTION.md for the problem's full description. import test def interweave(s1, s2): if len(s1 + s2) % 2 != 0: s2 += " " message = "" for first, second in zip(s1, s2): message += (first if not first.isdigit() else "") + (second if not second.isdigit() else "") return message.strip(" ") # Tests test.describe("Tests") msg1 = "hello" msg2 = "hello world" test.assert_equals(interweave("hlo", "el"), msg1, "['hlo', 'el'] should equal 'hello': ") test.assert_equals(interweave("hlowrd", "el ol"), msg2, "['hlowrd', 'el ol'] should equal 'hello world': ")
# Python 3.4 # Check DESCRIPTION.md for the problem's full description. import test def filter_lucky(lst): return [lucky_number for lucky_number in lst if "7" in str(lucky_number)] test.describe("Correct tests") test.it("First test") l = [1, 2, 4, 6, 7, 12, 30, 50, 60, 65, 70, 68, 69, 77, 80] test.assert_equals([7, 70, 77], filter_lucky(l)) test.it("Second test") l = [1, 2, 3, 4, 5, 6, 7, 17, 27, 50] test.assert_equals([7, 17, 27], filter_lucky(l))