take indefinite inputs in python separated by newlines - python-3.x

I want to take inputs separated by new lines, but i don't wan't it to take fixed amount of inputs. Then perform some action on given inputs. For more info look at this codechef problem.

What you can do is to use while loop and check the value of input in each iteration
input_list=[]
value=input()
while(value!=condition):
value=input()
input_list.append(value)

Related

Python Input loop not taking all inputs

python_code
I want to take all inputs and convert them into list of lists. But this program is not taking all inputs as intended. I may have made a basic mistake but I couldn't get my head around it
Try using readline() rather than input() at every step from start. Because I don't see any problem in the loop, my best guess is because you are using input() you are not reading escape characters which are there in these kind of inputs.

Random Characters in string python

how can i make a program that will take a sentence like
i can take you places you've never been before to this check photo as it wont let me add the aids code. Basically add extra random characters and words that i choose beforehand in random places

is there a way to calculate every possible order of operation for 1 operation in Python?

Let's say that I have a = '1+2*5/3', there's a specific order to which my machine will evaluate this statement (with eval(a))
I would like to know if there's a line of code (or a function? just an elegant way that could get the job done) that would calculate :
(1+2)*5/3
1+(2*5)/3
1+2*(5/3)
(1+2*5)/3
1+(2*5/3)
(1+2)*(5/3)
1+2*5/3
In this example, I used an operation with 4 factors, so I could just code 1 function for each possibility, but I need to do the same thing with 6 factors and that would just take way too much time and effort since the possibility of different operation order would increase exponentially
It would be also great that it returns everything in a dictionary in this form {operation:result} with the parentheses included, if not i'll find my way around it
edit: as requested, the main goal is to make a program that find the solution to the game " le compte est bon " brute force method, the rules can be found here : https://en.wikipedia.org/wiki/Des_chiffres_et_des_lettres#Le_compte_est_bon_.28.22the_total_is_right.22.29
This is going to be very hard. I recommend you follow these steps:
Create a list to check if the formula has already been calculated
Randomize the order (such as +-*/ and randomly place numbers
Check if rule number`s one is a valid formula. if not try number 1 again
Randomize the order (such as opening and closing parentheses and ^)
Check if the sentence above is a valid formula. if not try number 3 again.
Check the formula through the list and see if it has already been calculated. if it has been calculated then we don't use it and go back to number two. but...
If it is not in the list then we can use it.
Those are the basic steps for common known math symbols, but what about square root?
Another way to do this is by making python move the symbols over like you did with the parentheses, but for EVERYTHING (numbers and symbols(+-/*))
EDIT:
This was before the original question was changed.

Keeping Count Of Words

I am writing a program to keep count of 'good' and 'bad' words. The program is using two text files, one with good words and one with bad words, to detect the score. I currently have the following:
...
The program executes in Python, but I can't get it to keep count of the score. I'm not sure what's wrong.
There are no obvious errors in the code. Here are some things to checks:
1) Do the lines in the pos/neg file have just one word? If not, it needs to be split.
2) Is the case the same? If not, be sure to casefold both the target words and the input text.
3) Use of str.split() usually isn't the best way to split natural text that might contain punctuation. Consider something like re.findall(r"[A-Za-z\'\-]+", text).
4) You will be much better lookup performance is the pos/neg words are stored in sets rather than lists.

Vectorize string concatenation in matlab

I am doing a project where I would like to vectorize (if possible), these lines of code in Matlab:
for j=1:length(image_feature(i,:))
string1b=strcat(num2str(j),':',num2str(image_feature(i,j)));
write_file1b=[write_file1b string1b ' '];
end
Basically, what I want to get is an output string in the following way:
1:Value1 2:Value2 3:Value3 ....
Note that ValueX is a number so a real example would be an output like this:
1:23.2 2:34.3 3:110.8
Is this possible? I was thinking about doing something like creating another vector with values from 1 to j, and another j-long vector with only ":", and a num2str(image_feature(i,:)), and then hopefully there is a function f (like a vectorized strcat) that if I do:
f(num2str(1:j),colon_vector,num2str(image_feature(i,:)))
will give me the output I mention above.
Im not sure I understood your question but perhaps this might help
val=[23.2 34.3 110.8]
output = [1:length(val); val]
sprintf('%i: %f ',output)
As output I get
1: 23.200000 2: 34.300000 3: 110.800000
You can vectorize all your array operations to create an array or matrix of numbers very efficiently, but by the very nature of strings in MATLAB, you cannot vectorize the creation of your output string. Theoretically, if you have a fixed length string like in C++, you could concurrently write to different memory locations within the string, but that is not something that is supported by MATLAB. Even if it were, it looks like you have variable-length numbers, so even that would be difficult (unless you were to allocate a specific amount of space per number pair, leading to variable-length spaces between number pairs. It doesn't look like you want to do that, since your examples have exactly one space between all number pairs).
If you'd be interested in efficiently creating a vector, the answer provided by twerdster would accomplish that, but even in that code, the sprintf statement is not concurrent. His code does get rid of the for-loop, which improves efficiency, so I prefer his code to yours.

Resources