Home CodeWars - Popular 7 KYU Challenges
Post
Cancel

CodeWars - Popular 7 KYU Challenges

I’m currently working on solving 7 kyu challenges in problem-solving, which are a bit harder than the 8 kyu challenges. As we go along, I’ll guide you through tackling five well-known challenges at the 7 kyu level. And I’ll also show you some smart tricks to boost your skills. Let’s dive into these challenges together and figure out how to crack them!

codewars

Disemvowel Trolls

Trolls are attacking your comment section! A common way to deal with this situation is to remove all of the vowels from the trolls’ comments, neutralizing the threat. Your task is to write a function that takes a string and return a new string with all vowels removed. For example, the string “This website is for losers LOL!” would become “Ths wbst s fr lsrs LL!”.

Note: for this kata “y” isn’t considered a vowel.

Solution

1
2
def disemvowel(string):
    return "".join([char for char in string if char.lower() not in 'aeiou']) # List comprehension

Another Solution

1
2
def disemvowel(string):
    return "".join(c for c in string if c.lower() not in "aeiou") # Generator Expression

They are the same solution but approached differently. In the first solution, I used List Comprehension, while in the other, a Generator Expression was used



Vowel Count

Return the number (count) of vowels in the given string. We will consider a, e, i, o, u as vowels for this Kata (but not y). The input string will only consist of lower case letters and/or spaces.

Solution

1
2
3
4
def get_count(sentence):
    count = 0
    count = sum(1 for i in sentence if i in "aeiou") # Generator Expression
    return count

Enhanced Solution

1
2
def getCount(sentence):
    return sum(1 for i in sentence if i in "aeiou") # Generator Expression

Here, the main differnce is that we didn’t put the value in a varabile so, it’s almost the same



Descending Order

Your task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.

Examples:

Input: 42145 Output: 54421

Input: 145263 Output: 654321

Input: 123456789 Output: 987654321

Solution

1
2
3
4
5
def descending_order(num):
    l = [x for x in str(num)] # List Comprehension
    l = sorted(l, reverse=True)
    r = int("".join(l))
    return r

Enhanced Solution

1
2
def Descending_Order(num):
    return int("".join(sorted(str(num), reverse=True)))

We can reorganize everything in a one-liner instead of storing the values in variables (of course, if you won’t use them later)



Highest and Lowest

In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.

Examples

1
2
3
high_and_low("1 2 3 4 5")  # return "5 1"
high_and_low("1 2 -3 4 5") # return "5 -3"
high_and_low("1 9 3 4 -5") # return "9 -5"

Notes

  • All numbers are valid Int32, no need to validate them.
  • There will always be at least one number in the input string.
  • Output string must be two numbers separated by a single space, and highest number is first.

Solution

1
2
3
4
def high_and_low(numbers):
    x = numbers.split(" ")
    x = [int(num) for num in x] # Convert str to int using List Comprehension 
    return str(max(x)) + " " + str(min(x))

Enhanced Solution

1
2
3
def high_and_low(numbers):
    x = [int(num) for num in numbers.split(" ")] # List Comprehension
    return "%i %i" % (max(x),min(x))

%i is used to represent integer values in string formatting (not to convert integer values to strings)



Square Every Digit

Welcome. In this kata, you are asked to square every digit of a number and concatenate them.

Example #1: if we run 9119 through the function, 811181 will come out, because 92 is 81 and 12 is 1. (81-1-1-81)

Example #2: An input of 765 will/should return 493625 because 72 is 49, 62 is 36, and 52 is 25. (49-36-25)

Note: The function accepts an integer and returns an integer.

Happy Coding!

Solution

1
2
3
4
5
6
7
def square_digits(num):
    f = ""
    d = [int(x) for x in str(num)] # List Comprehension
    for i in d:
        sq = i ** 2
        f += str(sq)
    return int(f)

Enhanced Solution

1
2
3
4
5
def square_digits(num):
    r = ""
    for x in str(num):
        r += str(int(x)**2)
    return r

As we know, an int object is not iterable. So, one way to iterate through an integer is to convert it to a string, and that’s what I did in the Solution with the d List. On the other hand, if we look at the Enhanced Solution, we can see that we didn’t create a list we can directly use str(int(x)**2).


prfile-level

This post is licensed under CC BY 4.0 by the author.
Contents