samedi 23 juin 2018

if else statement not working on integer values

I have the following code:

-- content of sys.argv is 2 and 10 which is assigned to the specified variables. 

wthreshold, cthreshold = sys.argv
def Alerting():
    if PatternCount < wthreshold:
        print
        print LRangeA
        print
        print 'PatternMatch=(' + str(PatternCount) + '),' + 'ExactTimeFrame[' + str(BeginSearchDVar) + ',' + str(EndinSearchDVar) + ']=(Yes)'
        print
        sys.exit(0)
    elif PatternCount >= wthreshold and PatternCount < cthreshold:
        print
        print LRangeA
        print
        print 'PatternMatch=(' + str(PatternCount) + '),' + 'ExactTimeFrame[' + str(BeginSearchDVar) + ',' + str(EndinSearchDVar) + ']=(Yes)'
        print
        sys.exit(1)
    elif PatternCount >= cthreshold:
        print
        print LRangeA
        print
        print 'PatternMatch=(' + str(PatternCount) + '),' + 'ExactTimeFrame[' + str(BeginSearchDVar) + ',' + str(EndinSearchDVar) + ']=(Yes)'
        print
        sys.exit(2)
    else:
        print
        print LRangeA
        print
        print 'PatternMatch=(' + str(PatternCount) + '),' + 'ExactTimeFrame[' + str(BeginSearchDVar) + ',' + str(EndinSearchDVar) + ']=(Yes)'
        print
        sys.exit(3)


LRangeA = """this line 1
another line 2
one more line 3
line 4 
line 5
line 6
line 7
line 8"""
PatternCount = len(LRangeA.split('\n'))
Alerting()

When I run this code, it doesn't seem to be correctly checking the numbers in the if statements. The code always seems to go into the first if statement even though the value of PatternCount is 8.

How to put a certain string into *if* and check if it is a certain word [duplicate]

This question already has an answer here:

I am trying to scan a string and put it in an if like so: (this is just an example part of the whole program)

Scanner scan=new Scanner();
System.out.print("Enter the word...");
String a=scan.nextLine();
if (a=="Hi")
System.out.println("Hello!")
Else System.exit(0);

So im checking if the user said "Hi" , but it doesnt work like this, I want a way to do it.

I have just started learning java so my questions might seem a little amateur but your answers will help a lot.

I want make html option checked with condition from mysql data

so how to make condition in echo php, i have code the scanario is, data will edit and ind option will output value checked at same in database

<select required="" class="form-control select2_category" name="dokter">
   <option>Pilih Dokter....</option>
     <?php foreach ($dokter as $r) {
        echo "<option value='$r->dokter_name'>$r->dokter_name</option>";
       }
    ?>

FOR loop - while debugging each iteration works fine but when I run it I have different output [JAVA]

I'm working as an intern and I'm trying to build a Java Swing desktop application for my employer. The basic idea of the application is that it imports large data sets and calculate some of the cells in the row and adds new elements to each row based on those calculations. Whole data is stored in a Vector of vectors (tableData) and it is displayed afterwards in a JTable.

        // Calculating YTD
    DataImport.tableHeaders.add(8, "Budget YTD");
    DataImport.tableHeaders.add(9, "Actual YTD");
    DataImport.tableHeaders.add(10, "Variance YTD");
    double budgetYTD = 0;
    double actualYTD = 0;
    Object costCentre = tableData.get(0).get(0);
    Object expenseHead = tableData.get(0).get(1);
    TreeMap<String, ArrayList<Double>> visitedElements = new TreeMap<String, ArrayList<Double>>();
    for (int s = 0; s< y; s++) {

        String head = costCentre.toString() + expenseHead.toString();

        double currentBudget = Double.parseDouble(tableData.get(s).get(5));
        double currentActual = Double.parseDouble(tableData.get(s).get(6));

        if (costCentre.equals(tableData.get(s).get(0)) && expenseHead.equals(tableData.get(s).get(1))) {

            budgetYTD += currentBudget;
            tableData.get(s).add(8, roundOffTo2DecPlaces(budgetYTD));

            actualYTD += currentActual;
            tableData.get(s).add(9, roundOffTo2DecPlaces(actualYTD));

            double varianceYTD = budgetYTD - actualYTD;
            tableData.get(s).add(10, roundOffTo2DecPlaces(varianceYTD));
        }

        else if (visitedElements.containsKey(head)) {
            budgetYTD = visitedElements.get(head).get(0) + currentBudget;
            actualYTD = visitedElements.get(head).get(1) + currentActual;
            visitedElements.get(head).remove(0);
            visitedElements.get(head).remove(0);
            visitedElements.get(head).add(budgetYTD);
            visitedElements.get(head).add(actualYTD);
            double varianceYTD = budgetYTD - actualYTD;
            tableData.get(s).add(8, roundOffTo2DecPlaces(budgetYTD));
            tableData.get(s).add(9, roundOffTo2DecPlaces(actualYTD));
            tableData.get(s).add(10, roundOffTo2DecPlaces(varianceYTD));
            costCentre = tableData.get(s).get(0);
            expenseHead = tableData.get(s).get(1);
        }

        else {
            ArrayList<Double> list = new ArrayList<Double>();
            list.add(budgetYTD);
            list.add(actualYTD);
            String newHead = costCentre.toString() + expenseHead.toString();
            visitedElements.put(newHead, list);
            costCentre = tableData.get(s).get(0);
            expenseHead = tableData.get(s).get(1);
            budgetYTD = 0;
            actualYTD = 0;

            currentBudget = Double.parseDouble(tableData.get(s).get(5));
            budgetYTD += currentBudget;
            tableData.get(s).add(8, roundOffTo2DecPlaces(budgetYTD));

            currentActual = Double.parseDouble(tableData.get(s).get(6));
            actualYTD += currentActual;
            tableData.get(s).add(9, roundOffTo2DecPlaces(actualYTD));

            double varianceYTD = budgetYTD - actualYTD;
            tableData.get(s).add(10, roundOffTo2DecPlaces(varianceYTD));
        }
    }

