How do I print a particular character when up to that point in my code, no condition has been satisfied?
Example :
If I have an array of 100 numbers and if I traverse the array by 'for' loop and do not find my number, how do I print a single line " Not Found " ?
I am using C++.
You would typically create a boolean variable before the loop, maybe called found and set it to false. If the number is found in the loop, you would set found to true. After the loop is done, you would use an if statement to test if found is false and, if found to be so, output the " Not Found " message.
There are other ways that are usually better. But this is the simplest one that everyone should learn first.
Related
HELP! I'm getting a "too many arguments for this function" error.
=IF(B13>149,000,"T1",IF(B13>180,000,"T2",IF(B13>210,000,"T3",IF(B13>240,000,"T4",IF(B13>270,000,"T5",IF(B13>300,000,"T6"))))))
Remove the commas from the numbers; Excel thinks "comma" means "new argument." Like this...
=IF(B13>149000,"T1",IF(B13>180000,"T2",IF(B13>210000,"T3",IF(B13>240000,"T4",IF(B13>270000,"T5",IF(B13>300000,"T6"))))))
...but your code will still not do what you want it to do because assuming B13 is greater than 149000, we'll never get past evaluating your first IF and setting to T1, because it looks like the function parser stops after it finds a true condition (like B13 being greater than 149000; whether or not other IFs in your function would also evaluate to true doesn't matter to Excel - it already found a true condition). The solution is to reverse the order, like this:
=IF(B13>300000,"T6",IF(B13>270000,"T5",IF(B13>240000,"T4",IF(B13>210000,"T3",IF(B13>180000,"T2",IF(B13>149000,"T1"))))))
I am using the deepdiff function to find the difference between 2 dictionaries, which gives the output as: A = {'dictionary_item_added': set(["root['mismatched_element']"])}. How to print just 'mismatched_element'?
Try this:
set_item = A['dictionary_item_added'].pop()
print set_item[set_item.find("['")+2 : set_item.find("']")]
The first line gets the element from the set, the second removes the [] and everything around them, and prints.
This code does the specific task you asked for, but it's hard to generalize the solution without a more generalized question..
I have large text files with about 6 lines/instances of 3_xcalc_59 in which 59 is some 2-digit integer.
I am looking to increment these values of the text file by 1 every time I run the program.
I know I can increment a value defined in the code, but how can I increment an integer inside a line of text?
I was thinking the first part of the process would involve reading these lines and assigning them to string variables or a list, but I am not sure how to even do that.
I can find the lines by writing if line.startswith("3_xcalc") , but I'm not sure how to assign them to a list.
Simply writing
for line in open(inputfile, "w"):
line.startswith("3_xcalc") = listoflinesstartingwith3xcalc
Tells me "can't assign to function call", so that doesn't work, but I'm not sure what else to try.
Thank you.
How can I append user int inputs to a list with a while loop? So whenever the number the user inputs is bigger than zero will be added to the list, but when it is a negative number the while loop will break and will continue to the next action. I am a total beginner in python 3, I tried a few things but didn't work. Here is what I tried :
numbers=[]
number = int(input("Please input a number: "))
while number>=0:
numbers.append(number)
if number <0:
break
You have two logical errors there:
First, you're never re-prompting for the number once you enter the while loop. You need to get a new number inside the loop so that you decide what to do upon the next iteration (append to the list, or stop the loop).
Second, your test if number < 0 is superfluous. Your loop runs only as long as number is greater or equal to zero; so inside the loop, there's no way the number can be smaller than zero. The test at the while above is quite sufficient.
Personally I'd rewrite the loop into an endless loop while True: ... and inside the loop I'd first prompt for a number. If that number were <0, I'd break out of the loop. Else, the remainder of the loop would be to append the new number to your list.
But there are countless solutions. Good luck!
You could use a for-loop instead like this pseudo code
if number<0
// do nothing or something or whatever
else:
yourRange = range(0,number)
for count in yourRange:
numbers.append(number)
I am new to perl and I have a problem that I'm trying to solve. At this stage in my program, I have placed a file into an array and created a hash where all the keys are numbers, that increase by a user specified bin size, within a range The values of all keys are set to 0. My goal is to loop through the array and find numbers that match the keys of my hash, and increment the corresponding value by 1 in the event of a match. To make finding the specific value within the array a bit easier, each line of the array will only contain one number of interest, and this number will ALWAYS be a decimal, so maybe I can use the regex:
=~ m{(\d+\.\d+)}
to pick out the numbers of interest. After finding the number of interest, I need to round down the number (at the minute I an using "Math::Round 'nlowmult';") so that it can drop into the appropriate bin (if it exists), and if the bin does not exist, the loop needs to continue until all lines of the array have been scanned.
So the overall aim is to have a hash which has a record of the number of times that values in this array appear, within a user specified range and increment (bin size).
At the minute my code attempting this is (MathRound has been called earlier in the program):
my $msline;
foreach $msline (#msfile) {
chomp $msline;
my ($name, $pnum, $m2c, $charge, $missed, $sequence) = split (" ", $msline);
if ($m2c =~ /[$lowerbound,$upperbound]/) {
nlowmult ($binsize, $m2c);
$hash{$m2c}++;
}
}
NOTE: each line of the array contains 6 fields, with the number of interest always appearing in the third field "m2c".
The program isn't rounding the values down, neither is it adding values to the keys, it is making new keys and incrementing these. I also don't think using split is a good idea, since a real array will contain around 40,000 lines. This may make the hash population process really slow.
Where am I going wrong? Can anybody give me any tips as to how I can go about solving this problem? If any aspects of the problem needs explaining further, let me know!
Thank you in advance!
Change:
if ($m2c =~ /[$lowerbound,$upperbound]/) {
nlowmult ($binsize, $m2c);
$hash{$m2c}++;
}
to:
if ($m2c >= $lowerbound && $m2c <= $upperbound) {
$m2c = nlowmult ($binsize, $m2c);
$hash{$m2c}++;
}
You can't use a regular expression like that to test numeric ranges. And you're using the original value of $m2c as the hash key, not the rounded value.
I think the main problem is your line:
nlowmult ($binsize, $m2c);
Changing this line to:
$m2c = nlowmult ($binsize, $m2c);
would solve at least that problem, because nlowmult() doesn't actually modify $m2c. It just returns the rounded result. You need to tell perl to store that result back into $m2c.
You could combine that line and the one below it if you don't want to actually modify the contents of $m2c:
$hash{nlowmult ($binsize, $m2c)}++;
Probably not a compete answer, but I hope that helps.