Skip to content

Commit

Permalink
basic strings and arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
pronapro committed Jul 26, 2021
1 parent b98b3f2 commit 7923ff5
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 0 deletions.
13 changes: 13 additions & 0 deletions fizzbuzz.py
Original file line number Diff line number Diff line change
@@ -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)




33 changes: 33 additions & 0 deletions is_permutation.py
Original file line number Diff line number Diff line change
@@ -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



13 changes: 13 additions & 0 deletions is_prime.py
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions para_check.py
Original file line number Diff line number Diff line change
@@ -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("((()()))"))
47 changes: 47 additions & 0 deletions product_other.py
Original file line number Diff line number Diff line change
@@ -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



17 changes: 17 additions & 0 deletions sort_window.py
Original file line number Diff line number Diff line change
@@ -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]))

45 changes: 45 additions & 0 deletions sumtwo.py
Original file line number Diff line number Diff line change
@@ -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))
32 changes: 32 additions & 0 deletions unique.py
Original file line number Diff line number Diff line change
@@ -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?

0 comments on commit 7923ff5

Please sign in to comment.