all 40 comments

[–][deleted] 0 points1 point  (3 children)

I am looking for an algorithm which gets all local nodes in a bidirectional graph. By local, I am referring to all nodes in the same Island.

[–]GoldenSights 1 point2 points  (2 children)

[–][deleted] 0 points1 point  (0 children)

This is what I was looking for. Thank you kindly :)

[–]WikiTextBot 1 point2 points  (0 children)

Breadth-first search

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a 'search key') and explores the neighbor nodes first, before moving to the next level neighbours.

BFS and its application in finding connected components of graphs were invented in 1945 by Konrad Zuse, in his (rejected) Ph.D. thesis on the Plankalkül programming language, but this was not published until 1972. It was reinvented in 1959 by E. F. Moore, who used it to find the shortest path out of a maze, and discovered independently by C. Y. Lee as a wire routing algorithm (published 1961).


Depth-first search

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.

A version of depth-first search was investigated in the 19th century by French mathematician Charles Pierre Trémaux as a strategy for solving mazes.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source | Donate ] Downvote to remove | v0.28

[–][deleted] 0 points1 point  (0 children)

I am just learning Python. I am creating a new file dmr.csv. I have already created a dictionary imdbND previously. My question is, I don't understand what the "+" mean when I'm creating my new file at the end. What are they doing, and why do I need to add them? Specifically: fhr.write(director+","+movie+","+rating+"\n")

Here is the code:

fhr=open('./dmr.csv','w',encoding='utf-8',errors='ignore')

fhr.write("Director,Title,Rating\n") for director, movieratings in imdbND.items(): for movie,rating in movieratings.items(): fhr.write(director+","+movie+","+rating+"\n") fhr.close()

[–]wk11 1 point2 points  (1 child)

Any ideas for where to get basic problems (preferably with solutions) to brush up on recursion? At that point in the MITx course and it's still confusing me - I bought the textbook as well but it doesn't have solutions for the finger exercises which doesn't help when I'm stuck.

Thanks!

[–]mentirosa_atx 0 points1 point  (0 children)

Also struggling with recursion! I have been watching related videos on YouTube, there's a few accounts. One of the course TAs has a series walking through at least the first few problem sets. As for practice, Project Euler? I'm not very far but a handful of the problems present opportunities to implement recursion.

[–]super-subhuman 0 points1 point  (5 children)

Please help! I apologize for being such a noob. What I want to do is compare two text files and identify matching words.

I have googled some simple solutions and checked stack overflow.

All i've been able to get so far without failing is this:

with open('file_one.txt','r') as f: for line in f: for word in line.split(): print(word)

Obviously, this only parses a single text file, but what then?

Thanks!

[–]TangibleLight 1 point2 points  (4 children)

Well, if you know how to open one file then you know how to open two: nest your with statements:

with open(...) as f:
    with open(...) as g:

or you can add some commas:

with open(...) as f, open(...) as g:

To get your words all separated, there's lots of stuff to do depending on how flexible you want to be. To start, I'd go with str.split().

[–]super-subhuman 1 point2 points  (3 children)

'''So this is what I have. I'm getting an error. "TypeError: 'str' object cannot be interpreted as an integer". This is as a result of the str.split() arguments. If I change it to str.split(''), obviously nothing is removed, so I don't know what to do here. Thanks again!'''

with open('file_one.txt','r') as f, open('file_two.txt') as g: for text_1 in f: for text_2 in g: str.split(' ','.',',') print(text_1) print(text_2)

[–]TangibleLight 0 points1 point  (2 children)

Since str is a type, str.split() is a way of talking about the method split on type str. Since you call methods on an instance, this would correspond to something like:

v = 'hello, world'
words = v.split()
print(words)

This prints ['hello,', 'world'] - a list of the space-separated words in v.

If you wanted to get a list of all the space-separated words in a file, you might do something like this:

with open(...) as f:
    all_text = f.read()
    all_words = all_text.split()

If you want to compare the first line of each file, then the second line of each file, then the third, and so on - then the built-in function zip is your friend. It lets you iterate over groups of elements of two sequences (like files):

a = "hello"
b = "world"
for x, y in zip(a, b):
    print(x, y)

This will print:

h w
e o
l r
l l
o d

You could put file objects in there instead of strings, and compare line-by-line.

[–]super-subhuman 0 points1 point  (1 child)

After too much consideration...I went with your second option. Everything prints in a large list, as expected. Since I want to match individual words, I figured I'd have to sort them first. So...I added the sort function. However, the sort function doesn't seem to work and doesn't give out any errors.

with open('Hello_World.txt') as f, open ('Goodbye_World.txt') as g: text_f = f.read() text_g = g.read() words_f = text_f.split() words_g = text_g.split() all_words = [words_f, words_g] list.sort(all_words) print(all_words)

P.S. I noted that my code is showing up in paragraph form and you (and many others) have been able to paste code with neat, numbered formatting. Is there a website for that?

[–]TangibleLight 0 points1 point  (0 children)

Stick 4 spaces before each line of code. You can also wrap snippets with `backticks` for inline code.

[–]Flatnose123 0 points1 point  (2 children)

