0

I'm trying to write slicing code which gets a linked list, start,stop, step. My code is supposed to behave like same as using list[start:step:stop].

My problem starts when the user insert only 1 argument (let's says it was x) - then, x is supposed to get into stop and and not start. But, I have been told that optional arguments need to appear at the end of all the arguments.

Can someone tell me how can we insert only 1 input to the second argument while the first one is mandatory but the second one is not? By the way, i can't use built-in functions

5
  • 1
    Isn’t it start:stop:step? Commented Dec 29, 2013 at 16:06
  • Try to use Keyword arguements, where you can specify key=value type of arguements, and limit your positional/mandatory arguement inside your function/method Commented Dec 29, 2013 at 16:07
  • I can't use built-in functions.. that is the idea of the assigment. I will add this info to the description Commented Dec 29, 2013 at 16:07
  • Siva, can you give me a simple example? Commented Dec 29, 2013 at 16:10
  • @user3130794, Please see the Answer below Commented Dec 29, 2013 at 16:10

4 Answers 4

3

You could write a LinkedList class that defines the __getitem__ function to get access to python's notation.

class LinkedList:

    # Implement the Linked ...

    def __getitem__(self, slice):

        start = slice.start
        stop = slice.stop
        step = slice.step

        # Implement the function

Then you can use LinkedList like you want

l = LinkedList()
l[1]
l[1:10]
l[1:10:2]
Sign up to request clarification or add additional context in comments.

Comments

1

Using optional (named) arguments:

def foo(start, stop=None, step=1):
    if stop == None:
        start, stop = 0, start
    #rest of the code goes here

Then foo(5) == foo(0,5,1), but foo(1,5) == foo(1,5,1). I think this works, anyway... :)

1 Comment

I'd use None as sentinel, this will make things much clearer.
1

You could try:

def myF(*args):
    number_args = len(args)
    if number_args == 1:
        stop = ...
    elif number_args == 2:
        ...
    elif number_args == 3:
        ...
    else
        print "Error"

*args means that the arguments passed to the function myF will be stored in the variable args.

7 Comments

The number of arguments is len(args). Read more about this on Internet. @volcano this is just an example.
What If he passes 2 arguments and the very first one is supposed to be mandatory ???
@Crhistian, read about DRY on inetrnet. Even example should demonstrate efficient code
@volcano edited. @ Siva Cn I don't understand, maybe my bad English, can you give an example?
incredible.Didn't know I could do that. Thanks!
|
0
def func(arg1=None, arg2=None, arg3=None):
    if not arg1:
        print 'arg1 is mandatory'
        return False
    ## do your stuff here

arg1 = 1
arg2 = 4
arg3 = 1

## works fine
func(arg1=arg1, arg2=arg2, arg3=arg3)

## Evaluated as False, Since the first argument is missing
func(arg2=arg2, arg3=arg3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.