Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
import pandas as pd
 
 
header = pd.MultiIndex.from_product([['Semester1','Semester2'],['Maths','Science']])
d=([[12,45,67,56],[78,89,45,67],[45,67,89,90],[67,44,56,55]])
 
 
df = pd.DataFrame(d,
                  index=['Alisa','Bobby','Cathrine','Jack'],
                  columns=header)

# stack the dataframe
stacked_df=df.stack()

# unstack the dataframe
unstacked_df = stacked_df.unstack()

# stack the dataframe of column at level 0
stacked_df_lvl=df.stack(level=0)

# unstack the dataframe
unstacked_df1 = stacked_df_lvl.unstack()


Algorithms

...

Palindrome number - Determine whether an integer is a palindrome

Code Block
def is_palindrome(word):
    j = len(word)-1
    i =0
    while i<j and word[i].lower()==word[j].lower():
        i+=1
        j-=1
        
    return (i>=j)
    
print(is_palindrome('Deleveled'))


Two

...

sum -  return indices of the two numbers such that they add up to a specific target

Info

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

...

Code Block
def twoSum(self, nums, target):
        seen = {}
        for i, v in enumerate(nums):
            remaining = target - v
            if remaining in seen:
                return [seen[remaining], i]
            seen[v] = i
        return []


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


Info

Input: 123 → Output: 321
Input: -123 → Output: -321
Input: 120 → Output: 21


Code Block
class Solution(object):
    def reverse(self, x):
        if x >= 2**31-1 or x <= -2**31:
            return 0
        else:
            strg = str(x)
            
        if x >= 0 :
            revst = strg[::-1]
        else:
            temp = strg[1:] 
            temp2 = temp[::-1] 
            revst = "-" + temp2
            
        if int(revst) >= 2**31-1 or int(revst) <= -2**31:
            return 0
        else:
            return int(revst)



Merge two sorted linked lists and return it as a new list. 


Info

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

Code Block
class Solution:
	def mergeTwoLists(self, l1, l2):

	result = ListNode(0) # The new list we are going to eventually return
	head = result # keep a pointer to the head so we can return head.next in the end
	while(l1 != None and l2 != None): # This check is important in the case where one list is shorter than the other
		if l1.val < l2.val: # Add l1's value as a new node to result if its less than l2's
			result.next = ListNode(l1.val)
			l1 = l1.next
			result = result.next
		elif l2.val < l1.val: # Add l2's value as a new node to result if its less than l1's
			result.next = ListNode(l2.val)
			l2 = l2.next
			result = result.next
		else: # In this case, the values must be equal so add both to result and move the linked lists forward
			result.next = ListNode(l1.val)
			result = result.next
			result.next = ListNode(l2.val)
			result = result.next
			l1 = l1.next
			l2 = l2.next

	if l1 == None and l2 != None: # If l2 is longer than l1, add all of the remaining values of l2 to result
		while(l2 != None):
			result.next = ListNode(l2.val)
			result = result.next
			l2 = l2.next
	elif l2 == None and l1 != None: # if l1 is longer than l2, add all of the remaining values of l1 to result
		while(l1 != None):
			result.next = ListNode(l1.val)
			result = result.next
			l1 = l1.next

	return head.next # return the result