Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am solving a problem in codechef which is
Chef has a sequence of N numbers. He like a sequence better if the sequence contains his favorite sequence as a substring.
Given the sequence and his favorite sequence(F) check whether the favorite sequence is contained in the sequence
Input
The first line will contain the number of test cases and are followed
by the cases. Each test case consists of four lines: The length of
the sequence, the sequence N,the length of F and the sequence F
Output
Print "Yes" if the sequence contains the favourite sequence int it
otherwise print "No" Constraints
1<=T<=10
1 1
Input:
2
6
1 2 3 4 5 6
3
2 3 4
6
22 5 6 33 1 4
2
4 15
Output:
Yes
No
to this I wrote
`
for _ in xrange(int(raw_input())):
raw_input()
s = raw_input()
raw_input()
f = raw_input()
print "Yes" if f in s else "No"`
it returns correct result (as far as I have checked ) bu the grader returns wrong. why is this wrong ?
Imagine a scenario where the sequence is '12 3 4' and the subsequence is '2 3 4'. Your code will return True since '2 3 4' in '12 3 4' is True. You need to convert the sequence and subsequence to integers before doing the comparison.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to determine how many sentences there are in each row.
Sent
I went out for a walk.
I don't know. I think you're right!
so boring!!!
WTF?
Nothing
I created a list of punctuation symbols I am interested in for determining the number of sentences per each row:
Output
1
2
1
1
1
In order to get this result, I first considered to split each row whether I met a symbol (for instance . or ! or ?). But I do not know how to get the count.
My code is
import re
def sentence(sent):
return re.findall('[\w][\.!\?]', sent)
df['Sent'] = df['Sent'].apply(sentence)
Could you please give my advice on how to get it?
One idea if dont need last value like 1 use Series.str.count with regex for match letter with escaped .!?:
df['Output'] = df['Sent'].str.count('[\w][\.!\?]')
print (df)
Sent Output
0 I went out for a walk. 1
1 I don't know. I think you're right! 2
2 so boring!!! 1
3 WTF? 1
4 Nothing 0
If need replace 0 by 1:
df['Output'] = df['Sent'].str.count('[\w][\.!\?]').clip(lower=1)
print (df)
Sent Output
0 I went out for a walk. 1
1 I don't know. I think you're right! 2
2 so boring!!! 1
3 WTF? 1
4 Nothing 1
Another idea is use textstat lib:
import textstat
df['Output'] = df['Sent'].apply(textstat.sentence_count)
print (df)
Sent Output
0 I went out for a walk. 1
1 I don't know. I think you're right! 2
2 so boring!!! 1
3 WTF? 1
4 Nothing 1
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a map of several integer values and I would like to iterate over it and print its values. I tried this:
n = map(int, input().split())
1 2 3 4 5
for i in n:
print(i)
This gives me an error:
ValueError: invalid literal for int() with base 10: ' '
Doing the same operation above by using .strip() does the job of printing the integers.
del(n)
n = map(int, input().strip().split())
1 2 3 4 5
for i in n:
print(i)
1
2
3
4
5
What does an 'invalid literal for base 10' mean and why using .strip() fixes the error? Also, is a map object a single entity in Python since using range(map) gives error 'map' object cannot be interpreted as an integer?
for i in range(n):
print(i)
TypeError: 'map' object cannot be interpreted as an integer
map returns a generator that you can consume once, not twice.
n = map(int, input().strip().split())
print(*n)
will print them with a default seperator of ' '. If you want to do 2+ things with the result of your map, store it in a list:
n = list( map(int, input().strip().split()) )
so you are not operating on a generator - the list will keep the values for you to use a second/multiple times.
This
for i in range(map(int, input().strip().split())):
# do smth
does not work as map returns <map object at 0x7f9ff77c12b0> not an integer as needed for range.
From my previous experiences, you are probably consuming the map object before printing it out. map returns a consumable iterator so if you want to print the values of it, make sure you don't consume it.
For example,
>>> n = map(int, input().strip().split())
1 2 3 4 5
>>> for i in n:
... print(i)
...
1
2
3
4
5
>>> for i in n:
... print(i)
# prints nothing
The second code does not work because range takes an int object not a map object and n is a map object.
The first part works for me.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Do any popular Python style guides express a preference between:
x = (f'One plus one is '
f'equal to {1+1}')
and:
x = ('One plus one is '
f'equal to {1+1}')
Does it make sense to omit the f prefix for lines that don't involve interpolation? Should it be included for consistency?
If you look at the generated byte code for each of those two options, you will see that this both results in the same set of operations:
>>> dis.dis("""f'One plus one is ' f'equal to {1+1}'""")
1 0 LOAD_CONST 0 ('One plus one is equal to ')
2 LOAD_CONST 1 (2)
4 FORMAT_VALUE 0
6 BUILD_STRING 2
8 RETURN_VALUE
>>> dis.dis("""'One plus one is ' f'equal to {1+1}'""")
1 0 LOAD_CONST 0 ('One plus one is equal to ')
2 LOAD_CONST 1 (2)
4 FORMAT_VALUE 0
6 BUILD_STRING 2
8 RETURN_VALUE
So for Python, this both does exactly the same thing: The string is being concatenated at compile-time, and the whole string is being evaluated as a format string.
Note though that as per the PEP, f-strings are actually concatenated at run-time, to make sure that every format expression is evaluated independently:
Adjacent f-strings and regular strings are concatenated. Regular strings are concatenated at compile time, and f-strings are concatenated at run time.
That is why the following example will generate two FORMAT_VALUE opcodes:
>>> dis.dis("""f'{2+2}' f'{3+3}'""")
1 0 LOAD_CONST 0 (4)
2 FORMAT_VALUE 0
4 LOAD_CONST 1 (6)
6 FORMAT_VALUE 0
8 BUILD_STRING 2
10 RETURN_VALUE
This will not have an effect for f-strings that don’t actually contain any format placeholders though. Those will still be concatenated at compile-time:
>>> dis.dis("""f'foo' f'bar'""")
1 0 LOAD_CONST 0 ('foobar')
2 RETURN_VALUE
So you can just include the f prefix for constant strings anyway and when they don’t contain a format expression, they won’t end up being handled as format strings.
So in your particular case, since there is no functional difference and both options are compiled to the same thing, that leaves this as a pure style choice which everyone has to answer for themselves.
If you are looking at existing style guides, chances are that they are not covering f-strings yet. But then, you could use the same guideline as for raw strings, although I personally don’t expect style guides to really cover that area much. Just choose what looks best, is readable, and what makes most sense to you. After all, continued strings are not really that common (and you are more likely to have triple-quotes for longer strings).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Let's say I have four lists:
l1=[3,5,6]
l2=[0,2]
l3=[3,4,3,1,2]
l4=[2,3,2]
And I want to print them like this:
2
1
6 3 2
5 2 4 3
3 0 3 2
Can anyone help me, please?
If you are using Python 2.x you can use izip_longest() from itertools:
for i in reversed([' '.join(map(str,i)) for i in izip_longest(l1, l2, l3, l4, fillvalue=' ')]):
print i
In Python 3.x you can use zip_longest() from itertools.
Output:
2
1
6 3 2
5 2 4 3
3 0 3 2
you may be able to solve this problem by using multi-dimensional arrays to store the lists. then cycling through them checking for values to display while using an if statement to print a space if there is no value currently residing there (aka initialize the array to none where no value resides). I believe that if you structure it like that you may have some success
This question already has answers here:
Print in one line dynamically [duplicate]
(22 answers)
Closed 9 years ago.
I know how to type numbers from python.
>>> for a in range(1,11):
print(a)
1
2
3
4
5
6
7
8
9
10
Here the output is given in one line after the other.
So I want to type the numbers in the same line without using lists and stacks. I that possible?
Then how can I do that?
print automatically adds a newline character after the string you've entered, this is why it prints each number on a different line. To change this behavior, you must change end parameter on the function call.
for a in range(1,11):
print(a, end=' ')
Edit:
The end parameter holds a string which gets printed after the string you've entered. By default it's set to \n so after each print, \n is added:
print("Hello!") # actually prints "Hello!\n"
You can change the parameter to anything you like:
print("Hello!", end="...") # prints "Hello!..."
print("Hello!", end="") # prints "Hello!"
Try this:
>>> print(' '.join(str(i) for i in range(1,11)))
1 2 3 4 5 6 7 8 9 10