samedi 27 mars 2021

how to make 'B', 'C' codes are not to be loaded into memory to improve performance when 'A' is chosen among 'A', 'B', 'C' through if else?

Let's say we have car management software and codes are,

if (power== "gasoline")
{
   // many code
}
else if (power== "diesel")
{
   // many code
}
else if (power== "electric")
{
   // many code 
}
else if (power== "hybrid")
{
   // many code
}

As title of this question, I don't want the code of 'diesel', 'electric', 'hybrid' to be loaded into memory to improve performance when a user selects 'gasoline'.

Is there a excellent way for this case ?

Thank you !

How to have a toast appear on a if else statements on android studio

I have been trying to get a toast to appear on my screen whenever a user types a certain name in the edit text box. When the user successfully types in the correct name I want the toast to display a successful message and if the name is not correct I want the toast to display an error message. Whenever I run the code below and type a name nothing appears whether right or wrong. This is what I have so far.

package com.example.app1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Invites extends AppCompatActivity {
    EditText username;
    Button invite, finished;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invites);
        username = (EditText) findViewById(R.id.username);
        invite = (Button) findViewById(R.id.invite);
        finished = (Button) findViewById(R.id.finished);

        invite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.getText().toString().contains("Fname Lname")) {
                    Toast.makeText(Invites.this, "Invitation Sent", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(Invites.this, "User not found", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

My code is producing wrong output for a specific test case. What could be the problem with my code to fail a particular test case and how to solve it?

Question - You are given the array a consisting of n positive (greater than zero) integers.

In one move, you can choose two indices i and j (i≠j) such that the absolute difference between ai and aj is no more than one (|ai−aj|≤1) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).

Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.

You have to answer t independent test cases.

Input - The first line of the input contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤50) — the length of a. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤100), where ai is the i-th element of a.

Output - For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.

Code Forces question link - https://codeforces.com/problemset/problem/1399/A

My code -

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--){
        int a,count=0;
        cin >> a;
        int arr[a];
        for(int i=0;i<a;i++){
            cin >> arr[i];
        }
        for(int i=0;i<a;i++){
            if((arr[i]+1)==arr[i+1] || arr[i]==arr[i+1]){
                count++;
            }
        }
        if((a-count)>1){
            cout << "NO" << endl;
        }else{cout << "YES" << endl;}
    }
}

The code produces the correct output for all test cases except when the input is - 1 1 1 2 4. It produces YES while the output should be NO and even my code seems to work that way in producing NO. What could be the problem here and how do I fix it?

vendredi 26 mars 2021

Looping the list to get the last item

I want to get a single item in a list at last when the loop completes it's run,

word = 'DOOR'
# so the length of word is 4

actual_list = ['A', 'B', 'C', 'D', 'E', 'F']

Here, i want to run a loop on the actual_list with the length of the variable word, when the loop completed, it shows the last item present in the list. When i did this problem using pen and paper, i got the item E which is not removed by the loop. I also tried this code, but i couldn't got the result successfully,

word = 'DOOR'
print(len(word))

actual_list = ['A', 'B', 'C', 'D', 'E', 'F']

for w in actual_list[:]:
    if len(actual_list) > 1:
        del actual_list[len(word)-1]
        print(actual_list)

Explanation:
The loop remove the 4th item in the list and again the loop will run from the next item after the first item got removed,
Example: after the first loop['A', 'B', 'C', 'E', 'F'] , after the second loop ['A', 'C', 'E', 'F'] , after the third loop ['C', 'E' ,'F'], after the fourth loop ['E', 'F'] , after the fifth loop ['E'] likewise, loop continue it's run from the next item where it remove the last item.
When i run the above code, it says, the index gets out of range, i have been stuck here for almost 4 days. I want the result as,

# The last item in the list
['E']

Can anyone have solution for this?

How do you do if statement with words and it says my char cannot be deferenced

