Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,444,034 members, 5,502,887 topics. Date: Wednesday, 01 April 2020 at 02:46 AM

Criticize My Algorithm - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Criticize My Algorithm (247 Views)

Please Help Review Or Criticize School Management SDOSMS New Version 1.5 / Simple Algorithm Exercise / My C++ Voting System Program Algorithm (2) (3) (4)

(1) (Reply) (Go Down)

Criticize My Algorithm by Lawren76: 3:16pm On Mar 26
Hey Gurus in d house they is a python script am working on that makes make manipulation of scripts easy although python has it on easy methods to do so bt i had to re-invent d wheel with a couple of algorithm implemnetation i just want you guyz to help me scrutinize nd check my codes nd show me where i can improve on
You can DM me on whatsapp using this number 07013927220 to talk or just check the code here
from math import floor
##please##
class LMM:
def __init__(self,list):
self.list=list
def print(self):
for i in self.list:
print(i)
def Add(self,index,item):
indx=index
length=len(list)-1
self.list+=[0]
while length>=indx:
self.list[length+1]=self.list[length]
length-=1
self.list[indx]=item
return self.list
def Remove(self,index):
tempindx=index
indx=index
self.list[tempindx]=self.list[tempindx+1]
self.list.remove(self.list[indx])
return self.list
def Sort(self):
length=len(list)-1
for turns in range(length):
for i in range(length):
if self.list[i]>self.list[i+1]:
tempdata=self.list[i]
self.list[i]=self.list[i+1]
self.list[i+1]=tempdata
return self.list
def Reverse(self):
listx=[]
for i in range(len(self.list)-1,-1,-1):
listx.append(self.list[i])
return listx
def Index(self,num):
for i in range(len(self.list)):
if self.list[i]==num:
return i
def Bindex(self,num):
LB=0
UB=len(self.list)-1
Found=None
while Found==None:
if LB>UB:
print("The number {} does not exist".format(num))
break
Mid=floor((LB+((UB-LB)/2)))
if self.list[Mid]<num:
LB=Mid+1
if self.list[Mid]>num:
UB=Mid-1
if self.list[Mid]==num:
#print("Found {} at index:{}".format(num,Mid))
Found=True
break
return Mid


list=[1,2,3,4,5,6,7,8]
LMM=LMM(list)
a=LMM.Bindex(7)
print(a)

Thanks ur for time
Re: Criticize My Algorithm by Shepherdd: 3:31pm On Mar 26
Can you upload this as a GitHub gist and post a link. It's kinda hard to read because of Python's indentation.
Re: Criticize My Algorithm by Lawren76: 3:34pm On Mar 26
Shepherdd:
Can you upload this as a GitHub gist and post a link. It's kinda hard to read because of Python's indentation.

Sorry don't knw hw to wrk github that's y i left my number
Re: Criticize My Algorithm by Shepherdd: 3:54pm On Mar 26
By mere going through the code like that I could observe that your code will run in O(N²) quadratic time which simply translates that as your input increases the time the algorithm takes doubles the bubble sort is algorithm is a good example of this.

Your search method I think is doing a binary search but you fail to account for the fact that binary search only works on a sorted array. Try and add a line inside the search method to sort the array first.

If you feel comfortable with Python you could replace your sort method with a merge sort implementation which is very acceptable as it has a linear time O(n) worst case scenario.

There is a fairly correct saying that for loops are evil. Try and replace for loops with functional programming methods like filter, reduce e.t.c with Python lambda.

But all in all bravo to you for even trying to code this as many programmers only care about data types and algorithm a week to an interview.

2 Likes

Re: Criticize My Algorithm by Shepherdd: 4:14pm On Mar 26
Also try and work on your variable naming and comments keeping in mind that you won't be the only one working with your code and even you after some years might find it hard to figure what a line does when you don't have good variable names and concise comments.
Re: Criticize My Algorithm by Lawren76: 4:58pm On Mar 26
Shepherdd:
By mere going through the code like that I could observe that your code will run in O(N²) quadratic time which simply translates that as your input increases the time the algorithm takes doubles the bubble sort is algorithm is a good example of this.

Your search method I think is doing a binary search but you fail to account for the fact that binary search only works on a sorted array. Try and add a line inside the search method to sort the array first.

If you feel comfortable with Python you could replace your sort method with a merge sort implementation which is very acceptable as it has a linear time O(n) worst case scenario.

There is a fairly correct saying that for loops are evil. Try and replace for loops with functional programming methods like filter, reduce e.t.c with Python lambda.

But all in all bravo to you for even trying to code this as many programmers only care about data types and algorithm a week to an interview.

Thanks �
Re: Criticize My Algorithm by Lawren76: 5:02pm On Mar 26
Shepherdd:
Also try and work on your variable naming and comments keeping in mind that you won't be the only one working with your code and even you after some years might find it hard to figure what a line does when you don't have good variable names and concise comments.

Noted
Re: Criticize My Algorithm by damtan(m): 5:43pm On Mar 26
I've been searching for a link to download Python environment on my system for practice. I wanna start learning to code and all. Can anyone be of help please?
Re: Criticize My Algorithm by Anonime1105(m): 8:15pm On Mar 26
damtan:
I've been searching for a link to download Python environment on my system for practice. I wanna start learning to code and all. Can anyone be of help please?

Visit Python.org the official python programming language website

(1) (Reply)

Frontend Development Partner / Universal Windows Patform (UWP) With Xaml Skills / Robotics

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2020 Oluwaseun Osewa. All rights reserved. See How To Advertise. 58
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.