This piece of code should calculate 3 fields according to the other existing values in tableData and keep always track if a row has been visited (with the head variable), if it has already been visited the calculation is made by adding to the old value of that element the current one specific to that row, so my map holds the key - which is the element and a list of 2 double variables which represents the most recent values of that element after it has been calculated.

Eg:

map -> (head1, <1, 2>), (head2, <1, 2>), (head3, <1, 2>)

x iteration in loop -> found head2 with (5, 6) in that row calculate 5+1 and 6+2 add them to table update values in map

The only problem is that if i run the code, the else if statement seems to not fire but when I'm debugging, it fires and everything behaves as expected. I'm really confused and I run out of ideas. Can anyone share their opinion about this?

Conditional `try` statement with a "general" exception message

I want to try to load a file if a conditional is true. I want a second (one line) bit of code to execute if either a file exception occurs or the conditional is false.

In order to involve birds, consider the following code:

if try_to_fly:
    try:
        fly()
    except FlyError:
        walk("I'm walking instead of flying.")
else:
    walk("I'm walking stead of flying.")

# more lines below #

This does what I want to do. But, it has the line walk("I'm walking stead of flying.") twice, which would be nice to avoid.

Also, there's more code to do below this bit, so we can't return in the try statement without accounting for that code.

Is there a more pythonic way to do this?

vendredi 22 juin 2018

Encapsulate code in if statement or use a return to stop code execution?

Which one I should use to execute code only when a condition is verified?

   if(!condition.isVerified()))
       return;

   doStuff();
   ... 

or maybe this one

 if(condition.isVerified())
 {
   doStuff();
   ... 
 }

Using a function in condition statement

In this C code at the line number 112 how they have used if(solve_sudoku()) . How is the condition executed , when it will know that the condition is true .

If the if-statement works then will the whole program will get stoped by return 1 .

And how can we use a function in the if-statement which is already used in that , is there any use of different values of return in this ?

#include <stdio.h>

#define SIZE 9

//sudoku problem
int matrix[9][9] = {
    {6,5,0,8,7,3,0,9,0},
    {0,0,3,2,5,0,0,0,8},
    {9,8,0,1,0,4,3,5,7},
    {1,0,5,0,0,0,0,0,0},
    {4,0,0,0,0,0,0,0,2},
    {0,0,0,0,0,0,5,0,3},
    {5,7,8,3,0,1,0,2,6},
    {2,0,0,0,4,8,9,0,0},
    {0,9,0,6,2,5,0,8,1}
};

//function to print sudoku
void print_sudoku()
{
    int i,j;
    for(i=0;i<SIZE;i++)
    {
        for(j=0;j<SIZE;j++)
        {
            printf("%d\t",matrix[i][j]);
        }
        printf("\n\n");
    }
}

//function to check if all cells are assigned or not
//if there is any unassigned cell
//then this function will change the values of
//row and col accordingly
int number_unassigned(int *row, int *col)
{
    int num_unassign = 0;
    int i,j;
    for(i=0;i<SIZE;i++)
    {
        for(j=0;j<SIZE;j++)
        {
            //cell is unassigned
            if(matrix[i][j] == 0)
            {
                //changing the values of row and col
                *row = i;
                *col = j;
                //there is one or more unassigned cells
                num_unassign = 1;
                return num_unassign;
            }
        }
    }
    return num_unassign;
}

//function to check if we can put a
//value in a paticular cell or not
int is_safe(int n, int r, int c)
{
    int i,j;
    //checking in row
    for(i=0;i<SIZE;i++)
    {
        //there is a cell with same value
        if(matrix[r][i] == n)
            return 0;
    }
    //checking column
    for(i=0;i<SIZE;i++)
    {
        //there is a cell with the value equal to i
        if(matrix[i][c] == n)
            return 0;
    }
    //checking sub matrix
    int row_start = (r/3)*3;
    int col_start = (c/3)*3;
    for(i=row_start;i<row_start+3;i++)
    {
        for(j=col_start;j<col_start+3;j++)
        {
            if(matrix[i][j]==n)
                return 0;
        }
    }
    return 1;
}

//function to solve sudoku
//using backtracking
int solve_sudoku()
{
    int row;
    int col;
    //if all cells are assigned then the sudoku is already solved
    //pass by reference because number_unassigned will change the values of row and col
    if(number_unassigned(&row, &col) == 0)
        return 1;
    int n,i;
    //number between 1 to 9
    for(i=1;i<=SIZE;i++)
    {
        //if we can assign i to the cell or not
        //the cell is matrix[row][col]
        if(is_safe(i, row, col))
        {
            matrix[row][col] = i;
            //backtracking
            if(solve_sudoku())
                return 1;
            //if we can't proceed with this solution
            //reassign the cell
            matrix[row][col]=0;
        }
    }
    return 0;
}

int main()
{
    if (solve_sudoku())
        print_sudoku();
    else
        printf("No solution\n");
    return 0;
}