Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warm-up challenge Classwork #26

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Ignoring dir
.*/
10 changes: 10 additions & 0 deletions classwork/01/reverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Function to reverse array

def reverse(A):
A = A[::-1]
return A


if __name__ == '__main__':
a = [1, 2, 3, 4, 5]
print(reverse(a))
19 changes: 19 additions & 0 deletions classwork/02/sum_target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def target_sum(arr, target):
sum_indices = []

for i in range(len(arr)):
for j in range(i, len(arr) - 1):
if arr[i] + arr[j+1] == target:
sum_indices = [i, j + 1]

break
else:
continue
break

return sum_indices


if __name__ == '__main__':
print(target_sum([2, 4, 11, 8], 12))
print(target_sum([2, 3, 4], 6))
30 changes: 30 additions & 0 deletions classwork/03/evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import re


def evaluate(S):
# Remove all whitespaces in string
s = S.replace(' ', '')

# Split string at operators into array
arr = re.split(r'(\D)', s)

# Intialize first element of array as result
result = int(arr[0])

# Iterate through the array operating on the numbers depending on operator
# and update result
for i in range(0, len(arr) - 2, 2):

if arr[i + 1] == '+':
result += int(arr[i + 2])

else:
result -= int(arr[i + 2])

return result


if __name__ == '__main__':
s = "8 + 3 + 6 + 2 - 3"

print(evaluate(s))
89 changes: 89 additions & 0 deletions classwork/04/singly_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# the Node class - contains value and address to next node
class Node(object):
def __init__(self, val):
self.val = val
self.next = None

def get_data(self):
return self.val

def set_data(self, val):
self.val = val

def get_next(self):
return self.next

def set_next(self, next):
self.next = next


# the LinkedList class
class LinkedList(object):
def __init__(self, head=None):
self.head = head
self.count = 0

def get_count(self):
return self.count

def insert(self, data):
new_node = Node(data)
new_node.set_next(self.head)
self.head = new_node
self.count += 1

def find(self, val):
item = self.head
while (item != None):
if item.get_data() == val:
return item
else:
item = item.get_next()
return None

def deleteAt(self, idx):
if idx > self.count:
return
if self.head == None:
return
else:
tempIdx = 0
node = self.head
while tempIdx < idx-1:
node = node.get_next()
tempIdx += 1
node.set_next(node.get_next().get_next())
self.count -= 1

def printList(self):
tempnode = self.head
while (tempnode != None):
print("Node: ", tempnode.get_data())
tempnode = tempnode.get_next()

def sumList(self):
tempnode = self.head
self.sum = 0
while (tempnode != None):
self.sum += tempnode.get_data()
tempnode = tempnode.get_next()
print('Sum of list:', self.sum)



if __name__ == "__main__":
# create a linked list and insert some items
itemlist = LinkedList()
itemlist.insert(3)
itemlist.insert(10)
itemlist.insert(1)
itemlist.insert(5)
itemlist.insert(6)

#Print the List
itemlist.printList()

#GEt sum
itemlist.sumList()