IslandT: Return the highest volume of traffic during peak hour

In this article, we are going to create a function which will return a list of tuples that consist of a particular hour and the highest traffic volume for that particular hour. The stat has been taken every 10 minutes in each hour. For example, at 4.00pm the total numbers of traffics that pass through a junction for every 10 minutes are as follows: [23, 22, 45, 66, 54, 33]. The traffic volume measurement in this example will begin at 4.00pm and end at 8.00pm. Below is the solution to this problem.

 def traffic_count(array):      # first we will create the traffic volume list for 4pm, 5pm, 6pm and 7pm within a big list      count = 0     arr_stat = []     while(count < 24):         arr_stat.append(array[count:count+6])         count += 6      # then we will create the tuple which consists of time and the peak traffic at that hour     max = 0     arrs = []     time = 4          for elem in arr_stat:         for item in elem:             if max < item : #find out the higest stat                 max = item         arrs.append(((str(time) + ":00pm"), max))         time += 1         max = 0      return arrs 
 traffic_count([23,24,34,45,43,23,57,34,65,12,19,45, 54,65,54,43,89,48,42,55,22,69,23,93]) # if you enter the above data into the function then you will get below outcome! 

list of tuples consist of time vs traffic volume

Give your thought regarding this solution in the comment box below this post!

Planet Python

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