From 7923ff5cd091ab3574e92b102aad8d3bc6d7dceb Mon Sep 17 00:00:00 2001 From: pronapro Date: Mon, 26 Jul 2021 12:44:04 +0300 Subject: [PATCH] basic strings and arrays --- fizzbuzz.py | 13 +++++++++++++ is_permutation.py | 33 +++++++++++++++++++++++++++++++++ is_prime.py | 13 +++++++++++++ para_check.py | 31 +++++++++++++++++++++++++++++++ product_other.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ sort_window.py | 17 +++++++++++++++++ sumtwo.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ unique.py | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 231 insertions(+) create mode 100644 fizzbuzz.py create mode 100644 is_permutation.py create mode 100644 is_prime.py create mode 100644 para_check.py create mode 100644 product_other.py create mode 100644 sort_window.py create mode 100644 sumtwo.py create mode 100644 unique.py diff --git a/fizzbuzz.py b/fizzbuzz.py new file mode 100644 index 0000000..a82bae4 --- /dev/null +++ b/fizzbuzz.py @@ -0,0 +1,13 @@ +for i in range(1,100): + if i%5 ==0 and i%3==0: + print("fizzbuzz") + elif i%5 ==0: + print("buzz") + elif i%3 == 0: + print("fizz") + else: + print(i) + + + + diff --git a/is_permutation.py b/is_permutation.py new file mode 100644 index 0000000..e2b95c0 --- /dev/null +++ b/is_permutation.py @@ -0,0 +1,33 @@ + + +#use hash tables +def permutation(astring,bstring): + hash1 ={} + hash2 ={} + if len(astring) != len(bstring): + return False + for char in astring: + if char in hash1: + hash1[char] += 1 + hash1[char] = 1 + + for char in bstring: + if char in hash2: + hash2[char] += 1 + hash2[char] = 1 + return hash1 == hash2 + +""" +Describe what it means for two strings to be permutations of each other. Now, look at +that definition you provided. Can you check the strings against that definition? +Two strings that are permutations should have the same characters, but in different +orders. Can you make the orders the same? +Could a hash table be useful? +There is one solution that is 0 (N log N) time. Another solution uses some space, but +isO(N) time. + +""" +# instead of using hash dict use counter from collections + + + \ No newline at end of file diff --git a/is_prime.py b/is_prime.py new file mode 100644 index 0000000..2cc4616 --- /dev/null +++ b/is_prime.py @@ -0,0 +1,13 @@ +import math +def isprime(n): + # all numbers less than 2 are not primes + if n < 2: + return False + # loop from 2 to sqrt(n) + for i in range(2, int(math.ceil(math.sqrt(n)))): + # check if (n mod i) is equal to 0, if so then there are # two numbers, a and b, that can multiply to give n + if n % i == 0: + return False + + + return True \ No newline at end of file diff --git a/para_check.py b/para_check.py new file mode 100644 index 0000000..4442702 --- /dev/null +++ b/para_check.py @@ -0,0 +1,31 @@ +""" You are given a string with the symbols ( and ) and +you need to write a function that will determine if the parenthsis +are correctly nested in the string which means every opening +( has a closing ) +True +() +(()) +()()() +False +((()())) +(() +(((( +())() +()()()) +""" +def check(astring): + holder = 0 + open ="(" + close =")" + for i in astring: + if i ==open: + holder +=1 + elif i ==close: + holder -=1 + if holder== 0: + return True + else: + return False + + +print(check("((()()))")) \ No newline at end of file diff --git a/product_other.py b/product_other.py new file mode 100644 index 0000000..4fc511c --- /dev/null +++ b/product_other.py @@ -0,0 +1,47 @@ +""" +Given an array of integers, return a new array such +that each element at index i of the new array is the +product of all the numbers in the original array except +the one at i. For example, ifour input was [1, 2, 3, 4, 5], +the expected output would be [120, 60, 40, 30, 24]. Ifourinputwas [3, 2, 1], +the expected output would be [2, 3, 6]. +""" + +#O(n) +def product(nums): + prefix_product =[] + suffix_product =[] + for num in nums: + if prefix_product: + prefix_product.append(prefix_product[-1]*num) + else: + prefix_product.append(num) + for num in reversed(nums): + if suffix_product: + suffix_product.append(suffix_product[-1]*num) + else: + suffix_product.append(num) + suffix_product =list(reversed(suffix_product)) + #print(suffix_product) + #print(prefix_product) + result =[] + for i in range(len(nums)): + + if i ==0: + result.append(suffix_product[i+1]) + elif i == len(nums)-1: + result.append(prefix_product[i-1]) + else: + result.append( + prefix_product[i-1]*suffix_product[i+1] + ) + return result + + +print(product([1, 2, 3, 4, 5])) + + +#solution two + + + diff --git a/sort_window.py b/sort_window.py new file mode 100644 index 0000000..8450476 --- /dev/null +++ b/sort_window.py @@ -0,0 +1,17 @@ +""" +Given an array of integers that are out of order, determine the bounds of the smallest +window that must be sorted in order for the entire array to be sorted. For example, +given[3, 7, 5, 6, 9],youshouldreturn(1, 3). +""" +#0(nlogn) +def window(nums): + left,right =None,None + a =sorted(nums) + for i in range(len(nums)): + if nums[i]!=a[i] and left is None: + left =i + elif nums[i] !=a[i]: + right =i + return left,right +print(window([3, 7, 5, 6, 9])) + \ No newline at end of file diff --git a/sumtwo.py b/sumtwo.py new file mode 100644 index 0000000..a98522a --- /dev/null +++ b/sumtwo.py @@ -0,0 +1,45 @@ +def sum_two(nums,s): + hashtable ={} + for num in nums: + value = s-num + if value in hashtable: + return(value,num) + else: + hashtable[num] = 1 + +#print(sum_two([3,3],6)) + +#return index +def sum_two_index(nums,s): + hashtable ={} + index = [] + for i,num in enumerate(nums): + value = s-num + if value in hashtable: + index.append(hashtable[value]) + index.append(i) + else: + hashtable[num] = i + return index + +#print(sum_two_index([2,7,11,15],9)) + + +#better structure +def twoSum2(nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + hashtable ={} + + for i,num in enumerate(nums): + value = target-num + if value in hashtable: + return[hashtable[value],i] + + else: + hashtable[num] = i + +print(twoSum2([2,7,11,15],9)) \ No newline at end of file diff --git a/unique.py b/unique.py new file mode 100644 index 0000000..94de5b9 --- /dev/null +++ b/unique.py @@ -0,0 +1,32 @@ +def unique(astring): + for i in range(len(astring)): + for j in range(i+1,len(astring)): + if astring[i] == astring[j]: + return False + return True +#print(unique("unique")) +#print(unique("astring")) +#print(unique("n")) + +#check if length is greater than 128 + +def is_unique_chars(string): + + if len(string) > 128: + return False + + char_set = [False] * 128 + for char in string: + val = ord(char) + if char_set[val]: + # Char already found in string + return False + char_set[val] = True + + return True + +#use set or bits for improvements +#Try a hash table. +#Could a bit vector be useful? +#Can you solve it in O(N log N) time? What might a solution like that look like? +