main thread

Hi

in question 2b, the amount of +/-/* that occur are a function of the amount of digits in n, not n directly. Should I write, for example:

nplus = len(str(n))

nminus = 3*(len(str(n))-d)

or is there a more formal representation of the number of digits in n? (since my example is in Python, rather than being generic…)

Indeed, we saw in class and in the recitation how to obtain an exact mathematical expression for the representation length of a given number in a given basis. You should feel free to use it.

Hi,

Is it ok to define extra functions, besides the ones required in the assignment?

for example:

def useful_function(param1, param2): #recursive function body def altsum_digits(n,d): #function body useful_function(n,d) #rest of function body

If so- where should I include them?

Yes, you may define and use additional functions.

Place them in the relevant part of the skeleton file; the exact location (before/after altsum_digits(n,d)) doesn't matter, since the order of definition doesn't either. All that matters is that all the functions are defined before altsum_digits(n,d) is called.

Hi,

In question 1d, how should we handle input that the "end" index is smaller than the "start" index? is it valid to enter negative indexes as input? as negative indexes are counted from the list's end.

Thanks!

Hi,

if i did something like this: -(expression),

does it count like a minus or a multiply? (u can see this as a 0-exp or as -1*exp)

thanks

Arithmetic operators are shortcuts for invoking functions, in this case of class int.

0-x will invokes a subtraction method which takes two parameters.

(-1)*x will invokes a multiplication method which takes two parameters.

Interestingly, -x will invoke a third function which takes a single parameter (and returns this parameter in the opposite sign). Apparently there's an efficient implementation to this specific operation, and if you measure the running time you will indeed see a difference.

You can get a list of the methods invoked by the different operators if you type "help(int)" at the interactive IDLE prompt.

As for our exercise, I agree that this situation is not well defined. I suggest you replace your current syntax with one of the other variants; that should solve the counting issue (albeit at the price of a slightly higher running time).

another question about this topic, when i calculate the sum of d numbers, should i add it up as d times + or should i divide it to + and -?

and what about when i'm trying to approach the [d+i] char in my string, while i is used in the loop? does it count?

The question asks to count the number of additions and subtractions separately.

It also asks to count all operations that are not inside a loop, regardless of their exact context.

I do not understand what exactly are we asked to do in question 2b. May anybody explain it to me, please!

OSling, it'd be helpful if you could make your question more specific.

Hey.

In 2b - if I am adding a negative number, should I refer to it as plus?

e.g:

for i in range(n):

1 + (-1)*5

should be:

nplus = n

nmult = n

?

thanks.

Yep, we are counting binary operations - regardless of the sign of the operands.

hi,

In question 2b, am I allowed to use "mod" or should I write an arithmetic expression for it?

thanks

You are allowed to use Python's modulo, as we've done in class+tirgul.

does "10 = 10 + (-5)" count as an addition or as a subtraction?

idan, please take a look at the reply to Shrubbery above, and ask again if the matter is still unclear.

Hello.

in 2b how do I count the the action pow(-1,i%2).

to be more specific:

how do I count the power (and what if i i%2 is 0) ?

how do I count the modulu?

thanks.

No need to count modulo.

You can count a**b as (b-1) multiplications (although there's often an optimization going on there).

If b==0 then obviously no multiplications are performed.