1

The Question:

Without altering mult_tasks, write a definition for mult_tasks_line so that the doctests pass.
So:

print mult_tasks("3469") will produce:

(3*3) (3*4) (3*6) (3*9)
(4*3) (4*4) (4*6) (4*9)
(6*3) (6*4) (6*6) (6*9)
(9*3) (9*4) (9*6) (9*8)

def mult_tasks_line(first_num, str_numbers):

    """
    >>> mult_tasks_line("4", "3469")
    '(4*3) (4*4) (4*6) (4*9) '
    """
    #Add your code here

def mult_tasks(str_numbers):

    """
    >>> mult_tasks("246")
    '(2*2) (2*4) (2*6) \\n(4*2) (4*4) (4*6) \\n(6*2) (6*4) (6*6) \\n'
    >>> mult_tasks("1234")
    '(1*1) (1*2) (1*3) (1*4) \\n(2*1) (2*2) (2*3) (2*4) \\n(3*1) (3*2) (3*3) (3*4) \\n(4*1) (4*2) (4*3) (4*4) \\n'
    """

    #Do not alter any code in this function
    result_tasks = ""
    for ch in str_numbers:
        result_tasks += mult_tasks_line(ch, str_numbers) + "\n"
    return result_tasks


if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose = True)

I have already tried doing this:

def mult_tasks_line(first_num, str_numbers):
    digits=0
    for num in str_numbers:
        while digits<len(str_numbers):
            print "("+first_num+"*"+str_numbers[digits]+")",
            digits=digits+1

def mult_tasks(str_numbers):
    result_tasks = ""
    for ch in str_numbers:
        result_tasks += mult_tasks_line(ch, str_numbers),"\n"
    return result_tasks

This is what I tried, the first function works pretty close, but it does not have the single quote. when run mult_tasks_line("4", "3469”) it comes out (4*3) (4*4) (4*6) (4*9).

But the second function seems totally wrong. this is the result for the second function :

mult_tasks("246”)

(2*2) (2*4) (2*6) 

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module> mult_tasks("246")
File "/Users/HuesFile/Downloads/Mastery Test/2.py", line 25, in mult_tasks
result_tasks += mult_tasks_line(ch, str_numbers),"\n" 
TypeError: cannot concatenate 'str' and 'tuple' objects
Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
  • def mult_tasks_line(first_num, str_numbers): digits=0 for num in str_numbers: while digits – Darsolation May 12 '15 at 08:40
  • def mult_tasks(str_numbers): #Do not alter any code in this function result_tasks = "" for ch in str_numbers: result_tasks += mult_tasks_line(ch, str_numbers),"\n" return result_tasks – Darsolation May 12 '15 at 08:41
  • this is what i tried, the first function works pretty close but it does not have the single quote. when run >>> mult_tasks_line("4", "3469”) it comes out (4*3) (4*4) (4*6) (4*9). but the second one seems totally wrong. this is the result for the second function :>>> mult_tasks("246") (2*2) (2*4) (2*6) Traceback (most recent call last): File "", line 1, in mult_tasks("246") File "/Users/HuesFile/Downloads/Mastery Test/2.py", line 25, in mult_tasks result_tasks += mult_tasks_line(ch, str_numbers),"\n" TypeError: cannot concatenate 'str' and 'tuple' objects – Darsolation May 12 '15 at 08:43
  • Add these lines to your question. – Konstantin May 12 '15 at 08:53
  • Thx for suggetion, ive added them to question – Darsolation May 12 '15 at 08:58
  • add some code style, correct minor spelling error – Prashant Kumar May 14 '15 at 23:02

2 Answers2

0

From what I understood, you want to print the matrix of ordered tuples from a string of number-characters, while the seperator

def mult_tasks_line(first_num, str_numbers):
    return ''.join(
        ['({n1}*{n2}) '.format(n1 = first_num, n2 = n2 ) for n2 in str_numbers])

def mult_tasks(str_numbers):
    result_tasks = ""
    for ch in str_numbers: 
        result_tasks += mult_tasks_line(ch, str_numbers) + "\n" 
    return result_tasks

Then

print mult_tasks("3469")

gives

(3*3) (3*4) (3*6) (3*9) 
(4*3) (4*4) (4*6) (4*9) 
(6*3) (6*4) (6*6) (6*9) 
(9*3) (9*4) (9*6) (9*9) 

as desired. The result scales, in the sense that it works for an arbitrary string.

Do you wish the multiplications to be computed?

donbunkito
  • 488
  • 5
  • 9
  • print mult_tasks("3469") Traceback (most recent call last): File "", line 1, in print mult_tasks("3469") File "/Users/HuesFile/Downloads/Mastery Test/2.py", line 8, in mult_tasks result_tasks += mult_tasks_line(ch, str_numbers) + "\n" File "/Users/HuesFile/Downloads/Mastery Test/2.py", line 2, in mult_tasks_line return string.join(['({n1}*{n2}) '.format(n1 = first_num, n2 = n2 ) for n2 in str_numbers ], sep = '') NameError: global name 'string' is not defined – Darsolation May 12 '15 at 09:26
  • thx dude for your kind help, I tried your code but get the result showing as above. the question does not require compute the multiplication, but i think if it works out will be more helpful for me to understand what happens. – Darsolation May 12 '15 at 09:28
  • did you try `import string`; I edited the code above – donbunkito May 13 '15 at 10:20
0

Obviously mult_tasks contains an incorrect line

result_tasks += mult_tasks_line(ch, str_numbers),"\n"

result_tasks is a string and here it is concatenated with a tuple (They are constructed by commas with or without parentheses) mult_tasks_line(ch, str_numbers), "\n" which leads to exception.

You need to modify this line to

result_tasks += mult_tasks_line(ch, str_numbers) + "\n"

Another problem in your code, is that mult_tasks_line doesn't return value, but simply prints it. You want to do something like this

def mult_tasks_line(first_num, str_numbers):
    result = []
    for ch in str_numbers:
        result.append('({}*{})'.format(first_num, ch))
    return ' '.join(result)
Konstantin
  • 24,271
  • 5
  • 48
  • 65
  • Thx Alik, your codes seems perfect and it is really helpful for my understanding. can you tell me more about the string.append() and ‘’.join() function, because i think i still haven’t learn these stuff at moment. – Darsolation May 12 '15 at 09:34
  • @user3521788 [`append`](https://docs.python.org/2/tutorial/datastructures.html#more-on-lists) and [`join`](https://docs.python.org/2/library/stdtypes.html#str.join) are desribed in docs. You can google a lot of examples (see [this](http://www.tutorialspoint.com/python/string_join.htm) or [this](http://www.tutorialspoint.com/python/list_append.htm)). – Konstantin May 12 '15 at 09:41
  • it’s amazing! i really appreciate your help. Thx Alik – Darsolation May 12 '15 at 09:47