Object() takes no arguments - python-3.x

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))))

Related

Switching between classes and passing results

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

Saving a value, removing it and then running again with the rest of a list

Problem Description
I am firstly I am having a list of values like:
(1,2,3,1,3,2,2,2,1)
What I want to achieve is :
Find the most common one (working).
Delete it from the list (working).
Repeat the same thing for the remaining values in the list.
For instance
Taking this Tuple:
(1,2,3,1,3,2,2,2,1)
Store that 2 is the most common int, then
(1,3,3,1,1,)
Store that 1 is the second most common int, then
(3,3)
Store that 2 is the third most common int, then
My source Code
NOTE lets say that self.final_votes would be (1,2,3,1,3,2,2,2,1)
def erase_common(self):
possitions = []
counter = 0
common = self.final_votes[0]
for i in self.final_votes:
cand_freq = self.final_votes.count(i)
if(cand_freq> counter):
counter = cand_freq
common = i
possitions.append(common)
while common in self.final_votes:
self.final_votes.remove(common)
this is the whole code
class voting_system:
def __init__(self):
#self.tutor_group = self.get_tutor_group()
self.stud_num = self.get_stud_num()
self.cand_num = self.get_cand_num()
self.cand_name = self.get_cand_name()
self.final_votes = self.ind_votes()
self.erase_common()
# def get_tutor_group(self):
# tutor_groups = ("7A","7B","7C","7D","7E","7F","8A","8B","8C","8D","8E","8F","9A","9B","9C","9D","9E","9E",
# "9F","10A","10B","10C","10D","10E","10F","11A","11B","11C","11D","11E","11F")
#
# flag = True
# while flag == True:
# try:
# self.tutor_group = input("please enter the tutor group: ")
# if self.tutor_group not in tutor_groups:
# raise ValueError("Tutor group entred doesn't exist")
# except ValueError as error:
# print(error)
# flag = True
#
# else:
# flag = False
# return self.tutor_group
def get_stud_num(self):
# flag = True
# while flag == True:
# try:
# self.stud_num = int(input("Please enter number of students: "))
# if self.stud_num<0 or self.stud_num>35:
# raise ValueError("NUMBER OF STUDENTS INVALID")
# except ValueError as error:
# print(error)
# flag = True
#
# else:
# flag = False
self.stud_num = 7
return self.stud_num
def get_cand_num(self):
# flag = True
# while flag == True:
# try:
# self.cand_num = int(input("Please enter number of candidates: "))
# if self.cand_num>4:
# raise ValueError("NUMBER OF CANDIDATES INVALID")
# except ValueError as error:
# print(error)
# flag = True
#
# else:
# flag = False
self.cand_num = 3
return self.cand_num
def get_cand_name(self):
self.cand_name = []
for i in range(self.cand_num):
candidate = input("Enter candidate name: ")
self.cand_name.append(candidate)
return self.cand_name
def print_cands(self):
for i in range(len(self.cand_name)):
print(i+1, " : ", self.cand_name[i])
def ind_votes(self):
self.final_votes = []
self.print_cands()
abs_vote = 0
flag = True
while flag == True:
try:
for i in range(self.stud_num):
vote = input("To vote type 'V' and the candidates number, to abstent type 'A': ")
if vote.isalpha() == True:
abs_vote +=1
else:
self.final_votes.append(vote[1])
if vote[1] > str(self.cand_num):
raise ValueError("VOTED CANDIDATE NONEXISTENT")
except ValueError as error:
print(error)
flag = True
else:
flag = False
return self.final_votes
return abs_vote
def erase_common(self):
possitions = []
counter = 0
common = self.final_votes[0]
for i in self.final_votes:
cand_freq = self.final_votes.count(i)
if(cand_freq> counter):
counter = cand_freq
common = i
possitions.append(common)
while common in self.final_votes:
self.final_votes.remove(common)
run = voting_system()
any suggestions would be appreciated:)
Easier and probably the best:
User Python Counter from Collection.
from collections import Counter
final_votes = [1, 2, 3, 1, 3, 2, 2, 2, 1]
def erase_common(votes):
count_votes = Counter(votes)
return count_votes.most_common()
print(erase_common(final_votes)) # [(2, 4), (1, 3), (3, 2)]
votes = erase_common(final_votes)
for idx, votes in enumerate(votes):
print(f'{idx+1}) Vote {votes[0]} Total = {votes[1]}')
OUTPUT
1) Vote 2 Total = 4
2) Vote 1 Total = 3
3) Vote 3 Total = 2
Using groupby
#AlexMartelli has a really good answer regarding find most common element on list --> Find the most common element in a list
So you can implement this by doing this:
from itertools import groupby
final_votes = [1, 2, 3, 1, 3, 2, 2, 2, 1]
def most_common_oneliner(L):
grupped = groupby(sorted(L))
result = {group: len([*scores]) for group, scores in grupped}
final_sorted_votes = []
while result:
max = [(0, 0)]
for score, res in result.items():
if max[0][1] < res:
max[0] = (score, res)
del result[max[0][0]]
final_sorted_votes += max
return final_sorted_votes
result = most_common_oneliner(final_votes)
for idx, votes in enumerate(result):
print(f'{idx+1}) Vote {votes[0]} Total = {votes[1]}')
Documentation
Counter
Counter Implementation
most_common function inplementation
groupby
how-do-i-use-itertools-groupby Stack Overflow
geeksforgeeks.org | python-find-most-frequent-element-in-a-list

