Skip to content

Commit

Permalink
added test12 and renumbered several tests
Browse files Browse the repository at this point in the history
  • Loading branch information
okany committed Jul 13, 2021
1 parent 2100b51 commit 7aa412d
Show file tree
Hide file tree
Showing 88 changed files with 1,454 additions and 1,230 deletions.
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,44 @@

* test1 - finds the kth minimum number in a list
* test2 - implements preorder, inorder, and postorder traversal of a binary tree with and without using recursion
* test3 - manipulates a number string by adding a "*" between two consecutive even numbers and "-" between two consecutive odd numbers
* test4 - itermixes two strings after crerating chunks in size n e.g. ABCDEFG and 1234567890 with 2 char chunks -> AB12CD34EF56G7890
* test5 - finds the common words in two lists
* test6 - demonstrates trying and catching exceptions while getting an a positive integer as an input and printing its square
* test7 - counts the occurrence of each word in a list
* test3 - finds the best time to buy and sell a stock once or unlimited times or fix number of times to make the maximum profit
* test4 - recovers a binary search tree with 2 replaced nodes
* test5 - implements a search typehead
* test6 - implements bubble sort, insertion sort, quick sort, merge sort, and bucket sort algorithms
* test7 - finds the largest rectangle in a histogram
* test8 - finds duplicate characters in a string
* test9 - determines if two strings are anagram or not
* test10 - prints the first non-repeated character from a string
* test11 - reverses a string by using recursion
* test12 - checks if a string has all digits
* test13 - reverses words in a paragraph
* test12 - finds the median of an array which is created by merging two sorted arrays
* test13 - implements a simple vending machine
* test14 - removes duplicate entries in a list
* test15 - checks if 2 strings are rotation of each other
* test16 - reverse words in a given sentence without using any library method
* test16 - reverses words in a given sentence without using any library method
* test17 - finds the nth node from the end in a singly linked list
* test18 - creates a list with only unique items
* test19 - finds all combinations of a string
* test20 - checks if a string is a palindrome
* test21 - reverses a singly linked list
* test22 - implements a binary tree
* test22 - creates a binary tree iterator
* test23 - searches an integer in a presorted integer array which was rotated from an unknown pivot point (array has no duplicates)
* test24 - finds the middle node in a linked list in one pass
* test25 - prints all leaves of a binary tree
* test26 - implements bubble sort, insertion sort, quick sort, merge sort, and bucket sort algorithms
* test27 - implements a simple vending machine
* test26 - counts the occurrence of each word in a list
* test27 - implements a binary tree
* test28 - finds the least common ancestor in a binary search tree
* test29 - finds the maximum depth of a binary tree
* test30 - creates a binary tree iterator
* test31 - recovers a binary search tree with 2 replaced nodes
* test30 - returns level order of a binary tree
* test31 - finds the common words in two lists
* test32 - finds list of all unique triplets in a list of integers satisfying a+b+c=0 in non-descending order
* test33 - reads a text file and creates another text file with the reversed file content
* test34 - finds starting position of substrings in a string S where each substring is a combination of all substrings provided in a list L
* test35 - finds the best time to buy and sell a stock once or unlimited times or fix number of times to make the maximum profit
* test36 - finds the largest rectangle in a histogram
* test35 - intermixes two strings after crerating chunks in size n e.g. ABCDEFG and 1234567890 with 2 char chunks -> AB12CD34EF56G7890
* test36 - reverses words in a paragraph
* test37 - finds a, b, c, d values such that A[a]+A[b] = A[c] + A[d] where a < b and c < d and a < c and b != d and b != c
* test38 - finds largest distance between nodes of a tree
* test39 - finds stepping numbers between two integers e.g. A=10, B=40 output = [10, 12, 21, 23, 32, 34]
* test40 - returns level order of a binary tree
* test41 - implements a search typehead
* test42 - finds the kth minimum number in a list (another version)
* test40 - checks if a string has all digits
* test41 - demonstrates trying and catching exceptions while getting a positive integer as an input and printing its square
* test42 - finds the kth minimum number in a list (another version)
* test43 - manipulates a number string by adding a "*" between two consecutive even numbers and "-" between two consecutive odd numbers
2 changes: 1 addition & 1 deletion test12/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions test12/.idea/test12.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

