Reverse Words in a String

Problem 🤔

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: “Let’s take LeetCode contest”

Output: “s’teL ekat edoCteeL tsetnoc”

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

Code

def reverseWords(self, s):
    s = s.split(" ")

    for words in range(0,len(s)):
        s[words] = s[words][::-1]

    s = ' '.join(s)
    return s

Developer’s Notes 👨‍💻

Here I took advantage of what python had to offer such as the split and join methods.

I first split the string where there was a ” ” and put them into a list.

I then cycled through the list reassigning the index with the reverse of the word.

    I reversed the word using a reverse step method s[begin:end: step] with step being -1

After I had reversed all the words I joined them back into a string with a space between each index.

Array Partition

Problem 🤔

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Input: [1,4,3,2]

Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

Code

def quickSort(self,nums, low, high):
    if low < high:
        part = self.partition(nums, low,high)
        self.quickSort(nums,low,part-1)
        self.quickSort(nums,part+1,high)

def partition(self, nums, low, high):
    i = (low - 1)
    pivot = nums[high]
    for j in range(low, high):
        if nums[j] <= pivot:
            i += 1
            nums[i],nums[j] = nums[j],nums[i]
    nums[i+1],nums[high] = nums[high],nums[i+1]
    return(i+1)

def arrayPairSum(self, nums):
    temp = 0
    self.quickSort(nums,0,len(nums)-1)
    for index in range(0,len(nums)-1,2):
        temp += min(nums[index],nums[index+1])
    return temp

Developer’s Notes 👨‍💻

For this I ordered the list using a quicksort, then I cycled through the array took the pairs of numbers and summed the mins. I ordered the array since it would give the largest possible minimum for each pair.

ReverseNum

reverseNum

Problem

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123

Output:  321

Example 2:

Input: -123

Output: -321

Example 3:

Input: 120

Output: 21

Code

def reverse(self, x):

    y = str(x)

    #checks the number if negative it removes the - sign

    if y[0] == '-':

        negative = True

        y = y[1:]

        else:

            negative = False

    #reverses the string [begin:end:step]

    x = y[::-1]

    #if it is negative reverse the sign

    if negative:

        x =  '-' + x

    #checks to make sure it doesnt overflow the 32bit

    if(abs(int(x)) > (2 ** 31 - 1)):

        x = 0

    return(int(x))

Developer’s  Notes

There are a couple of ways to tackle this problem. Here I decide to take advantage of Python’s ability of string manipulation.

I converted the number to a string, checked for a negative, then reversed the string adding the negative if necessary.  At the end of the script, I made sure it was less than the 32bit requirement by a size comparison and returned the result.

Hamming Distance

Problem

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

1   (0 0 0 1)

4   (0 1 0 0)

          

Code

def distanceFinder(self,x,y,count):

    if x%2 != y%2:

        count+=1

    print(count)

    if x/2 == 0 and y/2 == 0:

        return count

    return self.distanceFinder(int(x/2),int(y/2),count)

def hammingDistance(self, x, y):

    return self.distanceFinder(x,y,0)

Developer’s Notes

For this problem, I took a basic recursion approach and tested the distance by finding if then modulus of 2 is different. If it is different then I increment a counter. Once the numbers are 0 then it returns the counter. Since the algorithm uses recursion and depths by half each time the BigO looks to be O(log x). It is due to the fact that x and y are the same lengths.

Back to Basics

It has been a while since I have posted and for somewhat good reasons. This past couple of weeks I have been bombarded with interviews, projects, and an assortment of random things. I have also taken some time to reflect on what I want this blog to be about. At the basic level, I want it to be a place where I can work out my thoughts and post interesting problems and that’s what I’m gonna do. Hope anyone reading this enjoys or can follow my randomness! 🙃