codingdirectional: Reverse a number with Python

In this snippet, we are going to create a python method to reverse the order of a number. This is one of the questions on Codewars. If you enter -123 into the method you will get -321. If you enter 1000 into the method you will get 1. Below is the entire solution.

 def reverse_number(n):     num_list = list(str(n))     num_list.reverse()      if "-" in num_list:         num_list.pop(len(num_list)-1)         num_list.insert(0, "-")      return int("".join(num_list)) 

If you do follow my website you know that I always write simple python code and post them here, but starting from the next article, I will stop posting python code for a while and start to talk about my python journey and the cool software which is related to python. I hope you will appreciate this new style of writing and thus will make learning python for everyone a lot more fun than just staring at the boring and sometimes long python snippet.

Like, share or follow me on Twitter.

If you have any solution for this problem do comment below.

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 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 list of the number and its power with python

Before we start our new python project here is another solution for one of the python’s question on codewars. In this example, we need to create a method which will accept a number and returns a list consists of a pair of a number and power that are equal to that input number. For example, the input number is 9, then the number and it’s power which is equals to that input number is [3, 2] because 3 * 3 = 9. If there is no number and power that match the input value then None will be returned. Here is the solution to the above mentioned question.

 def isPP(power):          for num in range(1, power):         for po in range(2, power):             if(num ** po == power):                 return [num, po]            return None 

The above method is only suitable to solve a number input which is below 1800, the program will become freeze if the number goes beyond that value (depends on the processing power of your own computer). Well, hope you like this quick solution, in the next chapter we will begin our project.

Planet Python

codingdirectional: Get the closest prime number with python

In this chapter, we will create a method which will return the value which needs to be added to the total of all the numbers within a list to get the closest prime number from that total. A prime number is a number which can only be divided by one and itself. 1 is not a prime number. The below program will add one to the sum of all the numbers within a list until it reaches the closest prime number from that total, it then will return the value which needs to be added to reach that prime status.

 def minimum_number(numbers):          total = sum(numbers)     n=0     notprime = True      if(total == 1):         return 1     elif(total == 2):         return 0     else:         while(notprime):                         for i in range(2,total):                              if(total%i == 0):                     notprime = True                     break                 else:                     notprime = False                                  if(notprime == True):                 total+=1                 n+=1             else:                 return n 

As you can see we will use the notprime flag to control the while loop until we get the answer which we need. Like, share or comment on this post, if you have nothing to do then why not donate some cryptocurrencies through the donation widget (on the sidebar of this website) to help funding this site, thank you for your help.

Planet Python