176 changes: 161 additions & 15 deletions test12/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# check if a string has all digits
# This script finds the median of an array which is created by merging two sorted arrays
#
# This script is a part of the Easy Python project which creates a number
# sample python scripts to answer simple programming questions. The
Expand All @@ -18,25 +18,171 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
class median_array():
def __init__(self, l1, l2):
if(len(l1)>=len(l2)):
self.l1 = l1
self.l2 = l2
else:
self.l1 = l2
self.l2 = l1

class digitstr(str):

def __init__(self, astr):
self = astr

def alldigits(self):
if self == "": return False
else: return(self.checkdig(self))
self.median = None

def checkdig(self, astr):
if(astr == ""):
def iseven(self, anum):
if anum == (anum >> 1) * 2:
# totlen is even
return True
elif(astr[0].isdigit()):
return self.checkdig(astr[1:])
else:
return False

def find_median(self):

med2 = len(self.l2) >> 1
totlen = len(self.l1)+len(self.l2)
even = self.iseven(totlen)
med = ((totlen+1) >> 1) - 1
med1 = med - med2 - 1
soln = sol1 = sol2 = None

# handle special cases that l1 and l2 has no intersections
if (self.l2[0] >= self.l1[-1]):
soln = sol1 = self.l1[med]
if even:
if med+1 == len(self.l1):
sol2 = self.l2[0]
else:
sol2 = self.l1[med + 1]
soln = (sol1 + sol2) / 2

return soln
elif (self.l2[-1]<=self.l1[0]):
if(med < len(self.l2)):
soln = sol1 = self.l2[med]
if even:
sol2 = self.l1[0]
soln = (sol1 + sol2) / 2
else:
soln = sol1 = self.l1[med - len(self.l2)]
if even:
sol2 = self.l1[med - len(self.l2) + 1]
soln = (sol1 + sol2) / 2

return soln

# print("med1={} med2={} total length={} median={}".format(med1, med2, totlen, med))

# set the search area in the shorter array
if self.l1[med1] > self.l2[med2]:
min2 = med2
max2 = len(self.l2) - 1
else:
min2 = 0
max2 = med2

while True:
if self.l1[med1] > self.l2[med2]:
# print("med1={} at {} med2={} at {} min2 {} max2 {}".
# format(self.l1[med1], med1, self.l2[med2], med2, min2, max2))
if(self.l1[med1] < self.l2[med2+1]):
# we found the median or exhausted one of the arrays
break
min2 = med2+1
if (min2 > max2):
# we hit the max or min of l2
break
move = (max2 - min2) + 1 >> 1
# print("move={}".format(move))
med1 -= move
med2 += move
elif self.l1[med1] < self.l2[med2]:
# print("med1={} at {} med2={} at {} min2 {} max2 {}".
# format(self.l1[med1], med1, self.l2[med2], med2, min2, max2))
if(self.l1[med1+1] > self.l2[med2]):
# we found the median or exhausted one of the arrays
break
max2 = med2-1
if (min2 > max2):
# we hit the max or min of l2
break
move = (max2 - min2) + 1 >> 1
# print("move={}".format(move))
med1 += move
med2 -= move
else:
# we found the median
break

# we exhausted all items in array 2
# search for solution in array 1
if self.l2[med2] >= self.l1[med1]:
sol1 = self.l2[med2]
else:
sol1 = self.l1[med1]
soln = sol1
# print ("solution1 is {} ".format(sol1))

if even:
if med2+1 < len(self.l2) and self.l2[med2 + 1] < self.l1[med1 + 1]:
sol2 = self.l2[med2 + 1]
else:
sol2 = self.l1[med1 + 1]
soln = (sol1+sol2)/2

# print ("solution2 is {} ".format(sol2))
# print ("final solution is {} ".format(soln))

return soln

if __name__ == '__main__':
l1 = [2, 5, 25, 37, 50, 66, 71, 72, 80, 91, 92, 100]
l2 = [75, 77, 79, 84, 86, 105, 107, 110]

ma = median_array(l1, l2)

med = ma.find_median()

print("TEST#1 - median of A1 {} and A2 {} is {}".format(l1, l2, med))

l1 = [2, 5, 25, 37, 50, 66, 71, 72, 80, 91, 92, 100]
l2 = [75, 77, 79, 84, 86, 105, 107, 110, 120]

