Above is a question from CodeWars, we will create the below python function to perform the above task.

def find_longest(string): longest_list = string.split(' ') longest = len(longest_list.pop(0)) for n in longest_list: if len(n) > longest: longest = len(n) return longest

- First, the function above will split the string into a list.
- Then it will use the length of the first word to compare to the remaining words within a for’s loop.
- If the length of any word within that list is longer than the length of the first word then the larger length will be assigned to the ‘longest’ variable which means that length will replace the length of the first word.
- Finally, return the longest length.

This will be the last time we solve the solution on CodeWars as from now onward we will concentrate on creating a project in python. My next project is a video editing python program written in Python, so stay tuned!

]]>Let say when we enter one into that function, the function will return 0 because the first even number is 0. If we enter two into that function, the function will return 2 because the second number of even numbers is 2. Besides that, we will also need to take care of the number that is smaller than 1 which is an invalid entry as one is the very first even number.

def nth_even(n): if n < 1: return 0 # return the first even number if the user has entered an invalid number else: return 2 * (n-1)

Enter your own solution in the comment box below!

]]>def merge_arrays(*kwargs): merge_list = [] for li in kwargs: merge_list += li merge_list = list(dict.fromkeys(merge_list)) merge_list.sort() return merge_list

Run the above code with the below line of statement and see the outcome by yourself.

print(merge_arrays([1,2,3,5,6,1], [1,3,5], [9,0]))

If you have a better idea, don’t forget to leave them under the comment box below!

]]>`/`

to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments which means after the / syntax we may specify a value for each parameter within that function. For example,
def f(a, b, /, c, d): print(a, b, c, d) f(10, 20, 30, d=40)

The below example will print out the sum of all the parameters within that function.

import math def e(a): return a * a def f(a, b, /, **kwargs): sum = a + b for num in kwargs: sum += kwargs[num] print(sum) f(2, 3, c=40, d=e(10), e=math .sin(60)) # output 144.695

The above syntax has been contributed by Pablo Galindo. Do you think the syntax is useful? Leave your comment below this post.

]]>The wide mouth frog is particularly interested in the eating habits of other creatures.

He just can’t stop asking the creatures he encounters what they like to eat. But then he meets the alligator who just LOVES to eat wide-mouthed frogs!

When he meets the alligator, it then makes a tiny mouth.

Your goal in this kata is to create the complete the mouth_size method this method takes one argument animal which corresponds to the animal encountered by the frog. If this one is an alligator (case insensitive) return small otherwise return wide.

def mouth_size(animal): if animal.lower() == "alligator": return "small" else: return "wide"

As you can see, if the animal is an alligator then the function will return “small” mouth, or else “wide” mouth will be returned!

I have started to write again, not just about the programming topic but also about other topics such as web development, browser and online business on this website, therefore stay tuned for more interesting stuff in the future!

]]>The returned value must be a string and have “***” between each of its letters.

You should not remove or add elements from/to the array.

Above is another problem in codewars, besides asking us to sort the array list and returning the first value in that list, we also need to insert stars within the characters.

def two_sort(array): array.sort() first_string = array[0] first_star_string = '' limit = len(first_string) for i in range(0, limit): if i == 0: first_star_string += first_string[i] else: first_star_string +='***'+first_string[i] return first_star_string

The python solution above is straight forward but needs further improvement if possible, do write down your own answer in the comment box below this post.

]]>def words_to_marks(s): word_list = list(s) a_z = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x','y', 'z'] sum = 0 for w in word_list: sum += a_z.index(w) + 1 return sum

As you might guess, the above is just another question from CodeWars.

Dear readers, I am working hard to build this website just for Python from now onward, if you like this post, do share the post on any social media site, thank you.

Leave your comment or provide your own solution below this post.

]]>In the below example, you need to decide which phrase to return from the array list which consists of good ideas ‘good’ and bad ideas ‘bad’. If there are one or two good ideas, return ‘Publish!’, if there are more than 2 return ‘I smell a series!’. If there are no good ideas, as is often the case, return ‘Fail!’.

def well(x): if x.count('good') > 2: return 'I smell a series!' elif x.count('good') > 0: return 'Publish!' else: return 'Fail!'

Write down your own python answer in below comment box.

If you like this post do share the post on any social media site to help this site to grow, thank you.

]]>E.g. If we have an array [1,2,3,4,6,7,8] then 1 then 2 then 3 then 4 are all consecutive but 6 is not, so that’s the first non-consecutive number.

If the whole array is consecutive then return None.

The array will always have at least 2 elements1 and all elements will be numbered. The numbers will also all be unique and in ascending order. The numbers could be positive or negative and the first non-consecutive could be either too!

Solution :

Take out the first number in the array, then continue to add one to that number, if one of the summation outcomes is not the same as the next number in the array then the program will return that next number or else the program will return None if no non-consecutive number has been found.

def first_non_consecutive(arr): seed = arr.pop(0) for num in arr: seed += 1 if num != seed: return num return None

Any thoughts about the above solution? Please comment below.

If you like any post on this website, please share on social media to help this site gets more readers!

]]>def odd_one(arr): for number in arr: if number % 2 != 0: return arr.index(number) return -1

The method above will loop through the number list to determine the position of the only odd number within that number list. If no odd number has been found then the method above will return -1!

That is a simple solution, if you have better idea then leave your comment below.

**Announcement:**

After a while of rest, I have begun to write again, I am planning to turn this website into a full tech website which will deal with programming, electronic gadget topic, and gaming. Feel free to subscribe to any