What is wrong here? Why is it showing type error when it shouldn't be?

class student :
def __init__(self,name,age,grade):
self.name = name
self.age = age
self.grade = grade # 0 - 100
def get_grade(self):
return self.grade
class Course :
def __init__(self, name, max_students):
self.name = name
self.max_students = max_students
self.students = []
def add_student(self, student):
if len(self.students) < self.max_students :
self.students.append(student)
return True
return False
def get_average_grade(self):
value = 0
for i in self.students :
value += student.get_grade() # this part had the error
return value / len(self.students)
s1 = student('Bob', 12, 50)
s2 = student('Joe', 12, 60)
s3 = student('Sadie', 12, 100)
Course1 = Course('Chemistry', 5)
Course1.add_student(s1)
Course1.add_student(s2)
Course1.add_student(s3)
print(Course1.students[0].name)
print(Course1.get_average_grade())
for i in self.students :
value += student.get_grade() # this part had the error
It has an error because you're using the class name student instead of the subject of the loop, i.
You'd be more successful if you instead used value += i.get_grade(), though I'd probably choose a better variable name as well.
Or you could replace the entire body of get_average_grade() with the more Pythonic:
return sum([item.get_grade() for item in self.students]) / len(self.students)
As an aside, you may also want to consider what will happen when there are no students in the list.

How to search in an id tree in the python program

