This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Usage: paren(0,4,'') | |
| def paren(sum,remaining,str): | |
| if sum < 0: | |
| return | |
| if remaining == 0: | |
| print str+')'*sum | |
| return | |
| paren(sum+1,remaining-1,str+'(') | |
| paren(sum-1,remaining,str+')') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def findOdd(arr): | |
| index = 0 | |
| count = 1 | |
| last_char = arr[index] | |
| while(index < len(arr)): | |
| if(arr[index] != last_char): | |
| break | |
| count+=1 | |
| index+=1 | |
| count2 = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def isInt(str): | |
| if len(str) < 1: | |
| return False | |
| if str[0] == '-': | |
| str = str[1:] | |
| dot = False | |
| for c in str: | |
| if c == '.' and dot == True: | |
| return False | |
| if c == '.' and len(str) > 1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string = "Hellooooooooooooooooooooooooo thiss is an example of the project to justify the code and we want a biggggggg word also." | |
| l=25 | |
| start=0 | |
| length = len(string) | |
| while(len(string[start:]) > l): | |
| line = string[start:start+l] | |
| if line.find(' ') == -1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sublength(a, b): | |
| hash_b = {} | |
| for char in set(b): | |
| hash_b[char] = -1 | |
| min_length = len(a)+1 | |
| for i, char in enumerate(a): | |
| if char in hash_b: | |
| hash_b[char] = i | |
| if (min(hash_b.values()) != -1) & (i - min(hash_b.values()) + 1 < min_length) : | |
| print hash_b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def swap(a, b): | |
| temp = a | |
| a = b | |
| b = temp | |
| return (a,b) | |
| def rotate(arr, by): | |
| if len(arr)%by == 0: | |
| for i in range(by): | |
| start, pos = arr[i], i+by |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class node: | |
| def __init__(self, val): | |
| self.value = val | |
| self.next = None | |
| class linked_list: | |
| def __init__(self): | |
| self.head = None | |
| def insert(self, value): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def reverse(string, start, end): | |
| rev = "" | |
| for i in range(0, end - start +1): | |
| rev += string[end - i] | |
| return rev | |
| def reverse_words(sent): | |
| rev_sent = reverse(sent, 0, len(sent)-1) | |
| rev_words = ""; i = 0; j = i | |
| while(True): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def swap(arr, a, b): | |
| temp = arr[a]; arr[a] = arr[b]; arr[b] = temp | |
| def correct_pivot_pos(arr): | |
| pivot = 0 | |
| if len(arr) < 2 : return arr | |
| start = 1; end = len(arr) - 1 | |
| while(start < end): | |
| while((start < len(arr)) and (arr[start] < arr[pivot])): start+= 1 | |
| while((end > 0) and (arr[end] > arr[pivot])): end -= 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def encoding_ways(str): | |
| if((len(str) == 1) or (len(str) == 0)): return 1 | |
| total1 = encoding_ways(str[1:]) | |
| if((str[0] == '1') or ((str[0] == '2') and (1 <= int(str[1])) and (int(str[1]) <= 6))): | |
| total1 = total1 + encoding_ways(str[2:]) | |
| return total1 | |
| print encoding_ways('12') |
OlderNewer