so I'm making this program where the objective is: Create a program that will compute a fare of a passenger. The passenger has 3 types namely ordinary, student and senior. Based on the type of passenger it has an equivalent discount for the student has 20% discount, for the senior it has 30% discount and for the ordinary passenger no discount at all. The formula for computing the distance is the same as the previous problem before. The first 10 kilometers is minimum fare of 20 pesos and for the distance greater than 10 will be 2.50 pesos per kilometer plus the minimum fare of 20 pesos.

    public static void main(String[] args) 
{
    char Type;
    int dist, f0r;
    double price, dcprice, dscnt;
    Scanner input = new Scanner (System.in);
    
    System.out.println("Enter Type of Passenger (Ordinary/Student/Senior): ");
    Type = input.next().charAt(0);
    
    if (Type.equals("Ordinary"))
    {
        System.out.println("Enter Distance Travelled: ");
        dist = input.nextInt();
        
        f0r = dist - 10;
        price = f0r * 2.50 + 20;
        
        System.out.println("the total price is; "+ price);
    }
    else
    {
        
    }
}    

} I'm having this issue where im imputting the word Ordinary but its just stops and did not continue to the if statement where the rest of the code is.

I have also tried it with

    {
        char Type, Ordinary = 0, Student = 0, Senior = 0;
        int dist, f0r;
        double price, dcprice, dscnt;
        Scanner input = new Scanner (System.in);
        
        System.out.println("Enter Type of Passenger (Ordinary/Student/Senior): ");
        Type = input.next().charAt(0);
        ```
        if (Type == Ordinary)
and it still did the same thing
What am I doing Wrong?


update I fixed the thing and heres my take

    public static void main(String[] args) 
    {
        String Ordinary = "Ordinary";
        String Student = "Student";
        String Senior = "Senior";
        int dist, f0r;
        double price, dcprice, dscnt;
        Scanner input = new Scanner (System.in);
        
        System.out.println("Enter Type of Passenger (Ordinary/Student/Senior): ");
        String Type = input.nextLine();
        
        if (Type.equals(Ordinary))
        {
            System.out.println("Enter Distance Travelled: ");
            dist = input.nextInt();
            
            f0r = dist - 10;
            price = f0r * 2.50 + 20;
            
            System.out.println("the total price is; "+ price);
        }
        else if (Type.equals(Student))
        {
            System.out.println("Enter Distance Travelled: ");
            dist = input.nextInt();
            
            f0r = dist - 10;
            price = f0r * 2.50 + 20;
            dscnt = price * .20;
            dcprice = price - dscnt;
            
             System.out.println("the total price is; "+ dcprice);
        }
    }    
}
please Review and is there still any error?

Java -- ++ in if statement

I am confused about the difference between this 3 statement. What differences between each of them? I know the differences between first and third, but how different is between first and second, and second to third. Thank!

    // first
    for (int i = 0, j = 0; i < s.length(); i++) {
        if (count[s.charAt(i)]-- > 0) {
            tLen--;
        }
    }

    // second
    for (int i = 0, j = 0; i < s.length(); i++) {
        count[s.charAt(i)]--;
        if (count[s.charAt(i)] > 0) {
            tLen--;
        }
    }
    

    // third
    for (int i = 0, j = 0; i < s.length(); i++) {
        
        if (count[s.charAt(i)] > 0) {
            count[s.charAt(i)]--;
            tLen--;
        }
    }

VBA Create array with userform items

I have a a list of checkbox in userform. I would like to create a code that is more efficient that the following one :

If checkbox1.value = true
Then Range("A1").value = 100
End if
If checkbox2.value = true
Then Range("A2").value = 200
End if
If checkbox3.value = true
Then Range("A3").value = 300
End if

The problem is that I have 40 checkbox and I would like to create something that says: in my list of checkbox, if it is true then add in my range A1. I'm not sure how to proceed but I tried this:

Dim Element as variant
For each element in MyList
If element.value = true Then 
For i = 1 to NumberOfTrueElement
Range("A" & i + 1).value = Mylist(i)
Next i
End if
Next

Mylist is the frame of all my checkboxes. Please help me if you have a hint.