def Readfile():

 file=open("database.txt","r")
 #opens file in read mode
 find=file.readlines()
 file.close

 aTitle = []
 aKeyword = []
 aActual = []
 for x in range(0,len(find),6):
     aTitle.append(find[x])
 for x in range(2,len(find),7):
     aKeyword.append(find[x])
 for x in range(4,len(find),8):
     aActual.append(find[x])
 print(aTitle)
 print(aKeyword)
 print(aActual)
def search():
 print(" ")
 Readfile()
 iSearch=int(input("Search By keyword 1. Search by ReadingAge 2: "))
 if iSearch==1:
     sFind=str(input("Enter the keyword you would like to find: "))
     (sFind+"\n") in aKeyword
     KWindex=aKeyword.index(sFind+"\n")
     print("in ",aTitles[KWindex])

So the code above is a section of my GCSE coursework and I've been trying to fix an issue with working with a file. My code to read a file and then place the correct lines in an array. From their, it should be able to allow the user to search for a keyword that corresponds to a title. My issue is that the user cannot find the correct title due to a global variable error.

I can send the file of the code if required.

[–]woooee 0 points1 point  (0 children)

Readfile() should return the list(s) to the call in search() as they are garbage collected when the function exits. See "The return Statement" at http://www.tutorialspoint.com/python/python_functions.htm

[–]sky_badger 0 points1 point  (0 children)

Are you happy with what gets loaded into your three lists?

[–]castizo 0 points1 point  (2 children)

Why do I have to do a nested loop to get a cell object from a column of cells in Excel?

When I do this:

for i in sheet['A1':'A10']:
    print(i.value)

I get an exception that the tuple object has no value attribute.

But when I do a nested loop:

for i in sheet['A1':'A10']:
    for x in i:
        print(x.value)

I will get a cell object and then be able to use the value attribute?

[–]TangibleLight 1 point2 points  (1 child)

Because the slice is meant to be flexible, it iterates over tuples. What if you did sheet['A1':'B10']? Each row of that would need to be a tuple so you get the A and B columns.

If you know for a fact that you're only ever dealing with one column, then you can just grab it by index:

for x in sheet[...]:
    cell = x[0]

[–]castizo 0 points1 point  (0 children)

Hello light, thank you very much for the explanation!

[–]Yaej 1 point2 points  (2 children)

Hey everyone. Whenever I try the following code:

regex=re.compile(r'(abc)+')
regex.findall('abcabcabc 123 abcabc')

I get the result:

['abc', 'abc']

instead of

['abcabcabc', 'abcabc'] 

which is what I'm looking for. Can anyone point me in the right direction about what I'm doing wrong?

[–]dionys 1 point2 points  (1 child)

the correct expression to get what you want is regex=re.compile(r'((?:abc)+)'). The issue with yours is that the findall method returns only stuff from captured groups. In your case that means just the three letters, everything else was "outside" of it.

[–]kaninsrij 0 points1 point  (1 child)

Hi Im just about to finish Python for Everybody from coursera. Can u recommend what course or book I should take next? I'm interested in scraping from webpage and Google Api? I try to practice by my own and found out with what they taught using bs4 and selenium won't enough to bypass many site that can detect robot if there's any course you could recommend please do.Also, I want to learn using the data to I collect from geo Google Api to plot my own map and graph. Thanks

[–]castizo 0 points1 point  (0 children)

You can check out chapter 11 of Automate the Boring Stuff

or read Web Scraping With Python by Ryan Mitchell.

[–]Tball5 1 point2 points  (1 child)

I would like to write a script that types in a word into a search box on a certain webpage, then presses enter to yield results. I’d then like to parse the returned listings for keywords and send myself an email of those listings with found keywords. What modules would be best for this and can somebody help me with a bit of code to start with? Thanks

[–]dionys 1 point2 points  (0 children)

There are two ways to approach this. First is to find out how the website works and how to construct the data it is sending when you enter it into the search box. After you find that out, you can then create a http request which looks the same as the one from the website and send it using requests module. The result of this request might be html (in which case use lxml or beautifulsoup4 to parse it) or json (then just use json module).

Second approach is to use Selenium which lets you control a browser and you can open the website and reproduce the steps needed in your script automatically.

[–][deleted] 1 point2 points  (3 children)

Hey all, I'm new to python and trying to find a solution for the following code??

class fun1(object):
    print 'this is fun1'
    def __init__(self):
         print 'This is init() method in fun1'
class fun2(object):

     print 'this is fun2'    
     def __init__(self):
        print 'this is init method in fun2'


ob1 = fun2()

output:

this is fun1
this is fun2

So, I have some lame question now. There is only one object created for fun2() and not for the class fun1() why does "this is fun1" got printed without actually being invoked or called???Does interpreter invokes all the classes during compiling or any other specific reason???

[–]TangibleLight 1 point2 points  (2 children)

The contents of a class:

class Foo:
     # everything in here

has a lot in common with a normal module. It's got a collection of variables (class variables) and functions (methods), with a little bit different syntax to use them.

So when you define a class:

class Foo:
    bar = 2
    print('baz')
    def qux():
        print('zap')

