codingdirectional: Count the number of occurrences of each character and return it as a list of tuples in order of appearance

In this example we are going to create a function which will count the number of occurrences of each character and return it as a list of tuples in order of appearance. For example,

 ordered_count("abracadabra") == [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)] 

The above is a 7 kyu question on CodeWars, this is the only question I can solve today after the first two fail attempts.

I am supposed to start the Blender project today but because I want to write a post for your people I have spent nearly an hour and a half working on those three python questions on CodeWars, I hope you people will really appreciate my effort and will share this post to help this website to grown.

 def ordered_count(input):      already = []     input_list = list(input)     return_list = []     for word in input_list:         if(word not in already):             return_list.append((word, input_list.count(word)))             already.append(word)      return return_list 

The solution above is short and solid, hope you like it.

Planet Python

codingdirectional: Return a welcome message based on the input language

Hello and welcome! I think I will start another new project after the previous sport application project in next month but at this moment we are going to solve a few python related problems from Codewars starting from this chapter onward, we are just going for the easy problem because we don’t want to waste too much time thinking about the solution to each question. In this episode, we are going to solve the below problem.

Your start-up’s BA has told marketing that your website has a large audience in Scandinavia and surrounding countries. Marketing thinks it would be great to welcome visitors to the site in their own language. Luckily you already use an API that detects the user’s location, so this is an easy win.

The Task

  • Think of a way to store the languages as a database (eg an object). The languages are listed below so you can copy and paste!
  • Write a ‘welcome’ function that takes a parameter ‘language’ (always a string), and returns a greeting – if you have it in your database. It should default to English if the language is not in the database, or in the event of an invalid input.

After solving the above problem on Codewars I have made one small change to that language database because I think the author of this question has forgotten to include one of the most important languages in the world besides English and I have put that language in front of all the European languages in the database so you can easily spot which language that the author of this problem has forgotten to include. I hope you do enjoy the below python solution!

 def greet(language):      welcome = {         'russian': 'добро пожаловать',         'english': 'Welcome',         'czech': 'Vitejte',         'danish': 'Velkomst',         'dutch': 'Welkom',         'estonian': 'Tere tulemast',         'finnish': 'Tervetuloa',         'flemish': 'Welgekomen',         'french': 'Bienvenue',         'german': 'Willkommen',         'irish': 'Failte',         'italian': 'Benvenuto',         'latvian': 'Gaidits',         'lithuanian': 'Laukiamas',         'polish': 'Witamy',         'spanish': 'Bienvenido',         'swedish': 'Valkommen',         'welsh': 'Croeso'}      if language in welcome.keys():         return welcome[language]     else:                  return "Welcome" 

If you run the above program with one of our favorite languages, you will get the below outcome.

 print(greet('spanish')) // Bienvenido 

Dude, I hope you do enjoy this solution and don’t forget to come back for more!

Planet Python

codingdirectional: Return a reverse order list for a number with python

Hello people, this will be the last article which we will solve a simple question in CodeWars, in the next chapter we will start our next python project. In this chapter, we will solve one of the questions in CodeWars and the question goes like this.

Given a number, find the reverse order’s numbers of that number until one and put them in a list. For example, number 6 will make the method to return a list of reverse numbers up until 1, [6,5,4,3,2,1]. Below is the solution.

 def reverse_seq(n):      list_ = []     while(n > 0):         list_.append(n)         n -= 1     return list_ 

So we have solved that simple question on CodeWars and we have received 2 kyu from our hard work. With that, we have temporary concluded the question solving chapter and will start our next python project in the next chapter.

Planet Python

codingdirectional: Return the probability of drawing a blue marble

It seems like I have not decide yet which project am I going to create next so why not just work on another python solution on CodeWars in this chapter. I think I will work on a few more python questions in the next few chapters before starting a brand new python project.

The python question I am about to solve goes like this.

You and a friend have decided to play a game to drill your statistical intuitions. The game works like this:

You have a bunch of red and blue marbles. To start the game you grab a handful of marbles of each color and put them into the bag, keeping track of how many of each color go in. You take turns reaching into the bag, guessing a color, and then pulling one marble out. You get a point if you guessed correctly. The trick is you only have three seconds to make your guess, so you have to think quickly.

You’ve decided to write a function, guessBlue() to help automatically calculate whether you should guess “blue” or “red”. The function should take four arguments:

  • the number of blue marbles you put in the bag to start
  • the number of red marbles you put in the bag to start
  • the number of blue marbles pulled out so far, and
  • the number of red marbles pulled out so far.

guessBlue() should return the probability of drawing a blue marble, expressed as a float. For example, guessBlue(5, 5, 2, 3) should return 0.6.

This question above actually needs us to think a few more times before we find out the solution but it has been put under the 8 kyu question, I think it should be at least 7 or 6 kyu question instead.

Before we write out the code let us solve the problem first by hand.

We need to get the quantity of the remaining blue marbles in the bag so

blue_remained = blue_start - blue_pulled

Same goes to red marbles

 red_remained = red_start - red_pulled 

Finally the probability of drawing a blue marble from the bag will become

 blue_remained/(blue_remained + red_remained) 

Here is the entire method.

 def guess_blue(blue_start, red_start, blue_pulled, red_pulled):          blue_remained = blue_start - blue_pulled     red_remained = red_start - red_pulled          return blue_remained/(blue_remained + red_remained) 

The solution above is not complex but you need to think a little bit to find that solution.

Planet Python

codingdirectional: Return a list of divisible numbers

It seems like I am still thinking about what is the new application to create for the next project so I think I will solve a few more easy questions from the Codewars website. All the questions that I am looking at will be simple because I don’t want to spend too much time each day just to solve hard question. OK, let us get to work.

Given a list of numbers as the first parameter and the divisor as the second parameter, create a method which will return a list of numbers that can be divided by the divisor. Below is the solution to that question!

 def divisible_by(numbers, divisor):      divisible_list = []     for item in numbers:         if(item % divisor == 0):             divisible_list.append(item)     return divisible_list 

I will solve a few more questions before start a new python project.

Do you want me to solve question or to create a brand new project?

Leave your comment below this post, if you want me to solve python problem instead of starting a new project then I think I will just concentrate on solving the python related problem in this website because I have already started a new project on another website and that will be a little bit too much for me to handle.

Planet Python