ma = median_array(l1, l2)

med = ma.find_median()

print("TEST#2 - median of A1 {} and A2 {} is {}".format(l1, l2, med))
l1 = [2]
l2 = [75]

ma = median_array(l1, l2)

med = ma.find_median()

print("TEST#3 - median of A1 {} and A2 {} is {}".format(l1, l2, med))
l1 = [20]
l2 = [20]

ma = median_array(l1, l2)

med = ma.find_median()

print("TEST#4 - median of A1 {} and A2 {} is {}".format(l1, l2, med))

l1 = [20]
l2 = [20, 21]

ma = median_array(l1, l2)

med = ma.find_median()

print("TEST#5 - median of A1 {} and A2 {} is {}".format(l1, l2, med))

l1 = [10, 18]
l2 = [20, 30, 40, 45]

ma = median_array(l1, l2)

med = ma.find_median()

astr = digitstr("23423423y5")
print("alldigit? = ", astr.alldigits())
print("TEST#6 - median of A1 {} and A2 {} is {}".format(l1, l2, med))
2 changes: 1 addition & 1 deletion test13/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test13/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
92 changes: 77 additions & 15 deletions test13/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This script reverses words in a paragraph
# This script implements a simple vending machine
#
# This script is a part of the Easy Python project which creates a number
# sample python scripts to answer simple programming questions. The
Expand All @@ -19,22 +19,84 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

def reverse(alist):
blist = []
for item in alist:
blist.insert(0,item)
return blist
class order():
def __init__(self, money, item):
self.money = money
self.item = item

def reverse2(alist):
blist = []
for item in alist[::-1]:
blist.append(item)
return blist
class item():
def __init__(self, name, price):
self.name = name
self.price = price

def to_str(self):
astr = "item = (name = " + self.name + " price = " + str(self.price) + ")"
return astr

class invitem():
def __init__(self, item, count):
self.item = item
self.count = count

def remove(self, num=1):
self.count = self.count - num

def add(self, num=1):
self.count = self.count + num

def print(self):
print("{}, count = {}".format(self.item.to_str(), self.count))

class vmachine():
def __init__(self, list):
self.dict = dict()
for each in list:
self.add(invitem(item(each[0], each[1]), each[2]))

def add(self, invitem):
if(self.dict.get(invitem.item.name)):
self.dict[invitem.item.name].count = self.dict[invitem.item.name].count + invitem.count
else:
self.dict[invitem.item.name] = invitem

def sell(self, order, num=1):
trans = False
invitem = self.dict.get(order.item)
remain = order.money

if(invitem):
cost = invitem.item.price*num
if(cost < order.money):
trans = True
remain = order.money - cost
invitem.count = invitem.count - num
return trans, remain
return trans, remain

def print(self):
for each in self.dict.values():
each.print()

if __name__ == "__main__":
astr = "In this study we used domain engineering as a method for gaining deeper formal understanding of a class of algorithms. Specifically, we analyzed 6 stemming algorithms from 4 different sub-domains of the conflation algorithms domain and developed formal domain models and generators based on these models. The application generator produces source code for not only affix removal but also successor variety, table lookup, and n-gram stemmers. The performance of the generated stemmers was compared with the stemmers developed manually in terms of stem similarity, source, and executable sizes, and development and execution times. Five of the stemmers generated by the application generator produced more than 99.9% identical stems with the manually developed stemmers. Some of the generated stemmers were as efficient as their manual equivalents and some were not."

alist = list(astr.split())
inv = [["choclate", 10, 25], ["gum", 1, 100], ["coke", 5, 50]]
vm = vmachine(inv)

vm.print()

money = 50

ord = order(money, "gum")
trans, money = vm.sell(ord, 5)
print("transaction = {}, remain = {}".format(trans, money))
vm.print()

ord = order(money, "choclate")
trans, money = vm.sell(ord, 6)
print("transaction = {}, remain = {}".format(trans, money))
vm.print()

print("reverse of {} is {}".format(alist, reverse(alist)))
print("reverse2 of {} is {}".format(alist, reverse2(alist)))
ord = order(money, "coke")
trans, money = vm.sell(ord, 2)
print("transaction = {}, remain = {}".format(trans, money))
vm.print()
File renamed without changes.
Loading

0 comments on commit 7aa412d

Please sign in to comment.