help ??

A forum for general discussion of the Python programming language.

help ??

Postby VenugopalaRao » Mon May 27, 2013 4:42 am

Code: Select all

def shuffle(input, i, j):
    pass
    input = input[i:j+1] +input[0:i] + input[j+1:]
def test_shuffle():
    input = [1, 2, 3, 4, 5, 6]
    shuffle(input, 1, 2)
    assert [2, 3, 1, 4, 5, 6] == input

i had done the above code but the problem is i had manipulated the "input" in function shuffle(input, i, j) but once i get back to the test_shuffle() function again the variable "input" does not reflect the changes made in shuffle(input, i, j) why ,please can any one describe why . and help how to reflect that change to the variable "input".
VenugopalaRao
 
Posts: 16
Joined: Wed May 15, 2013 5:05 pm

Re: help ??

Postby metulburr » Mon May 27, 2013 5:11 am

i had done the above code but the problem is i had manipulated the "input" in function shuffle(input, i, j) but once i get back to the test_shuffle() function again the variable "input" does not reflect the changes made in shuffle(input, i, j) why ,please can any one describe why . and help how to reflect that change to the variable "input".


you would need to return the var back to the caller

Code: Select all
def shuffle(input, i, j):
    input = input[i:j+1] +input[0:i] + input[j+1:]
    return input
def test_shuffle():
    input = [1, 2, 3, 4, 5, 6]
    input = shuffle(input, 1, 2)
    assert [2, 3, 1, 4, 5, 6] == input

test_shuffle()


also you could be a little more creative in creation of variable names. You are overwriting the builtin input() function in each function (assuming your using python 3.x). Its more confusing than anything else.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: help ??

Postby VenugopalaRao » Mon May 27, 2013 5:25 am

i need to reflect the change without returning it from the function.
VenugopalaRao
 
Posts: 16
Joined: Wed May 15, 2013 5:05 pm

Re: help ??

Postby metulburr » Mon May 27, 2013 5:53 am

i need to reflect the change without returning it from the function.

then use a class
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: help ??

Postby Crimson King » Mon May 27, 2013 6:11 am

VenugopalaRao wrote:i need to reflect the change without returning it from the function.


May i ask why would you need that?

You could use an empty list and extend it with the slices you performed in your shuffle function, but i guess that's like returning the value right?

This code is really ugly, use a class like metulburr suggested.


Code: Select all
def shuffle(input2, i, j):
    new_input.extend(input2[i:j+1])
    new_input.extend(input2[0:i])
    new_input.extend(input2[j+1:])
   

def test_shuffle():
    input = [1, 2, 3, 4, 5, 6]
    shuffle(input, 1, 2)
    assert [2, 3, 1, 4, 5, 6] == new_input

new_input = []
test_shuffle()
Last edited by Crimson King on Mon May 27, 2013 6:54 am, edited 1 time in total.
User avatar
Crimson King
 
Posts: 132
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: help ??

Postby metulburr » Mon May 27, 2013 6:51 am

May i ask why would you need that?

Its probably a stipulation of a course. If that is true, he should of stated the fact that it was for a course though.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: help ??

Postby micseydel » Mon May 27, 2013 7:56 am

Crimson King wrote:
Code: Select all
def shuffle(input2, i, j):
    new_input.extend(input2[i:j+1])
    new_input.extend(input2[0:i])
    new_input.extend(input2[j+1:])
   

def test_shuffle():
    input = [1, 2, 3, 4, 5, 6]
    shuffle(input, 1, 2)
    assert [2, 3, 1, 4, 5, 6] == new_input

new_input = []
test_shuffle()

Global variables are just about always a bad idea, and as such, always a bad idea to show newbies when it's not the absolutely ideal solution.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1506
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: help ??

Postby Crimson King » Mon May 27, 2013 4:11 pm

micseydel wrote:Global variables are just about always a bad idea, and as such, always a bad idea to show newbies when it's not the absolutely ideal solution.


Yes, Sorry about that.

Before that code i posted that it was ugly but perhaps i should've said something harsher like "always try to avoid this, bad practice". Didn't really know how to achieve the "need to reflect the change without returning it from the function" without a global.

I'll try to be more careful about this.
User avatar
Crimson King
 
Posts: 132
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: help ??

Postby micseydel » Mon May 27, 2013 10:26 pm

What I mean is, I don't think we should show them the bad version at all, even with the explanation (which they may not even read).

When a list is passed into a function, it can be modified in-place. It is said that the function "has a side effect" in that case and we try to avoid this usually, although it can be necessary since it can be tremendously faster and use less memory. Side effects are not nearly as big a deal as globals though.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1506
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 4 guests