You are not logged in. Please login at www.codechef.com to post your questions!

×

Nzec error using map function python for taking array input

I tried to solve the Problem. So for taking input, I used map function Solution 1, Second time I tried with to solve without map function Solution 2 and it worked

Logic is the same for both problems (even code), the difference is with how I'm taking input.
Please Explain!

asked 26 Jan, 15:55

ayush4's gravatar image

3★ayush4
163
accept rate: 10%

edited 26 Jan, 15:58


The error is not in the map part, it's here:

 temp = [False] * m 
 for i in range(n):
     value.append(temp)

As a simpler example, consider this:

a = [1]
b = a
a[0] = 0
print(b)

You might expect this to print [1] but it'll print [0]. It's got to do with how python stores objects. Long story short, a and b both refer to the same list. Hence, when you change the list referred to by a, you change the list referred to by b. In your snippet above, there's the same problem. If you write value[0][0]=True, value[i][0] will become True for all i.

Also, you don't really need to use map there, you can simply write arr.append(input().split()).

link

answered 27 Jan, 00:02

sddeltech's gravatar image

4★sddeltech
654
accept rate: 0%

edited 27 Jan, 00:21

I didn't read your complete solution but I am sure that the NZEC you're getting is just a symptom of this problem.

(27 Jan, 00:05) sddeltech4★

In Python 3, all input is converted to a string, so there is no need to use map.(1) The Python 3 input() is equivalent to the Python 2 raw_input().(2)

The str.split() string method, whenever no separator is specified, returns a list of words separated by any consecutive whitespace.(3) In this problem CLORGRID, strings are given without any whitespace. Therefore, given s = '..#',
s.split() = ['..#'].

Line 12 of your Solution 1 is arr.append(list(map(str, input().split()))).
For arr = [] and input '..#', this will result in arr = [['..#']]

To append a list of the individual characters of a string input to arr, I recommend using a list comprehension:
arr.append([c for c in input()])

link

answered 27 Jan, 00:03

adavis444's gravatar image

4★adavis444
111
accept rate: 0%

Thanks for the help

(27 Jan, 10:32) ayush43★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×44
×27
×9
×1

question asked: 26 Jan, 15:55

question was seen: 95 times

last updated: 27 Jan, 10:32