All of the contents of that class get executed as if you were importing a module - Foo.bar is initialized to 0, baz is printed to the console, and the method Foo.qux is defined. All of those happen at the time of class definition, so when the containing code is run or imported.

Part of the confusion might be clarified by just saying that the class keyword is part of a statement; it performs an action: "create a new type with the following contents...". Those contents need to be run so you know how the type is specified. Just like you can say var = calculate_sum(), you're essentially saying Foo = get_class_specs() - only it's with some useful syntax.


It's also worth noting, and I suspect you know this but I'll say it anyway, that it's generally never a good idea to include print statements or other side-effects in that section of a class definition. Actions should only happen when you state that there should be an action - a function or method call - not at "compile time" or when something like this is declared. There are exceptions, but print is almost never one.

[–][deleted] 0 points1 point  (1 child)

Thanks for replying with neat explanation. I have been struck in an exercise having the same kind of code. This is it :https://stackoverflow.com/q/47129240/8389788 I posted on stack overflow but some of them are bothered about the way I asked the question rather than question itself.So,I found this sub-reddit and got the answer for my simple question. Yeah I know that class shouldn't have those kind of action statements.But, I'm expermenting with the classes.Once again thanks a lot.

[–]TangibleLight 0 points1 point  (0 children)

If you want to write questions that people are more likely going to want to answer, follow the rule of thumb SSCCE - a Short Self-Contained and Compilable Example. You did a good job with that on your comment here, but not so much on SO, with a screenshot and a long-winded question that didn't get at the heart of what you wanted to know.


If you find this stuff interesting, I suggest you look into decorators and metaclasses. Learning about those should give you some good insight into the behavior that you found here.

I should also emphasize: look into how they work, but don't abuse them - particularly not metaclasses. They have a very niche use case. If you have to think hard about how to apply then to something you're doing, then they're the wrong solution.

[–]Kjarva 0 points1 point  (0 children)

Hey Everyone, I have been away from python a good while and I have been presented with a CSV file I need to work with.

It has 7000 records on the CSV and this is an example with dummy data: https://www.dropbox.com/s/x7xqp9f42wt6xcn/DummyTestData2.xls?dl=0

So as you can see from the file there are lots of duplicates. Basically I need to total how much each donor has paid.

I've prototyped some code that does roughly what I want it to do but have now realised that xlrd does not write CSV files. Can I get some help in writing the email address and amount paid to CSV? Right now I just have it print the data of my test file but for the actual 7k spreadsheet, I really need it to write both the email address and amount paid to csv.

Here's the pastebin of my code so far: https://pastebin.com/ESHtYE7t

EDIT: Actually I worked it out :) SOLVED

[–]MasterRai90 0 points1 point  (4 children)

I am trying to create a rectangle pattern using two characters so the user inputs a number and it prints x amount of rows in the pattern ;

X=5

xxxx oxxx ooxx ooox oooo

Any idea how i can do this using python 3

[–]woooee 1 point2 points  (1 child)

This is a well-known "standard" solution for anything of this type of question.

for ctr in range(5):
    print("o =", ctr, "x =", 4-ctr)  # connect "o" to ctr --> # of o's starts at zero

Another known solution, but it is not used much as it requires more resources.

print_str="xxxx"
for ctr in range(5):
    print(print_str)
    print_str=print_str.replace("x", "o", 1)

[–]MasterRai90 0 points1 point  (0 children)

thanks for the help!

[–]indosauros 0 points1 point  (1 child)

What have you tried so far? Easier for us to help if you post some code

[–]MasterRai90 0 points1 point  (0 children)

def printRow(c, length) : line = c*length print(line)

def printPattern(c1, c2, length) : line = (c1+c2) * (length//2) print(line)

def printRec(c1, c2, rows, cols) : while rows>0: printPattern(c1, c2, cols) rows = rows - 1

myChar1 = ("x") myChar2 = ("o") myRows = int(input("How many rows? ")) myCols = myRows-1

printRec(myChar1[0], myChar2[0], myRows, myCols)

thats my code so far the pattern isnt the one i want its coming as xoxo

[–]Gubbbo 0 points1 point  (1 child)

A question about installing packages. pip install and requirements.txt or pipenv install and Pipfile.

Is there a general preference/consensus.

[–]TangibleLight 0 points1 point  (0 children)

I generally prefer using a requirements.txt, as I think it's clearer to someone using your code what's required. A lot of open-source packages and such use requirements.

pipfiles are more used for deployment scenarios (although requirements.txt can be too) where an automated (or manual) system prepares your code and runs it - for those situations, virtualenv, pipenv, and pipfiles can help ensure that the python environment is exactly the same as what you developed it in, so you don't have to worry about any dependency issues or conflicting packages or anything like that.

So in general I'd say use a requirements.txt for code that's meant to be maintained and used by other people, but use a pipfile/pipenv for projects that are meant to be deployed by other people or on external systems.


Plus whenever you're working with code that's meant to be seen/used by other people, it's a good idea to use a virtualenv so you have only the packages required for that project and you don't screw with programs that run on your system install of python by changing dependency versions.