Switching between classes and passing results - python-3.x

I am trying to add different PlugLead's to the PlugBoard and extract the combination. In the first class I am extracting the one letter from the list should they match or return the input. e.g.
lead = PlugLead("AG")
assert(lead.encode("A") == "G")
class PlugLead:
def __init__(self, c):
self.c = c
def encode(self, l):
c0 = self.c[0]
c1 = self.c[1]
if len(l) == 1 and c0 == l:
return c1
elif len(l) == 1 and c1 == l:
return c0
else:
return l
class Plugboard:
def __init__(self):
self.__head = 0
self.leads = []
self.c = []
def add(self, item):
if self.__head >= 10:
print("leads already got 10 items")
elif item in self.leads:
print(f"leads already have this item: {item}")
else:
self.leads.append(item)
self.__head += 1
return self.leads
def encode(self)
lines = plugboard.leads
for word in lines:
word = word.split(",")
PlugLead.encode(word)
In the second class I am trying to add multiple combinations and then at the end pass the one letter to see what its match is in the Class PlugLead however am not able to switch between the two. In the class PlugLead I have a add function that allows the different combinations to be added up to 10 and then I would like to encode from this list the combination of the pairs. e.g.
plugboard = Plugboard()
plugboard.add(PlugLead("SZ"))
plugboard.add(PlugLead("GT"))
plugboard.add(PlugLead("DV"))
plugboard.add(PlugLead("KU"))
assert(plugboard.encode("K") == "U")
assert(plugboard.encode("A") == "A")

if you want to use PlugLead("{balabala}"),you need use __new__ to return a dict when you create an instance, not __init__.
you want a key-value pair in Plugboard, it should be a dict not list
fix some other typo and bugs.
code:
class PlugLead:
def __new__(self, c):
return {c[0]:c[1]}
class Plugboard:
def __init__(self):
self.__head = 0
self.leads = {}
def add(self, item):
if self.__head >= 10:
print("leads already got 10 items")
elif list(item.keys())[0] in self.leads.keys():
print(f"leads already have this item: {item}")
else:
self.leads.update(item)
self.__head += 1
return self.leads
def encode(self,key):
if key in self.leads:
return self.leads[key]
elif key in self.leads.values():
return list(self.leads.keys())[list(self.leads.values()).index(key)]
return key
plugboard = Plugboard()
plugboard.add(PlugLead("SZ"))
plugboard.add(PlugLead("GT"))
plugboard.add(PlugLead("DV"))
plugboard.add(PlugLead("KU"))
plugboard.add(PlugLead("KU"))
assert(plugboard.encode("K") == "U")
assert(plugboard.encode("U") == "K")
assert(plugboard.encode("A") == "A")
print(plugboard.encode("K"))
print(plugboard.encode("U"))
print(plugboard.encode("A"))
result:
leads already have this item: {'K': 'U'}
U
K
A

Related

How to find the shortest path

