mardi 28 juin 2016

How Do I Nest IF Functions in Order to Calculate Daily Revenue?

I am working on a data set and I need to calculate the daily revenue of fake AirBnb listings using nested IF statements. This is for a bootcamp I am attending, and I am stuck.

My instructions are as follows:

Estimate revenue per listing

  • Assume each booking always has 2 guests, unless the listing accommodates only one;
  • The booking is always for the minimum number of days allowed;
  • Only half of the bookings generate a review;
  • The extra person charge is per night (column name ‘extra_people’)

○ Format: have a column that calculates daily revenue (account for number of guests accommodated, number of guests included in the price, extra charge for additional people - using nested IF statements); another column would then calculates revenue per booking; finally, multiply that by the number of total stays the listings has had.

Using the data in my dataset, I am attempting to fill out column AA. So far this is what I have for my formula,

Formula

However, I get an error. I know the first part of this formula would work for the scenario in row 4. However, I need to make sure that the formula takes into account scenarios where the "guests included" is less than the "accommodates", because I must assume that each booking always has two guests except for when the listing only accommodates one person.

How should this formula be written? Can I not create equations within nested if formulas if it includes adding, subtracting, dividing, or multiplying columns together?

Dataset

Order in if-clause

What's the difference between

if( true == getBooleanValue() )

and

if( getBooleanValue() == true )

where getBooleanValue() returns true or false ?

Python: Nesting an "If" statement inside a "while" loop?

I'm new to Python, which is why I'm having trouble on a problem which others might find easy.

Background to this problem: Euler Project, question 2. The problem essentially asks us to add all the even terms in the Fibonacci sequence, so long that each term is under 4,000,000. I decided to do the problem a little differently than what many solutions online show, by calculating the nth Fibonacci term from a closed formula. For now, suppose this function is called Fibonacci(n).

What I'd essentially like to do is loop through an unknown amount of integers that represent the indexes of the Fibonacci set (i.e., 1, 2, 3, 4... etc) and plug each value into Fibonacci(n). If the result has no remainder when divided by 2, then add this Fibonacci number to some value initially set at 0.

Here's what I have so far:

def Fibonacci(n): 
    return (1/(5**0.5))*((((1+5**0.5)/2)**n)-(((1-5**0.5)/2)**n))

i=0
FibSum = 0
nFib = 0

while (nFib <= 10):

    nFib = Fibonacci(i)

    if(nFib%2==0):
        FibSum += nFib

    i += 1

print FibSum

(Yes, as you can see, I'm constraining the Fibonacci sequence to end at 10 as opposed to 4,000,000; this is done merely for testing's sake.)

Now, here's my problem: when I run this code, I get 2.0 instead of 10.0 (2 and 8 are the two Fibonacci numbers that should be added together).

How come? My guess is that the loop stops after it gets to the third Fibonacci number (2) and doesn't continue beyond that. Does anyone see something wrong with my code?

Please comment if you have any further questions. Thanks in advance.

lundi 27 juin 2016

For loop inside the if condition, but until complete the for loop if condition else part have to stop in android

I am having a problem in for loop inside the if condition, first I want to complete the for loop first check the if condition by looping after complete the for loop else part intent have to work.

Code:

 for (int k = 0; k < cartlistadp.size(); k++) {
    // custom dialog
    if (cartlistadp.get(k).getTotalOutOfStockQuantity().equalsIgnoreCase("0")) {
        item = true;
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.cartcustomdialog);

        // set the custom dialog components - text, image and button
        TextView name = (TextView) dialog.findViewById(R.id.name);
        TextView count = (TextView) dialog.findViewById(R.id.countofstock);
        ImageView img = (ImageView) dialog.findViewById(R.id.img);
        TextView cancel = (TextView) dialog.findViewById(R.id.cancel);
        TextView submit = (TextView) dialog.findViewById(R.id.submit);
        count.setText(k + " " + "Item out of Stock");
        name.setText(cartlistadp.get(k).getProductname());
        YelloPage.imageLoader.displayImage(cartlistadp.get(k).getProductimg(), img, options);
        pos = cartlistadp.get(k).getProductid();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!flag) {
                    //    wishlist.setBackgroundResource(R.drawable.wishnew);
                    flag = true;

                    String channel = wishshared.getString(Constants.productid, "['']");
                    JSONArray items;
                    String wishitem;
                    if (TextUtils.isEmpty(channel)) {
                        items = new JSONArray();
                        items.put(String.valueOf(pos));
                        wishitem = String.valueOf(items);
                        editors.putString(Constants.productid, wishitem);
                        editors.apply();
                        removeoutofstock();
                        Toast.makeText(context, "cartItems", Toast.LENGTH_LONG).show();
                        flag = false;

                    } else {

                        try {

                            Boolean found = false;
                            items = new JSONArray(channel);
                            for (int x = 0; x < items.length(); x++) {
                                if (pos.equalsIgnoreCase(items.getString(x))) {
                                    found = true;
                                    removeoutofstock();
                                }
                            }
                            if (!found) {
                                items.put(String.valueOf(pos));
                                wishitem = String.valueOf(items);
                                editors.putString(Constants.productid, wishitem);
                                removeoutofstock();
                                Log.i(Constants.productid, "wishitems");
                            }
                            editors.apply();
                            flag = false;

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    removeoutofstock();
                }

                Intent intent = ((Activity) context).getIntent();
                ((Activity) context).finish();
                context.startActivity(intent);
            }
        });
        dialog.show();
    } else {


        Intent i = new Intent(context, Delivery_Activity.class);
        context.startActivity(i);
        ((Activity) context).finish();
        // break;
    }

}

I know it's not essential question here, but little logical missing.

Anyone solve this problem.

Thanks in advance

Batch - Simple IF ELSE statement

set /p c=
if"%c%"=="cls" (
echo Print
echo %c%
pause
)

The above one is my small part of the batch code in which I am having problem, Any idea what have I done wrong?

Query variable value and set a conditional statement

I have an mySQL database for a 3rdparty application. One of the tables in this database is called Users.

There is a field in this table called USR_Role

I would like to query the Role field and if the user is a member of a particular role then set @@manager==1 else @@manager==0

Here is my current code:

$user = userInfo(@@USER_LOGGED);
$user1 = $user['Role'];

if ($user1 == 'AMA_NoTravelApproval'){
    @@Manager==1;}
else
{@@Manager==0;}

I know very little PHP at this stage but when debugging within the application I do not get an error so unsure if the code has an error or the application is not applying it.

How to combine AND and OR condition in bash script for if condition?

I was trying to combine logical AND & OR in a bash script within if condition. Somehow I am not getting the desired output and it is hard to troubleshoot. I am trying to validate the input parameters passed to a shell script for no parameter and the first parameter passed is valid or not.

if [ "$#" -ne 1 ] && ([ "$1" == "ABC" ] || [ "$1" == "DEF" ] || [ "$1" == "GHI" ] || [ "$1" == "JKL" ]) 
then
echo "Usage: ./myscript.sh [ABC | DEF | GHI | JKL]"
exit 1
fi

Can anyone point out what is going wrong here?