After the password is transformed, it is then called a password hash. Whenever you try to login, the website will transform the password you tried using the same hashing algorithm and simply see if the password hashes are the same.

Create the python function that converts a given string into an md5 hash. The return value should be encoded in hexadecimal.

For Examples

the function will turn “password” into “5f4dcc3b5aa765d61d8327deb882cf99”

We will use the `hashlib`

module to evoke the md5 function, but before we can turn any string into an md5 hash we need to make sure we have already encoded the string first. Finally, return the md5 hash in the hexadecimal format!

import hashlib def pass_hash(str): m = hashlib.md5(str.encode()) return m.hexdigest()

If you are using Python 3.x then you will need to use hashlib instead of the other python md5 hash’s library.

]]>Below is the entire problem we need to solve.

In cryptanalysis, words patterns can be a useful tool in cracking simple ciphers.

A word pattern is a description of the patterns of letters occurring in a word, where each letter is given an integer code in order of appearance. So the first letter is given the code 0, and second is then assigned 1 if it is different to the first letter or 0 otherwise, and so on.

As an example, the word “hello” would become “0.1.2.2.3”. For this task case-sensitivity is ignored, so “hello”, “helLo” and “heLlo” will all return the same word pattern.

Your task is to return the word pattern for a given word. All words provided will be non-empty strings of alphabetic characters only, i.e. matching the regex “[a-zA-Z]+”.

Here is the solution to the above problem.

def word_pattern(word): word_list = list(word) first = False crypto_str = "" increment = 0 word_dict = dict() # create an empty dictionary to keep increment number for char in word_list: char = char.lower() # change all characters to lower case first before make the comparison if not first: first = True crypto_str += str(increment) word_dict[char] = increment else: if char in word_dict: crypto_str += "." + str(word_dict[char]) else: increment += 1 crypto_str += "." + str(increment) word_dict[char] = increment return crypto_str

As you can see from above, we will assign 0 to the first character in the string and increase the decimal number each time we see a different word, but if the word has already appeared before then we will use back the same decimal number. All these are made possible with help from the Python dictionary object.

Do you have a better solution? Leave your own solution in the comment box below this post.

]]>You will be given two strings: lamps and drone. lamps represents a row of lamps, currently off, each represented by x. When these lamps are on, they should be represented by o.

The drone string represents the position of the drone T and its flight path up until this point =. The drone always flies left to right, and always begins at the start of the row of lamps. Anywhere the drone has flown, including its current position, will result in the lamp at that position switching on.

We will create the below function to return the resulting lamps string.

def fly_by(lamps, drone): lamps = list(lamps) for i in range(0, len(drone)): if (i < len(lamps)): lamps[i] = 'o' else: break return "".join(lamps)

As an example, if you enter these strings into the above function you will get the following outcome.

fly_by('xxxxxx', '====T') # return 'ooooox'

As you can see we are using the string length of the drone in the for loop to change the state of the lamp.

Hello, please only leave the comment which is related to the above topic, I will approve all comments that are related to the above topic but the comment must comes with a python solution.

]]>For example:

“abcdefghijklm” –> [“b”, “d”, “f”, “h”, “j”, “l”]

“a” –> “invalid string”

The solution is pretty simple, you certainly can write a shorter program but I do prefer to write a better program which will clearly show what this function is doing.

def even_chars(st): li_s = [] if(len(st) < 2 or len(st) > 100): return "invalid string" li = list(st) for i in range(0, len(st)): if(i%2 != 0): li_s.append(li[i]) return li_s

The list starts from index 0, therefore I will write an if statement to test whether the number is an odd number or not, if the number is an odd number then it will be appended into the list which will be returned to the caller of the function.

]]>For example, if you enter this list, [3.4, -4, 3.5, 7, 14] and pass in True as the second parameter to the below function.

import intesort as sortx print(sortx.intesort([3.4, -4, 3.5, 7, 14], True))

You will get [-4, 3.4, 3.5, 7, 14] in return where this function will sort the number list in the ascending order. If you pass in False then the outcome will be [14, 7, 3.5, 3.4, -4] instead. If you do not pass in the second parameter then the function will assume you want an ascending order list. If you do not pass in anything to the above function, then this function will return [].