In the function of find_shortest_func, i think if now position isn't "T" which is also known as the terminal or exit, then i will try to find for direction and see if it is "T", if not, check if it is space and i can go there. Besides, tell the next state function now output and dic to tell the place where i visited. But some errors occur and I don't know why.
I think the problem may occur where I tried to deepcopy the output list
import copy
def set_symbol(symbol_name):
def set_symbol_decorator(func):
def wrapper(self, symbol):
setattr(self, symbol_name, symbol)
return wrapper
return set_symbol_decorator
class Maze:
space_symbol = " "
obstacle_symbol = "X"
path_symbol = "•"
output = []
dis = 0
def __init__(self, input_string):
self.maze = []
if input_string.endswith("txt"):
with open(input_string) as f:
count = 0
for line in f.readlines():
self.maze.append([])
for j in line:
if j != '\n':
self.maze[count].append(j)
count += 1
else:
count = 0
for i in input_string.split("\n"):
self.maze.append([])
for j in i:
self.maze[count].append(j)
count += 1
def __str__(self):
output_string = ""
for i in range(20):
for j in range(20):
output_string += self.maze[i][j]
output_string += "\n"
return output_string
#set_symbol("space_symbol")
def set_space_symbol(self, change):
pass
#set_symbol("obstacle_symbol")
def set_obstacle_symbol(self, change):
pass
#set_symbol("path_symbol")
def set_path_symbol(self, change):
pass
def find_shortest_func(self, position: tuple, d: dict, out: list, dis: int):
dic = copy.deepcopy(d)
output = copy.deepcopy(out)
dic[(position[0], position[1])] = 1
output.append((position[0], (position[1])))
dis += 1
if self.maze[position[0]][position[1]] != "T":
if position[0]+1 < 20 and self.maze[position[0]+1][position[1]] == self.space_symbol and (position[0]+1, position[1]) not in dic:
self.find_shortest_func(
(position[0]+1, position[1]), dic, output, dis)
if position[1]+1 < 20 and self.maze[position[0]][position[1]+1] == self.space_symbol and (position[0], position[1]+1) not in dic:
self.find_shortest_func(
(position[0], position[1]+1), dic, output, dis)
if position[0]-1 >= 0 and self.maze[position[0]-1][position[1]] == self.space_symbol and (position[0]-1, position[1]) not in dic:
self.find_shortest_func(
(position[0]-1, position[1]), dic, output, dis)
if position[1]-1 >= 0 and self.maze[position[0]][position[1]-1] == self.space_symbol and (position[0], position[1]-1) not in dic:
self.find_shortest_func(
(position[0], position[1]-1), dic, output, dis)
if self.maze[position[0]][position[1]] == "T":
if dis < self.dis:
self.output = copy.deepcopy(output)
self.dis = dis
return
def find_shortest_path(self):
d = dict()
output = []
dis = -1
self.find_shortest_func((1, 0), d, output, dis)
return self.output, self.dis

Python - 'int' object is not callable

I'm writing a singly linked list that counts the number of words that get imported from a test file.
I initialized count inside of my class.
I defined the print function, that prints out each of the nodes, after they are sorted.
I defined a count class to iterate through the list and count up all the occurences of a word fed to it.
I want to pass in every node into count to count how many times it appears in my text file. When I try to do that I end up with 'int' object is not callable. Here is my code
class Linked_List:
def __init__(self):
self.head = None
self.count = 1
def print(self):
p = self.head
head = Linked_List_node(p.data)
while p is not None:
print(p.data, '-', self.count(p.data)) # This is where the error appears
p = p.next
def count(self, x):
# loop thru list for all x, if find x add 1 to count. Assign final count to that word.
with open('cleaned_test.txt', 'r') as f:
for line in f:
for word in line.split():
if word == x:
self.count += 1
def insert(self, x):
""""""
p = self.head
q = None
done = False
while not done:
if self.head == x:
done = True
elif p == None:
head = Linked_List_node(x)
q.next = head
done = True
elif x == p.data:
#head = Linked_List_node(x)
#head.counter += 1
done = True
elif x < p.data:
if self.head == p:
head = Linked_List_node(x)
head.next = p
self.head = head
done = True
else:
head = Linked_List_node(x)
head.next = p
q.next = head
done = True
q = p
if p is not None:
p = p.next
class Linked_List_node:
def __init__(self, value):
self.data = value
self.next = None
Relevant part from your code is:
class Linked_List:
def __init__(self):
# ...
self.count = 1
def count(self, x):
# ...
The self.count = 1 assignment overwrites the value of self.count for every Linked_List object that's created, so it refers to 1 instead of the method you defined on the class. Rename either the variable or the function.

Object() takes no arguments

I get, "TypeError: Question() takes no arguments" (where Question is an object of class Question) when trying to execute the code below.
I'm using jupyter notebook and have checked most of my indentation, Tthe class Question has 2 attributes, I have an init method...
Issue is related to the function find_best_split(rows) below.
I have added an image of the error console output here
class Question:
def __init__(self, column, value):
self.column = column
self.value = value
def match(self, example):
val = example[self.column]
if is_numeric(val):
return val >= self.value
else:
return val == self.value
def __repr__(self):
condition = "=="
if is_numeric(self.value):
condition = ">="
return "Is %s %s %s?" % (header[self.column], condition, str(self.value))
def partition(rows, question):
true_rows, false_rows = [], []
for row in rows:
if question.match(row):
true_rows.append(row)
else:
false_rows.append(row)
return true_rows, false_rows
` Error points to this function, specifically "question = Question(col, val)"
def find_best_split(rows):
best_gain = 0
best_question = None
current_uncertainty = gini(rows)
n_features = len(rows[0]) -1 # number of columns
for col in range(n_features):
values = set([row[col] for row in rows]) # unique values in the column
for val in values: #now for each value
question = Question(col, val)
# trying to split the data set
true_rows, false_rows = partition(rows, question)
# skips this split if it doesn't divide the data set.
if len(true_rows) == 0 or len(false_rows) == 0:
continue
# calculate the information gain from this split
gain = info_gain(true_rows, false_rows, current_uncertainty)
# you can use > instead of >= below but I wanted the data set to look a certain way for this example.
if gain >= best_gain:
best_gain, best_question = gain, question
return best_gain, best_quesiton
`
class Decision_Node:
def __init__(self, question, true_branch, false_branch):
self.question = question
self.true_branch = true_branch
self.false_branch = false_branch
def build_tree(rows):
gain, question = find_best_split(rows)
if gain == 0:
return Leaf(rows)
true_rows, false_rows = partition(rows, question)
true_branch = build_tree(true_rows)
false_branch = build_tree(false_rows)
return Decision_Node(question, true_branch, false_branch)
if __name__ == '__main__':
my_tree = build_tree(training_data)
print_tree(my_tree)
# Evaluate
testing_data = [
["Green", 3, "Mango"],
["Yellow", 4, "Mango"],
["Red", 2, "Grape"],
["Red", 1, "Grape"],
["Yellow", 3, "Lemon"],
]
for row in testing_data:
print ("Actual: %s. Predicted: %s" % (row[-1], print_leaf(classify(row, my_tree))))

trying to look for duplicates in an ordered list

So my programs function is to add the provided numbers to an ordered list, then search for the duplicates. I have a function that finds the duplicates of a number I asked and I know is a duplicate. Im trying to get the duplic function to read through the list and append the duplicates to a new list called seen.
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setNext(self,newnext):
self.next = newnext
class OrderedList:
def __init__(self):
self.head = None
def add(self, item):
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.getData() > item:
stop = True
else:
previous = current
current = current.getNext()
temp = Node(item)
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext (current)
previous.setNext(temp)
def prntlist(self):
prnt = self.head
while prnt != None:
print(prnt.data, end=" ")
prnt = prnt.next
print()
def duplic (self):
currnt = self.head
seen = set()
uniq = []
for i in range(int(currnt.data)):
if i not in seen:
uniq.append(i)
seen.add(i)
print (seen)
def count(self, item):# function to count the value
count = 0
ptr = self.head
while ptr != None:
if (ptr.data == item):
count += 1
ptr = ptr.next
return count
mylist = OrderedList()
mylist.add(23)
mylist.add(23)
mylist.add(10)
mylist.add(14)
mylist.add(5)
mylist.add(31)
mylist.add(35)
mylist.add(37)
mylist.add(26)
mylist.add(23)
mylist.add(29)
mylist.add(18)
mylist.add(2)
mylist.add(25)
mylist.prntlist()
print('Count of 23 in list: ', mylist.count(23))
print('Duplicates in list: ', mylist.duplic())
I would like it to print:
mylist
Count of 23 in list: 3
Duplicates in list: {23,23}
You may need to be more clear in the question..
From what i understand, you could write the function this way:
def duplic (self):
currnt = self.head
seen = set()
for i in currnt.data:
if currnt.data.count(i)>1:
seen.add(i)
print (seen)
The uniq seems to be useless. And, a set object cannot have two same elements, so you can't have {23,23}. (you can write this, but it will result in {23} in the end)

union of two default dictionaries

am trying to create a union of two default dictionaries. Here is the code for the method in my class:
def __add__(self,right):
mergedbag = copy.copy(self.bag_value)
for item in right:
if item not in mergedbag:mergedbag[item] = 0
mergedbag[item] += right[item]
return mergedbag
I create two default dictionaries:
b = Bag(['d','a','b','d','c','b','d'])
c = Bag(['d','a','b','d','c','b','d'])
the result of
print(b+c)
should be the total count of elements after the union..
Bag(a[a],b[4],c[3],d[6])
This is the error I keep getting:
Traceback (most recent call last):
File "D:\workspace33\courselib\driver.py", line 229, in driver
Command[print(b+c)]: exec(old,local,globl)
File "<string>", line 1, in <module>
File "D:\workspace33\Project2\src\bag.py", line 58, in __add__
mergedbag[item] += right[item]
TypeError: 'Bag' object is not subscriptable
Here is the new code:
class Bag:
def __init__(self, items = []):
self.bag_value = defaultdict(int)
for item in items:
self.bag_value[item] += 1
def __repr__(self):
bag_list = []
for item, count in self.bag_value.items():
bag_list.extend(list(item*count))
return 'Bag(' + str(bag_list) + ')'
def __str__(self):
return 'Bag(' + ','.join(str(item) + '[' + str(count) + ']' for item, count in self.bag_value.items()) + ')'
def __len__(self):
bag_len = 0
for value in self.bag_value:
bag_len += self.bag_value[value]
return bag_len
def unique(self):
return len(self.bag_value)
def __contains__(self, item):
return item in self.bag_value
def count(self, item):
return(self.bag_items.count(item))
def add(self, new):
self.bag_value[new] += 1
def __add__(self,right):
mergedbag = copy.copy(self.bag_value)
for item in right:
if item not in mergedbag:mergedbag[item] = 0
mergedbag[item] += right[item]
return mergedbag
def remove(self, item):
if item in self.bag_items:
del(item)
else:
raise ValueError(type_as_str(item) + ' not in bag.')
def __eq__(self, right):
if type(right) is not Bag:
raise TypeError('Cannot compare Bag with' + type_as_str(right) + '. Can only compare Bag with Bag')
else:
return (len(self) == len(right)) and (self.unique() == right.unique())
def __ne__(self, right):
return not self.__eq__(right)
def _bag_gen(self, bag_value):
for item in self.bag_value:
for count in range(self.bag_value[item]):
yield item
def __iter__(self):
return self._bag_gen(self.bag_value)
if __name__ == '__main__':
# bag = Bag(['d','a','b','d','c','b','d'])
# bag2 = Bag(['d','a','b','d','c','b','d'])
# bag3 = Bag(['d','a','b','d','c','b'])
# print(bag == bag2)
# print(bag == bag3)
# print(bag != bag2)
# print(bag != bag3)
import driver
driver.driver()
First of all, it is important to note that your Bag class is basically the same as a Counter. If you need any specific extension, just inherit from it and you are done. I would say that the following code just address all the functionality you are implementing yourself in your Bag class:
from collections import Counter
class Bag(Counter):
pass
b = Bag(['d','a','b','d','c','b','d'])
c = Bag(['d','a','b','d','c','b','d'])
print (b)
print (c)
print (b+c)
If you are doing an exercise to learn, the problem is that you are not implementing setitem and getitem methods in your Bag class to allow the [] notation. You could implement them or just access the attribute bag_value:
def __add__(self, right):
mergedbag = Bag()
mergedbag.bag_value = copy.copy(self.bag_value)
for item in right.bag_value.keys():
mergedbag.bag_value[item] += right.bag_value[item]
return mergedbag
Be sure to create and return a Bag object
The definition is
def __add__(self,other):
So you ought to merge the contents of self.items with other.items and then return the result.
Also, not sure if bag1+bag2 will work, but my pyfu is weak, you will explicitly have to iterate through them and add the counts together.
import copy
c = {'a':2,'b':1}
d = {'b':1,'c':1}
#
mergedBag = copy.copy(c) #might wanna use deepcopy?
for k in d:
if k not in mergedBag: mergedBag[k] = 0
mergedBag[k] += d[k]
print mergedBag

Resources