I'm a student and working on a small assignment where I need to collect inputs from the student on factors like kind of books they like to issue from the library. I've been provided id_tree class which I need to search using. As you can see I'm getting inputs from the console and I like to use that as the search criteria and get the recommendation from the id tree.
Just for testing purpose, I'm using out.py, but that needs to be replaced with id_tree search logic for which I'm struggling.
# k-Nearest Neighbors and Identification Trees
#api.py
import os
from copy import deepcopy
from functools import reduce
################################################################################
############################# IDENTIFICATION TREES #############################
################################################################################
class Classifier :
def __init__(self, name, classify_fn) :
self.name = str(name)
self._classify_fn = classify_fn
def classify(self, point):
try:
return self._classify_fn(point)
except KeyError as key:
raise ClassifierError("point has no attribute " + str(key) + ": " + str(point))
def copy(self):
return deepcopy(self)
def __eq__(self, other):
try:
return (self.name == other.name
and self._classify_fn.__code__.co_code == other._classify_fn.__code__.co_code)
except:
return False
def __str__(self):
return "Classifier<" + str(self.name) + ">"
__repr__ = __str__
## HELPER FUNCTIONS FOR CREATING CLASSIFIERS
def maybe_number(x) :
try :
return float(x)
except (ValueError, TypeError) :
return x
def feature_test(key) :
return Classifier(key, lambda pt : maybe_number(pt[key]))
def threshold_test(feature, threshold) :
return Classifier(feature + " > " + str(threshold),
lambda pt: "Yes" if (maybe_number(pt.get(feature)) > threshold) else "No")
## CUSTOM ERROR CLASSES
class NoGoodClassifiersError(ValueError):
def __init__(self, value=""):
self.value = value
def __str__(self):
return repr(self.value)
class ClassifierError(RuntimeError):
def __init__(self, value=""):
self.value = value
def __str__(self):
return repr(self.value)
class IdentificationTreeNode:
def __init__(self, target_classifier, parent_branch_name=None):
self.target_classifier = target_classifier
self._parent_branch_name = parent_branch_name
self._classification = None #value, if leaf node
self._classifier = None #Classifier, if tree continues
self._children = {} #dict mapping feature to node, if tree continues
self._data = [] #only used temporarily for printing with data
def get_parent_branch_name(self):
return self._parent_branch_name if self._parent_branch_name else "(Root node: no parent branch)"
def is_leaf(self):
return not self._classifier
def set_node_classification(self, classification):
self._classification = classification
if self._classifier:
print("Warning: Setting the classification", classification, "converts this node from a subtree to a leaf, overwriting its previous classifier:", self._classifier)
self._classifier = None
self._children = {}
return self
def get_node_classification(self):
return self._classification
def set_classifier_and_expand(self, classifier, features):
if classifier is None:
raise TypeError("Cannot set classifier to None")
if not isinstance_Classifier(classifier):
raise TypeError("classifier must be Classifier-type object: " + str(classifier))
self._classifier = classifier
try:
self._children = {feature:IdentificationTreeNode(self.target_classifier, parent_branch_name=str(feature))
for feature in features}
except TypeError:
raise TypeError("Expected list of feature names, got: " + str(features))
if len(self._children) == 1:
print("Warning: The classifier", classifier.name, "has only one relevant feature, which means it's not a useful test!")
if self._classification:
print("Warning: Setting the classifier", classifier.name, "converts this node from a leaf to a subtree, overwriting its previous classification:", self._classification)
self._classification = None
return self
def get_classifier(self):
return self._classifier
def apply_classifier(self, point):
if self._classifier is None:
raise ClassifierError("Cannot apply classifier at leaf node")
return self._children[self._classifier.classify(point)]
def get_branches(self):
return self._children
def copy(self):
return deepcopy(self)
def print_with_data(self, data):
tree = self.copy()
tree._assign_data(data)
print(tree.__str__(with_data=True))
def _assign_data(self, data):
if not self._classifier:
self._data = deepcopy(data)
return self
try:
pairs = list(self._soc(data, self._classifier).items())
except KeyError: #one of the points is missing a feature
raise ClassifierError("One or more points cannot be classified by " + str(self._classifier))
for (feature, branch_data) in pairs:
if feature in self._children:
self._children[feature]._assign_data(branch_data)
else: #feature branch doesn't exist
self._data.extend(branch_data)
return self
_ssc=lambda self,c,d:self.set_classifier_and_expand(c,self._soc(d,c))
_soc=lambda self,d,c:reduce(lambda b,p:b.__setitem__(c.classify(p),b.get(c.classify(p),[])+[p]) or b,d,{})
def __eq__(self, other):
try:
return (self.target_classifier == other.target_classifier
and self._parent_branch_name == other._parent_branch_name
and self._classification == other._classification
and self._classifier == other._classifier
and self._children == other._children
and self._data == other._data)
except:
return False
def __str__(self, indent=0, with_data=False):
newline = os.linesep
ret = ''
if indent == 0:
ret += (newline + "IdentificationTreeNode classifying by "
+ self.target_classifier.name + ":" + newline)
ret += " "*indent + (self._parent_branch_name + ": " if self._parent_branch_name else '')
if self._classifier:
ret += self._classifier.name
if with_data and self._data:
ret += self._render_points()
for (feature, node) in sorted(self._children.items()):
ret += newline + node.__str__(indent+1, with_data)
else: #leaf
ret += str(self._classification)
if with_data and self._data:
ret += self._render_points()
return ret
def _render_points(self):
ret = ' ('
first_point = True
for point in self._data:
if first_point:
first_point = False
else:
ret += ', '
ret += str(point.get("name","datapoint")) + ": "
try:
ret += str(self.target_classifier.classify(point))
except ClassifierError:
ret += '(unknown)'
ret += ')'
return ret
################################################################################
############################# k-NEAREST NEIGHBORS ##############################
################################################################################
class Point(object):
"""A Point has a name and a list or tuple of coordinates, and optionally a
classification, and/or alpha value."""
def __init__(self, coords, classification=None, name=None):
self.name = name
self.coords = coords
self.classification = classification
def copy(self):
return deepcopy(self)
def __getitem__(self, i): # make Point iterable
return self.coords[i]
def __eq__(self, other):
try:
return (self.coords == other.coords
and self.classification == other.classification)
except:
return False
def __str__(self):
ret = "Point(" + str(self.coords)
if self.classification:
ret += ", " + str(self.classification)
if self.name:
ret += ", name=" + str(self.name)
ret += ")"
return ret
__repr__ = __str__
################################################################################
############################### OTHER FUNCTIONS ################################
################################################################################
def is_class_instance(obj, class_name):
return hasattr(obj, '__class__') and obj.__class__.__name__ == class_name
def isinstance_Classifier(obj):
return is_class_instance(obj, 'Classifier')
def isinstance_IdentificationTreeNode(obj):
return is_class_instance(obj, 'IdentificationTreeNode')
def isinstance_Point(obj):
return is_class_instance(obj, 'Point')
#id_tree
from api import *
import math
log2 = lambda x: math.log(x, 2)
INF = float('inf')
import pandas as pd
def id_tree_classify_point(point, id_tree):
if id_tree.is_leaf():
return id_tree.get_node_classification()
else:
new_tree = id_tree.apply_classifier(point)
get_point = id_tree_classify_point(point, new_tree)
return get_point
def split_on_classifier(data, classifier):
"""Given a set of data (as a list of points) and a Classifier object, uses
the classifier to partition the data. Returns a dict mapping each feature
values to a list of points that have that value."""
#Dictionary which will contain the data after classification.
class_dict = {}
#Iterating through all the points in data
for i in range(len(data)):
get_value = classifier.classify(data[i])
if get_value not in class_dict:
class_dict[get_value] = [data[i]]
else:
class_dict[get_value].append(data[i])
return class_dict
def branch_disorder(data, target_classifier):
"""Given a list of points representing a single branch and a Classifier
for determining the true classification of each point, computes and returns
the disorder of the branch."""
#Getting data after classification based on the target_classifier
class_dict = split_on_classifier(data, target_classifier)
if (len(class_dict) == 1):
#Homogenous condition
return 0
else:
disorder = 0
for i in class_dict:
get_len = len(class_dict[i])
p_term = get_len/ float(len(data))
disorder += (-1) * p_term * log2(p_term)
return disorder
def average_test_disorder(data, test_classifier, target_classifier):
"""Given a list of points, a feature-test Classifier, and a Classifier
for determining the true classification of each point, computes and returns
the disorder of the feature-test stump."""
average_disorder = 0.0
#Getting all the branches after applying test_classifer
get_branches = split_on_classifier(data, test_classifier)
#Iterating through the branches
for i in get_branches:
disorder = branch_disorder(get_branches[i], target_classifier)
average_disorder += disorder * (len(get_branches[i])/ float(len(data)))
return average_disorder
#### CONSTRUCTING AN ID TREE
def find_best_classifier(data, possible_classifiers, target_classifier):
"""Given a list of points, a list of possible Classifiers to use as tests,
and a Classifier for determining the true classification of each point,
finds and returns the classifier with the lowest disorder. Breaks ties by
preferring classifiers that appear earlier in the list. If the best
classifier has only one branch, raises NoGoodClassifiersError."""
#Base values to start with
best_classifier = average_test_disorder(data, possible_classifiers[0], target_classifier)
store_classifier = possible_classifiers[0]
#Iterating over the list of possible classifiers
for i in range(len(possible_classifiers)):
avg_disorder = average_test_disorder(data, possible_classifiers[i], target_classifier)
if avg_disorder < best_classifier:
best_classifier = avg_disorder
store_classifier = possible_classifiers[i]
get_branches = split_on_classifier(data, store_classifier)
if len(get_branches)==1:
#Only 1 branch present
raise NoGoodClassifiersError
else:
return store_classifier
def construct_greedy_id_tree(data, possible_classifiers, target_classifier, id_tree_node=None):
"""Given a list of points, a list of possible Classifiers to use as tests,
a Classifier for determining the true classification of each point, and
optionally a partially completed ID tree, returns a completed ID tree by
adding classifiers and classifications until either perfect classification
has been achieved, or there are no good classifiers left."""
#print data
#print "possible", possible_classifiers
#print "target", target_classifier
if id_tree_node == None:
#Creating a new tree
id_tree_node = IdentificationTreeNode(target_classifier)
if branch_disorder(data, target_classifier) == 0:
id_tree_node.set_node_classification(target_classifier.classify(data[0]))
else:
try:
#Getting the best classifier from the options available
best_classifier = find_best_classifier(data, possible_classifiers, target_classifier)
get_branches = split_on_classifier(data, best_classifier)
id_tree_node = id_tree_node.set_classifier_and_expand(best_classifier, get_branches)
#possible_classifiers.remove(best_classifier)
branches = id_tree_node.get_branches()
for i in branches:
construct_greedy_id_tree(get_branches[i], possible_classifiers, target_classifier, branches[i])
except NoGoodClassifiersError:
pass
return id_tree_node
possible_classifiers = [feature_test('age'),
feature_test('gender'),
feature_test('duration'),
feature_test('Mood')
]
df1 = pd.read_csv("data_form.csv")
#df1 = df1.drop("age", axis=1)
print(df1)
a = []
with open("data_form.csv") as myfile:
firstline = True
for line in myfile:
if firstline:
mykeys = "".join(line.split()).split(',')
firstline = False
else:
values = "".join(line.split()).split(',')
a.append({mykeys[n]:values[n] for n in range(0,len(mykeys))})
keys = a[0].keys()
print(keys)
with open('data_clean.csv', 'w') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(a)
print(a)
tar = feature_test('genre')
print(construct_greedy_id_tree(a, possible_classifiers, tar))
#book_suggestion
import random
#from out import *
def genre(Mood, age, gender, duration):
print("Hi")
res_0= input("What's your name?")
res_1 = input("How are you, "+str(res_0)+"?")
if res_1 in ("good","fine","ok","nice"):
print ("Oh nice")
else:
print("Oh! It's alright")
Mood = input("What is your current mood?")
age = input("What is your age range : 10-12, 12-15,13-14,15-18,18+?")
gender = input("What is your gender?")
duration = input("How long do you want to read : 1week, 2weeks, 3weeks, 3+weeks, 2hours")
def get_book(genre):
suggestions = []
genre_to_book = {"Fantasy":["Just me and my babysitter - Mercer Mayer","Just Grandpa and me - Mercer Mayer","Just me and my babysitter - Mercer Mayer",
"The new Potty - Mercer Mayer","I was so mad - Mercer Mayer","Just me and my puppy" ,"Just a mess" ,"Me too"
,"The new Baby","Just shopping with mom"],
"Encyclopedias":["Brain Power - Paul Mcevoy", "My best books of snakes Gunzi Chrisitian","MY best books of MOON Grahame,Ian",
"The book of Planets Twist,Clint", "Do stars have points? Melvin", "Young discover series:cells Discovery Channel"]
,
"Action" : ["The Kane Chronicle:The Throne of Fire s Book 2 Riordan,Rick",
"Zane : ninja of ice Farshtey, Greg",
"Escape from Sentai Mountain Farshtey, Greg",
"Percy jackson Rick Riordan",
"The Kane Chronicle:The Throne of Fire s Book 2 Rick Riordan"],
"Comic" : ["Double Dork Diaries Russell Rachel Renée",
"Dork Dairies Russell Rachel Renee",
"Dork Dairies Russell Rachel Renée"],
"Mystery" : ["Sparkling Cyanide Christie Agatha",
"Poirot's Early Cases: Agatha Christie",
"The Name of this Book is Secret Bosch,Pseudonyuous"],
"Biographies" :["All by myself Mercer Mayer", "D Days prett bryan",
"Snake Bite Lane Andrew"] }
if (genre == "Fantasy"):
suggestions = [random.sample(genre_to_book["Fantasy"], 3)]
elif (genre == "Action"):
suggestions = [random.sample(genre_to_book["Action"], 3)]
elif (genre == "Comic"):
suggestions = [random.sample(genre_to_book["Comic"], 3)]
elif (genre == "Mystery"):
suggestions = [random.sample(genre_to_book["Mystery"], 3)]
elif (genre == "Encyclopedias"):
suggestions = random.sample(genre_to_book["Encyclopedias"], 3)
elif (genre == "Biographies"):
suggestions = random.sample(genre_to_book["Biographies"], 3)
return suggestions
print(get_book(genre(Mood, age, gender, duration)))
I want the program to not depend on out.py and and run on the information of id tree
The current implementation of the suggestions works by asking the user for a genre, then looking up a list of book titles in a dictionary using that genre as the key, then randomly selecting one of the titles and printing it. The current implementation also (presumably) constructs a IdentificationTreeNode containing recommendations, but then does nothing with it except printing it to the standard output.
The next step would be to not discard the tree, but save it in a variable and use in the recommendation process. Since the class structure is not given, it is not clear how this could be done, but it seems a reasonable assumption that it is possible to provide a keyword (the genre) and receive some collection of objects where each one contains data on a recommendation.
If constructing the IdentificationTreeNode is too costly to run on each recommendation request, it is possible to split the construction into its own script file and using python's pickle package to save the object in a file that can then be unpickled more quickly in the script performing the recommendations.

