Last active
March 3, 2018 02:41
-
-
Save ShaneRich5/ad2fe9e6fa5c8c41e5937cf422e9889a to your computer and use it in GitHub Desktop.
Code Wars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a function that takes an (unsigned) integer as input, and returns the number of bits that are equal to one in the binary representation of that number. | |
# Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case | |
def countBits(n): | |
binary = bin(n)[2:] | |
return len(filter(lambda x: x == '1', binary)) | |
# Alternative solution | |
# def countBits(n): | |
# return bin(n).count("1") | |
# test.assert_equals(countBits(0), 0); | |
# test.assert_equals(countBits(4), 1); | |
# test.assert_equals(countBits(7), 3); | |
# test.assert_equals(countBits(9), 2); | |
# test.assert_equals(countBits(10), 2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Count the number of Duplicates | |
# Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits. | |
# Example | |
# "abcde" -> 0 # no characters repeats more than once | |
# "aabbcde" -> 2 # 'a' and 'b' | |
# "aabBcde" -> 2 # 'a' occurs twice and 'b' twice (bandB) | |
# "indivisibility" -> 1 # 'i' occurs six times | |
# "Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice | |
# "aA11" -> 2 # 'a' and '1' | |
# "ABBA" -> 2 # 'A' and 'B' each occur twice | |
def duplicate_count(text): | |
letter_count = {} | |
for letter in text.lower(): | |
if letter in letter_count: | |
letter_count[letter] += 1 | |
else: | |
letter_count[letter] = 1 | |
return len(filter(lambda x: x > 1, letter_count.values())) | |
# # Alternate solution | |
# def duplicate_count(s): | |
# return len([c for c in set(s.lower()) if s.lower().count(c)>1]) | |
# test.assert_equals(duplicate_count("abcde"), 0) | |
# test.assert_equals(duplicate_count("abcdea"), 1) | |
# test.assert_equals(duplicate_count("indivisibility"), 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This kata is to practice simple string output. Jamie is a programmer, and James' girlfriend. She likes diamonds, and wants a diamond string from James. Since James doesn't know how to make this happen, he needs your help. | |
# ###Task: | |
# You need to return a string that displays a diamond shape on the screen using asterisk ("*") characters. Please see provided test cases for exact output format. | |
# The shape that will be returned from print method resembles a diamond, where the number provided as input represents the number of *’s printed on the middle line. The line above and below will be centered and will have 2 less *’s than the middle line. This reduction by 2 *’s for each line continues until a line with a single * is printed at the top and bottom of the figure. | |
# Return null if input is even number or negative (as it is not possible to print diamond with even number or negative number). | |
# Please see provided test case(s) for examples. | |
# Python Note | |
# Since print is a reserved word in Python, Python students must implement the diamond(n) method instead, and return None for invalid input. | |
# JS Note | |
# JS students, like Python ones, must implement the diamond(n) method, and return null for invalid input. | |
def diamond(n): | |
if n % 2 == 0 or n < 0: | |
return None | |
result = [] | |
for index, i in enumerate(range(1, n + 1)): | |
stars = index + i | |
if stars > n: | |
stars = n - (stars % n) | |
spaces = ' ' * ((n - stars) / 2) | |
result.append(spaces + '*' * stars + '\n') | |
return ''.join(result) | |
# test.assert_equals(diamond(1), "*\n") | |
# expected = " *\n" | |
# expected += "***\n" | |
# expected += " *\n" | |
# test.assert_equals(diamond(3), expected) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number. | |
# ! Keep in mind that your task is to help Bob solve a real IQ test, which means indexes of the elements start from 1 (not 0) | |
# ##Examples : | |
# iq_test("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even | |
# iq_test("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd | |
def iq_test(numbers): | |
numbers = map(int, numbers.split()) | |
is_list_even = len(filter(lambda x: x % 2 == 0, numbers)) > 1 | |
for index, num in enumerate(numbers): | |
if (is_list_even and num % 2 != 0) or (not is_list_even and num % 2 == 0): | |
return index + 1 | |
return 0 | |
# Top solution | |
# def iq_test(numbers): | |
# e = [int(i) % 2 == 0 for i in numbers.split()] | |
# return e.index(True) + 1 if e.count(True) == 1 else e.index(False) + 1 | |
# Test.assert_equals(iq_test("2 4 7 8 10"),3) | |
# Test.assert_equals(iq_test("1 2 2"), 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case. | |
# is_isogram("Dermatoglyphics" ) == true | |
# is_isogram("aba" ) == false | |
# is_isogram("moOse" ) == false # -- ignore letter case | |
def is_isogram(string): | |
present = [] | |
for i in string: | |
letter = i.lower() | |
if letter in present: | |
return False | |
present.append(letter) | |
return True | |
# # Another solution | |
# def is_isogram(string): | |
# return len(string) == len(set(string.lower())) | |
# Test.assert_equals(is_isogram("Dermatoglyphics"), True ) | |
# Test.assert_equals(is_isogram("isogram"), True ) | |
# Test.assert_equals(is_isogram("aba"), False, "same chars may not be adjacent" ) | |
# Test.assert_equals(is_isogram("moOse"), False, "same chars may not be same case" ) | |
# Test.assert_equals(is_isogram("isIsogram"), False ) | |
# Test.assert_equals(is_isogram(""), True, "an empty string is a valid isogram" ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This time no story, no theory. The examples below show you how to write function accum: | |
# Examples: | |
# accum("abcd") # "A-Bb-Ccc-Dddd" | |
# accum("RqaEzty") # "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy" | |
# accum("cwAt") # "C-Ww-Aaa-Tttt" | |
# The parameter of accum is a string which includes only letters from a..z and A..Z. | |
def accum(s): | |
result = [] | |
for index, letter in enumerate(s): | |
string = letter.upper() | |
string += letter.lower() * index | |
result.append(string) | |
return '-'.join(result) | |
# Simplier solution | |
# def accum(s): | |
# return '-'.join(c.upper() + c.lower() * i for i, c in enumerate(s)) | |
Test.describe("accum") | |
Test.it("Basic tests") | |
Test.assert_equals(accum("ZpglnRxqenU"), "Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu") | |
Test.assert_equals(accum("NyffsGeyylB"), "N-Yy-Fff-Ffff-Sssss-Gggggg-Eeeeeee-Yyyyyyyy-Yyyyyyyyy-Llllllllll-Bbbbbbbbbbb") | |
Test.assert_equals(accum("MjtkuBovqrU"), "M-Jj-Ttt-Kkkk-Uuuuu-Bbbbbb-Ooooooo-Vvvvvvvv-Qqqqqqqqq-Rrrrrrrrrr-Uuuuuuuuuuu") | |
Test.assert_equals(accum("EvidjUnokmM"), "E-Vv-Iii-Dddd-Jjjjj-Uuuuuu-Nnnnnnn-Oooooooo-Kkkkkkkkk-Mmmmmmmmmm-Mmmmmmmmmmm") | |
Test.assert_equals(accum("HbideVbxncC"), "H-Bb-Iii-Dddd-Eeeee-Vvvvvv-Bbbbbbb-Xxxxxxxx-Nnnnnnnnn-Cccccccccc-Ccccccccccc") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment