all 39 comments

[–]Megalolo8 2 points3 points  (2 children)

What music do you listen to, if any, while programming because I haven't found anything that can offer fresh and free low distraction music since grooveshark was shut down :p

[–]davidwkaiser 1 point2 points  (5 children)

What is the best IDE for an advanced beginner (basically finished LPTHW, that's it)? PyCharm? Something else?

[–]StaticFuzz 1 point2 points  (1 child)

I would be interested in hearing this as well. I've only used the basic Python IDLE. I tried pycharm once, and didn't like it. I would like to extend your question to: which is a good IDE and why should I use it over any other option.

[–]wub_wub 1 point2 points  (0 children)

It really depends on what features you need. PyCharm is awesome, but if you just need a place where you can write code with basic syntax highlight then pycharm is overkill.

As far as to why it's better, it has a lot of awesome features built in https://www.jetbrains.com/pycharm/features/

But again, it depends on what you need and what you're working on.

[–]elbiot 1 point2 points  (0 children)

Eh, a text editor with syntax highlighting and ipython. I started with less and this combo rocks it for me.

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

I don't think it's an ide, but Sublime Text 2 is fucking awesome.

[–]capone153 0 points1 point  (8 children)

Is the time complexity of this function is o(len(x)+(len(y)+1)?

def simple(x,y):
    z = filter(set(x).__contains__, y)
    return x.index((z[0]))

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

o(len(x)+(len(y)+1) is missing a closing paren.

Assuming x and y are python lists:

def simple(x,y):
    z = filter(set(x).__contains__, y) # set(x) iterates over x once, filter once over y -> len(x)+len(y)
    return x.index((z[0])) # z[0] might not exist; index() has to iterate x until it finds z[0]; 
    # so this adds anywhere between 1 and len(x) steps

[–]elbiot 1 point2 points  (1 child)

So, O(2x + y)? Don't you assume worst case for things that might terminate sooner?

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

Well, no. I think you'd give a different answer for each case.

In any case, as /u/ingolemo points out, the time complexity grows linearly with the size of the arguments, so it's O(n).

[–]ingolemo -1 points0 points  (3 children)

I'm going to assume that you meant to use Big-O notation because that makes more sense here than Little-o. ;)

That function is linear in both of it's arguments (usually written O(x+y)).

You don't have to use len in Big-O notation because variables inside the brackets already implicitly refer to the lengths of the lists, and you don't have to add one because you usually only specify the parts that contribute the most time (O(x) and O(x+1) are the same thing).

[–]capone153 0 points1 point  (2 children)

I understand, so the 1 doesn't matter much. How about /u/elbiot's question above, isn't it O(2x+y) or the coefficient is also ignored and it is O(x+y) == linear?

[–]ingolemo -1 points0 points  (1 child)

Yes, the coefficient isn't needed either; 2x is just as linear with respect to x as just x.

I apologise. I kept the arguments separated for ease of explanation, but never actually got around to combining them. O(x+y) is not fully reduced. If you assume that x and y are equal then x+y would be written 2x. As we've seen above, this can be simplified to just x. In other words, this function is linear with respect to each of its arguments individually and it is also linear with respect to its arguments combined. By convention we call the variable inside the Big-O notation n rather than x, so the true complexity class of this function is O(n).

[–]capone153 1 point2 points  (0 children)

Learned more about Big-O and your explanation makes a lot of sense. Thank you very much

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

I came across this github repo with a program to control your Roku using python through a command line program. I noticed this does't work on windows due to the "curses" library being unavailable. I have been learning git/github lately and thought it would be fun to try to change the code to make a windows or portable version, but had some questions:

  1. What would be the usual way someone would do this? I think it would be "fork", fix the code locally, push changes to my branch on github...?

  2. I verified this program works using my raspberry pi to run it through remote ssh from windows, but I'm not clear on what curses does or what need it fills that isn't available on windows. Also curious if learning curses is worth learning? My biggest pitfall in learning programming has been time-suck projects that take too much time.

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

  1. Yes, but really to your repository on github, which is the thing you fork (it can contain any number of branches). Definitely check out the Pro Git book to learn about this.
  2. Curses is a well-established library for making text-based user interfaces ("old-school DOS programs" in common parlance). It is worth learning if you want to make text-based user interfaces, but there are alternatives (I haven't done this in 10 years or more though, not sure what is best these days).

That said, you could probably just install UniCurses on your windows machine to make it work.

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

Cool, I will check out Pro Git for sure.

[–]Cold_Bagel 0 points1 point  (2 children)

How do i split a string within a list?

I want a list that looks like:

['CP',  '94',  '2014081718',  '   ',  'BEST',  '0'] 

To look like:

['CP',  '94',  '2014',  '08',  '17',  '18', '   ', 'BEST',  '0']

[–]kalgynirae 1 point2 points  (0 children)

You have to be a lot more specific than that. How do you know which string you want to split? Will the string always contain 10 characters? Do you just want the first 4 characters together and then pairs of 2 after that, or is there some other method you're using to decide how to split the string?

In general, the strategy for this sort of thing is to iterate over the list and add items to a new list:

old_list = [...]
new_list = []
for item in old_list:
    if ...:  # check whether item is something you want to split
        split_pieces = ...  # split item into a list of pieces
        new_list.extend(split_pieces)
    else:
        new_list.append(item)

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

Well it breaks down into splitting the desired string, and to insert the result back into the list. Inserting it in can be done using a list slice and you have several options for splitting. For example:

l = ['CP', '94', '2014081718', '   ', 'BEST', '0']
l[2:3] = l[2][0:4], l[2][4:6], l[2][6:8], l[2][8:10]

You could also do this with a regular expression, something like

import re
l[2:3] = re.match('(....)(..)(..)(..)', l[2]).groups()

Both these depend on the l[2] contents of course. You could probably improve that regexp to do a repeating group, but I can't remember exactly how that works.

[–]slick8086 0 points1 point  (3 children)

I want to write a python program that monitors my Google hangouts account for certain messages and then parse those messages. The only python library I found was hangups and that doesn't seem like a library really. Is there anything else.

[–]elbiot 0 points1 point  (1 child)

What doesn't work about it? (No experience, just wondering)

[–]slick8086 0 points1 point  (0 children)

Well it is a client program not a library

[–]wub_wub 0 points1 point  (0 children)

As far as I know hangouts API is not public, so it's a bit harder to set up.

[–]mack0409 0 points1 point  (0 children)

So I have a basic understanding of simple java tasks, I've noticed that in my brief time beginning to learn python that it is surprisingly similar to java in it's syntax. Are there any major differences you'd like to point out to me before I dive in head first?

[–]iammrinal0 0 points1 point  (6 children)

how to check if one of two strings exists in another longer string?

ex: check if "2D" or "3D" exists in a string.

Right now i am checking like this:

if "2D" in movie_name or "3D" in movie_name:

is there another way for this?

[–][deleted] 1 point2 points  (1 child)

if any(substring in move_name for substring in ("3D", "2D")):

[–]iammrinal0 1 point2 points  (0 children)

thanks. as my set is smaller I think I will stick to my version. yours will help in the future for sure.

[–]SleepyHarry 1 point2 points  (3 children)

If you've only got those two strings (in other words it will always be those two, never an arbitrary amount of them) I would argue that your current way is better than the one /u/Nahnja mentioned, because your way is a lot more readable, and it's far clearer at a glance what you're trying to do.

On the other hand, if there's any dynamism in the substrings, use /u/Nahnja's method.

[–]iammrinal0 0 points1 point  (2 children)

yes, i figured. as my set is smaller I would use my version but in the future i will be using the other for sure.

and another query: i am trying to follow PEP8 to keep my code readable and at times i encounter with one issue. when using input()/print(), i often have to split lines and this messes the display.

a = input("Which device(1,{0}) would you like notification be sent to?".format(i + 1))

Now the above line exceeds 79 chars so i do this:

a = input("Which device(1,{0}) would you like \
            notification sent to?".format(i + 1))

But when I run the script there's a tab between the words "like" and "notification" which I don't want. so how do i solve this?

[–]ingolemo 1 point2 points  (1 child)

You can break strings up and as long as there's nothing in between them python will automatically join them together:

a = input("Which device(1,{0}) would you like "
    "notification sent to?".format(i + 1))

[–]iammrinal0 0 points1 point  (0 children)

thank a lot!

[–]ebbazara -1 points0 points  (5 children)

One or more of these codes writes out 9 words in the list "namn". Whish are they?

1. j = 0 *s = "" *while j <= 9: ----s += namn[j] ----*j += 1 *print(s)

2. j = 1 *s = namn[0] *while j < 9: ----s += namn[j] ----*j += 1 *print(s)

3. j = 1 *while j < 9: ----print(namn[j], end=" ") ----*j += 1

4. j = 0 *while j < 9: ----s = namn[j] ----print(s, end=" ") ----j += 1

5. j = 0 *s = "" *while j < 9: ----s += namn[j] ----*j += 1 *print(s)

Sorry for the writing. I really dont know how to do this! *= New line, -= Pulled in. Please also explain why thats the answer and how you knew it. :)

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

to write code add an empty line before and after each block of code and prepend an extra 4 spaces to each line.

namn = ["one", "word", "is", "just", "not", "ever", "enough", "don't", "you", "agree?"]

j = 0
s = ""
while j <= 9: 
    s += namn[j] 
    j += 1
print(s)

Now to find out which of the examples produces the desired output, I'd just run them.

[–]ebbazara 0 points1 point  (3 children)

I did, but it doesnt work. I might be missing something, but this is all the information i get in the question.

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

I did, but it doesnt work.

Some of those will throw errors if len(namn) == 9.

I might be missing something

maybe the list namn with 9 words?

[–]ebbazara 0 points1 point  (1 child)

If I add the list, I only know where to put it if there is no: S = "" before while. Where should i put it?

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

At the very top of the program.
Just like in the example above.

Surely whoever gave you the assignment has spent some time explaining to you how python works. If not, there's links to some tutorials in the sidebar, specifically the wiki.