Skip to content
CodeWars - my code vs best solutions
  • AI Chat
  • Code
  • Report
  • Spinner

    8 KYU

    KATA - Convert a Number to a String!

    We need a function that can transform a number (integer) into a string. What ways of achieving this do you know?

    Examples (input --> output): 123 --> "123" 999 --> "999" -100 --> "-100"

    def number_to_string(num):
        return str(num)
    
    x = number_to_string(12939)
    print(type(x))

    KATA - Reversed Strings

    Complete the solution so that it reverses the string passed into it.

    'world' => 'dlrow' 'word' => 'drow'

    def solution(string):
        return ''.join(reversed(string))
    
    def solution2(string):
        return string[::-1]
    
    print(solution("Hello world"))
    print(solution2("Hello world"))

    KATA - Determine offspring sex based on genes XX and XY chromosomes

    The male gametes or sperm cells in humans and other mammals are heterogametic and contain one of two types of sex chromosomes. They are either X or Y. The female gametes or eggs however, contain only the X sex chromosome and are homogametic.

    The sperm cell determines the sex of an individual in this case. If a sperm cell containing an X chromosome fertilizes an egg, the resulting zygote will be XX or female. If the sperm cell contains a Y chromosome, then the resulting zygote will be XY or male.

    Determine if the sex of the offspring will be male or female based on the X or Y chromosome present in the male's sperm.

    If the sperm contains the X chromosome, return "Congratulations! You're going to have a daughter."; If the sperm contains the Y chromosome, return "Congratulations! You're going to have a son."

    def chromosome_check(sperm):
        if sperm == 'XX':
            x = "Congratulations! You're going to have a daughter."
        elif sperm == "XY":
            x = "Congratulations! You're going to have a son."
        return x
    
    def chromosome_check2(sperm):
        gender = {"XY" : "son", "XX" : "daughter"}
        return "Congratulations! You're going to have a {}.".format(gender[sperm])
    
    def chromosome_check3(sperm):
        return 'Congratulations! You\'re going to have a {}.'.format('son' if 'Y' in sperm else 'daughter')
    
    print(chromosome_check("XY"))
    print(chromosome_check("XX"))
    
    print(chromosome_check2("XY"))
    print(chromosome_check2("XX"))
    
    print(chromosome_check3("XY"))
    print(chromosome_check3("XX"))

    KATA - Counting sheep...

    Consider an array/list of sheep where some sheep may be missing from their place. We need a function that counts the number of sheep present in the array (true means present).

    For example,

    [True, True, True, False, True, True, True, True , True, False, True, False, True, False, False, True , True, True, True, True , False, False, True, True] The correct answer would be 17.

    Hint: Don't forget to check for bad values like null/undefined

    def count_sheeps(sheep):
        return sheep.count(True)
    
    array1 = [True, True, True, False, True, True, True, True,'',True, False,True, False, True, False, False, True, True, True, True, True, False, False, True, True]
    
    print(count_sheeps(array1))

    KATA - Be Concise - The Ternary Operator

    def describe_age(a): return "You're a(n) "+('kid' if a<13 else "teenager" if a<18 else "adult" if a<65 else "elderly")
    

    KATA - Convert number to reversed array of digits

    Given a random non-negative number, you have to return the digits of this number within an array in reverse order.

    Example(Input => Output): 35231 => [1,3,2,5,3] 0 => [0]

    def digitize(n):
        return list(map(int, str(n)))[::-1]
    
    print(digitize(548702838394))

    7 KYU

    KATA - Isograms

    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.

    "Dermatoglyphics" --> true "aba" --> false "moOse" --> false (ignore letter case)