Sort a unordered list class in python? solution?

I have a Unordered list class like this :
from node import Node
class UnorderedList:
"""
Unordered list
"""
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def __len__(self):
"""
returns the length of the list O(1)
"""
return self.size()
def add(self, item):
"""
Add item to list
"""
temp = Node(item)
temp.set_next(self.head)
self.head = temp
def size(self):
"""
Return size of list
"""
current = self.head
count = 0
while current != None:
count = count + 1
current = current.get_next()
return count
def set(self, index, newdata):
"""
Set node-data in list at specific index
"""
current = self.head
previous = None
for i in range(index):
previous = current
current = current.get_next()
if current != None:
temp = Node(newdata)
temp.set_next(current)
if previous is None:
self.head = temp
else:
previous.set_next(temp)
else:
raise("index out of range")
def getIndex(self, item):
"""get the index of an item, assume the first one (head pointing to)
is 0"""
index = 0
current = self.head
found = False
while current != None:
if current.get_data() == item:
found = True
break
else:
current = current.get_next()
index += 1
if not found:
index = None
return index
def get(self, index):
"""
Returns node data based on index
"""
current = self.head
for i in range(index):
current = current.get_next()
if current != None:
return current.get_data()
else:
raise("index out of range")
def search(self, item):
"""
Returns True if item found, else return False
"""
# Här ska du returnera en bool (True/False)
# beroende på om 'item' finns i listan
current = self.head
found = False
while current != None and not found:
if current.get_data() == item:
found = True
else:
current = current.get_next()
return found
def print_list(self):
"""
Prints each item in list
"""
# Traversera listan och gör en print() på varje element
result = "["
node = self.head
if node != None:
result += str(node.data)
node = node.next
while node:
result += ", " + str(node.data)
node = node.next
result += "]"
return result
def remove(self, item):
"""
Removes item from list
"""
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
I want to create a class list and then i can sort the list with my "bubblesort" function.
My "bubblesort" function look like this :
def bubble_sort(items):
""" Bubble sort """
Size = items.size()
for i in range(Size):
for j in range(Size-1-i):
if items.get(j) > items.get(j+1):
tmps = items.get(j+1)
items.set(j, items.get(j+1))
items.set(j+1, tmps)
return items
Now let us create a list :
// create the list
myListTwo = UnorderedList()
// Add the elements to the list
myListTwo.add(4)
myListTwo.add(50)
myListTwo.add(6)
myListTwo.add(10)
myListTwo.add(60)
//print the list :
print(myListTwo.print_list())
[60, 10, 6, 50, 4]
In this stage all work very well but the problem is when I want to sort the list with my bubble_sort function I got this result :
// bubble_sort my list
sorte = bubble_sort(myListTwo)
//print the list
print(sorte.print_list())
[10, 10, 10, 10, 60, 10, 6, 50, 4]
Any idea?
Thanx/Georges
So, you have two issues to your implementation.
The first one is the inner code of bubble sort. Change this
tmps = items.get(j+1)
to
tmps = items.get(j)
See here for more about swapping Python Simple Swap Function
The second issue is the set method. You should remove this
temp = Node(newdata)
temp.set_next(current)
if previous is None:
self.head = temp
else:
previous.set_next(temp)
and you should write something like this
current.set_data(newData)
(I don't know how exactly you implemented the Node class)

Resources