You can download this module on PyPI through below command on your command prompt. Enjoy!

pip install intesort

An example of how to use this module. Suppose you are given a list of numbers and you wish to turn them into an ascending list and then print stars on the scene based on those numbers. You can use the above module to help you reorganize the unsorted number list in the ascending order before printing the star on the scene.

import intesort as sortx li = [12, 4, 23, 3, 9] star = sortx.intesort(li) for num in star: str = '' for i in range(0, num): str += '*' print(str)

The above program will produce the below outcome.

*** **** ********* ************ ***********************

The numbers are sorted before the stars get printed on the screen!

]]>Our strategy here is to compare the first number with the remaining numbers from the list and to put the smallest number in the list to the head of the list. Next we will compare the second, third and the remaining numbers with the numbers after it and placed the next smallest number in the correct position of the array.

def solution(nums): if nums == None: return [] for i in range(0, len(nums)): for j in range(i, len(nums)): if(nums[i] > nums[j]): temp = nums[i] nums[i] = nums[j] nums[j] = temp return nums

The sorting solution above is nothing different than the solution for Java or C++ so you should be very familiar with it.

]]>You receive the name of a city as a string, and you need to return a string that shows how many times each letter shows up in the string by using an asterisk (*).

For example:

“Chicago” –> “c:**,h:*,i:*,a:*,g:*,o:*“

As you can see, the letter c is shown only once, but with 2 asterisks.

The return string should include only the letters (not the dashes, spaces, apostrophes, etc). There should be no spaces in the output, and the different letters are separated by a comma (,) as seen in the example above.

Note that the return string must list the letters in order of their first appearance in the original string.

I will solve the above problem using just a pure/core python module without importing any extra module.

def get_strings(city): city = "".join(city.lower().split()) # get rid of all spaces and turn all characters into lower case characters city_list = list(city) # next turn the city string into a list already = [] # this is used to hold the character which has already counted before city_string = '' # this is the city string which will be returned for word in city_list: if word not in already: count = city_list.count(word) city_string += word + ":" for i in range(0, count): city_string += "*" city_string+= "," already.append(word) return city_string[:-1] # don't forget to get rid of the last commaSave Up to 88% Off + Free Gift. Limited Time Offer

Do you like the above solution or do you have a better one? Let me know in the comment box below this post!

]]>The question is as follows:-

Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the long string on the inside. The strings will not be the same length, but they may be empty ( length 0 ).

For example:

solution(“1”, “22”) # returns “1221”

solution(“22”, “1”) # returns “1221”

And here is the solution for the above given question.

def solution(a, b): short ='' long = '' if len(a) > len(b): return b+a+b else: return a+b+a

Do you have a better solution for the above question? Post your own solution below this post.

]]>Write a Python function that will repeat the given string with the number of times that string supposed to be repeated. The solution to this question is as follows.

def repeat_str(repeat, string): repeatString = '' for i in range(0, repeat): repeatString += string return repeatString

As you can see from above, the repeated string will be returned to the caller of this function.

]]>The Quadratic Equation looks like this ax^2 + bx + c = 0. Our mission is to find the coefficients of the equations which is a, b, and c. The return type from the function is a Vector containing coefficients of the equations in the order (a, b, c). Since there are infinitely many solutions to this problem, we fix a = 1.

Below is the method to find the return Vector.

quadratic(1,2) = (1, -3, 2)

This means (x-1) * (x-2) = 0; when we do the multiplication this becomes x^2 – 3x + 2 = 0.

def quadratic(x1, x2): a = 1 b= a * -x2 + a * -x1 c = -x1 * -x2 return (a, b, c)

A very simple solution, we can also find the Vector even if a has a different value than one by creating another method with an extra ‘a’ parameter in the input.

quadratic(a, x1, x2)

This question is from CodeWars, which is a great place to learn how to code for free, besides Python, there are tons of other programming languages for you to select include Java and C#.

]]>