0
Having problem with adding vertical characters
Hey there. Suppose we have a n × n matrix and I want to add each element from each column to the variable 'vertical_chars', but it does not work properly, no idea why. Can you help me out ? https://code.sololearn.com/csFF1H2zUxnG/?ref=app Note that I have not only implemented what I just explained. That's a part of another problem. The for loop that adds characters into the variable vertical_chars does not work properly. example : strings = [ "ab", "cd" ] Now the variable vertical_chars should be [ ["a", "c"], ["b", "d"] ]
22 Respuestas
+ 8
Ali_combination ,
can you please give a proper input sample and also an output sample as you expect it?
it would also be helpful to remove everything from the code that is not needed to solve the problem.
EDITED:
if this Iis correct:
[ "ab", "cd" ] -> [ ["a", "c"], ["b", "d"] ]
is this also correct?
[ "ab1", "cd2", 'ef3' ] -> [['a', 'c', 'e'], ['b', 'd', 'f'], ['1', '2', '3']]
+ 8
Ali_combination ,
the creation of the `re-arranged` matrix can be done by using the zip() function. to do so, we can:
> use a for loop
or
> use a list comprehension
see the samples in the attached file:
https://code.sololearn.com/cS6cNyxfU1iW/?ref=app
+ 4
Ali_combination ,
(this reworked code does only take care about re-arranging characters. i have tried to be as close as possible with your code)
the code that was posted handles the input of the strings using nested for loops, and finally generates a list of individual characters. these are in the same sequence as the input is given ('vertical_chars'). but we are missing the re-arrangement as required. so i would like to take all complexity and redundancy out of the code: (see my explanations below the code. the key point is a slice (7) that is feeded by the for loop and the range() object.
https://code.sololearn.com/c77233418h4x/?ref=app
+ 2
'''
Ipang
you forgot numpy 😁.
it's my default for matrix operations.
'''
import numpy as np
src = [
"SLn2",
"oee0",
"lar2",
"ors2"
]
print(src)
mtx1 = np.array([[c for c in w] for w in src])
print(mtx1)
mtx2 = mtx1.T
print(mtx2)
res = [''.join(li) for li in mtx2]
print(res)
+ 2
'''
Ali_combination
here is Ipang 's code modified to accept user input
'''
def vertical_mixer( src ):
shortest = len( min( src, key = len ) )
res = [ [] for i in range( max( len( src ), shortest ) ) ]
for el in src:
for i in range( shortest ):
res[ i ].append( el[ i ] )
return res
src = input().split()
for el in vertical_mixer( src ):
print( *el )
"""
sample input:
SLn2 oee0 lar2 ors2
submit
or
ab cd
submit
"""
+ 2
Bob_Li I understand, but the person who introduces rules of the judge system is sadly not me :( .
Let me explain to you what I want to program. We are given a matrix n × n which is represented as n strings, each in a separate line, and each string consists of n characters. After the matrix, we are given another string which consists of k characters. Now we need to find out, how many of the given string can be spotted in the matrix ?
Example :
3 2
abc
bcd
dfg
ab
Answer : 2 . One horizontal ab and one vertical ab. Note that the first number in the first line refers to number n, and the second one refers to number k.
+ 1
Lothar this example you made is perfect. No error. And I want to implement the complete program. Now I can implement this very part and I will get the correct output :
for string in strings:
for i in range (n):
vertical_chars[i].append(string[i])
This works perfectly. But when I add it to the main program, it doesn't give the correct input. Im just like a deer in the headlights -_-
+ 1
Ipang good implementation, thanks. But I'd rather find out my mistake in my own program, I must know why it doesn't work ... I need to learn from my errors. Can you please let me know that?
+ 1
Ali_combination
For one thing, your code is a bit hard to understand..😅
The type of inputs you are using feels like ones you would need for C or C++. Python does not need to be that specific.
Perhaps you could provide a sample input or the starting code template.
Also, why are you only printing an int at the end?
total += find_sub_strings(string, criterion)
print(total)
The function returns an int. I thought you wanted an array?
+ 1
Bob_Li if you find it difficult to understand, it is because I didn't explain the whole problem. Now I just did, what I actually want to program. Hope I cleared it up well enough.
+ 1
I think I finally understand.
you are matching how many 'ab' is there in
abc
bcd
dfg
and the answer is 2
one horizontal and one vertical
this is harder...
+ 1
Lothar good use of unzipping 👌 thanks
+ 1
Lothar hi again. By writing this comment to you, I just want to say that, I just found out my error in my code :
https://code.sololearn.com/csFF1H2zUxnG/?ref=app
The variable vertical_chars does not include vertical characters, because I defined it in this way : [ [ ] ] * n
This way is wrong. Because if I append an element to one of its lists, it will be appended into all of its lists, or as an example :
lst = [ [ ] ] * 5
lst[0].append(1)
print ( lst )
>> [ [5], [5], [5], [5], [5] ]
Do you know any way to avoid this ? I remember asking this question already ( and ChaoticDawg answered me ) , in Sololearn forum, but I can't remember it any more. I guess it was done using copy module, not sure.
+ 1
Bob_Li that's correct 👍
+ 1
Lothar I see, that's a good implementation 👍 and thanks
0
Ali_combination
your input implementation is too complicated.
you could simplify it by just letting the user input a series of space separated strings and construct the base matrix from it. No need for other computable parameters.
ab cd
submit
or
ab1 cd2 ef3
submit
0
Bob_Li If I were able to change the way of giving inputs, I would really happy. But I have not designed such details in this problem, it's a problem in a problem-solving website. The judge system cannot be modified.
But my main question is, how come does not the nested for loop add i-th character of the string into the i-th list in the vertical_chars ?
0
Bob_Li yeah you got it.
Not too hard to solve. The only problem im currently dealing with, is what I explained in previous comments. I don't understand why it works like that. Print vertical_chars, you will see it prints chained horizontal characters of the n × n matrix, while it should print vertical characters of the matrix.
0
Ali_combination
I used Ipang's function as base. One input was not necessary.
https://code.sololearn.com/cLgxCfvRLyaR/?ref=app
0
Ali_combination
My suggestion is to group similar steps together.
Input processing.
function definition.
output processing.
and try to use inbuilt functions to simplify your code. here I used String.count(substring) to count the number of times the target 'ab' appears inside each word in the flatlist.
Type annotation is good practice, but since space on my phone is so small it was not practical.